Date:2010-11-19 23:36:31 (8 years 6 months ago)
Author:Werner Almesberger
Commit:769d31581dfbecd94a97201b7bddad3db9e53582
Message:qpkg: removed non-generic jrb access functions

- jrb.h, jrb.c (jrb_insert_str, jrb_insert_int, jrb_insert_dbl): removed
- jrb.h, jrb.c (jrb_find_str, jrb_find_int, jrb_find_dbl): removed
- jrb.h, jrb.c (jrb_find_gte_str, jrb_find_gte_int, jrb_find_gte_dbl): removed
- jrb.c (jrb_print_tree, jrb_iprint_tree): removed unused tree dump functions
Files: qpkg/jrb.c (4 diffs)
qpkg/jrb.h (2 diffs)

Change Details

qpkg/jrb.c
5050static JRB rprev(JRB n);
5151static void recolor(JRB n);
5252static void single_rotate(JRB y, int l);
53static void jrb_print_tree(JRB t, int level);
54static void jrb_iprint_tree(JRB t, int level);
5553
5654#define isred(n) (n->red)
5755#define isblack(n) (!isred(n))
...... 
167165  return head;
168166}
169167
170JRB jrb_find_gte_str(JRB n, char *key, int *fnd)
171{
172  int cmp;
173
174  *fnd = 0;
175  if (!ishead(n)) {
176    fprintf(stderr, "jrb_find_gte_str called on non-head %p\n", n);
177    exit(1);
178  }
179  if (n->parent == n) return n;
180  cmp = strcmp(key, n->blink->key.s);
181  if (cmp == 0) {
182    *fnd = 1;
183    return n->blink;
184  }
185  if (cmp > 0) return n;
186  else n = n->parent;
187  while (1) {
188    if (isext(n)) return n;
189    cmp = strcmp(key, getlext(n)->key.s);
190    if (cmp == 0) {
191      *fnd = 1;
192      return getlext(n);
193    }
194    if (cmp < 0) n = n->flink ; else n = n->blink;
195  }
196}
197
198JRB jrb_find_str(JRB n, char *key)
199{
200  int fnd;
201  JRB j;
202  j = jrb_find_gte_str(n, key, &fnd);
203  if (fnd) return j; else return NULL;
204}
205
206JRB jrb_find_gte_int(JRB n, int ikey, int *fnd)
207{
208  *fnd = 0;
209  if (!ishead(n)) {
210    fprintf(stderr, "jrb_find_gte_int called on non-head %p\n", n);
211    exit(1);
212  }
213  if (n->parent == n) return n;
214  if (ikey == n->blink->key.i) {
215    *fnd = 1;
216    return n->blink;
217  }
218  if (ikey > n->blink->key.i) return n;
219  else n = n->parent;
220  while (1) {
221    if (isext(n)) return n;
222    if (ikey == getlext(n)->key.i) {
223      *fnd = 1;
224      return getlext(n);
225    }
226    n = (ikey < getlext(n)->key.i) ? n->flink : n->blink;
227  }
228}
229
230JRB jrb_find_int(JRB n, int ikey)
231{
232  int fnd;
233  JRB j;
234
235  j = jrb_find_gte_int(n, ikey, &fnd);
236  if (fnd) return j; else return NULL;
237}
238
239JRB jrb_find_gte_dbl(JRB n, double dkey, int *fnd)
240{
241  *fnd = 0;
242  if (!ishead(n)) {
243    fprintf(stderr, "jrb_find_gte_dbl called on non-head %p\n", n);
244    exit(1);
245  }
246  if (n->parent == n) return n;
247  if (dkey == n->blink->key.d) {
248    *fnd = 1;
249    return n->blink;
250  }
251  if (dkey > n->blink->key.d) return n;
252  else n = n->parent;
253  while (1) {
254    if (isext(n)) return n;
255    if (dkey == getlext(n)->key.d) {
256      *fnd = 1;
257      return getlext(n);
258    }
259    n = (dkey < getlext(n)->key.d) ? n->flink : n->blink;
260  }
261}
262
263JRB jrb_find_dbl(JRB n, double dkey)
264{
265  int fnd;
266  JRB j;
267
268  j = jrb_find_gte_dbl(n, dkey, &fnd);
269  if (fnd) return j; else return NULL;
270}
271
272168JRB jrb_find_gte_gen(JRB n, Jval key,int (*fxn)(Jval, Jval), int *fnd)
273169{
274170  int cmp;
...... 
549445  }
550446}
551447
552
553void jrb_print_tree(JRB t, int level)
554{
555  int i;
556  if (ishead(t) && t->parent == t) {
557    printf("tree %p is empty\n", t);
558  } else if (ishead(t)) {
559    printf("Head: %p. Root = %p\n", t, t->parent);
560    jrb_print_tree(t->parent, 0);
561  } else {
562    if (isext(t)) {
563      for (i = 0; i < level; i++) putchar(' ');
564      printf("Ext node %p: %c,%c: p=%p, k=%s\n",
565              t, isred(t)?'R':'B', isleft(t)?'l':'r', t->parent, t->key.s);
566    } else {
567      jrb_print_tree(t->flink, level+2);
568      jrb_print_tree(t->blink, level+2);
569      for (i = 0; i < level; i++) putchar(' ');
570      printf("Int node %p: %c,%c: l=%p, r=%p, p=%p, lr=(%s,%s)\n",
571              t, isred(t)?'R':'B', isleft(t)?'l':'r', t->flink,
572              t->blink,
573              t->parent, getlext(t)->key.s, getrext(t)->key.s);
574    }
575  }
576}
577
578void jrb_iprint_tree(JRB t, int level)
579{
580  int i;
581  if (ishead(t) && t->parent == t) {
582    printf("tree %p is empty\n", t);
583  } else if (ishead(t)) {
584    printf("Head: %p. Root = %p, < = %p, > = %p\n",
585            t, t->parent, t->blink, t->flink);
586    jrb_iprint_tree(t->parent, 0);
587  } else {
588    if (isext(t)) {
589      for (i = 0; i < level; i++) putchar(' ');
590      printf("Ext node %p: %c,%c: p=%p, <=%p, >=%p k=%d\n",
591              t, isred(t)?'R':'B', isleft(t)?'l':'r', t->parent,
592              t->blink, t->flink, t->key.i);
593    } else {
594      jrb_iprint_tree(t->flink, level+2);
595      jrb_iprint_tree(t->blink, level+2);
596      for (i = 0; i < level; i++) putchar(' ');
597      printf("Int node %p: %c,%c: l=%p, r=%p, p=%p, lr=(%d,%d)\n",
598              t, isred(t)?'R':'B', isleft(t)?'l':'r', t->flink,
599              t->blink,
600              t->parent, getlext(t)->key.i, getrext(t)->key.i);
601    }
602  }
603}
604
605448int jrb_nblack(JRB n)
606449{
607450  int nb;
...... 
650493  return n->val;
651494}
652495
653#if 0
654static JRB jrb_insert_a(JRB nd, Jval key, Jval val)
655{
656  return jrb_insert_b(nd->flink, key, val);
657}
658#endif
659
660JRB jrb_insert_str(JRB tree, char *key, Jval val)
661{
662  Jval k;
663  int fnd;
664
665  k.s = key;
666  return jrb_insert_b(jrb_find_gte_str(tree, key, &fnd), k, val);
667}
668
669JRB jrb_insert_int(JRB tree, int ikey, Jval val)
670{
671  Jval k;
672  int fnd;
673
674  k.i = ikey;
675  return jrb_insert_b(jrb_find_gte_int(tree, ikey, &fnd), k, val);
676}
677
678JRB jrb_insert_dbl(JRB tree, double dkey, Jval val)
679{
680  Jval k;
681  int fnd;
682
683  k.d = dkey;
684  return jrb_insert_b(jrb_find_gte_dbl(tree, dkey, &fnd), k, val);
685}
686
687496JRB jrb_insert_gen(JRB tree, Jval key, Jval val,
688497                          int (*func)(Jval, Jval))
689498{
qpkg/jrb.h
6767   jrb_insert uses strcmp() as comparison funcion. jrb_inserti uses <>=,
6868   jrb_insertg uses func() */
6969
70extern JRB jrb_insert_str(JRB tree, char *key, Jval val);
71extern JRB jrb_insert_int(JRB tree, int ikey, Jval val);
72extern JRB jrb_insert_dbl(JRB tree, double dkey, Jval val);
7370extern JRB jrb_insert_gen(JRB tree, Jval key, Jval val, int (*func)(Jval,Jval));
7471
7572/* returns an external node in t whose value is equal k. Returns NULL if
7673   there is no such node in the tree */
7774
78extern JRB jrb_find_str(JRB root, char *key);
79extern JRB jrb_find_int(JRB root, int ikey);
80extern JRB jrb_find_dbl(JRB root, double dkey);
8175extern JRB jrb_find_gen(JRB root, Jval, int (*func)(Jval, Jval));
8276
8377
...... 
8579  k or whose value is the smallest value greater than k. Sets found to
8680  1 if the key was found, and 0 otherwise. */
8781
88extern JRB jrb_find_gte_str(JRB root, char *key, int *found);
89extern JRB jrb_find_gte_int(JRB root, int ikey, int *found);
90extern JRB jrb_find_gte_dbl(JRB root, double dkey, int *found);
9182extern JRB jrb_find_gte_gen(JRB root, Jval key,
9283                              int (*func)(Jval, Jval), int *found);
9384

Archive Download the corresponding diff file

Branches:
master



interactive