Class RowTree

Object
RowTree
Direct Known Subclasses:
IndexedRowTree

public class RowTree extends Object

RowTree is another representation of the Forest concept. It is implemented as a tree structure and is an alternative to ArrayForest when it comes to manipulating the forest.

RowTree is far more heavy on memory and GC - each row is a separate object + 52 bytes of payload. However, it is very fast with the modification operations.

Suggested use:

  • Do not use for long-term forest storage!
  • Do not use when ArrayForest is sufficient for the modifications you need.
  • Do not use when you can arrange the modifications you need in a bulk array-creation fashion.
  • DO use as a temporary object when you need a (potentially big) number of random changes with a forest and none of the above applies.

Additional concepts and terms:

  • Super-root is the "hidden" node that all root nodes are children of.
  • There's no concept of depth! Depth can be calculated by going up the parent chain, if needed. But it shouldn't be needed.
  • Flags field lets you store temporary bitmask data in each node to help your algorithms. Semantics is up to you.

Note that this class does not check invariants (like uniqueness of row IDs).

Not thread-safe.

This method does not and should not implement Forest because it does not provide indexed access.

See Also:
  • Constructor Details

    • RowTree

      public RowTree()
  • Method Details

    • getSuperRoot

      public RowTree.Node getSuperRoot()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • appendForest

      public void appendForest(@NotNull Forest forest)
      Appends forest to the end of the tree. Forest roots also become roots in the RowTree.
      Parameters:
      forest - the forest
    • appendForest

      public void appendForest(@NotNull Forest forest, @NotNull RowTree.Node underNode, @Nullable RowTree.Node afterNode)
      Appends forest at the specified place.
      Parameters:
      forest - the forest
      underNode - the would-be parent
      afterNode - the would-be preceding sibling, or null if the inserted nodes should come first under underNode
    • appendForest

      public int appendForest(@NotNull Forest forest, @NotNull RowTree.Node parentNode, @Nullable RowTree.Node afterNode, int parentIndex, int flags, @Nullable LongPredicate rowFilter)
      Generic utility method for adding the forest to the RowTree, with parameters for optimization.
      Parameters:
      forest - the source forest
      parentNode - the would-be parent
      afterNode - the would-be preceding sibling, or null if the inserted nodes should come first under underNode
      parentIndex - the index of the source row in forest - only children of of that row and their sub-trees are added (the row itself is not added); use -1 to insert the whole forest
      flags - initial flags for all created nodes
      rowFilter - when not null, each node is tested with this predicate; failed nodes and their sub-trees are skipped (even if some row in the sub-tree passes the filter)
      Returns:
      the index in the forest where iteration stopped (subtreeEnd for the parentNode)
    • insertNode

      public RowTree.Node insertNode(long rowId, int flags, RowTree.Node parentNode, RowTree.Node afterNode)
      Adds a node to the tree at the specified position.
      Parameters:
      rowId - row ID
      parentNode - the would-be parent
      afterNode - the would-be preceding sibling, or null if the inserted nodes should come first under underNode
      flags - initial flags for all created nodes
      Returns:
      inserted node
    • moveNode

      public RowTree.Node moveNode(RowTree.Node node, RowTree.Node parentNode, RowTree.Node afterNode)
      Moves node within a tree
      Parameters:
      node - node to move
      parentNode - the would-be parent
      afterNode - the would-be preceding sibling, or null if the inserted nodes should come first under underNode
      Returns:
      node
    • remove

      public void remove(RowTree.Node node)
      Removes node and its sub-tree from the tree.
      Parameters:
      node - node
    • toForest

      public ArrayForest toForest()
      Creates Forest representing this RowTree.
      Returns:
      ArrayForest
    • createNode

      protected RowTree.Node createNode(long rowId, int flags)
      Called always to create a new node for insertion in this tree. Intended to be overridden with additional checks and maintaining state.
    • forgetNode

      protected void forgetNode(RowTree.Node node)
      Called always when a node is removed (with its sub-nodes). Not called for sub-nodes explicitly. Intended to be overridden with additional checks and maintaining state.