]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_big_list.c
Patch from Scott McKellar:
[OpenSRF.git] / src / libopensrf / osrf_big_list.c
1 #include <opensrf/osrf_big_list.h>
2
3
4 osrfBigList* osrfNewBigList() {
5         osrfBigList* list = safe_malloc(sizeof(osrfBigList));
6         list->list = (Pvoid_t) NULL;
7         list->size = 0;
8         list->freeItem = NULL;
9         return list;
10 }
11
12
13 int osrfBigListPush( osrfBigList* list, void* item ) {
14         if(!(list && item)) return -1;
15         Word_t* value;
16         unsigned long index = -1;
17         JLL(value, list->list, index );
18         osrfBigListSet( list, item, index+1 );
19         return 0;
20 }
21
22
23 void* osrfBigListSet( osrfBigList* list, void* item, unsigned long position ) {
24         if(!list || position < 0) return NULL;
25
26         Word_t* value;
27         void* olditem = osrfBigListRemove( list, position );
28
29         JLI( value, list->list, position ); 
30         *value = (Word_t) item;
31         __osrfBigListSetSize( list );
32
33         return olditem;
34 }
35
36
37 void* osrfBigListGetIndex( osrfBigList* list, unsigned long position ) {
38         if(!list) return NULL;
39
40         Word_t* value;
41         JLG( value, list->list, position );
42         if(value) return (void*) *value;
43         return NULL;
44 }
45
46 void osrfBigListFree( osrfBigList* list ) {
47         if(!list) return;
48
49         Word_t* value;
50         unsigned long index = -1;
51         JLL(value, list->list, index );
52         int retcode;
53
54         while (value != NULL) {
55                 if(list->freeItem) 
56                         list->freeItem( (void*) *value );
57                 JLD(retcode, list->list, index);
58                 JLP(value, list->list, index);
59         }               
60
61         free(list);
62 }
63
64 void* osrfBigListRemove( osrfBigList* list, int position ) {
65         if(!list) return NULL;
66
67         int retcode;
68         Word_t* value;
69         JLG( value, list->list, position );
70         void* olditem = NULL;
71
72         if( value ) {
73
74                 olditem = (void*) *value;
75                 if( olditem ) {
76                         JLD(retcode, list->list, position );
77                         if(retcode == 1) {
78                                 if(list->freeItem) {
79                                         list->freeItem( olditem );
80                                         olditem = NULL;
81                                 }
82                                 __osrfBigListSetSize( list );
83                         }
84                 }
85         }
86
87         return olditem;
88 }
89
90
91 int osrfBigListFind( osrfBigList* list, void* addr ) {
92         if(!(list && addr)) return -1;
93
94         Word_t* value;
95         unsigned long index = -1;
96         JLL(value, list->list, index );
97
98         while (value != NULL) {
99                 if( (void*) *value == addr )
100                         return index;
101                 JLP(value, list->list, index);
102         }
103
104         return -1;
105 }
106
107
108
109 void __osrfBigListSetSize( osrfBigList* list ) {
110         if(!list) return;
111
112         Word_t* value;
113         unsigned long index = -1;
114         JLL(value, list->list, index );
115         list->size = index + 1;
116 }
117
118
119 unsigned long osrfBigListGetCount( osrfBigList* list ) {
120         if(!list) return -1;
121         unsigned long retcode = -1;
122         JLC( retcode, list->list, 0, -1 );
123         return retcode;
124 }
125
126
127 void* osrfBigListPop( osrfBigList* list ) {
128         if(!list) return NULL;
129         return osrfBigListRemove( list, list->size - 1 );
130 }
131
132
133 osrfBigBigListIterator* osrfNewBigListIterator( osrfBigList* list ) {
134         if(!list) return NULL;
135         osrfBigBigListIterator* itr = safe_malloc(sizeof(osrfBigBigListIterator));
136         itr->list = list;
137         itr->current = 0;
138         return itr;
139 }
140
141 void* osrfBigBigListIteratorNext( osrfBigBigListIterator* itr ) {
142         if(!(itr && itr->list)) return NULL;
143
144         Word_t* value;
145         if(itr->current >= itr->list->size) return NULL;
146         JLF( value, itr->list->list, itr->current );
147         if(value) {
148                 itr->current++;
149                 return (void*) *value;
150         }
151         return NULL;
152 }
153
154 void osrfBigBigListIteratorFree( osrfBigBigListIterator* itr ) {
155         if(!itr) return;
156         free(itr);
157 }
158
159
160
161 void osrfBigBigListIteratorReset( osrfBigBigListIterator* itr ) {
162         if(!itr) return;
163         itr->current = 0;
164 }
165
166
167 void osrfBigListVanillaFree( void* item ) {
168         free(item);
169 }
170
171 void osrfBigListSetDefaultFree( osrfBigList* list ) {
172         if(!list) return;
173         list->freeItem = osrfBigListVanillaFree;
174 }