_info 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "config.h"
  4. /**
  5. * sparse_bsearch - search a sorted array with some invalid entries
  6. *
  7. * bsearch() is great for searching an array, but if you are deleting from
  8. * the array, you then need to memmove() to keep it dense.
  9. *
  10. * Sometimes there is a useful invalid value which can be used to mark deleted
  11. * entries, but such a "gappy" array breaks bsearch. This function allows
  12. * "invalid" entries in your array, which are ignored for the search.
  13. *
  14. * Example:
  15. * #include <ccan/sparse_bsearch/sparse_bsearch.h>
  16. *
  17. * static bool val_valid(unsigned int *val)
  18. * {
  19. * return *val != 0;
  20. * }
  21. * static int val_cmp(const unsigned int *a, const unsigned int *b)
  22. * {
  23. * return (int)((*a) - (*b));
  24. * }
  25. * static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
  26. *
  27. * // Return true if this value is in set, and remove it.
  28. * bool remove_from_values(unsigned int val)
  29. * {
  30. * unsigned int *p;
  31. * // We use 5 here, but ccan/array_size.h is better!
  32. * p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
  33. * if (!p)
  34. * return false;
  35. * *p = 0;
  36. * return true;
  37. * }
  38. *
  39. * int main(int argc, char *argv[])
  40. * {
  41. * int i, val;
  42. * for (i = 1; i < argc; i++) {
  43. * val = atoi(argv[i]);
  44. * if (remove_from_values(val))
  45. * printf("%i removed.\n", val);
  46. * else
  47. * printf("%i wasn't there.\n", val);
  48. * }
  49. * return 0;
  50. * }
  51. */
  52. int main(int argc, char *argv[])
  53. {
  54. /* Expect exactly one argument */
  55. if (argc != 2)
  56. return 1;
  57. if (strcmp(argv[1], "depends") == 0) {
  58. printf("ccan/typesafe_cb\n"
  59. "ccan/check_type\n");
  60. return 0;
  61. }
  62. return 1;
  63. }