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
|
/*
* CVector.c
* CVector
*
* Created by Herbert J. Bernstein on 11/28/08.
* Copyright 2008 Herbert J. Bernstein. All rights reserved.
*
*/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CVector API UNDER THE TERMS OF THE LGPL *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef USE_LOCAL_HEADERS
#include "CVector.h"
#else
#include <CVector.h>
#endif
/* CVectorCreate -- create a generic vector */
int CVectorCreate(CVectorHandle CVECTOR_FAR * vectorhandle, const size_t elementsize, const size_t capacity) {
size_t cap = capacity;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
*vectorhandle = (CVectorHandle)CVECTOR_MALLOC(sizeof(CVector));
if (*vectorhandle==NULL) {
return CVECTOR_MALLOC_FAILED;
}
(*vectorhandle)->size = 0;
(*vectorhandle)->flags = 0;
(*vectorhandle)->capacity = 0;
(*vectorhandle)->elementsize = elementsize;
if (!cap) { cap = 10; }
(*vectorhandle)->array = (void CVECTOR_FAR *)CVECTOR_MALLOC(cap*elementsize);
if ((*vectorhandle)->array) {
(*vectorhandle)->capacity = cap;
return 0;
}
CVECTOR_FREE(*vectorhandle);
*vectorhandle = NULL;
return CVECTOR_MALLOC_FAILED;
}
/* CVectorAddElement -- add an element to a generic vector
equivalent to vector::push_back */
int CVectorAddElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element) {
size_t newcap;
int errorcode;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if ( (vectorhandle->flags&CVECTOR_FLAGS_NO_RESIZE) ) { return CVECTOR_NO_RESIZE; }
if (vectorhandle->size >= vectorhandle->capacity) {
newcap = vectorhandle->capacity*2;
if (newcap < 1) { newcap = 1; }
errorcode = CVectorSetCapacity (vectorhandle, newcap);
if (errorcode != 0) {
newcap = vectorhandle->capacity+(size_t)(vectorhandle->capacity>>2);
if (newcap < 1) { newcap = 1; }
errorcode = CVectorSetCapacity (vectorhandle, newcap);
if (errorcode != 0) { return errorcode; }
}
}
CVECTOR_MEMMOVE(((char CVECTOR_FAR *)(vectorhandle->array))+vectorhandle->size*vectorhandle->elementsize,
(const char CVECTOR_FAR *)element, vectorhandle->elementsize);
vectorhandle->size ++;
return 0;
}
/* CVectorGetElement -- get a copy of an element from a generic vector */
int CVectorGetElement(const CVectorHandle vectorhandle, void CVECTOR_FAR * element, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index < vectorhandle->size) {
CVECTOR_MEMMOVE((char *)element,((char *)(vectorhandle->array))+index*vectorhandle->elementsize,
vectorhandle->elementsize);
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorGetElementptr -- get a pointer to an element from a generic vector */
int CVectorGetElementptr(const CVectorHandle vectorhandle, void CVECTOR_FAR ** elementptr, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index < vectorhandle->size) {
*elementptr = (void CVECTOR_FAR*)(((char *)(vectorhandle->array))+index*vectorhandle->elementsize);
vectorhandle->flags |= CVECTOR_FLAGS_NO_RELOCATION;
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorSetElement -- set a copy of an element into a generic vector */
int CVectorSetElement(const CVectorHandle vectorhandle, const void CVECTOR_FAR * element, const size_t index) {
size_t newcap;
int errorcode;
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (index >= vectorhandle->capacity) {
newcap = index+vectorhandle->capacity+1;
errorcode = CVectorSetCapacity(vectorhandle, newcap);
if (errorcode != 0 ) {
newcap = index*1.2;
if (newcap < index+128) { newcap = index+128; }
errorcode = CVectorSetCapacity(vectorhandle, newcap);
if (errorcode != 0) { return errorcode; }
}
}
if (index < vectorhandle->capacity) {
CVECTOR_MEMMOVE(((char *)(vectorhandle->array))+index*vectorhandle->elementsize,(char *)element,
vectorhandle->elementsize);
if (index >= vectorhandle->size) { vectorhandle->size = index+1; }
return 0;
} else {
return CVECTOR_NOT_FOUND;
}
}
/* CVectorRemoveElement -- remove an element from a generic vector */
/* keeps elements 0 -- index-1, discards element index
moves elements index+1 through vectorhandle->size-1
into element index through vectorhandle->size-2
i.e. moves characters (index+1)*(vectorhandle->elementsize)
through (vectorhandle->size)*(vectorhandle->elementsize)-1
to index*(vectorhandle->elementsize)
*/
int CVectorRemoveElement(const CVectorHandle vectorhandle, const size_t index) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (vectorhandle->flags&CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if (index >= vectorhandle->size) { return CVECTOR_NOT_FOUND; }
if (index == vectorhandle->size-1) {
vectorhandle->size--;
return 0;
}
CVECTOR_MEMMOVE((char *)vectorhandle->array+index*(vectorhandle->elementsize),
(char *)vectorhandle->array+(index+1)*(vectorhandle->elementsize),
(vectorhandle->size-1-index)*(vectorhandle->elementsize));
vectorhandle->size--;
return 0;
}
/* CVectorClear -- clear a generic vector */
int CVectorClear(const CVectorHandle vectorhandle) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (vectorhandle->flags & CVECTOR_FLAGS_NO_RESIZE) { return CVECTOR_NO_RESIZE; }
vectorhandle->size = 0;
return 0;
}
/* CVectorFree -- remove a generic vector */
int CVectorFree(CVectorHandle CVECTOR_FAR * vectorhandle) {
if (vectorhandle==NULL) { return CVECTOR_BAD_ARGUMENT; }
if (*vectorhandle) {
if ((*vectorhandle)->flags & CVECTOR_FLAGS_NO_RESIZE) { return CVECTOR_NO_RESIZE; }
if ((*vectorhandle)->flags & CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if ((*vectorhandle)->array) {
CVECTOR_FREE((*vectorhandle)->array);
}
CVECTOR_FREE(*vectorhandle);
}
*vectorhandle = 0;
return 0;
}
/* CVectorGetCapacity - function to return the CVector capacity */
int CVectorGetCapacity(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * capacity) {
if ((vectorhandle==NULL)||!(capacity)) { return CVECTOR_BAD_ARGUMENT; }
*capacity = vectorhandle->capacity;
return 0;
}
/* CVectorGetSize - function to return the CVector size */
int CVectorGetSize(const CVectorHandle vectorhandle, size_t CVECTOR_FAR * size) {
if ((vectorhandle==NULL)||!(size)) { return CVECTOR_BAD_ARGUMENT; }
*size = vectorhandle->size;
return 0;
}
/* CVectorGetFlags - function to return the CVector flags */
int CVectorGetFlags(const CVectorHandle vectorhandle, unsigned int CVECTOR_FAR * flags) {
if ((vectorhandle==NULL)||!(flags)) { return CVECTOR_BAD_ARGUMENT; }
*flags = vectorhandle->flags;
return 0;
}
/* CVectorSetCapacity - function to set the CVector capacity */
int CVectorSetCapacity(const CVectorHandle vectorhandle, const size_t capacity) {
void CVECTOR_FAR * temparray;
if ((vectorhandle==NULL) || capacity < vectorhandle->size) { return CVECTOR_BAD_ARGUMENT; }
if (capacity == vectorhandle->capacity) { return 0; }
if (vectorhandle->flags&CVECTOR_FLAGS_NO_RELOCATION) { return CVECTOR_NO_RELOCATION; }
if (capacity) {
temparray = CVECTOR_MALLOC(capacity*vectorhandle->elementsize);
if (!temparray) { return CVECTOR_MALLOC_FAILED; }
if (vectorhandle->size) {
CVECTOR_MEMMOVE((char *)temparray, (char *)vectorhandle->array, vectorhandle->size*vectorhandle->elementsize);
}
CVECTOR_FREE(vectorhandle->array);
} else {
temparray = NULL;
}
vectorhandle->array = temparray;
vectorhandle->capacity = capacity;
return 0;
}
/* CVectorSetSize - function to set the CVector size */
int CVectorSetSize(const CVectorHandle vectorhandle, const size_t size) {
int errorcode;
if ((vectorhandle==NULL) ) { return CVECTOR_BAD_ARGUMENT; }
if (size == vectorhandle->size) { return 0; }
if ((vectorhandle->flags & CVECTOR_FLAGS_NO_RESIZE)) { return CVECTOR_NO_RESIZE; }
if ( size > vectorhandle->capacity ) {
if ((vectorhandle->flags & CVECTOR_FLAGS_NO_RELOCATION) ) { return CVECTOR_NO_RELOCATION; }
errorcode = CVectorSetCapacity(vectorhandle,size);
if (errorcode != 0) { return errorcode; }
}
if ( size <= vectorhandle->capacity ) {
if (size > vectorhandle->size) {
CVECTOR_MEMSET(((char *)vectorhandle->array)+(vectorhandle->size)*(vectorhandle->elementsize),
0, (vectorhandle->size-size)*(vectorhandle->elementsize));
}
vectorhandle->size = size;
}
return 0;
}
/* CVectorSetFags - function to set the CVector flags */
int CVectorSetFlags(const CVectorHandle vectorhandle, const unsigned int flags) {
if ((vectorhandle==NULL) ) { return CVECTOR_BAD_ARGUMENT; }
vectorhandle->flags = flags;
return 0;
}
#ifdef __cplusplus
}
#endif
|