bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
5.2.9
by Gustav Hartvigsson
* Added license to files |
1 |
/*
|
2 |
Copyright (c) 2013-2014 Gustav Hartvigsson
|
|
3 |
||
4 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 |
of this software and associated documentation files (the "Software"), to deal
|
|
6 |
in the Software without restriction, including without limitation the rights
|
|
7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 |
copies of the Software, and to permit persons to whom the Software is
|
|
9 |
furnished to do so, subject to the following conditions:
|
|
10 |
||
11 |
The above copyright notice and this permission notice shall be included in
|
|
12 |
all copies or substantial portions of the Software.
|
|
13 |
||
14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 |
THE SOFTWARE.
|
|
21 |
*/
|
|
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
22 |
|
23 |
#ifndef __H_DYNAMIC_ARRAY__
|
|
24 |
#define __H_DYNAMIC_ARRAY__
|
|
25 |
#include <stdlib.h> |
|
26 |
#include "defs.h" |
|
27 |
||
|
5.2.9
by Gustav Hartvigsson
* Added license to files |
28 |
BEGIN_DECLS
|
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
29 |
|
30 |
/** @file
|
|
31 |
* Dynamic Array.
|
|
32 |
*
|
|
33 |
* This file contains an imlpementation of a "dynamic" array, it is usefule when
|
|
34 |
* dealing with lare amounts of data that may change over time, or with an
|
|
35 |
* unknowned numebr of items.
|
|
36 |
*
|
|
37 |
* Note that accsess time is constant, but write time is not guarenteed to be.
|
|
38 |
*
|
|
39 |
* When the size of the array is equal to the number of elements in it, it will
|
|
40 |
* re-allocate the array with a larger size.
|
|
41 |
*/
|
|
42 |
||
43 |
/**
|
|
44 |
* The padding that is added when expanding the array.
|
|
45 |
*/
|
|
46 |
#define ARRAY_PADDING 8
|
|
47 |
||
48 |
/**
|
|
49 |
* an opaque data structure that represents the dynamic array.
|
|
50 |
*/
|
|
51 |
typedef struct DynamicArray DynamicArray; |
|
52 |
||
53 |
/**
|
|
54 |
* Create a new dynamic array.
|
|
55 |
*
|
|
56 |
* @param len The length of the initial array.
|
|
57 |
* @param free_func The function to be used when freeing the items. Can be \c NULL.
|
|
58 |
* If free_func is NULL, it will use the standard library's
|
|
59 |
* <tt>free()</tt>.
|
|
60 |
*
|
|
61 |
* Normally a function with the signature <tt> (DynamicArray * self,
|
|
62 |
_pointer item, _pointer data) </tt> should be used but cast to FreeFunc.
|
|
63 |
*/
|
|
64 |
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func); |
|
65 |
||
66 |
/**
|
|
67 |
* Frees the dynamic array.
|
|
68 |
*
|
|
69 |
* after this is run the data will be lost.
|
|
70 |
*/
|
|
71 |
void dynamic_array_free (DynamicArray * self); |
|
72 |
||
73 |
||
74 |
/**
|
|
75 |
* Get an item from the array.
|
|
76 |
*/
|
|
77 |
_pointer dynamic_array_get (DynamicArray * self, size_t index); |
|
78 |
||
79 |
/**
|
|
80 |
* Get the length of the array.
|
|
81 |
*/
|
|
82 |
size_t dynamic_array_len (DynamicArray * self); |
|
83 |
||
84 |
||
85 |
/**
|
|
86 |
* Get the size of the array, this is not the same as the length of the array.
|
|
87 |
* The size is the number of elements that can be allocated without resizing
|
|
88 |
* the array.
|
|
89 |
*
|
|
90 |
* To get the length of the array use dynamic_array_len ().
|
|
91 |
*/
|
|
92 |
size_t dynamic_array_size (DynamicArray * self); |
|
93 |
||
94 |
||
95 |
/**
|
|
96 |
* Add an item to the array.
|
|
97 |
*/
|
|
98 |
void dynamic_array_add (DynamicArray * self, _pointer item); |
|
99 |
||
100 |
/**
|
|
101 |
* Dumps a copy of the array. Must be cast.
|
|
102 |
*
|
|
103 |
* Is not freed when the dynamic array is freed.
|
|
104 |
*
|
|
105 |
* Is null-terminated.
|
|
106 |
*/
|
|
107 |
_pointer* dynamic_array_dump_array (DynamicArray * self); |
|
108 |
||
109 |
/**
|
|
110 |
* Use a function on the array.
|
|
111 |
*/
|
|
112 |
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func, |
|
113 |
_pointer data); |
|
114 |
||
115 |
/** TODO
|
|
116 |
* same as dynamic_array_for_each (), with the difference that it returns a new
|
|
117 |
* DynamicArray.
|
|
118 |
*/
|
|
119 |
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self, |
|
120 |
ForEachFunc func, |
|
121 |
_pointer data); |
|
122 |
||
|
5.2.9
by Gustav Hartvigsson
* Added license to files |
123 |
END_DECLS
|
124 |
||
|
5.2.8
by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js |
125 |
#endif /* #define __H_DYNAMIC_ARRAY__ */ |