source: trunk/include/range_list.h @ 98

Last change on this file since 98 was 98, checked in by tim, 16 years ago

adding experimental library which will be used to speed up hbin lookups

File size: 4.9 KB
Line 
1/*
2 * Copyright (C) 2008 Timothy D. Morgan
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
16 *
17 * $Id: $
18 */
19
20#include <stdlib.h>
21#include <stdbool.h>
22#include <stdint.h>
23#include <string.h>
24
25#ifndef _RANGE_LIST_H
26#define _RANGE_LIST_H
27
28
29typedef struct _range_list_element
30{
31  uint32_t offset;
32  uint32_t length;
33  void* data;
34} range_list_element;
35
36
37typedef struct _range_list
38{
39  range_list_element** elements;
40  uint32_t elem_alloced;
41  uint32_t size;
42} range_list;
43
44
45/* range_list_new():
46 *  Allocates a new range_list.
47 *
48 * Returns:
49 *  A newly allocated range_list, or NULL if an error occurred.
50 */
51range_list* range_list_new();
52
53
54/* range_list_free():
55 *  Frees the memory associated with a range_list, including the elements, but
56 *  not any data parameters referenced by those elements.
57 *
58 * Arguments:
59 *  rl -- the range_list to be free()d.
60 */
61void range_list_free(range_list* rl);
62
63
64/* range_list_size():
65 *  Query the current number of elements on a range_list
66 *
67 * Arguments:
68 *  rl -- the range_list to query
69 *
70 * Returns:
71 *  The number of elements currently in the list.
72 */
73uint32_t range_list_size(const range_list* rl);
74
75
76/* range_list_add():
77 *  Adds an element to the range_list. 
78 *  The new element must not overlap with others.
79 *  NOTE: this is a slow operation.
80 *
81 * Arguments:
82 *  rl     -- the range list to update
83 *  offset -- the starting point for the range
84 *  length -- the lenght of the range
85 *  data   -- misc data associated with this range element
86 * Returns:
87 *  true on success, false on failure.
88 *  Failures can occur due to memory limitations, max_size limitations,
89 *  or if the submitted range overlaps with an existing element.  Other
90 *  errors may also be possible.
91 */
92bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data);
93
94
95/* range_list_remove():
96 *  Removes an element from the list.  The element data structure will be
97 *  freed, but the data property will not be.
98 *
99 * Arguments:
100 *  rl     -- the range_list to modify
101 *  index  -- the element index to remove
102 *
103 * Returns:
104 *  true if the element was successfully removed, false otherwise.
105 */
106bool range_list_remove(range_list* rl, uint32_t index);
107
108
109/* range_list_get():
110 *  Retrieves the element for a given index.
111 *
112 * Arguments:
113 *  rl    -- the range_list being queried.
114 *  index -- the element index desired.
115 *
116 * Returns:
117 *  The element for a given index, or NULL if the element is not available.
118 */
119const range_list_element* range_list_get(const range_list* rl, uint32_t index);
120
121
122/* range_list_find():
123 *  Attempts to find the unique element whose range encompasses offset.
124 *
125 * Arguments:
126 *  rl     -- the range_list being queried.
127 *  offset -- the location for which an element is desired.
128 *
129 * Returns:
130 *  A matching element index or a negative value if none could be found.
131 */
132int32_t range_list_find(const range_list* rl, uint32_t offset);
133
134
135/* range_list_find_data():
136 *  Same as range_list_find(), but returns the data associated with an element.
137 *
138 * Arguments:
139 *  rl     -- the range_list being queried.
140 *  offset -- the address to search for in the ranges
141 *
142 * Returns:
143 *  The data element of the matching element index or NULL if none could
144 *  be found.
145 *
146 *  NOTE: May also return NULL if an element matched but if the data
147 *        element was never set.
148 */
149void* range_list_find_data(const range_list* rl, uint32_t offset);
150
151
152/* range_list_split_element():
153 *  Splits an existing element into two elements in place.
154 *
155 *  The resulting list will contain an additional element whose offset
156 *  is the one provided and whose length extends to the end of the old element
157 *  (the one identified by the index).  The original element's offset will
158 *  remain the same while it's length is shortened such that it is contiguous
159 *  with the newly created element.  The newly created element will have an index
160 *  of one more than the current element.
161 *
162 *  Both the original element and the newly created element will reference the
163 *  original element's data.
164 *
165 * Arguments:
166 *  rl     -- the range_list to modify
167 *  index  -- the index of the element to be split
168 *  offset -- the at which the element will be split
169 *
170 * Returns:
171 *  true if the element was successfully split, false otherwise.
172 *   
173 *
174 */
175bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset);
176
177#endif
Note: See TracBrowser for help on using the repository browser.