simple_vector.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stddef.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "simple_vector.h"
  35. //initial vector capacity when fisrt item is added to vector
  36. #define INIT_SIZE 10
  37. //max vector capacity of a vector
  38. #define MAX_SIZE 10000
  39. //init vector to all zero
  40. void vector_init(simple_vector* v)
  41. {
  42. if (v)
  43. {
  44. v->size = 0;
  45. v->capacity = 0;
  46. v->data = NULL;
  47. }
  48. }
  49. //return current number of items the vector holds
  50. uint32_t vector_size(const simple_vector* v)
  51. {
  52. if (v)
  53. return v->size;
  54. else
  55. return 0;
  56. }
  57. //push a pointer to the end of the vector
  58. //return 0 if success, return 1 if memory malloc failure.
  59. errno_t vector_push_back(simple_vector* v, const void* data)
  60. {
  61. if (v)
  62. {
  63. if (v->capacity == 0) {
  64. //first item
  65. v->data = (void**)malloc(sizeof(void*) * INIT_SIZE);
  66. if (v->data ==NULL)
  67. return 1;
  68. v->capacity = INIT_SIZE;
  69. memset(v->data, '\0', sizeof(void*) * INIT_SIZE);
  70. }
  71. else if (v->size == v->capacity) {
  72. void** new_data;
  73. if( v->capacity >= MAX_SIZE - INIT_SIZE)
  74. return 1;
  75. //increate size by INIT_SIZE
  76. new_data = (void**)realloc(v->data, sizeof(void*) *( v->capacity + INIT_SIZE));
  77. if (new_data ==NULL)
  78. return 1;
  79. memset(&new_data[v->capacity], '\0', sizeof(void*) * INIT_SIZE);
  80. v->data = new_data;
  81. v->capacity += INIT_SIZE;
  82. }
  83. //assign new item
  84. v->data[v->size] = const_cast<void*>(data);
  85. v->size++;
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. //get the item pointer in the vector
  91. //return 0 if success, return 1 if index is out of range or data pointer is invalid.
  92. errno_t vector_get(const simple_vector* v, uint32_t index, void** data)
  93. {
  94. if (!v || index >= v->size || !data)
  95. return 1;
  96. *data = v->data[index];
  97. return 0;
  98. }
  99. //set the pointer in the vector
  100. //return 0 if success, return 1 if index is out of range.
  101. errno_t vector_set(simple_vector* v, uint32_t index, const void* data)
  102. {
  103. if (!v || index >= v->size)
  104. return 1;
  105. v->data[index] = const_cast<void*>(data);
  106. return 0;
  107. }
  108. //release memory used by the vector
  109. void vector_free(simple_vector* v)
  110. {
  111. if (v)
  112. {
  113. v->size = 0;
  114. v->capacity = 0;
  115. if(v->data)
  116. {
  117. free(v->data);
  118. v->data = NULL;
  119. }
  120. }
  121. }