1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
#ifndef VECTOR_H_
#define VECTOR_H_
/** @file vector.h
*
* An implementation of dynamic array.
*
* Example:
* @code
* size_t i;
* Vector(int, vec);
* VectorPushBack(vec, 1);
* VectorPushBack(vec, 2);
* VectorPushBack(vec, 3);
* for ( i = 0; i < VectorSize(vec); i++ )
* printf("%d\n", VectorPtr(vec)[i]);
* VectorFree(vec);
* @endcode
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ Includes :
*/
#include <stddef.h>
#include <string.h>
#include "declare.h"
/*
#] Includes :
#[ Vector :
#[ VectorStruct :
*/
/**
* A struct for vector objects.
*
* @param T the type of elements.
*/
#define VectorStruct(T) \
struct { \
T *ptr; \
size_t size; \
size_t capacity; \
}
/*
#] VectorStruct :
#[ Vector :
*/
/**
* Defines and initialises a vector X of the type T.
* The user must call VectorFree(X) after the use of X.
*
* @param T the type of elements.
* @param X the name of vector object
*/
#define Vector(T, X) \
VectorStruct(T) X = { NULL, 0, 0 }
/*
#] Vector :
#[ DeclareVector :
*/
/**
* Declares a vector X of the type T.
* The user must call VectorInit(X) before the use of X.
*
* @param T the type of elements.
* @param X the name of vector object
*/
#define DeclareVector(T, X) \
VectorStruct(T) X
/*
#] DeclareVector :
#[ VectorInit :
*/
/**
* Initialises a vector X of the type T.
* The user must call VectorFree(X) after the use of X.
*
* @param X the vector object.
*/
#define VectorInit(X) \
do { \
(X).ptr = NULL; \
(X).size = 0; \
(X).capacity = 0; \
} while (0)
/*
#] VectorInit :
#[ VectorFree :
*/
/**
* Frees the buffer allocated by the vector X.
*
* @param X the vector object.
*/
#define VectorFree(X) \
do { \
M_free((X).ptr, "VectorFree:" #X); \
(X).ptr = NULL; \
(X).size = 0; \
(X).capacity = 0; \
} while (0)
/*
#] VectorFree :
#[ VectorPtr :
*/
/**
* Returns the pointer to the buffer for the vector X.
* NULL when VectorCapacity(X) == 0.
*
* @param X the vector object.
* @return the pointer to the allocated buffer for the vector.
*/
#define VectorPtr(X) \
((X).ptr)
/*
#] VectorPtr :
#[ VectorFront :
*/
/**
* Returns the first element of the vector X.
* Undefined when VectorSize(X) == 0.
*
* @param X the vector object.
* @return the first element of the vector.
*/
#define VectorFront(X) \
((X).ptr[0])
/*
#] VectorFront :
#[ VectorBack :
*/
/**
* Returns the last element of the vector X.
* Undefined when VectorSize(X) == 0.
*
* @param X the vector object.
* @return the last element of the vector.
*/
#define VectorBack(X) \
((X).ptr[(X).size - 1])
/*
#] VectorBack :
#[ VectorSize :
*/
/**
* Returns the size of the vector X.
*
* @param X the vector object.
* @return the size of the vector.
*/
#define VectorSize(X) \
((X).size)
/*
#] VectorSize :
#[ VectorCapacity :
*/
/**
* Returns the capacity (the number of the already allocated elements) of the vector X.
*
* @param X the vector object.
* @return the capacity of the vector.
*/
#define VectorCapacity(X) \
((X).capacity)
/*
#] VectorCapacity :
#[ VectorEmpty :
*/
/**
* Returns true the size of the vector X is zero.
*
* @param X the vector object.
* @return true if the vector has no elements, false otherwise.
*/
#define VectorEmpty(X) \
((X).size == 0)
/*
#] VectorEmpty :
#[ VectorClear :
*/
/**
* Sets the size of the vector X to zero.
*
* @param X the vector object.
*/
#define VectorClear(X) \
do { (X).size = 0; } while (0)
/*
#] VectorClear :
#[ VectorReserve :
*/
/**
* Requires that the capacity of the vector X is equal to or lager than newcapacity.
*
* @param X the vector object.
* @param newcapacity the capacity to be reserved.
*/
#define VectorReserve(X, newcapacity) \
do { \
size_t v_tmp_newcapacity_ = (newcapacity); \
if ( (X).capacity < v_tmp_newcapacity_ ) { \
void *v_tmp_newptr_; \
v_tmp_newcapacity_ = (v_tmp_newcapacity_ * 3) / 2; \
if ( v_tmp_newcapacity_ < 4 ) v_tmp_newcapacity_ = 4; \
v_tmp_newptr_ = Malloc1(sizeof((X).ptr[0]) * v_tmp_newcapacity_, "VectorReserve:" #X); \
if ( (X).ptr != NULL ) { \
memcpy(v_tmp_newptr_, (X).ptr, (X).size * sizeof((X).ptr[0])); \
M_free((X).ptr, "VectorReserve:" #X); \
} \
(X).ptr = v_tmp_newptr_; \
(X).capacity = v_tmp_newcapacity_; \
} \
} while (0)
/*
#] VectorReserve :
#[ VectorPushBack :
*/
/**
* Adds an element x at the end of the vector X.
*
* @param X the vector object.
* @param x the element to be added.
*/
#define VectorPushBack(X, x) \
do { \
VectorReserve((X), (X).size + 1); \
(X).ptr[(X).size++] = (x); \
} while (0)
/*
#] VectorPushBack :
#[ VectorPushBacks :
*/
/**
* Adds an n elements of src at the end of the vector X.
*
* @param X the vector object.
* @param src the starting address of the buffer storing elements to be added.
* @param n the number of elements to be added.
*/
#define VectorPushBacks(X, src, n) \
do { \
size_t v_tmp_n_ = (n); \
VectorReserve((X), (X).size + v_tmp_n_); \
memcpy((X).ptr + (X).size, (src), v_tmp_n_ * sizeof((X).ptr[0])); \
(X).size += v_tmp_n_; \
} while (0)
/*
#] VectorPushBacks :
#[ VectorPopBack :
*/
/**
* Removes the last element of the vector X.
* VectorSize(X) must be > 0.
*
* @param X the vector object.
*/
#define VectorPopBack(X) \
do { (X).size --; } while (0)
/*
#] VectorPopBack :
#[ VectorInsert :
*/
/**
* Inserts an element x at the specified index of the vector X.
* The index must be 0 <= index < VectorSize(X).
*
* @param X the vector object.
* @param index the position at which the element will be inserted.
* @param x the element to be inserted.
*/
#define VectorInsert(X, index, x) \
do { \
size_t v_tmp_index_ = (index); \
VectorReserve((X), (X).size + 1); \
memmove((X).ptr + v_tmp_index_ + 1, (X).ptr + v_tmp_index_, ((X).size - v_tmp_index_) * sizeof((X).ptr[0])); \
(X).ptr[v_tmp_index_] = (x); \
(X).size++; \
} while (0)
/*
#] VectorInsert :
#[ VectorInserts :
*/
/**
* Inserts an n elements of src at the specified index of the vector X.
* The index must be 0 <= index < VectorSize(X).
*
* @param X the vector object.
* @param index the position at which the elements will be inserted.
* @param src the starting address of the buffer storing elements to be inserted.
* @param n the number of elements to be inserted.
*/
#define VectorInserts(X, index, src, n) \
do { \
size_t v_tmp_index_ = (index), v_tmp_n_ = (n); \
VectorReserve((X), (X).size + v_tmp_n_); \
memmove((X).ptr + v_tmp_index_ + v_tmp_n_, (X).ptr + v_tmp_index_, ((X).size - v_tmp_index_) * sizeof((X).ptr[0])); \
memcpy((X).ptr + v_tmp_index_, (src), v_tmp_n_ * sizeof((X).ptr[0])); \
(X).size += v_tmp_n_; \
} while (0)
/*
#] VectorInserts :
#[ VectorErase :
*/
/**
* Removes an element at the specified index of the vector X.
* The index must be 0 <= index < VectorSize(X).
*
* @param X the vector object.
* @param index the position of the element to be removed.
*/
#define VectorErase(X, index) \
do { \
size_t v_tmp_index_ = (index); \
memmove((X).ptr + v_tmp_index_, (X).ptr + v_tmp_index_ + 1, ((X).size - v_tmp_index_ - 1) * sizeof((X).ptr[0])); \
(X).size--; \
} while (0)
/*
#] VectorErase :
#[ VectorErases :
*/
/**
* Removes an n elements at the specified index of the vector X.
* The index must be 0 <= index < VectorSize(X) - n + 1.
*
* @param X the vector object.
* @param index the starting position of the elements to be removed.
* @param n the number of elements to be removed.
*/
#define VectorErases(X, index, n) \
do { \
size_t v_tmp_index_ = (index), v_tmp_n_ = (n); \
memmove((X).ptr + v_tmp_index_, (X).ptr + v_tmp_index_ + v_tmp_n_, ((X).size - v_tmp_index_ - 1) * sizeof((X).ptr[0])); \
(X).size -= v_tmp_n_; \
} while (0)
/*
#] VectorErases :
#] Vector :
*/
#endif /* VECTOR_H_ */
|