原创作品,转载请标明:http://www.52php.cn/article/p-bsovodom-ep.html
CCArray是从cocos2d中移植过来的,类似于Apple的NSMutableArray,但是比NSMutableArray更为的好用。要注意的是虽然CCArray和CCDictionary可以管理cocos2d-x中绝大多数的类,但是仍然无法替代STL库,STL库更为强有力。
1.API
先看一下CCArray可以帮我们做什么。
1.1.创建
-
- staticCCArray*create();
-
- staticCCArray*create(CCObject*pObject,…);
-
- staticCCArray*createWithObject(CCObject*pObject);
-
- staticCCArray*createWithCapacity(unsignedintcapacity);
-
- staticCCArray*createWithArray(CCArray*otherArray);
1.2.添加
- voidaddObject(CCObject*object);
-
- voidaddObjectsFromArray(CCArray*otherArray);
-
- voidinsertObject(CCObject*object,unsignedintindex);
1.3.删除
- voidremoveLastObject(boolbReleaSEObj=true);
-
- voidremoveObject(CCObject*object,boolbReleaSEObj=true);
-
- voidremoveObjectAtIndex(unsignedintindex,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">
- voidremoveObjectsInArray(CCArray*otherArray);
-
- voidremoveAllObjects();
-
- voidfastRemoveObject(CCObject*object);
-
- voidfastRemoveObjectAtIndex(unsignedintindex);
1.4.操作元素
- unsignedintcount()const;
-
- unsignedintcapacity()const;
-
- unsignedintindexOfObject(CCObject*object)const;
-
- CCObject*objectAtIndex(unsignedintindex);
-
- CCObject*lastObject();
-
- CCObject*randomObject();
-
- boolcontainsObject(CCObject*object)const;
-
- boolisEqualToArray(CCArray*pOtherArray);
1.5.操作array内容
- voidexchangeObject(CCObject*object1,CCObject*object2);
-
- voidexchangeObjectAtIndex(unsignedintindex1,unsignedintindex2);
-
- voidreplaceObjectAtIndex(unsignedintuIndex,CCObject*pObject,boolbReleaSEObject=true);
-
- voidreverSEObjects();
-
- voidreduceMemoryFootprint();
2.remove和fastremove
从1.3可以看出删除有两种方式,普通删除和快速删除,它们有什么区别呢?
2.1.普通删除
- voidccArrayRemoveObjectAtIndex(ccArray*arr,unsignedintindex,boolbReleaSEObj)
- {
- CCAssert(arr&&arr->num>0&&index<arr->num,"Invalidindex.Outofbounds");
-
- if(bReleaSEObj)
- CC_SAFE_RELEASE(arr->arr[index]);
- }
-
- arr->num--;
-
- unsignedintremaining=arr->num-index;
- if(remaining>0)
-
- memmove((void*)&arr->arr[index],(void*)&arr->arr[index+1],remaining*sizeof(CCObject*));
- }
- }
2.2.快速删除
- voidccArrayFastRemoveObjectAtIndex(ccArray*arr,unsignedintindex)
-
- CC_SAFE_RELEASE(arr->arr[index]);
-
- unsignedintlast=--arr->num;
-
- arr->arr[index]=arr->arr[last];
- }
2.3.总结
如果有array={0,1,2,3,4,5},如果要删除3,使用普通删除得到的结果{0,5},使用快速删除得到的结果是{0,5,4}。可以看出快速删除的效率比普通删除效率高,就差在移动元素的时间复杂度上。
3.内存分配
3.1.容量和个数
CCArray中容量和个数并不是同一个概念。个数<=容量。从添加元素的源码中可以看到在添加之前会先进行空间分配,所以它是一个动态分配内存的过程。如下
voidccArrayEnsureExtraCapacity(ccArray*arr,unsignedintextra)
- {
- while(arr->max<arr->num+extra)
- ccArrayDoubleCapacity(arr);
- }
所以,每次CCArray在插入数据时检测到空间不足会增加一倍空间,再进行检测,直到空间满足分配为止。
3.2.判等
判断2个CCArray是否相等使用isEqualToArray(),判断相等的条件是CCArray中的每个元素相等即可,与CCArray的容量无关。
4.效率
比起NSMutableArray,CCArray效率能高出10%左右,原因有三:
(1)它使用的是C接口,所以它不有Objective-C消息开销。
(2)它假定你知道你在做什么,所以它不花时间在安全检查上(如边界溢出,空间需求等)。
(3)在比较上使用了指针而不是isEqual。
除了CCArray,我们还看到了ccCArray,CCArray基本上都是调用了ccCArray的函数,为什么要分为2种?
仔细看一下CCArray是继承于CCObject,所以CCArray是用于处理cocos2d-x对象的,内存管理上也有cocos2d-x的autorelease等诸多特性。而ccCArray可以直接操作标准的C数据结构和类型。
5.CCARRAY_FOREACH和CCARRAY_FOREACH_REVERSE
宏定义,用于正向遍历和反向遍历CCArray元素
#defineCCARRAY_FOREACH(__array__,__object__)
- if((__array__)&&(__array__)->data->num>0)
- for(CCObject**__arr__=(__array__)->data->arr,**__end__=(__array__)->data->arr+(__array__)->data->num-1;
- __arr__<=__end__&&(((__object__)=*__arr__)!=NULL);
- __arr__++)
-
- #defineCCARRAY_FOREACH_REVERSE(__array__,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> for(CCObject**__arr__=(__array__)->data->arr+(__array__)->data->num-1,**__end__=(__array__)->data->arr;
- __arr__>=__end__&&(((__object__)=*__arr__)!=NULL);
- __arr__--)
6.示例
CCArray的使用示例在http://www.52php.cn/article/p-wfbmoohn-ep.html此文中有比较典型的应用,这里就不再详述。
7.注意
一般来说,CCArray不会被add到其他类,所以它的引用计数是1,而且被设置为自动释放。所以创建CCArray对象时要记得调用retain,而且在析构的时候也要调用release来释放内存。真心想吐槽。。。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|