]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_list.c
f04c6ec6295fa000ad3e4b4a258678df7c32f4ac
[Evergreen.git] / OpenSRF / 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
13 int osrfListPush( osrfList* list, void* item ) {
14         if(!(list)) return -1;
15         osrfListSet( list, item, list->size );
16         return 0;
17 }
18
19 void* osrfListSet( osrfList* list, void* item, unsigned int position ) {
20         if(!list || position < 0) return NULL;
21
22         int i;
23         int newsize = list->arrsize;
24         void** newarr;
25
26         while( position >= newsize ) 
27                 newsize += OSRF_LIST_INC_SIZE;
28
29         if( newsize > list->arrsize ) { /* expand the list if necessary */
30                 newarr = safe_malloc( newsize * sizeof(void*) );
31                 for( i = 0; i < list->arrsize; i++ ) 
32                         newarr[i] = list->arrlist[i];
33                 free(list->arrlist);
34                 list->arrlist = newarr;
35                 list->arrsize = newsize;
36         }
37
38         void* olditem = osrfListRemove( list, position );
39         list->arrlist[position] = item;
40         if( list->size == 0 || list->size <= position )
41                 list->size = position + 1;
42         return olditem;
43 }
44
45
46 void* osrfListGetIndex( osrfList* list, unsigned int position ) {
47         if(!list || position >= list->size) return NULL;
48         return list->arrlist[position];
49 }
50
51 void osrfListFree( osrfList* list ) {
52         if(!list) return;
53
54         if( list->freeItem ) {
55                 int i; void* val;
56                 for( i = 0; i < list->size; i++ ) {
57                         if( (val = list->arrlist[i]) ) 
58                                 list->freeItem(val);
59                 }
60         }
61
62         free(list->arrlist);
63         free(list);
64 }
65
66 void* osrfListRemove( osrfList* list, int position ) {
67         if(!list || position >= list->size) return NULL;
68
69         void* olditem = list->arrlist[position];
70         list->arrlist[position] = NULL;
71         if( list->freeItem ) {
72                 list->freeItem(olditem);
73                 olditem = NULL;
74         }
75
76         if( position == list->size - 1 ) list->size--;
77         return olditem;
78 }
79
80
81 int osrfListFind( osrfList* list, void* addr ) {
82         if(!(list && addr)) return -1;
83         int index;
84         for( index = 0; index < list->size; index++ ) {
85                 if( list->arrlist[index] == addr ) 
86                         return index;
87         }
88         return -1;
89 }
90
91
92 unsigned int osrfListGetCount( osrfList* list ) {
93         if(!list) return -1;
94         return list->size;
95 }
96
97
98 void* osrfListPop( osrfList* list ) {
99         if(!list) return NULL;
100         return osrfListRemove( list, list->size - 1 );
101 }
102
103
104 osrfListIterator* osrfNewListIterator( osrfList* list ) {
105         if(!list) return NULL;
106         osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator));
107         itr->list = list;
108         itr->current = 0;
109         return itr;
110 }
111
112 void* osrfListIteratorNext( osrfListIterator* itr ) {
113         if(!(itr && itr->list)) return NULL;
114         if(itr->current >= itr->list->size) return NULL;
115         return itr->list->arrlist[itr->current++];
116 }
117
118 void osrfListIteratorFree( osrfListIterator* itr ) {
119         if(!itr) return;
120         free(itr);
121 }
122
123
124 void osrfListIteratorReset( osrfListIterator* itr ) {
125         if(!itr) return;
126         itr->current = 0;
127 }
128
129
130 void osrfListVanillaFree( void* item ) {
131         free(item);
132 }
133
134 void osrfListSetDefaultFree( osrfList* list ) {
135         if(!list) return;
136         list->freeItem = osrfListVanillaFree;
137 }