uvectr64.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 1999-2015, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. ******************************************************************************
  8. */
  9. #include "uvectr64.h"
  10. #include "cmemory.h"
  11. #include "putilimp.h"
  12. U_NAMESPACE_BEGIN
  13. #define DEFAULT_CAPACITY 8
  14. /*
  15. * Constants for hinting whether a key is an integer
  16. * or a pointer. If a hint bit is zero, then the associated
  17. * token is assumed to be an integer. This is needed for iSeries
  18. */
  19. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector64)
  20. UVector64::UVector64(UErrorCode &status) :
  21. count(0),
  22. capacity(0),
  23. maxCapacity(0),
  24. elements(nullptr)
  25. {
  26. _init(DEFAULT_CAPACITY, status);
  27. }
  28. UVector64::UVector64(int32_t initialCapacity, UErrorCode &status) :
  29. count(0),
  30. capacity(0),
  31. maxCapacity(0),
  32. elements(nullptr)
  33. {
  34. _init(initialCapacity, status);
  35. }
  36. void UVector64::_init(int32_t initialCapacity, UErrorCode &status) {
  37. // Fix bogus initialCapacity values; avoid malloc(0)
  38. if (initialCapacity < 1) {
  39. initialCapacity = DEFAULT_CAPACITY;
  40. }
  41. if (maxCapacity>0 && maxCapacity<initialCapacity) {
  42. initialCapacity = maxCapacity;
  43. }
  44. if (initialCapacity > static_cast<int32_t>(INT32_MAX / sizeof(int64_t))) {
  45. initialCapacity = uprv_min(DEFAULT_CAPACITY, maxCapacity);
  46. }
  47. elements = static_cast<int64_t*>(uprv_malloc(sizeof(int64_t) * initialCapacity));
  48. if (elements == nullptr) {
  49. status = U_MEMORY_ALLOCATION_ERROR;
  50. } else {
  51. capacity = initialCapacity;
  52. }
  53. }
  54. UVector64::~UVector64() {
  55. uprv_free(elements);
  56. elements = nullptr;
  57. }
  58. /**
  59. * Assign this object to another (make this a copy of 'other').
  60. */
  61. void UVector64::assign(const UVector64& other, UErrorCode &ec) {
  62. if (ensureCapacity(other.count, ec)) {
  63. setSize(other.count);
  64. for (int32_t i=0; i<other.count; ++i) {
  65. elements[i] = other.elements[i];
  66. }
  67. }
  68. }
  69. bool UVector64::operator==(const UVector64& other) {
  70. int32_t i;
  71. if (count != other.count) return false;
  72. for (i=0; i<count; ++i) {
  73. if (elements[i] != other.elements[i]) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. void UVector64::setElementAt(int64_t elem, int32_t index) {
  80. if (0 <= index && index < count) {
  81. elements[index] = elem;
  82. }
  83. /* else index out of range */
  84. }
  85. void UVector64::insertElementAt(int64_t elem, int32_t index, UErrorCode &status) {
  86. // must have 0 <= index <= count
  87. if (0 <= index && index <= count && ensureCapacity(count + 1, status)) {
  88. for (int32_t i=count; i>index; --i) {
  89. elements[i] = elements[i-1];
  90. }
  91. elements[index] = elem;
  92. ++count;
  93. }
  94. /* else index out of range */
  95. }
  96. void UVector64::removeAllElements() {
  97. count = 0;
  98. }
  99. UBool UVector64::expandCapacity(int32_t minimumCapacity, UErrorCode &status) {
  100. if (U_FAILURE(status)) {
  101. return false;
  102. }
  103. if (minimumCapacity < 0) {
  104. status = U_ILLEGAL_ARGUMENT_ERROR;
  105. return false;
  106. }
  107. if (capacity >= minimumCapacity) {
  108. return true;
  109. }
  110. if (maxCapacity>0 && minimumCapacity>maxCapacity) {
  111. status = U_BUFFER_OVERFLOW_ERROR;
  112. return false;
  113. }
  114. if (capacity > (INT32_MAX - 1) / 2) { // integer overflow check
  115. status = U_ILLEGAL_ARGUMENT_ERROR;
  116. return false;
  117. }
  118. int32_t newCap = capacity * 2;
  119. if (newCap < minimumCapacity) {
  120. newCap = minimumCapacity;
  121. }
  122. if (maxCapacity > 0 && newCap > maxCapacity) {
  123. newCap = maxCapacity;
  124. }
  125. if (newCap > static_cast<int32_t>(INT32_MAX / sizeof(int64_t))) { // integer overflow check
  126. // We keep the original memory contents on bad minimumCapacity/maxCapacity.
  127. status = U_ILLEGAL_ARGUMENT_ERROR;
  128. return false;
  129. }
  130. int64_t* newElems = static_cast<int64_t*>(uprv_realloc(elements, sizeof(int64_t) * newCap));
  131. if (newElems == nullptr) {
  132. // We keep the original contents on the memory failure on realloc.
  133. status = U_MEMORY_ALLOCATION_ERROR;
  134. return false;
  135. }
  136. elements = newElems;
  137. capacity = newCap;
  138. return true;
  139. }
  140. void UVector64::setMaxCapacity(int32_t limit) {
  141. U_ASSERT(limit >= 0);
  142. if (limit < 0) {
  143. limit = 0;
  144. }
  145. if (limit > static_cast<int32_t>(INT32_MAX / sizeof(int64_t))) { // integer overflow check for realloc
  146. // Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
  147. return;
  148. }
  149. maxCapacity = limit;
  150. if (capacity <= maxCapacity || maxCapacity == 0) {
  151. // Current capacity is within the new limit.
  152. return;
  153. }
  154. // New maximum capacity is smaller than the current size.
  155. // Realloc the storage to the new, smaller size.
  156. int64_t* newElems = static_cast<int64_t*>(uprv_realloc(elements, sizeof(int64_t) * maxCapacity));
  157. if (newElems == nullptr) {
  158. // Realloc to smaller failed.
  159. // Just keep what we had. No need to call it a failure.
  160. return;
  161. }
  162. elements = newElems;
  163. capacity = maxCapacity;
  164. if (count > capacity) {
  165. count = capacity;
  166. }
  167. }
  168. /**
  169. * Change the size of this vector as follows: If newSize is smaller,
  170. * then truncate the array, possibly deleting held elements for i >=
  171. * newSize. If newSize is larger, grow the array, filling in new
  172. * slots with nullptr.
  173. */
  174. void UVector64::setSize(int32_t newSize) {
  175. int32_t i;
  176. if (newSize < 0) {
  177. return;
  178. }
  179. if (newSize > count) {
  180. UErrorCode ec = U_ZERO_ERROR;
  181. if (!ensureCapacity(newSize, ec)) {
  182. return;
  183. }
  184. for (i=count; i<newSize; ++i) {
  185. elements[i] = 0;
  186. }
  187. }
  188. count = newSize;
  189. }
  190. U_NAMESPACE_END