lookup_class.c 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. /* build index of cell values from list */
  4. /* -1 means not found in list */
  5. int lookup_class(CELL * cats, /* input: category numbers to lookup */
  6. int ncats, /* input: number of categories to translate */
  7. CELL * list, /* input: list of known categories (sorted) */
  8. int nlist, /* input: "size" of the list - number of cats in list. */
  9. CELL * class /* output: resultant class - where each cat is found in list */
  10. )
  11. {
  12. int left, right, cur;
  13. CELL c;
  14. while (ncats-- > 0) {
  15. c = *cats++; /* extract the category */
  16. if (Rast_is_c_null_value(&c)) {
  17. *class++ = -1;
  18. continue;
  19. }
  20. left = 0;
  21. right = nlist - 1;
  22. for (;;) {
  23. cur = (left + right) / 2;
  24. if (c < list[cur])
  25. right = cur - 1;
  26. else
  27. left = cur + 1;
  28. if (c == list[cur]) {
  29. *class++ = cur;
  30. break;
  31. }
  32. else if (left > right) {
  33. *class++ = -1; /* this should never happen */
  34. break;
  35. }
  36. }
  37. }
  38. return 0;
  39. }