4 #include "opensrf/utils.h"
6 #define OSRF_LIST_DEFAULT_SIZE 48 /* most opensrf lists are small... */
7 #define OSRF_LIST_INC_SIZE 256
8 #define OSRF_LIST_MAX_SIZE 10240
11 Items are stored as void*'s so it's up to the user to
12 manage the data wisely. Also, if the 'freeItem' callback is defined for the list,
13 then, it will be used on any item that needs to be freed, so don't mix data
14 types in the list if you want magic freeing */
16 struct __osrfListStruct {
17 unsigned int size; /* how many items in the list including NULL items between non-NULL items */
18 void (*freeItem) (void* item); /* callback for freeing stored items */
20 int arrsize; /* how big is the currently allocated array */
22 typedef struct __osrfListStruct osrfList;
25 struct __osrfListIteratorStruct {
29 typedef struct __osrfListIteratorStruct osrfListIterator;
33 Creates a new list iterator with the given list
35 osrfListIterator* osrfNewListIterator( osrfList* list );
38 Returns the next non-NULL item in the list, return NULL when
39 the end of the list has been reached
41 void* osrfListIteratorNext( osrfListIterator* itr );
44 Deallocates the given list
46 void osrfListIteratorFree( osrfListIterator* itr );
48 void osrfListIteratorReset( osrfListIterator* itr );
53 @param compress If true, the list will compress empty slots on delete. If item positionality
54 is not important, then using this feature is reccomended to keep the list from growing indefinitely.
55 if item positionality is not important.
56 @return The allocated list
58 osrfList* osrfNewList();
61 Pushes an item onto the end of the list. This always finds the highest index
62 in the list and pushes the new item into the list after it.
64 @param item The item to push
65 @return 0 on success, -1 on failure
67 int osrfListPush( osrfList* list, void* item );
71 * Removes the last item in the list
72 * See osrfListRemove for details on how the removed item is handled
73 * @return The item, unless 'freeItem' exists, then returns NULL
75 void* osrfListPop( osrfList* list );
78 Puts the given item into the list at the specified position. If there
79 is already an item at the given position and the list has it's
80 "freeItem" function defined, then it will be used to free said item.
81 If no 'freeItem' callback is defined, then the displaced item will
84 @param item The item to put into the list
85 @param position The position to place the item in
86 @return NULL in successfully inserting the new item and freeing
87 any displaced items. Returns the displaced item if no "freeItem"
90 void* osrfListSet( osrfList* list, void* item, unsigned int position );
93 Returns the item at the given position
95 @param postiont the position
97 void* osrfListGetIndex( osrfList* list, unsigned int position );
100 Frees the list and all list items (if the list has a "freeItem" function defined )
103 void osrfListFree( osrfList* list );
106 Removes the list item at the given index
108 @param position The position of the item to remove
109 @return A pointer to the item removed if "freeItem" is not defined
110 for this list, returns NULL if it is.
112 void* osrfListRemove( osrfList* list, int position );
115 Finds the list item whose void* is the same as the one passed in
117 @param addr The pointer connected to the list item we're to find
118 @return the index of the item, or -1 if the item was not found
120 int osrfListFind( osrfList* list, void* addr );
123 void __osrfListSetSize( osrfList* list );
127 @return The number of non-null items in the list
129 unsigned int osrfListGetCount( osrfList* list );
132 * May be used as a default memory freeing call
133 * Just calls free() on list items
135 void osrfListVanillaFree( void* item );
138 * Tells the list to just call 'free()' on each item when
139 * an item or the whole list is destroyed
141 void osrfListSetDefaultFree( osrfList* list );