1 /*
2 * XPilot NG, a multiplayer space war game.
3 *
4 * Copyright (C) 2005 Kristian S�derblom <kps@users.sourceforge.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef ARRAYLIST_H
22 #define ARRAYLIST_H
23
24 #ifndef TYPES_H
25 # include "types.h"
26 #endif
27
28 #include "xpcommon.h"
29
30 typedef struct {
31 void *elements;
32 size_t element_size;
33 size_t element_padded_size;
34 int num_elements;
35 int max_elements;
36 } arraylist_t;
37
Arraylist_get_element_pointer(arraylist_t * alp,int ind)38 static inline void *Arraylist_get_element_pointer(arraylist_t *alp, int ind)
39 {
40 return (uint8_t *)alp->elements + alp->element_padded_size * ind;
41 }
42
Arraylist_get_num_elements(arraylist_t * alp)43 static inline int Arraylist_get_num_elements(arraylist_t *alp)
44 {
45 return alp->num_elements;
46 }
47
Arraylist_get_max_elements(arraylist_t * alp)48 static inline int Arraylist_get_max_elements(arraylist_t *alp)
49 {
50 return alp->max_elements;
51 }
52
53 arraylist_t *Arraylist_alloc(size_t element_size);
54 void Arraylist_free(arraylist_t *alp);
55
56 /*
57 * Removes all of the elements from this list by setting number
58 * of elements to 0.
59 */
60 void Arraylist_clear(arraylist_t *alp);
61
62 /* Get element at index 'ind'. */
63 void *Arraylist_get(arraylist_t *alp, int ind);
64
65 /* Set element at index 'ind' (possibly overwriting old element). */
66 void Arraylist_set(arraylist_t *alp, int ind, void *element);
67
68 /* Add element at end of list, return its index or -1 if failed. */
69 int Arraylist_add(arraylist_t *alp, void *element);
70
71 /* Insert element at position 'ind', return its 'ind' or -1 if failed. */
72 int Arraylist_insert(arraylist_t *alp, int ind, void *element);
73
74 /*
75 * Remove element at 'ind', require preservation of element order.
76 * Slower than Arraylist_fast_remove, because this function might
77 * move other elements. Doesn't free any memory.
78 */
79 void Arraylist_remove(arraylist_t *alp, int ind);
80
81 /*
82 * Remove element at 'ind', don't require preservation of element order.
83 * Doesn't free any memory.
84 */
85 void Arraylist_fast_remove(arraylist_t *alp, int ind);
86
87 /* Make max elements equal num elements. */
88 void Arraylist_trim(arraylist_t *alp);
89
90 /* Make sure max elements is at least 'capacity', return 'capacity' or -1. */
91 int Arraylist_ensure_capacity(arraylist_t *alp, int capacity);
92
93 /* Returns true if this list contains the specified element. */
94 bool Arraylist_contains(arraylist_t *alp, void *element);
95
96 /* Sort arraylist entries, check qsort(3) for explanation of 'cmp'. */
97 void Arraylist_sort(arraylist_t *alp, int (*cmp)(const void *, const void *));
98
99 typedef struct {
100 uint8_t *current_element;
101 int current_ind;
102 int num_elements;
103 size_t element_padded_size;
104 } arraylist_iterator_t;
105
106
107 /* Might implement something like this, but don't count on it. */
108 arraylist_iterator_t *Arraylist_iterator(arraylist_t *alp);
109 void Arraylist_iterator_free(arraylist_iterator_t *ip);
110 /* Initialise static iterator whose pointer is 'ip' */
111 void Arraylist_static_iterator(arraylist_t *alp, arraylist_iterator_t *ip);
112 bool Iterator_has_next(arraylist_iterator_t *ip);
113 void *Iterator_get_next(arraylist_iterator_t *ip);
114
115
116 #endif
117