Blame view

ios/cocos2d/cocos/base/ccCArray.h 8.02 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
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
  /****************************************************************************
  Copyright (c) 2007      Scott Lembcke
  Copyright (c) 2010-2012 cocos2d-x.org
  Copyright (c) 2013-2016 Chukong Technologies Inc.
  Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  
  http://www.cocos2d-x.org
  
  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.
  ****************************************************************************/
  
  /**
   @file
   based on Chipmunk cpArray.
   ccArray is a faster alternative to NSMutableArray, it does pretty much the
   same thing (stores NSObjects and retains/releases them appropriately). It's
   faster because:
   - it uses a plain C interface so it doesn't incur Objective-c messaging overhead
   - it assumes you know what you're doing, so it doesn't spend time on safety checks
   (index out of bounds, required capacity etc.)
   - comparisons are done using pointer equality instead of isEqual
  
   There are 2 kind of functions:
   - ccArray functions that manipulates objective-c objects (retain and release are performed)
   - ccCArray functions that manipulates values like if they were standard C structures (no retain/release is performed)
   */
  
  #ifndef CC_ARRAY_H
  #define CC_ARRAY_H
  /// @cond DO_NOT_SHOW
  
  #include "base/ccMacros.h"
  #include "base/CCRef.h"
  
  #include <stdlib.h>
  #include <string.h>
  #include <limits.h>
  
  NS_CC_BEGIN
  
  // Easy integration
  #define CCARRAYDATA_FOREACH(__array__, __object__)															\
  __object__=__array__->arr[0]; for(ssize_t i=0, num=__array__->num; i<num; i++, __object__=__array__->arr[i])	\
  
  
  typedef struct _ccArray {
  	ssize_t num, max;
  	Ref** arr;
  } ccArray;
  
  /** Allocates and initializes a new array with specified capacity */
  ccArray* ccArrayNew(ssize_t capacity);
  
  /** Frees array after removing all remaining objects. Silently ignores nil arr. */
  void ccArrayFree(ccArray*& arr);
  
  /** Doubles array capacity */
  void ccArrayDoubleCapacity(ccArray *arr);
  
  /** Increases array capacity such that max >= num + extra. */
  void ccArrayEnsureExtraCapacity(ccArray *arr, ssize_t extra);
  
  /** shrinks the array so the memory footprint corresponds with the number of items */
  void ccArrayShrink(ccArray *arr);
  
  /** Returns index of first occurrence of object, NSNotFound if object not found. */
  ssize_t ccArrayGetIndexOfObject(ccArray *arr, Ref* object);
  
  /** Returns a Boolean value that indicates whether object is present in array. */
  bool ccArrayContainsObject(ccArray *arr, Ref* object);
  
  /** Appends an object. Behavior undefined if array doesn't have enough capacity. */
  void ccArrayAppendObject(ccArray *arr, Ref* object);
  
  /** Appends an object. Capacity of arr is increased if needed. */
  void ccArrayAppendObjectWithResize(ccArray *arr, Ref* object);
  
  /** Appends objects from plusArr to arr. 
   Behavior undefined if arr doesn't have enough capacity. */
  void ccArrayAppendArray(ccArray *arr, ccArray *plusArr);
  
  /** Appends objects from plusArr to arr. Capacity of arr is increased if needed. */
  void ccArrayAppendArrayWithResize(ccArray *arr, ccArray *plusArr);
  
  /** Inserts an object at index */
  void ccArrayInsertObjectAtIndex(ccArray *arr, Ref* object, ssize_t index);
  
  /** Swaps two objects */
  void ccArraySwapObjectsAtIndexes(ccArray *arr, ssize_t index1, ssize_t index2);
  
  /** Removes all objects from arr */
  void ccArrayRemoveAllObjects(ccArray *arr);
  
  /** Removes object at specified index and pushes back all subsequent objects.
   Behavior undefined if index outside [0, num-1]. */
  void ccArrayRemoveObjectAtIndex(ccArray *arr, ssize_t index, bool releaseObj = true);
  
  /** Removes object at specified index and fills the gap with the last object,
   thereby avoiding the need to push back subsequent objects.
   Behavior undefined if index outside [0, num-1]. */
  void ccArrayFastRemoveObjectAtIndex(ccArray *arr, ssize_t index);
  
  void ccArrayFastRemoveObject(ccArray *arr, Ref* object);
  
  /** Searches for the first occurrence of object and removes it. If object is not
   found the function has no effect. */
  void ccArrayRemoveObject(ccArray *arr, Ref* object, bool releaseObj = true);
  
  /** Removes from arr all objects in minusArr. For each object in minusArr, the
   first matching instance in arr will be removed. */
  void ccArrayRemoveArray(ccArray *arr, ccArray *minusArr);
  
  /** Removes from arr all objects in minusArr. For each object in minusArr, all
   matching instances in arr will be removed. */
  void ccArrayFullRemoveArray(ccArray *arr, ccArray *minusArr);
  
  // 
  // // ccCArray for Values (c structures)
  
  typedef struct _ccCArray {
      ssize_t num, max;
      void** arr;
  } ccCArray;
  
  /** Allocates and initializes a new C array with specified capacity */
  ccCArray* ccCArrayNew(ssize_t capacity);
  
  /** Frees C array after removing all remaining values. Silently ignores nil arr. */
  void ccCArrayFree(ccCArray *arr);
  
  /** Doubles C array capacity */
  void ccCArrayDoubleCapacity(ccCArray *arr);
  
  /** Increases array capacity such that max >= num + extra. */
  void ccCArrayEnsureExtraCapacity(ccCArray *arr, ssize_t extra);
  
  /** Returns index of first occurrence of value, NSNotFound if value not found. */
  ssize_t ccCArrayGetIndexOfValue(ccCArray *arr, void* value);
  
  /** Returns a Boolean value that indicates whether value is present in the C array. */
  bool ccCArrayContainsValue(ccCArray *arr, void* value);
  
  /** Inserts a value at a certain position. Behavior undefined if array doesn't have enough capacity */
  void ccCArrayInsertValueAtIndex( ccCArray *arr, void* value, ssize_t index);
  
  /** Appends an value. Behavior undefined if array doesn't have enough capacity. */
  void ccCArrayAppendValue(ccCArray *arr, void* value);
  
  /** Appends an value. Capacity of arr is increased if needed. */
  void ccCArrayAppendValueWithResize(ccCArray *arr, void* value);
  
  /** Appends values from plusArr to arr. Behavior undefined if arr doesn't have
   enough capacity. */
  void ccCArrayAppendArray(ccCArray *arr, ccCArray *plusArr);
  
  /** Appends values from plusArr to arr. Capacity of arr is increased if needed. */
  void ccCArrayAppendArrayWithResize(ccCArray *arr, ccCArray *plusArr);
  
  /** Removes all values from arr */
  void ccCArrayRemoveAllValues(ccCArray *arr);
  
  /** Removes value at specified index and pushes back all subsequent values.
   Behavior undefined if index outside [0, num-1].
   @since v0.99.4
   */
  void ccCArrayRemoveValueAtIndex(ccCArray *arr, ssize_t index);
  
  /** Removes value at specified index and fills the gap with the last value,
   thereby avoiding the need to push back subsequent values.
   Behavior undefined if index outside [0, num-1].
   @since v0.99.4
   */
  void ccCArrayFastRemoveValueAtIndex(ccCArray *arr, ssize_t index);
  
  /** Searches for the first occurrence of value and removes it. If value is not found the function has no effect.
   @since v0.99.4
   */
  void ccCArrayRemoveValue(ccCArray *arr, void* value);
  
  /** Removes from arr all values in minusArr. For each Value in minusArr, the first matching instance in arr will be removed.
   @since v0.99.4
   */
  void ccCArrayRemoveArray(ccCArray *arr, ccCArray *minusArr);
  
  /** Removes from arr all values in minusArr. For each value in minusArr, all matching instances in arr will be removed.
   @since v0.99.4
   */
  void ccCArrayFullRemoveArray(ccCArray *arr, ccCArray *minusArr);
  
  NS_CC_END
  	
  /// @endcond
  #endif // CC_ARRAY_H