]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_hash.h
moved osrf_hash code to osrf_big_hash as the Judy big hash implementation
[Evergreen.git] / OpenSRF / src / libstack / 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 "osrf_list.h"
7
8 #define OSRF_HASH_MAXKEY 256
9
10 #define OSRF_HASH_KEY_MASK 0x7FF  /* hash keys are a maximun of 2047 in size */
11 #define OSRF_HASH_KEY_SIZE 2048  /* size of the container list for the keys */
12
13
14 struct __osrfHashStruct {
15         osrfList* hash; /* this hash */
16         void (*freeItem) (char* key, void* item);       /* callback for freeing stored items */
17         unsigned int size;
18 };
19 typedef struct __osrfHashStruct osrfHash;
20
21 struct _osrfHashNodeStruct {
22         char* key;
23         void* item;
24 };
25 typedef struct _osrfHashNodeStruct osrfHashNode;
26
27
28 struct __osrfHashIteratorStruct {
29         char* current;
30         int currentIdx;
31         osrfHash* hash;
32         osrfStringArray* keys;
33 };
34 typedef struct __osrfHashIteratorStruct osrfHashIterator;
35
36 osrfHashNode* osrfNewHashNode(char* key, void* item);
37 void osrfHashNodeFree(osrfHashNode*);
38
39 /**
40   Allocates a new hash object
41   */
42 osrfHash* osrfNewHash();
43
44 /**
45   Sets the given key with the given item
46   if "freeItem" is defined and an item already exists at the given location, 
47   then old item is freed and the new item is put into place.
48   if "freeItem" is not defined and an item already exists, the old item
49   is returned.
50   @return The old item if exists and there is no 'freeItem', returns NULL
51   otherwise
52   */
53 void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... );
54
55 /**
56   Removes an item from the hash.
57   if 'freeItem' is defined it is used and NULL is returned,
58   else the freed item is returned
59   */
60 void* osrfHashRemove( osrfHash* hash, const char* key, ... );
61
62 void* osrfHashGet( osrfHash* hash, const char* key, ... );
63
64
65 /**
66   @return A list of strings representing the keys of the hash. 
67   caller is responsible for freeing the returned string array 
68   with osrfStringArrayFree();
69   */
70 osrfStringArray* osrfHashKeys( osrfHash* hash );
71
72 /**
73   Frees a hash
74   */
75 void osrfHashFree( osrfHash* hash );
76
77 /**
78   @return The number of items in the hash
79   */
80 unsigned long osrfHashGetCount( osrfHash* hash );
81
82
83
84
85 /**
86   Creates a new list iterator with the given list
87   */
88 osrfHashIterator* osrfNewHashIterator( osrfHash* hash );
89
90 /**
91   Returns the next non-NULL item in the list, return NULL when
92   the end of the list has been reached
93   */
94 void* osrfHashIteratorNext( osrfHashIterator* itr );
95
96 /**
97   Deallocates the given list
98   */
99 void osrfHashIteratorFree( osrfHashIterator* itr );
100
101 void osrfHashIteratorReset( osrfHashIterator* itr );
102
103 #endif