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 */
10 #define OSRF_HASH_LIST_SIZE 0x10 /* size of the main hash list */
14 #define OSRF_HASH_NODE_FREE(h, n) \
16 if(h->freeItem) h->freeItem(n->key, n->item);\
17 free(n->key); free(n); \
21 struct __osrfHashStruct {
22 osrfList* hash; /* this hash */
23 void (*freeItem) (char* key, void* item); /* callback for freeing stored items */
25 osrfStringArray* keys;
27 typedef struct __osrfHashStruct osrfHash;
29 struct _osrfHashNodeStruct {
33 typedef struct _osrfHashNodeStruct osrfHashNode;
36 struct __osrfHashIteratorStruct {
40 osrfStringArray* keys;
42 typedef struct __osrfHashIteratorStruct osrfHashIterator;
44 osrfHashNode* osrfNewHashNode(char* key, void* item);
45 void* osrfHashNodeFree(osrfHash*, osrfHashNode*);
48 Allocates a new hash object
50 osrfHash* osrfNewHash();
53 Sets the given key with the given item
54 if "freeItem" is defined and an item already exists at the given location,
55 then old item is freed and the new item is put into place.
56 if "freeItem" is not defined and an item already exists, the old item
58 @return The old item if exists and there is no 'freeItem', returns NULL
61 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
64 Removes an item from the hash.
65 if 'freeItem' is defined it is used and NULL is returned,
66 else the freed item is returned
68 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
70 void* osrfHashGet( osrfHash* hash, const char* key, ... );
74 @return A list of strings representing the keys of the hash.
75 caller is responsible for freeing the returned string array
76 with osrfStringArrayFree();
78 osrfStringArray* osrfHashKeys( osrfHash* hash );
80 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
85 void osrfHashFree( osrfHash* hash );
88 @return The number of items in the hash
90 unsigned long osrfHashGetCount( osrfHash* hash );
96 Creates a new list iterator with the given list
98 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
100 int osrfHashIteratorHasNext( osrfHashIterator* itr );
103 Returns the next non-NULL item in the list, return NULL when
104 the end of the list has been reached
106 void* osrfHashIteratorNext( osrfHashIterator* itr );
109 Deallocates the given list
111 void osrfHashIteratorFree( osrfHashIterator* itr );
113 void osrfHashIteratorReset( osrfHashIterator* itr );