src/cx/tree.h

changeset 901
109567325fe7
parent 900
793fe631e877
child 902
5ed7f634f046
equal deleted inserted replaced
900:793fe631e877 901:109567325fe7
792 offsetof(struct cx_tree_node_base_s, last_child),\ 792 offsetof(struct cx_tree_node_base_s, last_child),\
793 offsetof(struct cx_tree_node_base_s, prev), \ 793 offsetof(struct cx_tree_node_base_s, prev), \
794 offsetof(struct cx_tree_node_base_s, next) 794 offsetof(struct cx_tree_node_base_s, next)
795 795
796 /** 796 /**
797 * Macro for obtaining the node pointer layout for a specific tree.
798 */
799 #define cx_tree_node_layout(tree) \
800 (tree)->loc_parent,\
801 (tree)->loc_children,\
802 (tree)->loc_last_child,\
803 (tree)->loc_prev, \
804 (tree)->loc_next
805
806 /**
797 * The class definition for arbitrary trees. 807 * The class definition for arbitrary trees.
798 */ 808 */
799 struct cx_tree_class_s { 809 struct cx_tree_class_s {
800 /** 810 /**
801 * Destructor function. 811 * Destructor function.
1097 __attribute__((__nonnull__, __warn_unused_result__)) 1107 __attribute__((__nonnull__, __warn_unused_result__))
1098 static inline CxTreeVisitor cxTreeVisitor(CxTree *tree) { 1108 static inline CxTreeVisitor cxTreeVisitor(CxTree *tree) {
1099 return tree->cl->visitor(tree); 1109 return tree->cl->visitor(tree);
1100 } 1110 }
1101 1111
1112 /**
1113 * Adds a new node to the tree.
1114 *
1115 * \attention The node may be externally created, but MUST obey the same rules
1116 * as if it was created by the tree itself with #cxTreeAddChild() (e.g. use
1117 * the same allocator).
1118 *
1119 * @param tree the tree
1120 * @param parent the parent of the node to add
1121 * @param child the node to add
1122 */
1123 __attribute__((__nonnull__))
1124 static inline void cxTreeAddChildNode(
1125 CxTree *tree,
1126 void *parent,
1127 void *child) {
1128 cx_tree_link(parent, child, cx_tree_node_layout(tree));
1129 tree->size++;
1130 }
1131
1132 /**
1133 * Creates a new node and adds it to the tree.
1134 *
1135 * With this function you can decide where exactly the new node shall be added.
1136 * If you specified an appropriate search function, you may want to consider
1137 * leaving this task to the tree by using #cxTreeInsert().
1138 *
1139 * Be aware that adding nodes at arbitrary locations in the tree might cause
1140 * wrong or undesired results when subsequently invoking #cxTreeInsert() and
1141 * the invariant imposed by the search function does not hold any longer.
1142 *
1143 * @param tree the tree
1144 * @param parent the parent node of the new node
1145 * @param data the data that will be submitted to the create function
1146 * @return zero when the new node was created, non-zero on allocation failure
1147 * @see cxTreeInsert()
1148 */
1149 __attribute__((__nonnull__))
1150 int cxTreeAddChild(
1151 CxTree *tree,
1152 void *parent,
1153 const void *data
1154 );
1155
1156 /**
1157 * Removes a node from the tree.
1158 *
1159 * If the node is not part of the tree, the behavior is undefined.
1160 *
1161 * \note The destructor function, if any, will \em not be invoked.
1162 *
1163 * @param tree the tree
1164 * @param node the node to remove
1165 */
1166 __attribute__((__nonnull__))
1167 static inline void cxTreeRemove(
1168 CxTree *tree,
1169 void *node) {
1170 cx_tree_unlink(node, cx_tree_node_layout(tree));
1171 tree->size--;
1172 }
1173
1102 #ifdef __cplusplus 1174 #ifdef __cplusplus
1103 } // extern "C" 1175 } // extern "C"
1104 #endif 1176 #endif
1105 1177
1106 #endif //UCX_TREE_H 1178 #endif //UCX_TREE_H

mercurial