ccCArray.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /**
  24. @file
  25. based on Chipmunk cpArray.
  26. ccArray is a faster alternative to NSMutableArray, it does pretty much the
  27. same thing (stores NSObjects and retains/releases them appropriately). It's
  28. faster because:
  29. - it uses a plain C interface so it doesn't incur Objective-c messaging overhead
  30. - it assumes you know what you're doing, so it doesn't spend time on safety checks
  31. (index out of bounds, required capacity etc.)
  32. - comparisons are done using pointer equality instead of isEqual
  33. There are 2 kind of functions:
  34. - ccArray functions that manipulates objective-c objects (retain and release are performed)
  35. - ccCArray functions that manipulates values like if they were standard C structures (no retain/release is performed)
  36. */
  37. #ifndef CC_ARRAY_H
  38. #define CC_ARRAY_H
  39. /// @cond DO_NOT_SHOW
  40. #include "base/ccMacros.h"
  41. #include "base/CCRef.h"
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <limits.h>
  45. NS_CC_BEGIN
  46. // Easy integration
  47. #define CCARRAYDATA_FOREACH(__array__, __object__) \
  48. __object__=__array__->arr[0]; for(ssize_t i=0, num=__array__->num; i<num; i++, __object__=__array__->arr[i]) \
  49. typedef struct _ccArray {
  50. ssize_t num, max;
  51. Ref** arr;
  52. } ccArray;
  53. /** Allocates and initializes a new array with specified capacity */
  54. ccArray* ccArrayNew(ssize_t capacity);
  55. /** Frees array after removing all remaining objects. Silently ignores nil arr. */
  56. void ccArrayFree(ccArray*& arr);
  57. /** Doubles array capacity */
  58. void ccArrayDoubleCapacity(ccArray *arr);
  59. /** Increases array capacity such that max >= num + extra. */
  60. void ccArrayEnsureExtraCapacity(ccArray *arr, ssize_t extra);
  61. /** shrinks the array so the memory footprint corresponds with the number of items */
  62. void ccArrayShrink(ccArray *arr);
  63. /** Returns index of first occurrence of object, NSNotFound if object not found. */
  64. ssize_t ccArrayGetIndexOfObject(ccArray *arr, Ref* object);
  65. /** Returns a Boolean value that indicates whether object is present in array. */
  66. bool ccArrayContainsObject(ccArray *arr, Ref* object);
  67. /** Appends an object. Behavior undefined if array doesn't have enough capacity. */
  68. void ccArrayAppendObject(ccArray *arr, Ref* object);
  69. /** Appends an object. Capacity of arr is increased if needed. */
  70. void ccArrayAppendObjectWithResize(ccArray *arr, Ref* object);
  71. /** Appends objects from plusArr to arr.
  72. Behavior undefined if arr doesn't have enough capacity. */
  73. void ccArrayAppendArray(ccArray *arr, ccArray *plusArr);
  74. /** Appends objects from plusArr to arr. Capacity of arr is increased if needed. */
  75. void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr);
  76. /** Inserts an object at index */
  77. void ccArrayInsertObjectAtIndex(ccArray *arr, Ref* object, ssize_t index);
  78. /** Swaps two objects */
  79. void ccArraySwapObjectsAtIndexes(ccArray *arr, ssize_t index1, ssize_t index2);
  80. /** Removes all objects from arr */
  81. void ccArrayRemoveAllObjects(ccArray *arr);
  82. /** Removes object at specified index and pushes back all subsequent objects.
  83. Behavior undefined if index outside [0, num-1]. */
  84. void ccArrayRemoveObjectAtIndex(ccArray *arr, ssize_t index, bool releaseObj = true);
  85. /** Removes object at specified index and fills the gap with the last object,
  86. thereby avoiding the need to push back subsequent objects.
  87. Behavior undefined if index outside [0, num-1]. */
  88. void ccArrayFastRemoveObjectAtIndex(ccArray *arr, ssize_t index);
  89. void ccArrayFastRemoveObject(ccArray *arr, Ref* object);
  90. /** Searches for the first occurrence of object and removes it. If object is not
  91. found the function has no effect. */
  92. void ccArrayRemoveObject(ccArray *arr, Ref* object, bool releaseObj = true);
  93. /** Removes from arr all objects in minusArr. For each object in minusArr, the
  94. first matching instance in arr will be removed. */
  95. void ccArrayRemoveArray(ccArray *arr, ccArray *minusArr);
  96. /** Removes from arr all objects in minusArr. For each object in minusArr, all
  97. matching instances in arr will be removed. */
  98. void ccArrayFullRemoveArray(ccArray *arr, ccArray *minusArr);
  99. //
  100. // // ccCArray for Values (c structures)
  101. typedef struct _ccCArray {
  102. ssize_t num, max;
  103. void** arr;
  104. } ccCArray;
  105. /** Allocates and initializes a new C array with specified capacity */
  106. ccCArray* ccCArrayNew(ssize_t capacity);
  107. /** Frees C array after removing all remaining values. Silently ignores nil arr. */
  108. void ccCArrayFree(ccCArray *arr);
  109. /** Doubles C array capacity */
  110. void ccCArrayDoubleCapacity(ccCArray *arr);
  111. /** Increases array capacity such that max >= num + extra. */
  112. void ccCArrayEnsureExtraCapacity(ccCArray *arr, ssize_t extra);
  113. /** Returns index of first occurrence of value, NSNotFound if value not found. */
  114. ssize_t ccCArrayGetIndexOfValue(ccCArray *arr, void* value);
  115. /** Returns a Boolean value that indicates whether value is present in the C array. */
  116. bool ccCArrayContainsValue(ccCArray *arr, void* value);
  117. /** Inserts a value at a certain position. Behavior undefined if array doesn't have enough capacity */
  118. void ccCArrayInsertValueAtIndex( ccCArray *arr, void* value, ssize_t index);
  119. /** Appends an value. Behavior undefined if array doesn't have enough capacity. */
  120. void ccCArrayAppendValue(ccCArray *arr, void* value);
  121. /** Appends an value. Capacity of arr is increased if needed. */
  122. void ccCArrayAppendValueWithResize(ccCArray *arr, void* value);
  123. /** Appends values from plusArr to arr. Behavior undefined if arr doesn't have
  124. enough capacity. */
  125. void ccCArrayAppendArray(ccCArray *arr, ccCArray *plusArr);
  126. /** Appends values from plusArr to arr. Capacity of arr is increased if needed. */
  127. void ccCArrayAppendArrayWithResize(ccCArray *arr, ccCArray *plusArr);
  128. /** Removes all values from arr */
  129. void ccCArrayRemoveAllValues(ccCArray *arr);
  130. /** Removes value at specified index and pushes back all subsequent values.
  131. Behavior undefined if index outside [0, num-1].
  132. @since v0.99.4
  133. */
  134. void ccCArrayRemoveValueAtIndex(ccCArray *arr, ssize_t index);
  135. /** Removes value at specified index and fills the gap with the last value,
  136. thereby avoiding the need to push back subsequent values.
  137. Behavior undefined if index outside [0, num-1].
  138. @since v0.99.4
  139. */
  140. void ccCArrayFastRemoveValueAtIndex(ccCArray *arr, ssize_t index);
  141. /** Searches for the first occurrence of value and removes it. If value is not found the function has no effect.
  142. @since v0.99.4
  143. */
  144. void ccCArrayRemoveValue(ccCArray *arr, void* value);
  145. /** Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be removed.
  146. @since v0.99.4
  147. */
  148. void ccCArrayRemoveArray(ccCArray *arr, ccCArray *minusArr);
  149. /** Removes from arr all values in minusArr. For each value in minusArr, all matching instances in arr will be removed.
  150. @since v0.99.4
  151. */
  152. void ccCArrayFullRemoveArray(ccCArray *arr, ccCArray *minusArr);
  153. NS_CC_END
  154. /// @endcond
  155. #endif // CC_ARRAY_H