dict.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. *** :: Dict ::
  3. ***
  4. *** Maps (char*) to (void*)
  5. ***
  6. *** Keys are copied and memory managed by dict
  7. *** Values are _not_ managed by dict
  8. *** User must delete them themselves
  9. ***
  10. **/
  11. #ifndef dict_h
  12. #define dict_h
  13. #include "cengine.h"
  14. /* Buckets (Linked List) */
  15. struct bucket {
  16. char* key;
  17. void* item;
  18. struct bucket* next;
  19. };
  20. struct bucket* bucket_new(char* string, void* item);
  21. void bucket_map(struct bucket* b, void func(void*) );
  22. void bucket_filter_map(struct bucket* b, int filter(void*) , void func(void*) );
  23. void bucket_delete_with(struct bucket* b, void func(void*) );
  24. void bucket_delete_recursive(struct bucket* b);
  25. void bucket_print(struct bucket* b);
  26. /* dict */
  27. typedef struct {
  28. int size;
  29. struct bucket** buckets;
  30. } dict;
  31. dict* dict_new(int size);
  32. void dict_delete(dict* d);
  33. bool dict_contains(dict* d, char* string);
  34. void* dict_get(dict* d, char* string);
  35. void dict_set(dict* d, char* string, void* item);
  36. void dict_remove_with(dict* d, char* string, void func(void*));
  37. void dict_map(dict* d, void func(void*));
  38. void dict_filter_map(dict* d, int filter(void*) , void func(void*) );
  39. void dict_print(dict* d);
  40. char* dict_find(dict* d, void* item);
  41. #endif