]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_hash.h
Patches from Scott McKellar covering:
[OpenSRF.git] / include / opensrf / osrf_hash.h
1 #ifndef OSRF_HASH_H
2 #define OSRF_HASH_H
3
4 #include <opensrf/utils.h>
5 #include <opensrf/string_array.h>
6 #include <opensrf/osrf_list.h>
7
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 */
11
12
13 /* used internally */
14 #define OSRF_HASH_NODE_FREE(h, n) \
15         if(h && n) { \
16                 if(h->freeItem) h->freeItem(n->key, n->item);\
17                 free(n->key); free(n); \
18         }
19
20
21 struct __osrfHashStruct {
22         osrfList* hash; /* this hash */
23         void (*freeItem) (char* key, void* item);       /* callback for freeing stored items */
24         unsigned int size;
25         osrfStringArray* keys;
26 };
27 typedef struct __osrfHashStruct osrfHash;
28
29 struct _osrfHashNodeStruct {
30         char* key;
31         void* item;
32 };
33 typedef struct _osrfHashNodeStruct osrfHashNode;
34
35
36 struct __osrfHashIteratorStruct {
37         char* current;
38         int currentIdx;
39         osrfHash* hash;
40         osrfStringArray* keys;
41 };
42 typedef struct __osrfHashIteratorStruct osrfHashIterator;
43
44 osrfHashNode* osrfNewHashNode(char* key, void* item);
45 void* osrfHashNodeFree(osrfHash*, osrfHashNode*);
46
47 /**
48   Allocates a new hash object
49   */
50 osrfHash* osrfNewHash();
51
52 /**
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
57   is returned.
58   @return The old item if exists and there is no 'freeItem', returns NULL
59   otherwise
60   */
61 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
62
63 /**
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
67   */
68 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
69
70 void* osrfHashGet( osrfHash* hash, const char* key, ... );
71
72
73 /**
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();
77   */
78 osrfStringArray* osrfHashKeys( osrfHash* hash );
79
80 osrfStringArray* osrfHashKeysInc( osrfHash* hash );
81
82 /**
83   Frees a hash
84   */
85 void osrfHashFree( osrfHash* hash );
86
87 /**
88   @return The number of items in the hash
89   */
90 unsigned long osrfHashGetCount( osrfHash* hash );
91
92
93
94
95 /**
96   Creates a new list iterator with the given list
97   */
98 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
99
100 int osrfHashIteratorHasNext( osrfHashIterator* itr );
101
102 /**
103   Returns the next non-NULL item in the list, return NULL when
104   the end of the list has been reached
105   */
106 void* osrfHashIteratorNext( osrfHashIterator* itr );
107
108 /**
109   Deallocates the given list
110   */
111 void osrfHashIteratorFree( osrfHashIterator* itr );
112
113 void osrfHashIteratorReset( osrfHashIterator* itr );
114
115 #endif