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