index.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "index.h"
  2. static void
  3. index_list_dispose(void *list, void *extra)
  4. {
  5. (void) extra;
  6. list_dispose(list);
  7. free(list);
  8. }
  9. Disposer index_list_disposer = {
  10. .extra = NULL,
  11. .dispose = index_list_dispose
  12. };
  13. Nit_error
  14. index_init(Index *index, int bin_pos)
  15. {
  16. return map_init(index, &index_list_disposer, bin_pos);
  17. }
  18. Nit_error
  19. index_add(Index *index, Set *keys, void *dat)
  20. {
  21. int bin;
  22. Set_entry *entry;
  23. Nit_error error;
  24. set_foreach (keys, bin, entry) {
  25. List *list;
  26. if (map_get(index, entry->key_len,
  27. entry->dat, (void **) &list)) {
  28. List_entry *lentry;
  29. list_foreach (lentry, list)
  30. if (lentry->data == dat)
  31. break;
  32. if (lentry)
  33. continue;
  34. } else {
  35. if (!(list = malloc(sizeof(*list))))
  36. return NIT_ERROR_MEMORY;
  37. list_init(list, NULL);
  38. if ((error = map_add(index, entry->key_len,
  39. entry->dat, list))) {
  40. free(list);
  41. return error;
  42. }
  43. }
  44. if ((error = list_add(list, NULL, dat)))
  45. return error;
  46. }
  47. return NIT_ERROR_FINE;
  48. }
  49. int
  50. index_get(const Index *index, uint32_t key_len, const void *key, List **val)
  51. {
  52. return map_get(index, key_len, key, (void **) val);
  53. }