-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.cpp
445 lines (371 loc) · 10.8 KB
/
tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
* Copyright (c) Microsoft Corporation. All Rights Reserved.
*/
/*
* tree.cpp
*
* data type providing a map between a KEY and a VALUE. The KEY is a
* 32-bit DWORD, and the VALUE is any arbitrary area of storage.
*
* memory is allocated using HeapAlloc.
*
* currently implemented as a unbalanced binary tree.
*
*/
#include "precomp.h"
#include "tree.h"
/* -- data types ----------------------------------------------- */
/* on creating a tree, we return a TREE handle. This is in fact a pointer
* to a struct tree, defined here.
*/
struct tree {
TREEITEM first;
};
/* each element in the tree is stored in a TREEITEM. a TREEITEM handle
* is a pointer to a struct treeitem, defined here
*/
struct treeitem {
TREE root;
TREEKEY key;
TREEITEM left, right;
UINT length; /* length of the user's data */
LPVOID data; /* pointer to our copy of the users data */
};
/* -- internal functions ---------------------------------------------*/
/* free up an element of the tree. recursively calls itself to
* free left and right children
*/
void
tree_delitem(TREEITEM item)
{
if (item == NULL) {
return;
}
if (item->left != NULL) {
tree_delitem(item->left);
}
if (item->right != NULL) {
tree_delitem(item->right);
}
if (item->data != NULL) {
HeapFree(GetProcessHeap(), NULL, item->data);
}
HeapFree(GetProcessHeap(), NULL, item);
}
/* create a new treeitem, with a data block of length bytes.
* if the value pointer is not NULL, initialise the data block with
* the contents of value.
*/
TREEITEM
tree_newitem(TREE root, TREEKEY key, LPVOID value, UINT length)
{
TREEITEM item;
item = (TREEITEM) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct treeitem));
if (item == NULL)
return NULL;
item->root = root;
item->key = key;
item->left = NULL;
item->right = NULL;
item->length = length;
item->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, length);
if (item->data == NULL)
return NULL;
if (value != NULL) {
memcpy(item->data, value, length);
}
return(item);
}
/* find the item with the given key. if it does not exist, return
* the parent item to which it would be attached. returns NULL if
* no items in the tree
*/
TREEITEM
tree_getitem(TREE tree, TREEKEY key)
{
TREEITEM item, prev;
prev = NULL;
for (item = tree->first; item != NULL; ) {
if (item->key == key) {
return(item);
}
/* not this item - go on to the correct child item.
* remember this item as if the child is NULL, this item
* will be the correct insertion point for the new item
*/
prev = item;
if (key < item->key) {
item = item->left;
} else {
item = item->right;
}
}
/* prev is the parent - or null if nothing in tree */
return(prev);
}
/* --- external functions ------------------------------------------ */
/*
* create an empty tree.
*/
TREE APIENTRY
tree_create()
{
TREE tree;
tree = (TREE) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct tree));
if (tree == NULL)
return NULL;
tree->first = NULL;
return(tree);
}
/*
* delete an entire tree, including all the user data
*/
void APIENTRY
tree_delete(TREE tree)
{
tree_delitem(tree->first);
HeapFree(GetProcessHeap(), NULL, tree);
}
/*
* add a new element to the tree, mapping the key given to the value given.
* The value is a block of storage: a copy of this is inserted into the tree.
* we return a pointer to the copy of the data in the tree.
*
* the value pointer can be NULL: in this case, we insert a block of
* length bytes, but don't initialise it. you get a pointer to it and
* can initialise it yourself.
*
* if the key already exists, the value will be replaced with the new data.
*/
LPVOID APIENTRY
tree_update(TREE tree, TREEKEY key, LPVOID value, UINT length)
{
TREEITEM item;
/* find the place in the tree for this key to go */
item = tree_getitem(tree, key);
if (item == NULL) {
/* there is nothing in the tree: this item should
* go at the top
*/
tree->first = tree_newitem(tree, key, value, length);
return(tree->first->data);
}
/* is this the same key? */
if (item->key == key) {
/* this key already inserted. re-alloc the data */
if (length != item->length) {
HeapFree(GetProcessHeap(), NULL, item->data);
item->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, length);
if (item->data == NULL)
return NULL;
}
/* don't initialise block if no pointer passed */
if (value != NULL) {
memcpy(item->data, value, length);
}
return(item->data);
}
/* not the same key - getitem returned the parent for
* the new tree. insert it as a child of item.
*/
return(tree_addafter(tree, &item, key, value, length));
}
/*
* return a pointer to the value (data block) for a given key. returns
* null if not found.
*/
LPVOID APIENTRY
tree_find(TREE tree, TREEKEY key)
{
TREEITEM item;
/* find the correct place in the tree */
item = tree_getitem(tree, key);
if (item == NULL) {
/* nothing in the tree */
return(NULL);
}
if (item->key != key) {
/* this key not in. getitem has returned parent */
return(NULL);
}
/* found the right element - return pointer to the
* data block
*/
return(item->data);
}
/*
* next two routines are an optimisation for a common tree operation. in
* this case, the user will want to insert a new element only if
* the key is not there. if it is there, he will want to modify the
* existing value (increment a reference count, for example).
*
* if tree_search fails to find the key, it will return a TREEITEM handle
* for the parent. This can be passed to tree_addafter to insert the
* new element without re-searching the tree.
*/
/*
* find an element. if not, find it's correct parent item
*/
LPVOID APIENTRY
tree_search(TREE tree, TREEKEY key, PTREEITEM pplace)
{
TREEITEM item;
item = tree_getitem(tree, key);
if (item == NULL) {
/* no items in tree. set placeholder to NULL to
* indicate insert at top of tree
*/
*pplace = NULL;
/* return NULL to indicate key not found */
return(NULL);
}
if (item->key == key) {
/* found the key already there -
* set pplace to null just for safety
*/
*pplace = NULL;
/* give the user a pointer to his data */
return(item->data);
}
/* key was not found - getitem has returned the parent
* - set this as the place for new insertions
*/
*pplace = item;
/* return NULL to indicate that the key was not found */
return(NULL);
}
/*
* insert a key in the position already found by tree_search.
*
* return a pointer to the user's data in the tree. if the value
* pointer passed in is null, then we allocate the block, but don't
* initialise it to anything.
*/
LPVOID APIENTRY
tree_addafter(TREE tree, PTREEITEM place, TREEKEY key, LPVOID value, UINT length)
{
TREEITEM item, child;
item = *place;
if (item == NULL) {
tree->first = tree_newitem(tree, key, value, length);
return (tree->first->data);
}
child = tree_newitem(tree, key, value, length);
if (child->key < item->key ) {
/* should go on left leg */
if (item->left != NULL) {
Trace_Error(NULL, "TREE: left leaf leg not free", FALSE);
}
item->left = child;
} else {
if (item->right != NULL) {
Trace_Error(NULL, "TREE: right leaf leg not free", FALSE);
}
item->right = child;
}
return(child->data);
}
/* --- ctree ------------------------------------------------------*/
/*
* ctree is a class of tree built on top of the tree interface. a
* ctree keeps count of the number of insertions of identical keys.
*
* we do this be adding a long counter to the beginning of the user
* data before inserting into the tree. if the key is not found, we set
* this to one. If the key was already there, we *do not* insert the
* data (data is always from the first insertion) - we simply increment
* the count.
*/
/*
* create a tree for use by CTREE - same as an ordinary tree
*/
TREE APIENTRY
ctree_create()
{
return(tree_create());
}
/*
* delete a ctree - same as for TREE
*/
void APIENTRY
ctree_delete(TREE tree)
{
tree_delete(tree);
}
/* insert an element in the tree. if the element is not there,
* insert the data and set the reference count for this key to 1.
* if the key was there already, don't change the data, just increment
* the reference count
*
* if the value pointer is not null, we initialise the value block
* in the tree to contain this.
*
* we return a pointer to the users data in the tree
*/
LPVOID APIENTRY
ctree_update(TREE tree, TREEKEY key, LPVOID value, UINT length)
{
TREEITEM item;
LONG_PTR FAR * pcounter;
LPVOID datacopy;
pcounter = (LONG_PTR *)tree_search(tree, key, &item);
if (pcounter == NULL) {
/* element not found - insert a new one
* the data block for this element should be
* the user's block with our reference count at
* the beginning
*/
pcounter = (LONG_PTR *)tree_addafter(tree, &item, key, NULL,
length + sizeof(LONG_PTR));
*pcounter = 1;
/* add on size of one long to get the start of the user
* data
*/
datacopy = pcounter + 1;
if (value != NULL) {
memcpy(datacopy, value, length);
}
return(datacopy);
}
/* key was already there - increment reference count and
* return pointer to data
*/
(*pcounter)++;
/* add on size of one long to get the start of the user
* data
*/
datacopy = pcounter + 1;
return(datacopy);
}
/* return the reference count for this key */
long APIENTRY
ctree_getcount(TREE tree, TREEKEY key)
{
LONG_PTR FAR * pcounter;
pcounter = (LONG_PTR *)tree_find(tree, key);
if (pcounter == NULL) {
return(0);
}
return((long)*pcounter);
}
/* return a pointer to the user's data block for this key,
* or NULL if key not present
*/
LPVOID APIENTRY
ctree_find(TREE tree, TREEKEY key)
{
LONG_PTR FAR * pcounter;
pcounter = (LONG_PTR *)tree_find(tree, key);
if (pcounter == NULL) {
return(0);
}
/* increment pointer by size of 1 long to point to
* user's datablock
*/
return(pcounter+1);
}