/simpletypesystem/trunk

To get this branch, use:
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"
30 by Gustav Hartvigsson
* Made the code compile using CMake.
27
#include "Func.h"
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
28
5.2.9 by Gustav Hartvigsson
* Added license to files
29
BEGIN_DECLS
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
30
31
/** @file
32
 * Dynamic Array.
33
 *
34
 * This file contains an imlpementation of a "dynamic" array, it is usefule when
35
 * dealing with lare amounts of data that may change over time, or with an
36
 * unknowned numebr of items.
37
 * 
38
 * Note that accsess time is constant, but write time is not guarenteed to be.
39
 * 
40
 * When the size of the array is equal to the number of elements in it, it will
41
 * re-allocate the array with a larger size.
42
 */
43
44
/**
45
 * The padding that is added when expanding the array.
46
 */
47
#define ARRAY_PADDING 8
48
49
/**
50
 * an opaque data structure that represents the dynamic array.
51
 */
52
typedef struct DynamicArray DynamicArray;
53
54
/**
55
 * Create a new dynamic array.
56
 *
57
 * @param len The length of the initial array.
58
 * @param free_func The function to be used when freeing the items. Can be \c NULL.
59
 *        If free_func is NULL, it will use the standard library's 
60
 *       <tt>free()</tt>.
61
 *        
62
 *        Normally a function with the signature <tt> (DynamicArray * self,
30 by Gustav Hartvigsson
* Made the code compile using CMake.
63
          spointer item, spointer data) </tt> should be used but cast to FreeFunc.
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
64
 */
65
DynamicArray * dynamic_array_new (size_t len, FreeFunc free_func);
66
67
/**
68
 * Frees the dynamic array.
69
 * 
70
 * after this is run the data will be lost.
71
 */
72
void dynamic_array_free (DynamicArray * self);
73
74
75
/**
76
 * Get an item from the array.
77
 */
30 by Gustav Hartvigsson
* Made the code compile using CMake.
78
spointer dynamic_array_get (DynamicArray * self, size_t index);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
79
80
/**
81
 * Get the length of the array.
82
 */
83
size_t dynamic_array_len (DynamicArray * self);
84
85
86
/**
87
 * Get the size of the array, this is not the same as the length of the array.
88
 * The size is the number of elements that can be allocated without resizing
89
 * the array.
90
 * 
91
 * To get the length of the array use dynamic_array_len ().
92
 */
93
size_t dynamic_array_size (DynamicArray * self);
94
95
96
/**
97
 * Add an item to the array.
98
 */
30 by Gustav Hartvigsson
* Made the code compile using CMake.
99
void dynamic_array_add (DynamicArray * self, spointer item);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
100
101
/**
102
 * Dumps a copy of the array. Must be cast.
103
 * 
104
 * Is not freed when the dynamic array is freed.
105
 * 
106
 * Is null-terminated.
107
 */
30 by Gustav Hartvigsson
* Made the code compile using CMake.
108
spointer* dynamic_array_dump_array (DynamicArray * self);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
109
110
/**
111
 * Use a function on the array.
112
 */
113
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func,
30 by Gustav Hartvigsson
* Made the code compile using CMake.
114
                             spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
115
116
/** TODO
117
 * same as dynamic_array_for_each (), with the difference that it returns a new
118
 * DynamicArray.
119
 */
120
DynamicArray * dynamic_array_for_each_with_return (DynamicArray * self,
121
                                                   ForEachFunc func,
30 by Gustav Hartvigsson
* Made the code compile using CMake.
122
                                                   spointer data);
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
123
5.2.9 by Gustav Hartvigsson
* Added license to files
124
END_DECLS
125
5.2.8 by Gustav Hartvigsson
* Copied over LinkedList and DynamicArray from c_sdl_js
126
#endif /* #define __H_DYNAMIC_ARRAY__ */