]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_list.c
moved osrf_hash code to osrf_big_hash as the Judy big hash implementation
[OpenSRF.git] / src / libstack / osrf_list.c
1 #include "osrf_list.h"
2
3 osrfList* osrfNewList() {
4         osrfList* list = safe_malloc(sizeof(osrfList));
5         list->size              = 0;
6         list->freeItem = NULL;
7         list->arrsize   = OSRF_LIST_DEFAULT_SIZE;
8         list->arrlist   = safe_malloc( list->arrsize * sizeof(void*) );
9         return list;
10 }
11
12 osrfList* osrfNewListSize( unsigned int size ) {
13         osrfList* list = safe_malloc(sizeof(osrfList));
14         list->size              = 0;
15         list->freeItem = NULL;
16         list->arrsize   = size;
17         list->arrlist   = safe_malloc( list->arrsize * sizeof(void*) );
18         return list;
19 }
20
21
22 int osrfListPush( osrfList* list, void* item ) {
23         if(!(list)) return -1;
24         osrfListSet( list, item, list->size );
25         return 0;
26 }
27
28 int osrfListPushFirst( osrfList* list, void* item ) {
29         if(!(list && item)) return -1;
30         int i;
31         for( i = 0; i < list->size; i++ ) 
32                 if(!list->arrlist[i]) break;
33         osrfListSet( list, item, i );
34         return list->size;
35 }
36
37 void* osrfListSet( osrfList* list, void* item, unsigned int position ) {
38         if(!list || position < 0) return NULL;
39
40         int i;
41         int newsize = list->arrsize;
42         void** newarr;
43
44         while( position >= newsize ) 
45                 newsize += OSRF_LIST_INC_SIZE;
46
47         if( newsize > list->arrsize ) { /* expand the list if necessary */
48                 newarr = safe_malloc( newsize * sizeof(void*) );
49                 for( i = 0; i < list->arrsize; i++ ) 
50                         newarr[i] = list->arrlist[i];
51                 free(list->arrlist);
52                 list->arrlist = newarr;
53                 list->arrsize = newsize;
54         }
55
56         void* olditem = osrfListRemove( list, position );
57         list->arrlist[position] = item;
58         if( list->size == 0 || list->size <= position )
59                 list->size = position + 1;
60         return olditem;
61 }
62
63
64 void* osrfListGetIndex( osrfList* list, unsigned int position ) {
65         if(!list || position >= list->size) return NULL;
66         return list->arrlist[position];
67 }
68
69 void osrfListFree( osrfList* list ) {
70         if(!list) return;
71
72         if( list->freeItem ) {
73                 int i; void* val;
74                 for( i = 0; i < list->size; i++ ) {
75                         if( (val = list->arrlist[i]) ) 
76                                 list->freeItem(val);
77                 }
78         }
79
80         free(list->arrlist);
81         free(list);
82 }
83
84 void* osrfListRemove( osrfList* list, int position ) {
85         if(!list || position >= list->size) return NULL;
86
87         void* olditem = list->arrlist[position];
88         list->arrlist[position] = NULL;
89         if( list->freeItem ) {
90                 list->freeItem(olditem);
91                 olditem = NULL;
92         }
93
94         if( position == list->size - 1 ) list->size--;
95         return olditem;
96 }
97
98
99 int osrfListFind( osrfList* list, void* addr ) {
100         if(!(list && addr)) return -1;
101         int index;
102         for( index = 0; index < list->size; index++ ) {
103                 if( list->arrlist[index] == addr ) 
104                         return index;
105         }
106         return -1;
107 }
108
109
110 unsigned int osrfListGetCount( osrfList* list ) {
111         if(!list) return -1;
112         return list->size;
113 }
114
115
116 void* osrfListPop( osrfList* list ) {
117         if(!list) return NULL;
118         return osrfListRemove( list, list->size - 1 );
119 }
120
121
122 osrfListIterator* osrfNewListIterator( osrfList* list ) {
123         if(!list) return NULL;
124         osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator));
125         itr->list = list;
126         itr->current = 0;
127         return itr;
128 }
129
130 void* osrfListIteratorNext( osrfListIterator* itr ) {
131         if(!(itr && itr->list)) return NULL;
132         if(itr->current >= itr->list->size) return NULL;
133         return itr->list->arrlist[itr->current++];
134 }
135
136 void osrfListIteratorFree( osrfListIterator* itr ) {
137         if(!itr) return;
138         free(itr);
139 }
140
141
142 void osrfListIteratorReset( osrfListIterator* itr ) {
143         if(!itr) return;
144         itr->current = 0;
145 }
146
147
148 void osrfListVanillaFree( void* item ) {
149         free(item);
150 }
151
152 void osrfListSetDefaultFree( osrfList* list ) {
153         if(!list) return;
154         list->freeItem = osrfListVanillaFree;
155 }