0
|
1 /* ============================================================================
|
|
2 * Freetype GL - A C OpenGL Freetype engine
|
|
3 * Platform: Any
|
|
4 * WWW: http://code.google.com/p/freetype-gl/
|
|
5 * ----------------------------------------------------------------------------
|
|
6 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are met:
|
|
10 *
|
|
11 * 1. Redistributions of source code must retain the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer.
|
|
13 *
|
|
14 * 2. Redistributions in binary form must reproduce the above copyright
|
|
15 * notice, this list of conditions and the following disclaimer in the
|
|
16 * documentation and/or other materials provided with the distribution.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
|
|
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
21 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 *
|
|
29 * The views and conclusions contained in the software and documentation are
|
|
30 * those of the authors and should not be interpreted as representing official
|
|
31 * policies, either expressed or implied, of Nicolas P. Rougier.
|
|
32 * ============================================================================
|
|
33 */
|
|
34 #ifndef __VECTOR_H__
|
|
35 #define __VECTOR_H__
|
|
36
|
|
37 #include <stdlib.h>
|
|
38
|
|
39 #ifdef __cplusplus
|
|
40 extern "C" {
|
|
41 #endif
|
|
42
|
|
43 #include <stddef.h>
|
|
44
|
|
45 /**
|
|
46 * @file vector.h
|
|
47 * @author Nicolas Rougier (Nicolas.Rougier@inria.fr)
|
|
48 *
|
|
49 * @defgroup vector Vector
|
|
50 *
|
|
51 * The vector structure and accompanying functions loosely mimic the STL C++
|
|
52 * vector class. It is used by @ref texture-atlas (for storing nodes), @ref
|
|
53 * texture-font (for storing glyphs) and @ref font-manager (for storing fonts).
|
|
54 * More information at http://www.cppreference.com/wiki/container/vector/start
|
|
55 *
|
|
56 * <b>Example Usage</b>:
|
|
57 * @code
|
|
58 * #include "vector.h"
|
|
59 *
|
|
60 * int main( int arrgc, char *argv[] )
|
|
61 * {
|
|
62 * int i,j = 1;
|
|
63 * vector_t * vector = vector_new( sizeof(int) );
|
|
64 * vector_push_back( &i );
|
|
65 *
|
|
66 * j = * (int *) vector_get( vector, 0 );
|
|
67 * vector_delete( vector);
|
|
68 *
|
|
69 * return 0;
|
|
70 * }
|
|
71 * @endcode
|
|
72 *
|
|
73 * @{
|
|
74 */
|
|
75
|
|
76 /**
|
|
77 * Generic vector structure.
|
|
78 *
|
|
79 * @memberof vector
|
|
80 */
|
|
81 typedef struct
|
|
82 {
|
|
83 /** Pointer to dynamically allocated items. */
|
|
84 void * items;
|
|
85
|
|
86 /** Number of items that can be held in currently allocated storage. */
|
|
87 size_t capacity;
|
|
88
|
|
89 /** Number of items. */
|
|
90 size_t size;
|
|
91
|
|
92 /** Size (in bytes) of a single item. */
|
|
93 size_t item_size;
|
|
94 } vector_t;
|
|
95
|
|
96
|
|
97 /**
|
|
98 * Creates a new empty vector.
|
|
99 *
|
|
100 * @param item_size item size in bytes
|
|
101 * @return a new empty vector
|
|
102 *
|
|
103 */
|
|
104 vector_t *
|
|
105 vector_new( size_t item_size );
|
|
106
|
|
107
|
|
108 /**
|
|
109 * Deletes a vector.
|
|
110 *
|
|
111 * @param self a vector structure
|
|
112 *
|
|
113 */
|
|
114 void
|
|
115 vector_delete( vector_t *self );
|
|
116
|
|
117
|
|
118 /**
|
|
119 * Returns a pointer to the item located at specified index.
|
|
120 *
|
|
121 * @param self a vector structure
|
|
122 * @param index the index of the item to be returned
|
|
123 * @return pointer on the specified item
|
|
124 */
|
|
125 const void *
|
|
126 vector_get( const vector_t *self,
|
|
127 size_t index );
|
|
128
|
|
129
|
|
130 /**
|
|
131 * Returns a pointer to the first item.
|
|
132 *
|
|
133 * @param self a vector structure
|
|
134 * @return pointer on the first item
|
|
135 */
|
|
136 const void *
|
|
137 vector_front( const vector_t *self );
|
|
138
|
|
139
|
|
140 /**
|
|
141 * Returns a pointer to the last item
|
|
142 *
|
|
143 * @param self a vector structure
|
|
144 * @return pointer on the last item
|
|
145 */
|
|
146 const void *
|
|
147 vector_back( const vector_t *self );
|
|
148
|
|
149
|
|
150 /**
|
|
151 * Check if an item is contained within the vector.
|
|
152 *
|
|
153 * @param self a vector structure
|
|
154 * @param item item to be searched in the vector
|
|
155 * @param cmp a pointer a comparison function
|
|
156 * @return 1 if item is contained within the vector, 0 otherwise
|
|
157 */
|
|
158 int
|
|
159 vector_contains( const vector_t *self,
|
|
160 const void *item,
|
|
161 int (*cmp)(const void *, const void *) );
|
|
162
|
|
163
|
|
164 /**
|
|
165 * Checks whether the vector is empty.
|
|
166 *
|
|
167 * @param self a vector structure
|
|
168 * @return 1 if the vector is empty, 0 otherwise
|
|
169 */
|
|
170 int
|
|
171 vector_empty( const vector_t *self );
|
|
172
|
|
173
|
|
174 /**
|
|
175 * Returns the number of items
|
|
176 *
|
|
177 * @param self a vector structure
|
|
178 * @return number of items
|
|
179 */
|
|
180 size_t
|
|
181 vector_size( const vector_t *self );
|
|
182
|
|
183
|
|
184 /**
|
|
185 * Reserve storage such that it can hold at last size items.
|
|
186 *
|
|
187 * @param self a vector structure
|
|
188 * @param size the new storage capacity
|
|
189 */
|
|
190 void
|
|
191 vector_reserve( vector_t *self,
|
|
192 const size_t size );
|
|
193
|
|
194
|
|
195 /**
|
|
196 * Returns current storage capacity
|
|
197 *
|
|
198 * @param self a vector structure
|
|
199 * @return storage capacity
|
|
200 */
|
|
201 size_t
|
|
202 vector_capacity( const vector_t *self );
|
|
203
|
|
204
|
|
205 /**
|
|
206 * Decrease capacity to fit actual size.
|
|
207 *
|
|
208 * @param self a vector structure
|
|
209 */
|
|
210 void
|
|
211 vector_shrink( vector_t *self );
|
|
212
|
|
213
|
|
214 /**
|
|
215 * Removes all items.
|
|
216 *
|
|
217 * @param self a vector structure
|
|
218 */
|
|
219 void
|
|
220 vector_clear( vector_t *self );
|
|
221
|
|
222
|
|
223 /**
|
|
224 * Replace an item.
|
|
225 *
|
|
226 * @param self a vector structure
|
|
227 * @param index the index of the item to be replaced
|
|
228 * @param item the new item
|
|
229 */
|
|
230 void
|
|
231 vector_set( vector_t *self,
|
|
232 const size_t index,
|
|
233 const void *item );
|
|
234
|
|
235
|
|
236 /**
|
|
237 * Erase an item.
|
|
238 *
|
|
239 * @param self a vector structure
|
|
240 * @param index the index of the item to be erased
|
|
241 */
|
|
242 void
|
|
243 vector_erase( vector_t *self,
|
|
244 const size_t index );
|
|
245
|
|
246
|
|
247 /**
|
|
248 * Erase a range of items.
|
|
249 *
|
|
250 * @param self a vector structure
|
|
251 * @param first the index of the first item to be erased
|
|
252 * @param last the index of the last item to be erased
|
|
253 */
|
|
254 void
|
|
255 vector_erase_range( vector_t *self,
|
|
256 const size_t first,
|
|
257 const size_t last );
|
|
258
|
|
259
|
|
260 /**
|
|
261 * Appends given item to the end of the vector.
|
|
262 *
|
|
263 * @param self a vector structure
|
|
264 * @param item the item to be inserted
|
|
265 */
|
|
266 void
|
|
267 vector_push_back( vector_t *self,
|
|
268 const void *item );
|
|
269
|
|
270
|
|
271 /**
|
|
272 * Removes the last item of the vector.
|
|
273 *
|
|
274 * @param self a vector structure
|
|
275 */
|
|
276 void
|
|
277 vector_pop_back( vector_t *self );
|
|
278
|
|
279
|
|
280 /**
|
|
281 * Resizes the vector to contain size items
|
|
282 *
|
|
283 * If the current size is less than size, additional items are appended and
|
|
284 * initialized with value. If the current size is greater than size, the
|
|
285 * vector is reduced to its first size elements.
|
|
286 *
|
|
287 * @param self a vector structure
|
|
288 * @param size the new size
|
|
289 */
|
|
290 void
|
|
291 vector_resize( vector_t *self,
|
|
292 const size_t size );
|
|
293
|
|
294
|
|
295 /**
|
|
296 * Insert a single item at specified index.
|
|
297 *
|
|
298 * @param self a vector structure
|
|
299 * @param index location before which to insert item
|
|
300 * @param item the item to be inserted
|
|
301 */
|
|
302 void
|
|
303 vector_insert( vector_t *self,
|
|
304 const size_t index,
|
|
305 const void *item );
|
|
306
|
|
307
|
|
308 /**
|
|
309 * Insert raw data at specified index.
|
|
310 *
|
|
311 * @param self a vector structure
|
|
312 * @param index location before which to insert item
|
|
313 * @param data a pointer to the items to be inserted
|
|
314 * @param count the number of items to be inserted
|
|
315 */
|
|
316 void
|
|
317 vector_insert_data( vector_t *self,
|
|
318 const size_t index,
|
|
319 const void * data,
|
|
320 const size_t count );
|
|
321
|
|
322
|
|
323 /**
|
|
324 * Append raw data to the end of the vector.
|
|
325 *
|
|
326 * @param self a vector structure
|
|
327 * @param data a pointer to the items to be inserted
|
|
328 * @param count the number of items to be inserted
|
|
329 */
|
|
330 void
|
|
331 vector_push_back_data( vector_t *self,
|
|
332 const void * data,
|
|
333 const size_t count );
|
|
334
|
|
335
|
|
336 /**
|
|
337 * Sort vector items according to cmp function.
|
|
338 *
|
|
339 * @param self a vector structure
|
|
340 * @param cmp a pointer a comparison function
|
|
341 */
|
|
342 void
|
|
343 vector_sort( vector_t *self,
|
|
344 int (*cmp)(const void *, const void *) );
|
|
345
|
|
346
|
|
347 /** @} */
|
|
348
|
|
349 #ifdef __cplusplus
|
|
350 }
|
|
351 #endif
|
|
352
|
|
353 #endif /* __VECTOR_H__ */
|