find.c 427 B

12345678910111213141516171819202122232425262728
  1. #include <stdio.h>
  2. #include <grass/btree.h>
  3. int btree_find(const BTREE * B, const void *key, void **data)
  4. {
  5. int q;
  6. int dir;
  7. if (B->N <= 0)
  8. return 0;
  9. q = 1;
  10. while (q > 0) {
  11. dir = (*B->cmp) (B->node[q].key, key);
  12. if (dir == 0) {
  13. *data = B->node[q].data;
  14. return 1;
  15. }
  16. if (dir > 0)
  17. q = B->node[q].left; /* go left */
  18. else
  19. q = B->node[q].right; /* go right */
  20. }
  21. return 0;
  22. }