Class Node

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class Node extends ProxyInstance
The GNode struct represents one node in a n-ary tree.
  • Constructor Details

    • Node

      public Node(MemorySegment address)
      Create a Node proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Node

      public Node(Arena arena)
      Allocate a new Node.
      Parameters:
      arena - to control the memory allocation scope
    • Node

      public Node()
      Allocate a new Node. The memory is allocated with Arena.ofAuto().
    • Node

      public Node(MemorySegment data, Node next, Node prev, Node parent, Node children, Arena arena)
      Allocate a new Node with the fields set to the provided values.
      Parameters:
      data - value for the field data
      next - value for the field next
      prev - value for the field prev
      parent - value for the field parent
      children - value for the field children
      arena - to control the memory allocation scope
    • Node

      public Node(MemorySegment data, Node next, Node prev, Node parent, Node children)
      Allocate a new Node with the fields set to the provided values. The memory is allocated with Arena.ofAuto().
      Parameters:
      data - value for the field data
      next - value for the field next
      prev - value for the field prev
      parent - value for the field parent
      children - value for the field children
  • Method Details

    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • readData

      public MemorySegment readData()
      Read the value of the field data.
      Returns:
      The value of the field data
    • writeData

      public void writeData(MemorySegment data)
      Write a value in the field data.
      Parameters:
      data - The new value for the field data
    • readNext

      public Node readNext()
      Read the value of the field next.
      Returns:
      The value of the field next
    • writeNext

      public void writeNext(Node next)
      Write a value in the field next.
      Parameters:
      next - The new value for the field next
    • readPrev

      public Node readPrev()
      Read the value of the field prev.
      Returns:
      The value of the field prev
    • writePrev

      public void writePrev(Node prev)
      Write a value in the field prev.
      Parameters:
      prev - The new value for the field prev
    • readParent

      public Node readParent()
      Read the value of the field parent.
      Returns:
      The value of the field parent
    • writeParent

      public void writeParent(Node parent)
      Write a value in the field parent.
      Parameters:
      parent - The new value for the field parent
    • readChildren

      public Node readChildren()
      Read the value of the field children.
      Returns:
      The value of the field children
    • writeChildren

      public void writeChildren(Node children)
      Write a value in the field children.
      Parameters:
      children - The new value for the field children
    • new_

      public static Node new_(@Nullable MemorySegment data)
      Creates a new GNode containing the given data. Used to create the first node in a tree.
      Parameters:
      data - the data of the new node
      Returns:
      a new GNode
    • popAllocator

      @Deprecated public static void popAllocator()
      Deprecated.
    • pushAllocator

      @Deprecated public static void pushAllocator(Allocator allocator)
      Deprecated.
    • childIndex

      public int childIndex(@Nullable MemorySegment data)
      Gets the position of the first child of a GNode which contains the given data.
      Parameters:
      data - the data to find
      Returns:
      the index of the child of this Node which contains data, or -1 if the data is not found
    • childPosition

      public int childPosition(Node child)
      Gets the position of a GNode with respect to its siblings. child must be a child of node. The first child is numbered 0, the second 1, and so on.
      Parameters:
      child - a child of this Node
      Returns:
      the position of child with respect to its siblings
    • childrenForeach

      public void childrenForeach(Set<TraverseFlags> flags, @Nullable NodeForeachFunc func)
      Calls a function for each of the children of a GNode. Note that it doesn't descend beneath the child nodes. func must not do anything that would modify the structure of the tree.
      Parameters:
      flags - which types of children are to be visited, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      func - the function to call for each visited node
    • childrenForeach

      public void childrenForeach(TraverseFlags flags, @Nullable NodeForeachFunc func)
      Calls a function for each of the children of a GNode. Note that it doesn't descend beneath the child nodes. func must not do anything that would modify the structure of the tree.
      Parameters:
      flags - which types of children are to be visited, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      func - the function to call for each visited node
    • copy

      public Node copy()
      Recursively copies a GNode (but does not deep-copy the data inside the nodes, see g_node_copy_deep() if you need that).
      Returns:
      a new GNode containing the same data pointers
    • copyDeep

      public Node copyDeep(@Nullable CopyFunc copyFunc)
      Recursively copies a GNode and its data.
      Parameters:
      copyFunc - the function which is called to copy the data inside each node, or null to use the original data.
      Returns:
      a new GNode containing copies of the data in node.
      Since:
      2.4
    • depth

      public int depth()

      Gets the depth of a GNode.

      If this Node is null the depth is 0. The root node has a depth of 1. For the children of the root node the depth is 2. And so on.

      Returns:
      the depth of the GNode
    • destroy

      public void destroy()
      Removes this Node and its children from the tree, freeing any memory allocated.
    • find

      public Node find(TraverseType order, Set<TraverseFlags> flags, @Nullable MemorySegment data)
      Finds a GNode in a tree.
      Parameters:
      order - the order in which nodes are visited - TraverseType.IN_ORDER, TraverseType.PRE_ORDER, TraverseType.POST_ORDER, or TraverseType.LEVEL_ORDER
      flags - which types of children are to be searched, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      data - the data to find
      Returns:
      the found GNode, or null if the data is not found
    • find

      public Node find(TraverseType order, TraverseFlags flags, @Nullable MemorySegment data)
      Finds a GNode in a tree.
      Parameters:
      order - the order in which nodes are visited - TraverseType.IN_ORDER, TraverseType.PRE_ORDER, TraverseType.POST_ORDER, or TraverseType.LEVEL_ORDER
      flags - which types of children are to be searched, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      data - the data to find
      Returns:
      the found GNode, or null if the data is not found
    • findChild

      public Node findChild(Set<TraverseFlags> flags, @Nullable MemorySegment data)
      Finds the first child of a GNode with the given data.
      Parameters:
      flags - which types of children are to be searched, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      data - the data to find
      Returns:
      the found child GNode, or null if the data is not found
    • findChild

      public Node findChild(TraverseFlags flags, @Nullable MemorySegment data)
      Finds the first child of a GNode with the given data.
      Parameters:
      flags - which types of children are to be searched, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      data - the data to find
      Returns:
      the found child GNode, or null if the data is not found
    • firstSibling

      public Node firstSibling()
      Gets the first sibling of a GNode. This could possibly be the node itself.
      Returns:
      the first sibling of this Node
    • getRoot

      public Node getRoot()
      Gets the root of a tree.
      Returns:
      the root of the tree
    • insert

      public Node insert(int position, Node node)
      Inserts a GNode beneath the parent at the given position.
      Parameters:
      position - the position to place node at, with respect to its siblings If position is -1, node is inserted as the last child of this Node
      node - the GNode to insert
      Returns:
      the inserted GNode
    • insertAfter

      public Node insertAfter(Node sibling, Node node)
      Inserts a GNode beneath the parent after the given sibling.
      Parameters:
      sibling - the sibling GNode to place node after. If sibling is null, the node is inserted as the first child of parent.
      node - the GNode to insert
      Returns:
      the inserted GNode
    • insertBefore

      public Node insertBefore(Node sibling, Node node)
      Inserts a GNode beneath the parent before the given sibling.
      Parameters:
      sibling - the sibling GNode to place node before. If sibling is null, the node is inserted as the last child of parent.
      node - the GNode to insert
      Returns:
      the inserted GNode
    • isAncestor

      public boolean isAncestor(Node descendant)
      Returns true if this Node is an ancestor of descendant. This is true if node is the parent of descendant, or if node is the grandparent of descendant etc.
      Parameters:
      descendant - a GNode
      Returns:
      true if this Node is an ancestor of descendant
    • lastChild

      public Node lastChild()
      Gets the last child of a GNode.
      Returns:
      the last child of node, or null if this Node has no children
    • lastSibling

      public Node lastSibling()
      Gets the last sibling of a GNode. This could possibly be the node itself.
      Returns:
      the last sibling of this Node
    • maxHeight

      public int maxHeight()

      Gets the maximum height of all branches beneath a GNode. This is the maximum distance from the GNode to all leaf nodes.

      If this Node is null, 0 is returned. If this Node has no children, 1 is returned. If this Node has children, 2 is returned. And so on.

      Returns:
      the maximum height of the tree beneath this Node
    • nChildren

      public int nChildren()
      Gets the number of children of a GNode.
      Returns:
      the number of children of this Node
    • nNodes

      public int nNodes(Set<TraverseFlags> flags)
      Gets the number of nodes in a tree.
      Parameters:
      flags - which types of children are to be counted, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      Returns:
      the number of nodes in the tree
    • nNodes

      public int nNodes(TraverseFlags... flags)
      Gets the number of nodes in a tree.
      Parameters:
      flags - which types of children are to be counted, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      Returns:
      the number of nodes in the tree
    • nthChild

      public Node nthChild(int n)
      Gets a child of a GNode, using the given index. The first child is at index 0. If the index is too big, null is returned.
      Parameters:
      n - the index of the desired child
      Returns:
      the child of this Node at index n
    • prepend

      public Node prepend(Node node)
      Inserts a GNode as the first child of the given parent.
      Parameters:
      node - the GNode to insert
      Returns:
      the inserted GNode
    • reverseChildren

      public void reverseChildren()
      Reverses the order of the children of a GNode. (It doesn't change the order of the grandchildren.)
    • traverse

      public void traverse(TraverseType order, Set<TraverseFlags> flags, int maxDepth, @Nullable NodeTraverseFunc func)
      Traverses a tree starting at the given root GNode. It calls the given function for each node visited. The traversal can be halted at any point by returning true from func. func must not do anything that would modify the structure of the tree.
      Parameters:
      order - the order in which nodes are visited - TraverseType.IN_ORDER, TraverseType.PRE_ORDER, TraverseType.POST_ORDER, or TraverseType.LEVEL_ORDER.
      flags - which types of children are to be visited, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      maxDepth - the maximum depth of the traversal. Nodes below this depth will not be visited. If max_depth is -1 all nodes in the tree are visited. If depth is 1, only the root is visited. If depth is 2, the root and its children are visited. And so on.
      func - the function to call for each visited GNode
    • traverse

      public void traverse(TraverseType order, TraverseFlags flags, int maxDepth, @Nullable NodeTraverseFunc func)
      Traverses a tree starting at the given root GNode. It calls the given function for each node visited. The traversal can be halted at any point by returning true from func. func must not do anything that would modify the structure of the tree.
      Parameters:
      order - the order in which nodes are visited - TraverseType.IN_ORDER, TraverseType.PRE_ORDER, TraverseType.POST_ORDER, or TraverseType.LEVEL_ORDER.
      flags - which types of children are to be visited, one of TraverseFlags.ALL, TraverseFlags.LEAVES and TraverseFlags.NON_LEAVES
      maxDepth - the maximum depth of the traversal. Nodes below this depth will not be visited. If max_depth is -1 all nodes in the tree are visited. If depth is 1, only the root is visited. If depth is 2, the root and its children are visited. And so on.
      func - the function to call for each visited GNode
    • unlink

      public void unlink()
      Unlinks a GNode from a tree, resulting in two separate trees.