52 void const *left, |
52 void const *left, |
53 void const *right |
53 void const *right |
54 ); |
54 ); |
55 |
55 |
56 /** |
56 /** |
57 * Internal type for the list structure - use CxList instead. |
57 * List class type. |
58 */ |
58 */ |
59 typedef struct cx_list_s cx_list_s; |
59 typedef struct cx_list_class_s cx_list_class; |
|
60 |
|
61 /** |
|
62 * Structure for holding the base data of a list. |
|
63 */ |
|
64 struct cx_list_s { |
|
65 /** |
|
66 * The list class definition. |
|
67 */ |
|
68 cx_list_class *cl; |
|
69 /** |
|
70 * The allocator to use. |
|
71 */ |
|
72 CxAllocator *allocator; |
|
73 /** |
|
74 * The comparator function for the elements. |
|
75 */ |
|
76 CxListComparator cmpfunc; |
|
77 /** |
|
78 * The size of each element (payload only). |
|
79 */ |
|
80 size_t itemsize; |
|
81 /** |
|
82 * The size of the list (number of currently stored elements). |
|
83 */ |
|
84 size_t size; |
|
85 /** |
|
86 * The capacity of the list (maximum number of elements). |
|
87 */ |
|
88 size_t capacity; |
|
89 }; |
60 |
90 |
61 /** |
91 /** |
62 * The class definition for arbitrary lists. |
92 * The class definition for arbitrary lists. |
63 */ |
93 */ |
64 typedef struct { |
94 struct cx_list_class_s { |
65 /** |
95 /** |
66 * Member function for adding an element. |
96 * Member function for adding an element. |
67 */ |
97 */ |
68 int (*add)( |
98 int (*add)( |
69 cx_list_s *list, |
99 struct cx_list_s *list, |
70 void const *elem |
100 void const *elem |
71 ); |
101 ); |
72 |
102 |
73 /** |
103 /** |
74 * Member function for inserting an element. |
104 * Member function for inserting an element. |
75 */ |
105 */ |
76 int (*insert)( |
106 int (*insert)( |
77 cx_list_s *list, |
107 struct cx_list_s *list, |
78 size_t index, |
108 size_t index, |
79 void const *elem |
109 void const *elem |
80 ); |
110 ); |
81 |
111 |
82 /** |
112 /** |
83 * Member function for inserting an element relative to an iterator position. |
113 * Member function for inserting an element relative to an iterator position. |
84 */ |
114 */ |
85 int (*insert_iter)( |
115 int (*insert_iter)( |
86 CxIterator *iter, |
116 struct cx_iterator_s *iter, |
87 void const *elem, |
117 void const *elem, |
88 int prepend |
118 int prepend |
89 ); |
119 ); |
90 |
120 |
91 /** |
121 /** |
92 * Member function for removing an element. |
122 * Member function for removing an element. |
93 */ |
123 */ |
94 int (*remove)( |
124 int (*remove)( |
95 cx_list_s *list, |
125 struct cx_list_s *list, |
96 size_t index |
126 size_t index |
97 ); |
127 ); |
98 |
128 |
99 /** |
129 /** |
100 * Member function for element lookup. |
130 * Member function for element lookup. |
101 */ |
131 */ |
102 void *(*at)( |
132 void *(*at)( |
103 cx_list_s const *list, |
133 struct cx_list_s const *list, |
104 size_t index |
134 size_t index |
105 ); |
135 ); |
106 |
136 |
107 /** |
137 /** |
108 * Member function for finding an element. |
138 * Member function for finding an element. |
109 */ |
139 */ |
110 size_t (*find)( |
140 size_t (*find)( |
111 cx_list_s const *list, |
141 struct cx_list_s const *list, |
112 void const *elem |
142 void const *elem |
113 ); |
143 ); |
114 |
144 |
115 /** |
145 /** |
116 * Member function for sorting the list in place. |
146 * Member function for sorting the list in place. |
117 */ |
147 */ |
118 void (*sort)(cx_list_s *list); |
148 void (*sort)(struct cx_list_s *list); |
119 |
149 |
120 /** |
150 /** |
121 * Member function for comparing this list to another list of the same type. |
151 * Member function for comparing this list to another list of the same type. |
122 */ |
152 */ |
123 int (*compare)( |
153 int (*compare)( |
124 cx_list_s const *list, |
154 struct cx_list_s const *list, |
125 cx_list_s const *other |
155 struct cx_list_s const *other |
126 ); |
156 ); |
127 |
157 |
128 /** |
158 /** |
129 * Member function for reversing the order of the items. |
159 * Member function for reversing the order of the items. |
130 */ |
160 */ |
131 void (*reverse)(cx_list_s *list); |
161 void (*reverse)(struct cx_list_s *list); |
132 |
162 |
133 /** |
163 /** |
134 * Returns an iterator pointing to the specified index. |
164 * Returns an iterator pointing to the specified index. |
135 */ |
165 */ |
136 CxIterator (*iterator)( |
166 struct cx_iterator_s (*iterator)( |
137 cx_list_s *list, |
167 struct cx_list_s *list, |
138 size_t index |
168 size_t index |
139 ); |
169 ); |
140 } cx_list_class; |
|
141 |
|
142 /** |
|
143 * Structure for holding the base data of a list. |
|
144 */ |
|
145 struct cx_list_s { |
|
146 /** |
|
147 * The list class definition. |
|
148 */ |
|
149 cx_list_class *cl; |
|
150 /** |
|
151 * The allocator to use. |
|
152 */ |
|
153 CxAllocator allocator; |
|
154 /** |
|
155 * The comparator function for the elements. |
|
156 */ |
|
157 CxListComparator cmpfunc; |
|
158 /** |
|
159 * The size of each element (payload only). |
|
160 */ |
|
161 size_t itemsize; |
|
162 /** |
|
163 * The size of the list (number of currently stored elements). |
|
164 */ |
|
165 size_t size; |
|
166 /** |
|
167 * The capacity of the list (maximum number of elements). |
|
168 */ |
|
169 size_t capacity; |
|
170 }; |
170 }; |
171 |
171 |
172 /** |
172 /** |
173 * Common type for all list implementations. |
173 * Common type for all list implementations. |
174 */ |
174 */ |
175 typedef cx_list_s *CxList; |
175 typedef struct cx_list_s CxList; |
176 |
176 |
177 /** |
177 /** |
178 * Adds an item to the end of the list. |
178 * Adds an item to the end of the list. |
179 * |
179 * |
180 * @param list the list |
180 * @param list the list |
181 * @param elem a pointer to the element to add |
181 * @param elem a pointer to the element to add |
182 * @return zero on success, non-zero on memory allocation failure |
182 * @return zero on success, non-zero on memory allocation failure |
183 */ |
183 */ |
184 static inline int cxListAdd( |
184 static inline int cxListAdd( |
185 CxList list, |
185 CxList *list, |
186 void const *elem |
186 void const *elem |
187 ) { |
187 ) { |
188 return list->cl->add(list, elem); |
188 return list->cl->add(list, elem); |
189 } |
189 } |
190 |
190 |
248 */ |
248 */ |
249 static inline int cxListInsertBefore( |
249 static inline int cxListInsertBefore( |
250 CxIterator *iter, |
250 CxIterator *iter, |
251 void const *elem |
251 void const *elem |
252 ) { |
252 ) { |
253 return ((cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); |
253 return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1); |
254 } |
254 } |
255 |
255 |
256 /** |
256 /** |
257 * Removes the element at the specified index. |
257 * Removes the element at the specified index. |
258 * @param list the list |
258 * @param list the list |
259 * @param index the index of the element |
259 * @param index the index of the element |
260 * @return zero on success, non-zero if the index is out of bounds |
260 * @return zero on success, non-zero if the index is out of bounds |
261 */ |
261 */ |
262 static inline int cxListRemove( |
262 static inline int cxListRemove( |
263 CxList list, |
263 CxList *list, |
264 size_t index |
264 size_t index |
265 ) { |
265 ) { |
266 return list->cl->remove(list, index); |
266 return list->cl->remove(list, index); |
267 } |
267 } |
268 |
268 |
333 * |
333 * |
334 * \remark The underlying sort algorithm is implementation defined. |
334 * \remark The underlying sort algorithm is implementation defined. |
335 * |
335 * |
336 * @param list the list |
336 * @param list the list |
337 */ |
337 */ |
338 static inline void cxListSort(CxList list) { |
338 static inline void cxListSort(CxList *list) { |
339 list->cl->sort(list); |
339 list->cl->sort(list); |
340 } |
340 } |
341 |
341 |
342 /** |
342 /** |
343 * Reverses the order of the items. |
343 * Reverses the order of the items. |
344 * |
344 * |
345 * @param list the list |
345 * @param list the list |
346 */ |
346 */ |
347 static inline void cxListReverse(CxList list) { |
347 static inline void cxListReverse(CxList *list) { |
348 list->cl->reverse(list); |
348 list->cl->reverse(list); |
349 } |
349 } |
350 |
350 |
351 /** |
351 /** |
352 * Compares a list to another list of the same type. |
352 * Compares a list to another list of the same type. |