4 #include "opensrf/utils.h"
5 #include "opensrf/string_array.h"
8 /* 0x100 is a good size for small hashes */
9 #define OSRF_HASH_LIST_SIZE 0x100 /* size of the main hash list */
12 #define OSRF_HASH_NODE_FREE(h, n) \
14 if(h->freeItem) h->freeItem(n->key, n->item);\
15 free(n->key); free(n); \
19 struct __osrfHashStruct {
20 osrfList* hash; /* this hash */
21 void (*freeItem) (char* key, void* item); /* callback for freeing stored items */
23 osrfStringArray* keys;
25 typedef struct __osrfHashStruct osrfHash;
27 struct _osrfHashNodeStruct {
31 typedef struct _osrfHashNodeStruct osrfHashNode;
34 struct __osrfHashIteratorStruct {
38 osrfStringArray* keys;
40 typedef struct __osrfHashIteratorStruct osrfHashIterator;
42 osrfHashNode* osrfNewHashNode(char* key, void* item);
43 void* osrfHashNodeFree(osrfHash*, osrfHashNode*);
46 Allocates a new hash object
48 osrfHash* osrfNewHash();
51 Sets the given key with the given item
52 if "freeItem" is defined and an item already exists at the given location,
53 then old item is freed and the new item is put into place.
54 if "freeItem" is not defined and an item already exists, the old item
56 @return The old item if exists and there is no 'freeItem', returns NULL
59 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
62 Removes an item from the hash.
63 if 'freeItem' is defined it is used and NULL is returned,
64 else the freed item is returned
66 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
68 void* osrfHashGet( osrfHash* hash, const char* key, ... );
72 @return A list of strings representing the keys of the hash.
73 caller is responsible for freeing the returned string array
74 with osrfStringArrayFree();
76 osrfStringArray* osrfHashKeys( osrfHash* hash );
78 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
83 void osrfHashFree( osrfHash* hash );
86 @return The number of items in the hash
88 unsigned long osrfHashGetCount( osrfHash* hash );
94 Creates a new list iterator with the given list
96 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
98 int osrfHashIteratorHasNext( osrfHashIterator* itr );
101 Returns the next non-NULL item in the list, return NULL when
102 the end of the list has been reached
104 void* osrfHashIteratorNext( osrfHashIterator* itr );
107 Deallocates the given list
109 void osrfHashIteratorFree( osrfHashIterator* itr );
111 void osrfHashIteratorReset( osrfHashIterator* itr );