9 typedef struct W_TreeNode
{
12 /*unsigned int uflags:16;*/
18 struct W_TreeNode
*parent
;
20 WMFreeDataProc
*destructor
;
27 destroyNode(void *data
)
29 WMTreeNode
*aNode
= (WMTreeNode
*) data
;
31 if (aNode
->destructor
) {
32 (*aNode
->destructor
)(aNode
->data
);
35 WMFreeArray(aNode
->leaves
);
42 WMCreateTreeNode(void *data
)
44 return WMCreateTreeNodeWithDestructor(data
, NULL
);
49 WMCreateTreeNodeWithDestructor(void *data
, WMFreeDataProc
*destructor
)
53 aNode
= (WMTreeNode
*) wmalloc(sizeof(W_TreeNode
));
54 memset(aNode
, 0, sizeof(W_TreeNode
));
56 aNode
->destructor
= destructor
;
62 /*aNode->leaves = WMCreateArrayWithDestructor(1, destroyNode);*/
69 WMInsertItemInTree(WMTreeNode
*parent
, int index
, void *item
)
73 wassertrv(parent
!=NULL
, NULL
);
75 aNode
= WMCreateTreeNodeWithDestructor(item
, parent
->destructor
);
76 aNode
->parent
= parent
;
77 aNode
->depth
= parent
->depth
+1;
78 if (!parent
->leaves
) {
79 parent
->leaves
= WMCreateArrayWithDestructor(1, destroyNode
);
82 WMAddToArray(parent
->leaves
, aNode
);
84 WMInsertInArray(parent
->leaves
, index
, aNode
);
92 updateNodeDepth(WMTreeNode
*aNode
, int depth
)
99 for (i
=0; i
<WMGetArrayItemCount(aNode
->leaves
); i
++) {
100 updateNodeDepth(WMGetFromArray(aNode
->leaves
, i
), depth
+1);
107 WMInsertNodeInTree(WMTreeNode
*parent
, int index
, WMTreeNode
*aNode
)
109 wassertrv(parent
!=NULL
, NULL
);
110 wassertrv(aNode
!=NULL
, NULL
);
112 aNode
->parent
= parent
;
113 updateNodeDepth(aNode
, parent
->depth
+1);
114 if (!parent
->leaves
) {
115 parent
->leaves
= WMCreateArrayWithDestructor(1, destroyNode
);
118 WMAddToArray(parent
->leaves
, aNode
);
120 WMInsertInArray(parent
->leaves
, index
, aNode
);
128 WMDestroyTreeNode(WMTreeNode
*aNode
)
130 wassertr(aNode
!=NULL
);
132 if (aNode
->parent
&& aNode
->parent
->leaves
) {
133 WMRemoveFromArray(aNode
->parent
->leaves
, aNode
);
141 WMDeleteLeafForTreeNode(WMTreeNode
*aNode
, int index
)
143 wassertr(aNode
!=NULL
);
144 wassertr(aNode
->leaves
!=NULL
);
146 WMDeleteFromArray(aNode
->leaves
, index
);
151 sameData(void *item
, void *data
)
153 return (((WMTreeNode
*)item
)->data
== data
);
158 WMRemoveLeafForTreeNode(WMTreeNode
*aNode
, void *leaf
)
162 wassertr(aNode
!=NULL
);
163 wassertr(aNode
->leaves
!=NULL
);
165 index
= WMFindInArray(aNode
->leaves
, sameData
, leaf
);
166 if (index
!= WANotFound
) {
167 WMDeleteFromArray(aNode
->leaves
, index
);
173 WMReplaceDataForTreeNode(WMTreeNode
*aNode
, void *newData
)
177 wassertrv(aNode
!=NULL
, NULL
);
180 aNode
->data
= newData
;
187 WMGetDataForTreeNode(WMTreeNode
*aNode
)
194 WMGetTreeNodeDepth(WMTreeNode
*aNode
)
201 WMGetParentForTreeNode(WMTreeNode
*aNode
)
203 return aNode
->parent
;
208 WMSortLeavesForTreeNode(WMTreeNode
*aNode
, WMCompareDataProc
*comparer
)
210 wassertr(aNode
!=NULL
);
213 WMSortArray(aNode
->leaves
, comparer
);
219 sortLeavesForNode(WMTreeNode
*aNode
, WMCompareDataProc
*comparer
)
226 WMSortArray(aNode
->leaves
, comparer
);
227 for (i
=0; i
<WMGetArrayItemCount(aNode
->leaves
); i
++) {
228 sortLeavesForNode(WMGetFromArray(aNode
->leaves
, i
), comparer
);
234 WMSortTree(WMTreeNode
*aNode
, WMCompareDataProc
*comparer
)
236 wassertr(aNode
!=NULL
);
238 sortLeavesForNode(aNode
, comparer
);
243 findNodeInTree(WMTreeNode
*aNode
, WMMatchDataProc
*match
, void *cdata
)
246 if (aNode
->data
== cdata
) {
250 if ((*match
)(aNode
->data
, cdata
)) {
259 for (i
=0; i
<WMGetArrayItemCount(aNode
->leaves
); i
++) {
260 leaf
= findNodeInTree(WMGetFromArray(aNode
->leaves
, i
), match
, cdata
);
272 WMFindInTree(WMTreeNode
*aTree
, WMMatchDataProc
*match
, void *cdata
)
274 wassertrv(aTree
!=NULL
, NULL
);
276 return findNodeInTree(aTree
, match
, cdata
);