map.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef __MAP_H
  2. #define __MAP_H
  3. //*****************************************************************************
  4. //
  5. // map.h
  6. //
  7. // similar to a hashtable, but keys as well as values can be can be anything.
  8. //
  9. //*****************************************************************************
  10. typedef struct map_data MAP;
  11. typedef struct map_iterator MAP_ITERATOR;
  12. //
  13. // if hash_func and/or compares are null, generic hashing and comparing
  14. // functions are used.
  15. //
  16. // hash_func is expected to be a function that takes the key type used in
  17. // this map, and returns a long value based on that key.
  18. //
  19. // compares is expected to be a function that takes two keys and compares
  20. // them togher. If they are equal, 0 is returned. if key1 is less than key2,
  21. // -1 is returned. otherwise, 1 is returned. If this function is NULL, a
  22. // generic comparator is used that compares memory address of the two keys.
  23. MAP *newMap(void *hash_func, void *compares);
  24. MAP *newMapSize(void *hash_func, void *compares, int size);
  25. void deleteMap(MAP *map);
  26. int mapPut (MAP *map, const void *key, void *val);
  27. void *mapGet (MAP *map, const void *key);
  28. void *mapRemove (MAP *map, const void *key);
  29. int mapIn (MAP *map, const void *key);
  30. int mapSize (MAP *map);
  31. //*****************************************************************************
  32. // map iterator function prototypes
  33. //*****************************************************************************
  34. // iterate across all of the elements in a map
  35. #define ITERATE_MAP(key, val, it) \
  36. for(key = mapIteratorCurrentKey(it), val = mapIteratorCurrentVal(it); \
  37. key != NULL; \
  38. mapIteratorNext(it), \
  39. key = mapIteratorCurrentKey(it), val = mapIteratorCurrentVal(it))
  40. MAP_ITERATOR *newMapIterator(MAP *map);
  41. void deleteMapIterator(MAP_ITERATOR *I);
  42. void mapIteratorReset (MAP_ITERATOR *I);
  43. void mapIteratorNext (MAP_ITERATOR *I);
  44. void *mapIteratorCurrentVal (MAP_ITERATOR *I);
  45. const void *mapIteratorCurrentKey (MAP_ITERATOR *I);
  46. #endif // __MAP_H