]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_list.h
moved session code to osrfHash and osrfList instead of manual linked lists
[Evergreen.git] / OpenSRF / src / libstack / osrf_list.h
1 #ifndef OSRF_LIST_H
2 #define OSRF_LIST_H
3
4
5 #include <stdio.h>
6 #include "opensrf/utils.h"
7 #include <Judy.h>
8
9 /**
10   Items are stored as void*'s so it's up to the user to
11   manage the data wisely.  Also, if the 'freeItem' callback is defined for the list,
12   then, it will be used on any item that needs to be freed, so don't mix data
13   types in the list if you want magic freeing */
14
15 struct __osrfListStruct {
16         Pvoid_t list;                                                   /* the list */
17         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 */
19 };
20 typedef struct __osrfListStruct osrfList;
21
22
23 struct __osrfListIteratorStruct {
24         osrfList* list;
25         unsigned long current;
26 };
27 typedef struct __osrfListIteratorStruct osrfListIterator;
28
29
30 /**
31   Creates a new list iterator with the given list
32   */
33 osrfListIterator* osrfNewListIterator( osrfList* list );
34
35 /**
36   Returns the next non-NULL item in the list, return NULL when
37   the end of the list has been reached
38   */
39 void* osrfListIteratorNext( osrfListIterator* itr );
40
41 /**
42   Deallocates the given list
43   */
44 void osrfListIteratorFree( osrfListIterator* itr );
45
46 void osrfListIteratorReset( osrfListIterator* itr );
47
48
49 /**
50   Allocates a new list
51   @param compress If true, the list will compress empty slots on delete.  If item positionality
52   is not important, then using this feature is reccomended to keep the list from growing indefinitely.
53   if item positionality is not important.
54   @return The allocated list
55   */
56 osrfList* osrfNewList();
57
58 /**
59   Pushes an item onto the end of the list.  This always finds the highest index
60   in the list and pushes the new item into the list after it.
61   @param list The list
62   @param item The item to push
63   @return 0 on success, -1 on failure
64   */
65 int osrfListPush( osrfList* list, void* item );
66
67 /**
68   Puts the given item into the list at the specified position.  If there
69   is already an item at the given position and the list has it's 
70   "freeItem" function defined, then it will be used to free said item.
71   If no 'freeItem' callback is defined, then the displaced item will
72   be returned;
73   @param list The list
74   @param item The item to put into the list
75   @param position The position to place the item in
76   @return NULL in successfully inserting the new item and freeing
77   any displaced items.  Returns the displaced item if no "freeItem"
78   callback is defined.
79         */
80 void* osrfListSet( osrfList* list, void* item, unsigned long position );
81
82 /**
83   Returns the item at the given position
84   @param list The list
85   @param postiont the position
86   */
87 void* osrfListGetIndex( osrfList* list, unsigned long  position );
88
89 /**
90   Frees the list and all list items (if the list has a "freeItem" function defined )
91   @param list The list
92   */
93 void osrfListFree( osrfList* list );
94
95 /**
96   Removes the list item at the given index
97   @param list The list
98   @param position The position of the item to remove
99   @return A pointer to the item removed if "freeItem" is not defined
100   for this list, returns NULL if it is.
101   */
102 void* osrfListRemove( osrfList* list, int position );
103
104 /**
105   Finds the list item whose void* is the same as the one passed in
106   @param list The list
107   @param addr The pointer connected to the list item we're to find
108   @return the index of the item, or -1 if the item was not found
109   */
110 int osrfListFind( osrfList* list, void* addr );
111
112
113 void __osrfListSetSize( osrfList* list );
114
115 /**
116   @return The number of non-null items in the list
117   */
118 unsigned long osrfListGetCount( osrfList* list );
119
120
121 #endif