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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/*
The MIT License(MIT)
Copyright(c) 2016 Peter Goldsborough
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define __STDC_WANT_LIB_EXT1__ 1
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "third_party/vector/vector.h"
/***** PRIVATE *****/
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static bool _vector_should_grow(Vector *vector) {
assert(vector->size <= vector->capacity);
return vector->size == vector->capacity;
}
#if 0
static bool _vector_should_shrink(Vector *vector) {
assert(vector->size <= vector->capacity);
return vector->size == vector->capacity * VECTOR_SHRINK_THRESHOLD;
}
#endif // 0
static void *_vector_offset(Vector *vector, size_t index) {
// return vector->data + (index * vector->element_size);
return (unsigned char *)vector->data + (index * vector->element_size);
}
#if 0
static const void *_vector_const_offset(const Vector *vector, size_t index) {
// return vector->data + (index * vector->element_size);
return (unsigned char *)vector->data + (index * vector->element_size);
}
#endif // 0
static void _vector_assign(Vector *vector, size_t index, void *element) {
/* Insert the element */
void *offset = _vector_offset(vector, index);
memcpy(offset, element, vector->element_size);
}
#if 0
static int _vector_move_right(Vector *vector, size_t index) {
assert(vector->size < vector->capacity);
/* The location where to start to move from. */
void *offset = _vector_offset(vector, index);
/* How many to move to the right. */
size_t elements_in_bytes = (vector->size - index) * vector->element_size;
#ifdef __STDC_LIB_EXT1__
size_t right_capacity_in_bytes =
(vector->capacity - (index + 1)) * vector->element_size;
/* clang-format off */
int return_code = memmove_s(
offset + vector->element_size,
right_capacity_in_bytes,
offset,
elements_in_bytes);
/* clang-format on */
return return_code == 0 ? VECTOR_SUCCESS : VECTOR_ERROR;
#else
// memmove(offset + vector->element_size, offset, elements_in_bytes);
memmove((unsigned char *)offset + vector->element_size, offset,
elements_in_bytes);
return VECTOR_SUCCESS;
#endif
}
static void _vector_move_left(Vector *vector, size_t index) {
size_t right_elements_in_bytes;
void *offset;
/* The offset into the memory */
offset = _vector_offset(vector, index);
/* How many to move to the left */
right_elements_in_bytes = (vector->size - index - 1) * vector->element_size;
// memmove(offset, offset + vector->element_size, right_elements_in_bytes);
memmove(offset, (unsigned char *)offset + vector->element_size,
right_elements_in_bytes);
}
#endif // 0
static int _vector_reallocate(Vector *vector, size_t new_capacity) {
size_t new_capacity_in_bytes;
void *old;
assert(vector != NULL);
if (new_capacity < VECTOR_MINIMUM_CAPACITY) {
if (vector->capacity > VECTOR_MINIMUM_CAPACITY) {
new_capacity = VECTOR_MINIMUM_CAPACITY;
} else {
/* NO-OP */
return VECTOR_SUCCESS;
}
}
new_capacity_in_bytes = new_capacity * vector->element_size;
old = vector->data;
if ((vector->data = malloc(new_capacity_in_bytes)) == NULL) {
return VECTOR_ERROR;
}
#ifdef __STDC_LIB_EXT1__
/* clang-format off */
if (memcpy_s(vector->data,
new_capacity_in_bytes,
old,
aom_vector_byte_size(vector)) != 0) {
return VECTOR_ERROR;
}
/* clang-format on */
#else
memcpy(vector->data, old, aom_vector_byte_size(vector));
#endif
vector->capacity = new_capacity;
free(old);
return VECTOR_SUCCESS;
}
static int _vector_adjust_capacity(Vector *vector) {
return _vector_reallocate(vector,
MAX(1, vector->size * VECTOR_GROWTH_FACTOR));
}
#if 0
static void _vector_swap(size_t *first, size_t *second) {
size_t temp = *first;
*first = *second;
*second = temp;
}
#endif // 0
int aom_vector_setup(Vector *vector, size_t capacity, size_t element_size) {
assert(vector != NULL);
if (vector == NULL) return VECTOR_ERROR;
vector->size = 0;
vector->capacity = MAX(VECTOR_MINIMUM_CAPACITY, capacity);
vector->element_size = element_size;
vector->data = malloc(vector->capacity * element_size);
return vector->data == NULL ? VECTOR_ERROR : VECTOR_SUCCESS;
}
#if 0
int aom_vector_copy(Vector *destination, Vector *source) {
assert(destination != NULL);
assert(source != NULL);
assert(aom_vector_is_initialized(source));
assert(!aom_vector_is_initialized(destination));
if (destination == NULL) return VECTOR_ERROR;
if (source == NULL) return VECTOR_ERROR;
if (aom_vector_is_initialized(destination)) return VECTOR_ERROR;
if (!aom_vector_is_initialized(source)) return VECTOR_ERROR;
/* Copy ALL the data */
destination->size = source->size;
destination->capacity = source->size * 2;
destination->element_size = source->element_size;
/* Note that we are not necessarily allocating the same capacity */
destination->data = malloc(destination->capacity * source->element_size);
if (destination->data == NULL) return VECTOR_ERROR;
memcpy(destination->data, source->data, aom_vector_byte_size(source));
return VECTOR_SUCCESS;
}
int aom_vector_copy_assign(Vector *destination, Vector *source) {
assert(destination != NULL);
assert(source != NULL);
assert(aom_vector_is_initialized(source));
assert(aom_vector_is_initialized(destination));
if (destination == NULL) return VECTOR_ERROR;
if (source == NULL) return VECTOR_ERROR;
if (!aom_vector_is_initialized(destination)) return VECTOR_ERROR;
if (!aom_vector_is_initialized(source)) return VECTOR_ERROR;
aom_vector_destroy(destination);
return aom_vector_copy(destination, source);
}
int aom_vector_move(Vector *destination, Vector *source) {
assert(destination != NULL);
assert(source != NULL);
if (destination == NULL) return VECTOR_ERROR;
if (source == NULL) return VECTOR_ERROR;
*destination = *source;
source->data = NULL;
return VECTOR_SUCCESS;
}
int aom_vector_move_assign(Vector *destination, Vector *source) {
aom_vector_swap(destination, source);
return aom_vector_destroy(source);
}
int aom_vector_swap(Vector *destination, Vector *source) {
void *temp;
assert(destination != NULL);
assert(source != NULL);
assert(aom_vector_is_initialized(source));
assert(aom_vector_is_initialized(destination));
if (destination == NULL) return VECTOR_ERROR;
if (source == NULL) return VECTOR_ERROR;
if (!aom_vector_is_initialized(destination)) return VECTOR_ERROR;
if (!aom_vector_is_initialized(source)) return VECTOR_ERROR;
_vector_swap(&destination->size, &source->size);
_vector_swap(&destination->capacity, &source->capacity);
_vector_swap(&destination->element_size, &source->element_size);
temp = destination->data;
destination->data = source->data;
source->data = temp;
return VECTOR_SUCCESS;
}
#endif // 0
int aom_vector_destroy(Vector *vector) {
assert(vector != NULL);
if (vector == NULL) return VECTOR_ERROR;
free(vector->data);
vector->data = NULL;
return VECTOR_SUCCESS;
}
/* Insertion */
int aom_vector_push_back(Vector *vector, void *element) {
assert(vector != NULL);
assert(element != NULL);
if (_vector_should_grow(vector)) {
if (_vector_adjust_capacity(vector) == VECTOR_ERROR) {
return VECTOR_ERROR;
}
}
_vector_assign(vector, vector->size, element);
++vector->size;
return VECTOR_SUCCESS;
}
#if 0
int aom_vector_push_front(Vector *vector, void *element) {
return aom_vector_insert(vector, 0, element);
}
int aom_vector_insert(Vector *vector, size_t index, void *element) {
void *offset;
assert(vector != NULL);
assert(element != NULL);
assert(index <= vector->size);
if (vector == NULL) return VECTOR_ERROR;
if (element == NULL) return VECTOR_ERROR;
if (vector->element_size == 0) return VECTOR_ERROR;
if (index > vector->size) return VECTOR_ERROR;
if (_vector_should_grow(vector)) {
if (_vector_adjust_capacity(vector) == VECTOR_ERROR) {
return VECTOR_ERROR;
}
}
/* Move other elements to the right */
if (_vector_move_right(vector, index) == VECTOR_ERROR) {
return VECTOR_ERROR;
}
/* Insert the element */
offset = _vector_offset(vector, index);
memcpy(offset, element, vector->element_size);
++vector->size;
return VECTOR_SUCCESS;
}
int aom_vector_assign(Vector *vector, size_t index, void *element) {
assert(vector != NULL);
assert(element != NULL);
assert(index < vector->size);
if (vector == NULL) return VECTOR_ERROR;
if (element == NULL) return VECTOR_ERROR;
if (vector->element_size == 0) return VECTOR_ERROR;
if (index >= vector->size) return VECTOR_ERROR;
_vector_assign(vector, index, element);
return VECTOR_SUCCESS;
}
/* Deletion */
int aom_vector_pop_back(Vector *vector) {
assert(vector != NULL);
assert(vector->size > 0);
if (vector == NULL) return VECTOR_ERROR;
if (vector->element_size == 0) return VECTOR_ERROR;
--vector->size;
#ifndef VECTOR_NO_SHRINK
if (_vector_should_shrink(vector)) {
_vector_adjust_capacity(vector);
}
#endif
return VECTOR_SUCCESS;
}
int aom_vector_pop_front(Vector *vector) { return aom_vector_erase(vector, 0); }
int aom_vector_erase(Vector *vector, size_t index) {
assert(vector != NULL);
assert(index < vector->size);
if (vector == NULL) return VECTOR_ERROR;
if (vector->element_size == 0) return VECTOR_ERROR;
if (index >= vector->size) return VECTOR_ERROR;
/* Just overwrite */
_vector_move_left(vector, index);
#ifndef VECTOR_NO_SHRINK
if (--vector->size == vector->capacity / 4) {
_vector_adjust_capacity(vector);
}
#endif
return VECTOR_SUCCESS;
}
int aom_vector_clear(Vector *vector) { return aom_vector_resize(vector, 0); }
/* Lookup */
void *aom_vector_get(Vector *vector, size_t index) {
assert(vector != NULL);
assert(index < vector->size);
if (vector == NULL) return NULL;
if (vector->element_size == 0) return NULL;
if (index >= vector->size) return NULL;
return _vector_offset(vector, index);
}
const void *aom_vector_const_get(const Vector *vector, size_t index) {
assert(vector != NULL);
assert(index < vector->size);
if (vector == NULL) return NULL;
if (vector->element_size == 0) return NULL;
if (index >= vector->size) return NULL;
return _vector_const_offset(vector, index);
}
void *aom_vector_front(Vector *vector) { return aom_vector_get(vector, 0); }
void *aom_vector_back(Vector *vector) {
return aom_vector_get(vector, vector->size - 1);
}
#endif // 0
/* Information */
bool aom_vector_is_initialized(const Vector *vector) {
return vector->data != NULL;
}
size_t aom_vector_byte_size(const Vector *vector) {
return vector->size * vector->element_size;
}
#if 0
size_t aom_vector_free_space(const Vector *vector) {
return vector->capacity - vector->size;
}
bool aom_vector_is_empty(const Vector *vector) { return vector->size == 0; }
/* Memory management */
int aom_vector_resize(Vector *vector, size_t new_size) {
if (new_size <= vector->capacity * VECTOR_SHRINK_THRESHOLD) {
vector->size = new_size;
if (_vector_reallocate(vector, new_size * VECTOR_GROWTH_FACTOR) == -1) {
return VECTOR_ERROR;
}
} else if (new_size > vector->capacity) {
if (_vector_reallocate(vector, new_size * VECTOR_GROWTH_FACTOR) == -1) {
return VECTOR_ERROR;
}
}
vector->size = new_size;
return VECTOR_SUCCESS;
}
int aom_vector_reserve(Vector *vector, size_t minimum_capacity) {
if (minimum_capacity > vector->capacity) {
if (_vector_reallocate(vector, minimum_capacity) == VECTOR_ERROR) {
return VECTOR_ERROR;
}
}
return VECTOR_SUCCESS;
}
int aom_vector_shrink_to_fit(Vector *vector) {
return _vector_reallocate(vector, vector->size);
}
#endif // 0
/* Iterators */
Iterator aom_vector_begin(Vector *vector) { return aom_vector_iterator(vector, 0); }
#if 0
Iterator aom_vector_end(Vector *vector) {
return aom_vector_iterator(vector, vector->size);
}
#endif // 0
Iterator aom_vector_iterator(Vector *vector, size_t index) {
Iterator iterator = { NULL, 0 };
assert(vector != NULL);
assert(index <= vector->size);
if (vector == NULL) return iterator;
if (index > vector->size) return iterator;
if (vector->element_size == 0) return iterator;
iterator.pointer = _vector_offset(vector, index);
iterator.element_size = vector->element_size;
return iterator;
}
void *aom_iterator_get(Iterator *iterator) { return iterator->pointer; }
#if 0
int aom_iterator_erase(Vector *vector, Iterator *iterator) {
size_t index = aom_iterator_index(vector, iterator);
if (aom_vector_erase(vector, index) == VECTOR_ERROR) {
return VECTOR_ERROR;
}
*iterator = aom_vector_iterator(vector, index);
return VECTOR_SUCCESS;
}
#endif // 0
void aom_iterator_increment(Iterator *iterator) {
assert(iterator != NULL);
// iterator->pointer += iterator->element_size;
iterator->pointer =
(unsigned char *)iterator->pointer + iterator->element_size;
}
#if 0
void aom_iterator_decrement(Iterator *iterator) {
assert(iterator != NULL);
// iterator->pointer -= iterator->element_size;
iterator->pointer =
(unsigned char *)iterator->pointer - iterator->element_size;
}
void *aom_iterator_next(Iterator *iterator) {
void *current = iterator->pointer;
aom_iterator_increment(iterator);
return current;
}
void *aom_iterator_previous(Iterator *iterator) {
void *current = iterator->pointer;
aom_iterator_decrement(iterator);
return current;
}
bool aom_iterator_equals(Iterator *first, Iterator *second) {
assert(first->element_size == second->element_size);
return first->pointer == second->pointer;
}
bool aom_iterator_is_before(Iterator *first, Iterator *second) {
assert(first->element_size == second->element_size);
return first->pointer < second->pointer;
}
bool aom_iterator_is_after(Iterator *first, Iterator *second) {
assert(first->element_size == second->element_size);
return first->pointer > second->pointer;
}
size_t aom_iterator_index(Vector *vector, Iterator *iterator) {
assert(vector != NULL);
assert(iterator != NULL);
// return (iterator->pointer - vector->data) / vector->element_size;
return ((unsigned char *)iterator->pointer - (unsigned char *)vector->data) /
vector->element_size;
}
#endif // 0
|