apr_skiplist.h revision 266733
1266733Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2266733Speter * contributor license agreements.  See the NOTICE file distributed with
3266733Speter * this work for additional information regarding copyright ownership.
4266733Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5266733Speter * (the "License"); you may not use this file except in compliance with
6266733Speter * the License.  You may obtain a copy of the License at
7266733Speter *
8266733Speter *     http://www.apache.org/licenses/LICENSE-2.0
9266733Speter *
10266733Speter * Unless required by applicable law or agreed to in writing, software
11266733Speter * distributed under the License is distributed on an "AS IS" BASIS,
12266733Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13266733Speter * See the License for the specific language governing permissions and
14266733Speter * limitations under the License.
15266733Speter */
16266733Speter
17266733Speter#ifndef APR_SKIPLIST_H
18266733Speter#define APR_SKIPLIST_H
19266733Speter/**
20266733Speter * @file apr_skiplist.h
21266733Speter * @brief APR skip list implementation
22266733Speter */
23266733Speter
24266733Speter#include "apr.h"
25266733Speter#include "apr_portable.h"
26266733Speter#include <stdlib.h>
27266733Speter
28266733Speter#ifdef __cplusplus
29266733Speterextern "C" {
30266733Speter#endif /* __cplusplus */
31266733Speter
32266733Speter/**
33266733Speter * @defgroup apr_skiplist Skip list implementation
34266733Speter * Refer to http://en.wikipedia.org/wiki/Skip_list for information
35266733Speter * about the purpose of and ideas behind skip lists.
36266733Speter * @ingroup APR
37266733Speter * @{
38266733Speter */
39266733Speter
40266733Speter/**
41266733Speter * apr_skiplist_compare is the function type that must be implemented
42266733Speter * per object type that is used in a skip list for comparisons to maintain
43266733Speter * order
44266733Speter * */
45266733Spetertypedef int (*apr_skiplist_compare) (void *, void *);
46266733Speter
47266733Speter/**
48266733Speter * apr_skiplist_freefunc is the function type that must be implemented
49266733Speter * to handle elements as they are removed from a skip list.
50266733Speter */
51266733Spetertypedef void (*apr_skiplist_freefunc) (void *);
52266733Speter
53266733Speter/** Opaque structure used to represent the skip list */
54266733Speterstruct apr_skiplist;
55266733Speter/** Opaque structure used to represent the skip list */
56266733Spetertypedef struct apr_skiplist apr_skiplist;
57266733Speter
58266733Speter/**
59266733Speter * Opaque structure used to represent abstract nodes in the skip list
60266733Speter * (an abstraction above the raw elements which are collected in the
61266733Speter * skip list).
62266733Speter */
63266733Speterstruct apr_skiplistnode;
64266733Speter/** Opaque structure */
65266733Spetertypedef struct apr_skiplistnode apr_skiplistnode;
66266733Speter
67266733Speter/**
68266733Speter * Allocate memory using the same mechanism as the skip list.
69266733Speter * @param sl The skip list
70266733Speter * @param size The amount to allocate
71266733Speter * @remark If a pool was provided to apr_skiplist_init(), memory will
72266733Speter * be allocated from the pool or from a free list maintained with
73266733Speter * the skip list.  Otherwise, memory will be allocated using the
74266733Speter * C standard library heap functions.
75266733Speter */
76266733SpeterAPR_DECLARE(void *) apr_skiplist_alloc(apr_skiplist *sl, size_t size);
77266733Speter
78266733Speter/**
79266733Speter * Free memory using the same mechanism as the skip list.
80266733Speter * @param sl The skip list
81266733Speter * @param mem The object to free
82266733Speter * @remark If a pool was provided to apr_skiplist_init(), memory will
83266733Speter * be added to a free list maintained with the skip list and be available
84266733Speter * to operations on the skip list or to other calls to apr_skiplist_alloc().
85266733Speter * Otherwise, memory will be freed using the  C standard library heap
86266733Speter * functions.
87266733Speter */
88266733SpeterAPR_DECLARE(void) apr_skiplist_free(apr_skiplist *sl, void *mem);
89266733Speter
90266733Speter/**
91266733Speter * Allocate a new skip list
92266733Speter * @param sl The pointer in which to return the newly created skip list
93266733Speter * @param p The pool from which to allocate the skip list (optional).
94266733Speter * @remark Unlike most APR functions, a pool is optional.  If no pool
95266733Speter * is provided, the C standard library heap functions will be used instead.
96266733Speter */
97266733SpeterAPR_DECLARE(apr_status_t) apr_skiplist_init(apr_skiplist **sl, apr_pool_t *p);
98266733Speter
99266733Speter/**
100266733Speter * Set the comparison functions to be used for searching the skip list.
101266733Speter * @param sl The skip list
102266733Speter * @param XXX1 FIXME
103266733Speter * @param XXX2 FIXME
104266733Speter *
105266733Speter * @remark If existing comparison functions are being replaced, the index
106266733Speter * will be replaced during this call.  That is a potentially expensive
107266733Speter * operation.
108266733Speter */
109266733SpeterAPR_DECLARE(void) apr_skiplist_set_compare(apr_skiplist *sl, apr_skiplist_compare XXX1,
110266733Speter                             apr_skiplist_compare XXX2);
111266733Speter
112266733Speter/**
113266733Speter * Set the indexing functions to the specified comparison functions and
114266733Speter * rebuild the index.
115266733Speter * @param sl The skip list
116266733Speter * @param XXX1 FIXME
117266733Speter * @param XXX2 FIXME
118266733Speter *
119266733Speter * @remark If an index already exists, it will not be replaced and the
120266733Speter * comparison functions will not be changed.
121266733Speter */
122266733SpeterAPR_DECLARE(void) apr_skiplist_add_index(apr_skiplist *sl, apr_skiplist_compare XXX1,
123266733Speter                        apr_skiplist_compare XXX2);
124266733Speter
125266733Speter/**
126266733Speter * Return the list maintained by the skip list abstraction.
127266733Speter * @param sl The skip list
128266733Speter */
129266733SpeterAPR_DECLARE(apr_skiplistnode *) apr_skiplist_getlist(apr_skiplist *sl);
130266733Speter
131266733Speter/**
132266733Speter * Return the next matching element in the skip list using the specified
133266733Speter * comparison function.
134266733Speter * @param sl The skip list
135266733Speter * @param data The value to search for
136266733Speter * @param iter A pointer to the returned skip list node representing the element
137266733Speter * found
138266733Speter * @param func The comparison function to use
139266733Speter */
140266733SpeterAPR_DECLARE(void *) apr_skiplist_find_compare(apr_skiplist *sl,
141266733Speter                               void *data,
142266733Speter                               apr_skiplistnode **iter,
143266733Speter                               apr_skiplist_compare func);
144266733Speter
145266733Speter/**
146266733Speter * Return the next matching element in the skip list using the current comparison
147266733Speter * function.
148266733Speter * @param sl The skip list
149266733Speter * @param data The value to search for
150266733Speter * @param iter A pointer to the returned skip list node representing the element
151266733Speter * found
152266733Speter */
153266733SpeterAPR_DECLARE(void *) apr_skiplist_find(apr_skiplist *sl, void *data, apr_skiplistnode **iter);
154266733Speter
155266733Speter/**
156266733Speter * Return the next element in the skip list.
157266733Speter * @param sl The skip list
158266733Speter * @param iter On entry, a pointer to the skip list node to start with; on return,
159266733Speter * a pointer to the skip list node representing the element returned
160266733Speter * @remark If iter points to a NULL value on entry, NULL will be returned.
161266733Speter */
162266733SpeterAPR_DECLARE(void *) apr_skiplist_next(apr_skiplist *sl, apr_skiplistnode **iter);
163266733Speter
164266733Speter/**
165266733Speter * Return the previous element in the skip list.
166266733Speter * @param sl The skip list
167266733Speter * @param iter On entry, a pointer to the skip list node to start with; on return,
168266733Speter * a pointer to the skip list node representing the element returned
169266733Speter * @remark If iter points to a NULL value on entry, NULL will be returned.
170266733Speter */
171266733SpeterAPR_DECLARE(void *) apr_skiplist_previous(apr_skiplist *sl, apr_skiplistnode **iter);
172266733Speter
173266733Speter/**
174266733Speter * Insert an element into the skip list using the specified comparison function.
175266733Speter * @param sl The skip list
176266733Speter * @param data The element to insert
177266733Speter * @param comp The comparison function to use for placement into the skip list
178266733Speter */
179266733SpeterAPR_DECLARE(apr_skiplistnode *) apr_skiplist_insert_compare(apr_skiplist *sl,
180266733Speter                                          void *data, apr_skiplist_compare comp);
181266733Speter
182266733Speter/**
183266733Speter * Insert an element into the skip list using the existing comparison function.
184266733Speter * @param sl The skip list
185266733Speter * @param data The element to insert
186266733Speter * @remark If no comparison function has been set for the skip list, the element
187266733Speter * will not be inserted and NULL will be returned.
188266733Speter */
189266733SpeterAPR_DECLARE(apr_skiplistnode *) apr_skiplist_insert(apr_skiplist* sl, void *data);
190266733Speter
191266733Speter/**
192266733Speter * Remove an element from the skip list using the specified comparison function for
193266733Speter * locating the element.
194266733Speter * @param sl The skip list
195266733Speter * @param data The element to remove
196266733Speter * @param myfree A function to be called for each removed element
197266733Speter * @param comp The comparison function to use for placement into the skip list
198266733Speter * @remark If the element is not found, 0 will be returned.  Otherwise, the heightXXX
199266733Speter * will be returned.
200266733Speter */
201266733SpeterAPR_DECLARE(int) apr_skiplist_remove_compare(apr_skiplist *sl, void *data,
202266733Speter                               apr_skiplist_freefunc myfree, apr_skiplist_compare comp);
203266733Speter
204266733Speter/**
205266733Speter * Remove an element from the skip list using the existing comparison function for
206266733Speter * locating the element.
207266733Speter * @param sl The skip list
208266733Speter * @param data The element to remove
209266733Speter * @param myfree A function to be called for each removed element
210266733Speter * @remark If the element is not found, 0 will be returned.  Otherwise, the heightXXX
211266733Speter * will be returned.
212266733Speter * @remark If no comparison function has been set for the skip list, the element
213266733Speter * will not be removed and 0 will be returned.
214266733Speter */
215266733SpeterAPR_DECLARE(int) apr_skiplist_remove(apr_skiplist *sl, void *data, apr_skiplist_freefunc myfree);
216266733Speter
217266733Speter/**
218266733Speter * Remove all elements from the skip list.
219266733Speter * @param sl The skip list
220266733Speter * @param myfree A function to be called for each removed element
221266733Speter */
222266733SpeterAPR_DECLARE(void) apr_skiplist_remove_all(apr_skiplist *sl, apr_skiplist_freefunc myfree);
223266733Speter
224266733Speter/**
225266733Speter * Remove each element from the skip list.
226266733Speter * @param sl The skip list
227266733Speter * @param myfree A function to be called for each removed element
228266733Speter */
229266733SpeterAPR_DECLARE(void) apr_skiplist_destroy(apr_skiplist *sl, apr_skiplist_freefunc myfree);
230266733Speter
231266733Speter/**
232266733Speter * Return the first element in the skip list, leaving the element in the skip list.
233266733Speter * @param sl The skip list
234266733Speter * @param myfree A function to be called for the removed element
235266733Speter * @remark NULL will be returned if there are no elements
236266733Speter */
237266733SpeterAPR_DECLARE(void *) apr_skiplist_pop(apr_skiplist *sl, apr_skiplist_freefunc myfree);
238266733Speter
239266733Speter/**
240266733Speter * Return the first element in the skip list, leaving the element in the skip list.
241266733Speter * @param sl The skip list
242266733Speter * @remark NULL will be returned if there are no elements
243266733Speter */
244266733SpeterAPR_DECLARE(void *) apr_skiplist_peek(apr_skiplist *sl);
245266733Speter
246266733Speter/**
247266733Speter * Merge two skip lists.  XXX SEMANTICS
248266733Speter * @param sl1 One of two skip lists to be merged
249266733Speter * @param sl2 The other of two skip lists to be merged
250266733Speter */
251266733SpeterAPR_DECLARE(apr_skiplist *) apr_skiplist_merge(apr_skiplist *sl1, apr_skiplist *sl2);
252266733Speter
253266733Speter/** @} */
254266733Speter
255266733Speter#ifdef __cplusplus
256266733Speter}
257266733Speter#endif
258266733Speter
259266733Speter#endif /* ! APR_SKIPLIST_H */
260