9b78af230f78da3301c75c49e27b825944e6bfbe
[pspp-builds.git] / src / libpspp / bt.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 /* Balanced tree (BT) data structure.
20
21    The client should not need to be aware of the form of
22    balancing applied to the balanced tree, as its operation is
23    fully encapsulated.  The current implementation is a scapegoat
24    tree.  Scapegoat trees have the advantage over many other
25    forms of balanced trees that they do not store any additional
26    information in each node; thus, they are slightly more
27    space-efficient than, say, red-black or AVL trees.  Compared
28    to splay trees, scapegoat trees provide guaranteed logarithmic
29    worst-case search time and do not restructure the tree during
30    a search.
31
32    For information on scapegoat trees, see Galperin and Rivest,
33    "Scapegoat Trees", Proc. 4th ACM-SIAM Symposium on Discrete
34    Algorithms, or <http://en.wikipedia.org/wiki/Scapegoat_tree>,
35    which includes source code and links to other resources, such
36    as the Galperin and Rivest paper.
37
38    One potentially tricky part of scapegoat tree design is the
39    choice of alpha, which is a real constant that must be greater
40    than 0.5 and less than 1.  We must be able to efficiently
41    calculate h_alpha = floor(log(n)/log(1/alpha)) for integer n >
42    0.  One way to do so would be to maintain a table relating
43    h_alpha to the minimum value of n that yields that h_alpha.
44    Then, we can track the value of h_alpha(n) in the number of
45    nodes in the tree n as nodes are inserted and deleted.
46
47    This implementation uses a different approach.  We choose
48    alpha = sqrt(2)/2 = 1/sqrt(2) ~= .707.  Then, computing
49    h_alpha is a matter of computing a logarithm in base sqrt(2).
50    This is easy: we simply compute twice the base-2 logarithm,
51    then adjust upward by 1 if necessary.  See calculate_h_alpha
52    for details. */
53
54 /* These library routines have no external dependencies other
55    than the standard C library.
56
57    If you add routines in this file, please add a corresponding
58    test to bt-test.c.  This test program should achieve 100%
59    coverage of lines and branches in this code, as reported by
60    "gcov -b". */
61
62 #ifdef HAVE_CONFIG_H
63 #include <config.h>
64 #endif
65
66 #include <libpspp/bt.h>
67
68 #include <limits.h>
69 #include <stdbool.h>
70 #include <stdint.h>
71
72 static void rebalance_subtree (struct bt *, struct bt_node *, size_t);
73
74 static struct bt_node **down_link (struct bt *, struct bt_node *);
75 static inline struct bt_node *sibling (struct bt_node *p);
76 static size_t count_nodes_in_subtree (const struct bt_node *);
77
78 static inline int floor_log2 (size_t);
79 static inline int calculate_h_alpha (size_t);
80
81 /* Initializes BT as an empty BT that uses the given COMPARE
82    function, passing in AUX as auxiliary data. */
83 void
84 bt_init (struct bt *bt,
85          bt_compare_func *compare,
86          const void *aux)
87 {
88   bt->root = NULL;
89   bt->compare = compare;
90   bt->aux = aux;
91   bt->size = 0;
92   bt->max_size = 0;
93 }
94
95 /* Inserts the given NODE into BT.
96    Returns a null pointer if successful.
97    Returns the existing node already in BT equal to NODE, on
98    failure. */
99 struct bt_node *
100 bt_insert (struct bt *bt, struct bt_node *node)
101 {
102   int depth = 0;
103
104   node->down[0] = NULL;
105   node->down[1] = NULL;
106
107   if (bt->root == NULL)
108     {
109       bt->root = node;
110       node->up = NULL;
111     }
112   else
113     {
114       struct bt_node *p = bt->root;
115       for (;;)
116         {
117           int cmp, dir;
118
119           cmp = bt->compare (node, p, bt->aux);
120           if (cmp == 0)
121             return p;
122           depth++;
123
124           dir = cmp > 0;
125           if (p->down[dir] == NULL)
126             {
127               p->down[dir] = node;
128               node->up = p;
129               break;
130             }
131           p = p->down[dir];
132         }
133     }
134
135   bt->size++;
136   if (bt->size > bt->max_size)
137     bt->max_size = bt->size;
138
139   if (depth > calculate_h_alpha (bt->size))
140     {
141       /* We use the "alternative" method of finding a scapegoat
142          node described by Galperin and Rivest. */
143       struct bt_node *s = node;
144       size_t size = 1;
145       int i;
146
147       for (i = 1; ; i++)
148         if (i < depth)
149           {
150             size += 1 + count_nodes_in_subtree (sibling (s));
151             s = s->up;
152             if (i > calculate_h_alpha (size))
153               {
154                 rebalance_subtree (bt, s, size);
155                 break;
156               }
157           }
158         else
159           {
160             rebalance_subtree (bt, bt->root, bt->size);
161             bt->max_size = bt->size;
162             break;
163           }
164     }
165
166   return NULL;
167 }
168
169 /* Deletes P from BT. */
170 void
171 bt_delete (struct bt *bt, struct bt_node *p)
172 {
173   struct bt_node **q = down_link (bt, p);
174   struct bt_node *r = p->down[1];
175   if (r == NULL)
176     {
177       *q = p->down[0];
178       if (*q)
179         (*q)->up = p->up;
180     }
181   else if (r->down[0] == NULL)
182     {
183       r->down[0] = p->down[0];
184       *q = r;
185       r->up = p->up;
186       if (r->down[0] != NULL)
187         r->down[0]->up = r;
188     }
189   else
190     {
191       struct bt_node *s = r->down[0];
192       while (s->down[0] != NULL)
193         s = s->down[0];
194       r = s->up;
195       r->down[0] = s->down[1];
196       s->down[0] = p->down[0];
197       s->down[1] = p->down[1];
198       *q = s;
199       if (s->down[0] != NULL)
200         s->down[0]->up = s;
201       s->down[1]->up = s;
202       s->up = p->up;
203       if (r->down[0] != NULL)
204         r->down[0]->up = r;
205     }
206   bt->size--;
207
208   /* We approximate .707 as .75 here.  This is conservative: it
209      will cause us to do a little more rebalancing than strictly
210      necessary to maintain the scapegoat tree's height
211      invariant. */
212   if (bt->size < bt->max_size * 3 / 4 && bt->size > 0)
213     {
214       rebalance_subtree (bt, bt->root, bt->size);
215       bt->max_size = bt->size;
216     }
217 }
218
219 /* Returns the node with minimum value in BT, or a null pointer
220    if BT is empty. */
221 struct bt_node *
222 bt_first (const struct bt *bt)
223 {
224   struct bt_node *p = bt->root;
225   if (p != NULL)
226     while (p->down[0] != NULL)
227       p = p->down[0];
228   return p;
229 }
230
231 /* Returns the node with maximum value in BT, or a null pointer
232    if BT is empty. */
233 struct bt_node *
234 bt_last (const struct bt *bt)
235 {
236   struct bt_node *p = bt->root;
237   if (p != NULL)
238     while (p->down[1] != NULL)
239       p = p->down[1];
240   return p;
241 }
242
243 /* Searches BT for a node equal to TARGET.
244    Returns the node if found, or a null pointer otherwise. */
245 struct bt_node *
246 bt_find (const struct bt *bt, const struct bt_node *target)
247 {
248   const struct bt_node *p;
249   int cmp;
250
251   for (p = bt->root; p != NULL; p = p->down[cmp > 0])
252     {
253       cmp = bt->compare (target, p, bt->aux);
254       if (cmp == 0)
255         return (struct bt_node *) p;
256     }
257
258   return NULL;
259 }
260
261 /* Searches BT for, and returns, the first node in in-order whose
262    value is greater than or equal to TARGET.  Returns a null
263    pointer if all nodes in BT are less than TARGET.
264
265    Another way to look at the return value is that it is the node
266    that would be returned by "bt_next (BT, TARGET)" if TARGET
267    were inserted in BT (assuming that TARGET would not be a
268    duplicate). */
269 struct bt_node *
270 bt_find_ge (const struct bt *bt, const struct bt_node *target)
271 {
272   const struct bt_node *p = bt->root;
273   const struct bt_node *q = NULL;
274   while (p != NULL)
275     {
276       int cmp = bt->compare (target, p, bt->aux);
277       if (cmp > 0)
278         p = p->down[1];
279       else
280         {
281           q = p;
282           if (cmp < 0)
283             p = p->down[0];
284           else
285             break;
286         }
287     }
288   return (struct bt_node *) q;
289 }
290
291 /* Searches BT for, and returns, the last node in in-order whose
292    value is less than or equal to TARGET, which should not be in
293    BT.  Returns a null pointer if all nodes in BT are greater
294    than TARGET.
295
296    Another way to look at the return value is that it is the node
297    that would be returned by "bt_prev (BT, TARGET)" if TARGET
298    were inserted in BT (assuming that TARGET would not be a
299    duplicate). */
300 struct bt_node *
301 bt_find_le (const struct bt *bt, const struct bt_node *target)
302 {
303   const struct bt_node *p = bt->root;
304   const struct bt_node *q = NULL;
305   while (p != NULL)
306     {
307       int cmp = bt->compare (target, p, bt->aux);
308       if (cmp < 0)
309         p = p->down[0];
310       else
311         {
312           q = p;
313           if (cmp > 0)
314             p = p->down[1];
315           else
316             break;
317         }
318     }
319   return (struct bt_node *) q;
320 }
321
322 /* Returns the node in BT following P in in-order.
323    If P is null, returns the minimum node in BT.
324    Returns a null pointer if P is the maximum node in BT or if P
325    is null and BT is empty. */
326 struct bt_node *
327 bt_next (const struct bt *bt, const struct bt_node *p)
328 {
329   if (p == NULL)
330     return bt_first (bt);
331   else if (p->down[1] == NULL)
332     {
333       struct bt_node *q;
334       for (q = p->up; ; p = q, q = q->up)
335         if (q == NULL || p == q->down[0])
336           return q;
337     }
338   else
339     {
340       p = p->down[1];
341       while (p->down[0] != NULL)
342         p = p->down[0];
343       return (struct bt_node *) p;
344     }
345 }
346
347 /* Returns the node in BT preceding P in in-order.
348    If P is null, returns the maximum node in BT.
349    Returns a null pointer if P is the minimum node in BT or if P
350    is null and BT is empty. */
351 struct bt_node *
352 bt_prev (const struct bt *bt, const struct bt_node *p)
353 {
354   if (p == NULL)
355     return bt_last (bt);
356   else if (p->down[0] == NULL)
357     {
358       struct bt_node *q;
359       for (q = p->up; ; p = q, q = q->up)
360         if (q == NULL || p == q->down[1])
361           return q;
362     }
363   else
364     {
365       p = p->down[0];
366       while (p->down[1] != NULL)
367         p = p->down[1];
368       return (struct bt_node *) p;
369     }
370 }
371
372 /* Moves P around in BT to compensate for its key having
373    changed.  Returns a null pointer if successful.  If P's new
374    value is equal to that of some other node in BT, returns the
375    other node after removing P from BT.
376
377    This function is an optimization only if it is likely that P
378    can actually retain its relative position in BT, e.g. its key
379    has only been adjusted slightly.  Otherwise, it is more
380    efficient to simply remove P from BT, change its key, and
381    re-insert P.
382
383    It is not safe to update more than one node's key, then to
384    call this function for each node.  Instead, update a single
385    node's key, call this function, update another node's key, and
386    so on.  Alternatively, remove all affected nodes from the
387    tree, update their keys, then re-insert all of them. */
388 struct bt_node *
389 bt_changed (struct bt *bt, struct bt_node *p)
390 {
391   struct bt_node *prev = bt_prev (bt, p);
392   struct bt_node *next = bt_next (bt, p);
393
394   if ((prev != NULL && bt->compare (prev, p, bt->aux) >= 0)
395       || (next != NULL && bt->compare (p, next, bt->aux) >= 0))
396     {
397       bt_delete (bt, p);
398       return bt_insert (bt, p);
399     }
400   return NULL;
401  }
402
403 /* BT nodes may be moved around in memory as necessary, e.g. as
404    the result of an realloc operation on a block that contains a
405    node.  Once this is done, call this function passing node P
406    that was moved and its BT before attempting any other
407    operation on BT.
408
409    It is not safe to move more than one node, then to call this
410    function for each node.  Instead, move a single node, call
411    this function, move another node, and so on.  Alternatively,
412    remove all affected nodes from the tree, move them, then
413    re-insert all of them. */
414 void
415 bt_moved (struct bt *bt, struct bt_node *p)
416 {
417   if (p->up != NULL)
418     {
419       int d = p->up->down[0] == NULL || bt->compare (p, p->up, bt->aux) > 0;
420       p->up->down[d] = p;
421     }
422   else
423     bt->root = p;
424   if (p->down[0] != NULL)
425     p->down[0]->up = p;
426   if (p->down[1] != NULL)
427     p->down[1]->up = p;
428 }
429 \f
430 /* Tree rebalancing.
431
432    This algorithm is from Q. F. Stout and B. L. Warren, "Tree
433    Rebalancing in Optimal Time and Space", CACM 29(1986):9,
434    pp. 902-908.  It uses O(N) time and O(1) space to rebalance a
435    subtree that contains N nodes. */
436
437 static void tree_to_vine (struct bt_node **);
438 static void vine_to_tree (struct bt_node **, size_t count);
439
440 /* Rebalances the subtree in BT rooted at SUBTREE, which contains
441    exactly COUNT nodes. */
442 static void
443 rebalance_subtree (struct bt *bt, struct bt_node *subtree, size_t count)
444 {
445   struct bt_node *up = subtree->up;
446   struct bt_node **q = down_link (bt, subtree);
447   tree_to_vine (q);
448   vine_to_tree (q, count);
449   (*q)->up = up;
450 }
451
452 /* Converts the subtree rooted at *Q into a vine (a binary search
453    tree in which all the right links are null), and updates *Q to
454    point to the new root of the subtree. */
455 static void
456 tree_to_vine (struct bt_node **q)
457 {
458   struct bt_node *p = *q;
459   while (p != NULL)
460     if (p->down[1] == NULL)
461       {
462         q = &p->down[0];
463         p = *q;
464       }
465     else
466       {
467         struct bt_node *r = p->down[1];
468         p->down[1] = r->down[0];
469         r->down[0] = p;
470         p = r;
471         *q = r;
472       }
473 }
474
475 /* Performs a compression transformation COUNT times, starting at
476    *Q, and updates *Q to point to the new root of the subtree. */
477 static void
478 compress (struct bt_node **q, unsigned long count)
479 {
480   while (count--)
481     {
482       struct bt_node *red = *q;
483       struct bt_node *black = red->down[0];
484
485       *q = black;
486       red->down[0] = black->down[1];
487       black->down[1] = red;
488       red->up = black;
489       if (red->down[0] != NULL)
490         red->down[0]->up = red;
491       q = &black->down[0];
492     }
493 }
494
495 /* Converts the vine rooted at *Q, which contains exactly COUNT
496    nodes, into a balanced tree, and updates *Q to point to the
497    new root of the balanced tree. */
498 static void
499 vine_to_tree (struct bt_node **q, size_t count)
500 {
501   size_t leaf_nodes = count + 1 - ((size_t) 1 << floor_log2 (count + 1));
502   size_t vine_nodes = count - leaf_nodes;
503
504   compress (q, leaf_nodes);
505   while (vine_nodes > 1)
506     {
507       vine_nodes /= 2;
508       compress (q, vine_nodes);
509     }
510   while ((*q)->down[0] != NULL)
511     {
512       (*q)->down[0]->up = *q;
513       q = &(*q)->down[0];
514     }
515 }
516 \f
517 /* Other binary tree helper functions. */
518
519 /* Returns the address of the pointer that points down to P
520    within BT. */
521 static struct bt_node **
522 down_link (struct bt *bt, struct bt_node *p)
523 {
524   struct bt_node *q = p->up;
525   return q != NULL ? &q->down[q->down[0] != p] : &bt->root;
526 }
527
528 /* Returns node P's sibling; that is, the other child of its
529    parent.  P must not be the root. */
530 static inline struct bt_node *
531 sibling (struct bt_node *p)
532 {
533   struct bt_node *q = p->up;
534   return q->down[q->down[0] == p];
535 }
536
537 /* Returns the number of nodes in the given SUBTREE. */
538 static size_t
539 count_nodes_in_subtree (const struct bt_node *subtree)
540 {
541   /* This is an in-order traversal modified to iterate only the
542      nodes in SUBTREE. */
543   size_t count = 0;
544   if (subtree != NULL)
545     {
546       const struct bt_node *p = subtree;
547       while (p->down[0] != NULL)
548         p = p->down[0];
549       for (;;)
550         {
551           count++;
552           if (p->down[1] != NULL)
553             {
554               p = p->down[1];
555               while (p->down[0] != NULL)
556                 p = p->down[0];
557             }
558           else
559             {
560               for (;;)
561                 {
562                   const struct bt_node *q;
563                   if (p == subtree)
564                     goto done;
565                   q = p;
566                   p = p->up;
567                   if (p->down[0] == q)
568                     break;
569                 }
570             }
571         }
572     }
573  done:
574   return count;
575 }
576 \f
577 /* Arithmetic. */
578
579 /* Returns the number of high-order 0-bits in X.
580    Undefined if X is zero. */
581 static inline int
582 count_leading_zeros (size_t x)
583 {
584 #if __GNUC__ >= 4
585 #if SIZE_MAX > ULONG_MAX
586   return __builtin_clzll (x);
587 #elif SIZE_MAX > UINT_MAX
588   return __builtin_clzl (x);
589 #else
590   return __builtin_clz (x);
591 #endif
592 #else
593   /* This algorithm is from _Hacker's Delight_ section 5.3. */
594   size_t y;
595   int n;
596
597 #define COUNT_STEP(BITS)                        \
598         y = x >> BITS;                          \
599         if (y != 0)                             \
600           {                                     \
601             n -= BITS;                          \
602             x = y;                              \
603           }
604
605   n = sizeof (size_t) * CHAR_BIT;
606 #if SIZE_MAX >> 31 >> 31 >> 2
607   COUNT_STEP (64);
608 #endif
609 #if SIZE_MAX >> 31 >> 1
610   COUNT_STEP (32);
611 #endif
612   COUNT_STEP (16);
613   COUNT_STEP (8);
614   COUNT_STEP (4);
615   COUNT_STEP (2);
616   y = x >> 1;
617   return y != 0 ? n - 2 : n - x;
618 #endif
619 }
620
621 /* Returns floor(log2(x)).
622    Undefined if X is zero. */
623 static inline int
624 floor_log2 (size_t x)
625 {
626   return sizeof (size_t) * CHAR_BIT - 1 - count_leading_zeros (x);
627 }
628
629 /* Returns floor(pow(sqrt(2), x * 2 + 1)).
630    Defined for X from 0 up to the number of bits in size_t minus
631    1. */
632 static inline size_t
633 pow_sqrt2 (int x)
634 {
635   /* These constants are sqrt(2) multiplied by 2**63 or 2**31,
636      respectively, and then rounded to nearest. */
637 #if SIZE_MAX >> 31 >> 1
638   return (UINT64_C(0xb504f333f9de6484) >> (63 - x)) + 1;
639 #else
640   return (0xb504f334 >> (31 - x)) + 1;
641 #endif
642 }
643
644 /* Returns floor(log(n)/log(sqrt(2))).
645    Undefined if N is 0. */
646 static inline int
647 calculate_h_alpha (size_t n)
648 {
649   int log2 = floor_log2 (n);
650
651   /* The correct answer is either 2 * log2 or one more.  So we
652      see if n >= pow(sqrt(2), 2 * log2 + 1) and if so, add 1. */
653   return (2 * log2) + (n >= pow_sqrt2 (log2));
654 }
655