next.c 581 B

1234567891011121314151617181920212223242526272829303132
  1. #include <grass/btree.h>
  2. int btree_next(BTREE * B, void **key, void **data)
  3. {
  4. int q;
  5. if (B->N <= 0)
  6. return 0;
  7. /* if rewound, start at root and go all the way to the left */
  8. if (B->cur == 0)
  9. B->cur = 1;
  10. /* go to the right */
  11. else
  12. B->cur = B->node[B->cur].right;
  13. if (B->cur == 0) /* no more */
  14. return 0;
  15. if (B->cur < 0) /* thread. stop here */
  16. B->cur = -(B->cur);
  17. else /* go all the way left */
  18. while ((q = B->node[B->cur].left))
  19. B->cur = q;
  20. *key = B->node[B->cur].key;
  21. *data = B->node[B->cur].data;
  22. return 1;
  23. }