4 #include <opensrf/utils.h>
10 #define OSRF_LIST_GET_INDEX(l, i) (!(l) || (i) >= (l)->size) ? NULL: (l)->arrlist[(i)]
13 Items are stored as void*'s so it's up to the user to
14 manage the data wisely. Also, if the 'freeItem' callback is defined for the list,
15 then, it will be used on any item that needs to be freed, so don't mix data
16 types in the list if you want magic freeing */
18 struct _osrfListStruct {
19 unsigned int size; /* how many items in the list including NULL items between non-NULL items */
20 void (*freeItem) (void* item); /* callback for freeing stored items */
22 int arrsize; /* how big is the currently allocated array */
24 typedef struct _osrfListStruct osrfList;
27 struct _osrfListIteratorStruct {
31 typedef struct _osrfListIteratorStruct osrfListIterator;
33 osrfList* osrfNewListSize( unsigned int size );
37 Creates a new list iterator with the given list
39 osrfListIterator* osrfNewListIterator( const osrfList* list );
42 Returns the next non-NULL item in the list, return NULL when
43 the end of the list has been reached
45 void* osrfListIteratorNext( osrfListIterator* itr );
48 Deallocates the given list
50 void osrfListIteratorFree( osrfListIterator* itr );
52 void osrfListIteratorReset( osrfListIterator* itr );
57 @return The allocated list
59 osrfList* osrfNewList();
62 Pushes an item onto the end of the list. This always finds the highest index
63 in the list and pushes the new item into the list after it.
65 @param item The item to push
66 @return 0 on success, -1 on failure
68 int osrfListPush( osrfList* list, void* item );
72 * Removes the last item in the list
73 * See osrfListRemove for details on how the removed item is handled
74 * @return The item, unless 'freeItem' exists, then returns NULL
76 void* osrfListPop( osrfList* list );
79 Puts the given item into the list at the specified position. If there
80 is already an item at the given position and the list has its
81 "freeItem" function defined, then it will be used to free said item.
82 If no 'freeItem' callback is defined, then the displaced item will
85 @param item The item to put into the list
86 @param position The position to place the item in
87 @return NULL in successfully inserting the new item and freeing
88 any displaced items. Returns the displaced item if no "freeItem"
91 void* osrfListSet( osrfList* list, void* item, unsigned int position );
94 Returns the item at the given position
96 @param postiont the position
98 void* osrfListGetIndex( const osrfList* list, unsigned int position );
101 Frees the list and all list items (if the list has a "freeItem" function defined )
104 void osrfListFree( osrfList* list );
107 Removes the list item at the given index
109 @param position The position of the item to remove
110 @return A pointer to the item removed if "freeItem" is not defined
111 for this list, returns NULL if it is.
113 void* osrfListRemove( osrfList* list, unsigned int position );
116 Removes the list item at the given index, without freeing it
118 @param position The position of the item to remove
119 @return A pointer to the item extracted, or NULL
120 if there is nothing to extract
122 void* osrfListExtract( osrfList* list, unsigned int position );
125 Finds the list item whose void* is the same as the one passed in
127 @param addr The pointer connected to the list item we're to find
128 @return the index of the item, or -1 if the item was not found
130 int osrfListFind( const osrfList* list, void* addr );
133 @return The number of non-null items in the list
135 unsigned int osrfListGetCount( const osrfList* list );
138 * May be used as a default memory freeing call
139 * Just calls free() on list items
141 void osrfListVanillaFree( void* item );
144 * Tells the list to just call 'free()' on each item when
145 * an item or the whole list is destroyed
147 void osrfListSetDefaultFree( osrfList* list );
150 * Inserts the new item at the first free (null) slot
151 * in the array. Item is shoved onto the end of the
152 * list if there are no null slots */
153 int osrfListPushFirst( osrfList* list, void* item );