ccCArray.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /****************************************************************************
  2. Copyright (c) 2007 Scott Lembcke
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "base/ccCArray.h"
  24. #include "base/ccTypes.h"
  25. NS_CC_BEGIN
  26. /** Allocates and initializes a new array with specified capacity */
  27. ccArray* ccArrayNew(ssize_t capacity)
  28. {
  29. if (capacity == 0)
  30. capacity = 7;
  31. ccArray *arr = (ccArray*)malloc( sizeof(ccArray) );
  32. arr->num = 0;
  33. arr->arr = (Ref**)calloc(capacity, sizeof(Ref*));
  34. arr->max = capacity;
  35. return arr;
  36. }
  37. /** Frees array after removing all remaining objects. Silently ignores nullptr arr. */
  38. void ccArrayFree(ccArray*& arr)
  39. {
  40. if( arr == nullptr )
  41. {
  42. return;
  43. }
  44. ccArrayRemoveAllObjects(arr);
  45. free(arr->arr);
  46. free(arr);
  47. arr = nullptr;
  48. }
  49. void ccArrayDoubleCapacity(ccArray *arr)
  50. {
  51. arr->max *= 2;
  52. Ref** newArr = (Ref**)realloc( arr->arr, arr->max * sizeof(Ref*) );
  53. // will fail when there's not enough memory
  54. CCASSERT(newArr != 0, "ccArrayDoubleCapacity failed. Not enough memory");
  55. arr->arr = newArr;
  56. }
  57. void ccArrayEnsureExtraCapacity(ccArray *arr, ssize_t extra)
  58. {
  59. while (arr->max < arr->num + extra)
  60. {
  61. CCLOGINFO("cocos2d: ccCArray: resizing ccArray capacity from [%d] to [%d].",
  62. static_cast<int>(arr->max),
  63. static_cast<int>(arr->max*2));
  64. ccArrayDoubleCapacity(arr);
  65. }
  66. }
  67. void ccArrayShrink(ccArray *arr)
  68. {
  69. ssize_t newSize = 0;
  70. //only resize when necessary
  71. if (arr->max > arr->num && !(arr->num==0 && arr->max==1))
  72. {
  73. if (arr->num!=0)
  74. {
  75. newSize=arr->num;
  76. arr->max=arr->num;
  77. }
  78. else
  79. {//minimum capacity of 1, with 0 elements the array would be free'd by realloc
  80. newSize=1;
  81. arr->max=1;
  82. }
  83. arr->arr = (Ref**)realloc(arr->arr,newSize * sizeof(Ref*) );
  84. CCASSERT(arr->arr!=nullptr,"could not reallocate the memory");
  85. }
  86. }
  87. /** Returns index of first occurrence of object, CC_INVALID_INDEX if object not found. */
  88. ssize_t ccArrayGetIndexOfObject(ccArray *arr, Ref* object)
  89. {
  90. const auto arrNum = arr->num;
  91. Ref** ptr = arr->arr;
  92. for (ssize_t i = 0; i < arrNum; ++i, ++ptr)
  93. {
  94. if (*ptr == object)
  95. return i;
  96. }
  97. return CC_INVALID_INDEX;
  98. }
  99. /** Returns a Boolean value that indicates whether object is present in array. */
  100. bool ccArrayContainsObject(ccArray *arr, Ref* object)
  101. {
  102. return ccArrayGetIndexOfObject(arr, object) != CC_INVALID_INDEX;
  103. }
  104. /** Appends an object. Behavior undefined if array doesn't have enough capacity. */
  105. void ccArrayAppendObject(ccArray *arr, Ref* object)
  106. {
  107. CCASSERT(object != nullptr, "Invalid parameter!");
  108. object->retain();
  109. arr->arr[arr->num] = object;
  110. arr->num++;
  111. }
  112. /** Appends an object. Capacity of arr is increased if needed. */
  113. void ccArrayAppendObjectWithResize(ccArray *arr, Ref* object)
  114. {
  115. ccArrayEnsureExtraCapacity(arr, 1);
  116. ccArrayAppendObject(arr, object);
  117. }
  118. /** Appends objects from plusArr to arr. Behavior undefined if arr doesn't have
  119. enough capacity. */
  120. void ccArrayAppendArray(ccArray *arr, ccArray *plusArr)
  121. {
  122. for (ssize_t i = 0; i < plusArr->num; i++)
  123. {
  124. ccArrayAppendObject(arr, plusArr->arr[i]);
  125. }
  126. }
  127. /** Appends objects from plusArr to arr. Capacity of arr is increased if needed. */
  128. void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr)
  129. {
  130. ccArrayEnsureExtraCapacity(arr, plusArr->num);
  131. ccArrayAppendArray(arr, plusArr);
  132. }
  133. /** Inserts an object at index */
  134. void ccArrayInsertObjectAtIndex(ccArray *arr, Ref* object, ssize_t index)
  135. {
  136. CCASSERT(index<=arr->num, "Invalid index. Out of bounds");
  137. CCASSERT(object != nullptr, "Invalid parameter!");
  138. ccArrayEnsureExtraCapacity(arr, 1);
  139. ssize_t remaining = arr->num - index;
  140. if (remaining > 0)
  141. {
  142. memmove((void *)&arr->arr[index+1], (void *)&arr->arr[index], sizeof(Ref*) * remaining );
  143. }
  144. object->retain();
  145. arr->arr[index] = object;
  146. arr->num++;
  147. }
  148. /** Swaps two objects */
  149. void ccArraySwapObjectsAtIndexes(ccArray *arr, ssize_t index1, ssize_t index2)
  150. {
  151. CCASSERT(index1>=0 && index1 < arr->num, "(1) Invalid index. Out of bounds");
  152. CCASSERT(index2>=0 && index2 < arr->num, "(2) Invalid index. Out of bounds");
  153. Ref* object1 = arr->arr[index1];
  154. arr->arr[index1] = arr->arr[index2];
  155. arr->arr[index2] = object1;
  156. }
  157. /** Removes all objects from arr */
  158. void ccArrayRemoveAllObjects(ccArray *arr)
  159. {
  160. while (arr->num > 0)
  161. {
  162. (arr->arr[--arr->num])->release();
  163. }
  164. }
  165. /** Removes object at specified index and pushes back all subsequent objects.
  166. Behavior undefined if index outside [0, num-1]. */
  167. void ccArrayRemoveObjectAtIndex(ccArray *arr, ssize_t index, bool releaseObj/* = true*/)
  168. {
  169. CCASSERT(arr && arr->num > 0 && index>=0 && index < arr->num, "Invalid index. Out of bounds");
  170. if (releaseObj)
  171. {
  172. CC_SAFE_RELEASE(arr->arr[index]);
  173. }
  174. arr->num--;
  175. ssize_t remaining = arr->num - index;
  176. if(remaining>0)
  177. {
  178. memmove((void *)&arr->arr[index], (void *)&arr->arr[index+1], remaining * sizeof(Ref*));
  179. }
  180. }
  181. /** Removes object at specified index and fills the gap with the last object,
  182. thereby avoiding the need to push back subsequent objects.
  183. Behavior undefined if index outside [0, num-1]. */
  184. void ccArrayFastRemoveObjectAtIndex(ccArray *arr, ssize_t index)
  185. {
  186. CC_SAFE_RELEASE(arr->arr[index]);
  187. auto last = --arr->num;
  188. arr->arr[index] = arr->arr[last];
  189. }
  190. void ccArrayFastRemoveObject(ccArray *arr, Ref* object)
  191. {
  192. auto index = ccArrayGetIndexOfObject(arr, object);
  193. if (index != CC_INVALID_INDEX)
  194. {
  195. ccArrayFastRemoveObjectAtIndex(arr, index);
  196. }
  197. }
  198. /** Searches for the first occurrence of object and removes it. If object is not
  199. found the function has no effect. */
  200. void ccArrayRemoveObject(ccArray *arr, Ref* object, bool releaseObj/* = true*/)
  201. {
  202. auto index = ccArrayGetIndexOfObject(arr, object);
  203. if (index != CC_INVALID_INDEX)
  204. {
  205. ccArrayRemoveObjectAtIndex(arr, index, releaseObj);
  206. }
  207. }
  208. /** Removes from arr all objects in minusArr. For each object in minusArr, the
  209. first matching instance in arr will be removed. */
  210. void ccArrayRemoveArray(ccArray *arr, ccArray *minusArr)
  211. {
  212. for (ssize_t i = 0; i < minusArr->num; i++)
  213. {
  214. ccArrayRemoveObject(arr, minusArr->arr[i]);
  215. }
  216. }
  217. /** Removes from arr all objects in minusArr. For each object in minusArr, all
  218. matching instances in arr will be removed. */
  219. void ccArrayFullRemoveArray(ccArray *arr, ccArray *minusArr)
  220. {
  221. ssize_t back = 0;
  222. for (ssize_t i = 0; i < arr->num; i++)
  223. {
  224. if (ccArrayContainsObject(minusArr, arr->arr[i]))
  225. {
  226. CC_SAFE_RELEASE(arr->arr[i]);
  227. back++;
  228. }
  229. else
  230. {
  231. arr->arr[i - back] = arr->arr[i];
  232. }
  233. }
  234. arr->num -= back;
  235. }
  236. //
  237. // // ccCArray for Values (c structures)
  238. /** Allocates and initializes a new C array with specified capacity */
  239. ccCArray* ccCArrayNew(ssize_t capacity)
  240. {
  241. if (capacity == 0)
  242. {
  243. capacity = 7;
  244. }
  245. ccCArray *arr = (ccCArray*)malloc(sizeof(ccCArray));
  246. arr->num = 0;
  247. arr->arr = (void**)malloc(capacity * sizeof(void*));
  248. arr->max = capacity;
  249. return arr;
  250. }
  251. /** Frees C array after removing all remaining values. Silently ignores nullptr arr. */
  252. void ccCArrayFree(ccCArray *arr)
  253. {
  254. if (arr == nullptr)
  255. {
  256. return;
  257. }
  258. ccCArrayRemoveAllValues(arr);
  259. free(arr->arr);
  260. free(arr);
  261. }
  262. /** Doubles C array capacity */
  263. void ccCArrayDoubleCapacity(ccCArray *arr)
  264. {
  265. ccArrayDoubleCapacity((ccArray*)arr);
  266. }
  267. /** Increases array capacity such that max >= num + extra. */
  268. void ccCArrayEnsureExtraCapacity(ccCArray *arr, ssize_t extra)
  269. {
  270. ccArrayEnsureExtraCapacity((ccArray*)arr,extra);
  271. }
  272. /** Returns index of first occurrence of value, CC_INVALID_INDEX if value not found. */
  273. ssize_t ccCArrayGetIndexOfValue(ccCArray *arr, void* value)
  274. {
  275. for(ssize_t i = 0; i < arr->num; i++)
  276. {
  277. if( arr->arr[i] == value )
  278. return i;
  279. }
  280. return CC_INVALID_INDEX;
  281. }
  282. /** Returns a Boolean value that indicates whether value is present in the C array. */
  283. bool ccCArrayContainsValue(ccCArray *arr, void* value)
  284. {
  285. return ccCArrayGetIndexOfValue(arr, value) != CC_INVALID_INDEX;
  286. }
  287. /** Inserts a value at a certain position. Behavior undefined if array doesn't have enough capacity */
  288. void ccCArrayInsertValueAtIndex( ccCArray *arr, void* value, ssize_t index)
  289. {
  290. CCASSERT( index < arr->max, "ccCArrayInsertValueAtIndex: invalid index");
  291. auto remaining = arr->num - index;
  292. // make sure it has enough capacity
  293. if (arr->num + 1 == arr->max)
  294. {
  295. ccCArrayDoubleCapacity(arr);
  296. }
  297. // last Value doesn't need to be moved
  298. if( remaining > 0) {
  299. // tex coordinates
  300. memmove((void *)&arr->arr[index+1], (void *)&arr->arr[index], sizeof(void*) * remaining );
  301. }
  302. arr->num++;
  303. arr->arr[index] = value;
  304. }
  305. /** Appends an value. Behavior undefined if array doesn't have enough capacity. */
  306. void ccCArrayAppendValue(ccCArray *arr, void* value)
  307. {
  308. arr->arr[arr->num] = value;
  309. arr->num++;
  310. // double the capacity for the next append action
  311. // if the num >= max
  312. if (arr->num >= arr->max)
  313. {
  314. ccCArrayDoubleCapacity(arr);
  315. }
  316. }
  317. /** Appends an value. Capacity of arr is increased if needed. */
  318. void ccCArrayAppendValueWithResize(ccCArray *arr, void* value)
  319. {
  320. ccCArrayEnsureExtraCapacity(arr, 1);
  321. ccCArrayAppendValue(arr, value);
  322. }
  323. /** Appends values from plusArr to arr. Behavior undefined if arr doesn't have
  324. enough capacity. */
  325. void ccCArrayAppendArray(ccCArray *arr, ccCArray *plusArr)
  326. {
  327. for( ssize_t i = 0; i < plusArr->num; i++)
  328. {
  329. ccCArrayAppendValue(arr, plusArr->arr[i]);
  330. }
  331. }
  332. /** Appends values from plusArr to arr. Capacity of arr is increased if needed. */
  333. void ccCArrayAppendArrayWithResize(ccCArray *arr, ccCArray *plusArr)
  334. {
  335. ccCArrayEnsureExtraCapacity(arr, plusArr->num);
  336. ccCArrayAppendArray(arr, plusArr);
  337. }
  338. /** Removes all values from arr */
  339. void ccCArrayRemoveAllValues(ccCArray *arr)
  340. {
  341. arr->num = 0;
  342. }
  343. /** Removes value at specified index and pushes back all subsequent values.
  344. Behavior undefined if index outside [0, num-1].
  345. @since v0.99.4
  346. */
  347. void ccCArrayRemoveValueAtIndex(ccCArray *arr, ssize_t index)
  348. {
  349. for( ssize_t last = --arr->num; index < last; index++)
  350. {
  351. arr->arr[index] = arr->arr[index + 1];
  352. }
  353. }
  354. /** Removes value at specified index and fills the gap with the last value,
  355. thereby avoiding the need to push back subsequent values.
  356. Behavior undefined if index outside [0, num-1].
  357. @since v0.99.4
  358. */
  359. void ccCArrayFastRemoveValueAtIndex(ccCArray *arr, ssize_t index)
  360. {
  361. ssize_t last = --arr->num;
  362. arr->arr[index] = arr->arr[last];
  363. }
  364. /** Searches for the first occurrence of value and removes it. If value is not found the function has no effect.
  365. @since v0.99.4
  366. */
  367. void ccCArrayRemoveValue(ccCArray *arr, void* value)
  368. {
  369. auto index = ccCArrayGetIndexOfValue(arr, value);
  370. if (index != CC_INVALID_INDEX)
  371. {
  372. ccCArrayRemoveValueAtIndex(arr, index);
  373. }
  374. }
  375. /** Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be removed.
  376. @since v0.99.4
  377. */
  378. void ccCArrayRemoveArray(ccCArray *arr, ccCArray *minusArr)
  379. {
  380. for(ssize_t i = 0; i < minusArr->num; i++)
  381. {
  382. ccCArrayRemoveValue(arr, minusArr->arr[i]);
  383. }
  384. }
  385. /** Removes from arr all values in minusArr. For each value in minusArr, all matching instances in arr will be removed.
  386. @since v0.99.4
  387. */
  388. void ccCArrayFullRemoveArray(ccCArray *arr, ccCArray *minusArr)
  389. {
  390. ssize_t back = 0;
  391. for(ssize_t i = 0; i < arr->num; i++)
  392. {
  393. if( ccCArrayContainsValue(minusArr, arr->arr[i]) )
  394. {
  395. back++;
  396. }
  397. else
  398. {
  399. arr->arr[i - back] = arr->arr[i];
  400. }
  401. }
  402. arr->num -= back;
  403. }
  404. NS_CC_END