Freeciv-3.2
Loading...
Searching...
No Matches
specvec.h
Go to the documentation of this file.
1/**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
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
14/* specvectors: "specific vectors".
15
16 This file is used to implement resizable arrays.
17
18 Before including this file, you must define the following:
19 SPECVEC_TAG - this tag will be used to form names for functions etc.
20 You may also define:
21 SPECVEC_TYPE - the typed vector will contain pointers to this type;
22 If SPECVEC_TYPE is not defined, then 'struct SPECVEC_TAG' is used.
23 At the end of this file, these (and other defines) are undef-ed.
24
25 Assuming SPECVEC_TAG were 'foo', and SPECVEC_TYPE were 'foo_t',
26 including this file would provide a struct definition for:
27 struct foo_vector;
28 and prototypes for the following functions:
29 void foo_vector_init(struct foo_vector *tthis);
30 void foo_vector_reserve(struct foo_vector *tthis, int n);
31 int foo_vector_size(const struct foo_vector *tthis);
32 foo_t *foo_vector_get(struct foo_vector *tthis, int svindex);
33 void foo_vector_copy(struct foo_vector *to,
34 const struct foo_vector *from);
35 void foo_vector_free(struct foo_vector *tthis);
36 void foo_vector_append(struct foo_vector *tthis, foo_t pfoo);
37 void foo_vector_remove(struct foo_vector *tthis, int svindex);
38
39 Note this is not protected against multiple inclusions; this is
40 so that you can have multiple different specvectors. For each
41 specvector, this file should be included _once_, inside a .h file
42 which _is_ itself protected against multiple inclusions.
43*/
44
45#ifdef __cplusplus
46extern "C" {
47#endif /* __cplusplus */
48
49#include <string.h> /* for memcpy */
50
51/* utility */
52#include "mem.h"
53
54#ifndef SPECVEC_TAG
55#error Must define a SPECVEC_TAG to use this header
56#endif
57#ifndef SPECVEC_TYPE
58#define SPECVEC_TYPE struct SPECVEC_TAG
59#endif
60
61#define SPECVEC_PASTE_(x,y) x ## y
62#define SPECVEC_PASTE(x,y) SPECVEC_PASTE_(x,y)
63
64#define SPECVEC_VECTOR struct SPECVEC_PASTE(SPECVEC_TAG, _vector)
65
66#define SPECVEC_FOO(suffix) SPECVEC_PASTE(SPECVEC_TAG, suffix)
67
69 /* Users are allowed to access the data pointer directly. */
70 SPECVEC_TYPE *p;
71
73};
74
75/**********************************************************************/
79{
80 tthis->p = NULL;
81 tthis->size = tthis->size_alloc = 0;
82}
83
84/**********************************************************************/
88 size_t size)
89{
90 if (size > tthis->size_alloc) {
91 size_t new_size = MAX(size, tthis->size_alloc * 2);
92
94 new_size * sizeof(*tthis->p));
95 tthis->size_alloc = new_size;
96 }
97 tthis->size = size;
98}
99
100/**********************************************************************/
103static inline size_t SPECVEC_FOO(_vector_size) (const SPECVEC_VECTOR *tthis)
104{
105 return tthis->size;
106}
107
108/**********************************************************************/
112 *tthis,
113 int svindex)
114{
115 if (svindex == -1 && tthis->size > 0) {
116 return tthis->p + tthis->size - 1;
117 } else if (svindex >= 0 && (size_t)svindex < tthis->size) {
118 return tthis->p + svindex;
119 } else {
120 return NULL;
121 }
122}
123
124/**********************************************************************/
129static inline void SPECVEC_FOO(_vector_copy) (SPECVEC_VECTOR *to,
130 const SPECVEC_VECTOR *from)
131{
132 SPECVEC_FOO(_vector_reserve) (to, from->size);
133 if (from->size > 0) {
134 memcpy(to->p, from->p, from->size * sizeof(*to->p));
135 }
136}
137
138/**********************************************************************/
142{
143 if (tthis->p) {
144 free(tthis->p);
145 }
147}
148
149/**********************************************************************/
153 SPECVEC_TYPE const pfoo)
154{
156 tthis->p[tthis->size - 1] = pfoo;
157}
158
159/**********************************************************************/
163 const int svindex)
164{
165 size_t i;
166
167 /* Be consistent with SPECVEC_FOO_vector_get(): Allow negative element
168 * number. */
169 const size_t rmpos = (svindex < 0 ?
171 (size_t)svindex);
172
174
175 for (i = rmpos; (i + 1) < SPECVEC_FOO(_vector_size)(tthis); i++) {
176 /* Relocate the elements following the deleted element. */
177 tthis->p[i] = tthis->p[i + 1];
178 }
179
182}
183
184
185
186#define TYPED_VECTOR_ITERATE(atype, vector, var) { \
187 unsigned int myiter##var; \
188 atype *var; \
189 for (myiter##var = 0; myiter##var < (vector)->size; myiter##var++) { \
190 var = &(vector)->p[myiter##var]; \
191
192/* Balance for above: */
193#define VECTOR_ITERATE_END }}
194
195
196#undef SPECVEC_TAG
197#undef SPECVEC_TYPE
198#undef SPECVEC_PASTE_
199#undef SPECVEC_PASTE
200#undef SPECVEC_VECTOR
201#undef SPECVEC_FOO
202
203#ifdef __cplusplus
204}
205#endif /* __cplusplus */
char * incite_cost
Definition comments.c:75
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_realloc(ptr, sz)
Definition mem.h:36
#define MAX(x, y)
Definition shared.h:54
static void SPECVEC_FOO() _vector_append(SPECVEC_VECTOR *tthis, SPECVEC_TYPE const pfoo)
Definition specvec.h:152
static void SPECVEC_FOO() _vector_free(SPECVEC_VECTOR *tthis)
Definition specvec.h:141
static void SPECVEC_FOO() _vector_init(SPECVEC_VECTOR *tthis)
Definition specvec.h:78
static void SPECVEC_FOO() _vector_remove(SPECVEC_VECTOR *tthis, const int svindex)
Definition specvec.h:162
#define SPECVEC_FOO(suffix)
Definition specvec.h:66
static void SPECVEC_FOO() _vector_copy(SPECVEC_VECTOR *to, const SPECVEC_VECTOR *from)
Definition specvec.h:129
static size_t SPECVEC_FOO() _vector_size(const SPECVEC_VECTOR *tthis)
Definition specvec.h:103
size_t size
Definition specvec.h:72
size_t size_alloc
Definition specvec.h:72
#define SPECVEC_VECTOR
Definition specvec.h:64
static void SPECVEC_FOO() _vector_reserve(SPECVEC_VECTOR *tthis, size_t size)
Definition specvec.h:87
static SPECVEC_TYPE *SPECVEC_FOO() _vector_get(const SPECVEC_VECTOR *tthis, int svindex)
Definition specvec.h:111
#define SPECVEC_TYPE
Definition specvec.h:58