Cocos2d-x3.1Vector的使用

原创 2014年08月04日 17:25:57
//.h
class VectorUnitLayout : public Layout
{
public:
    CREATE_FUNC(VectorUnitLayout);
    virtual bool init();
    void constFunc(const Vector<Node*> &vec) const;
    
};
//.cpp
bool VectorUnitLayout::init()
{
    bool bRet = false;
    do{
        CC_BREAK_IF(!Layout::init());
        Vector<Node*> vec;
        CCASSERT(vec.empty(), "");
        CCASSERT(vec.capacity() == 0, "");//容器剩余容量
        CCASSERT(vec.size() == 0, "");
        CCASSERT(vec.max_size() > 0, "");
        
        auto node1 = Node::create();//引用计数为1
        node1->setTag(1);
        vec.pushBack(node1);//引用计数+1
        CCASSERT(node1->getReferenceCount() == 2, "");//得到引用计数
        
        auto node2 = Node::create();
        node2->setTag(2);
        vec.pushBack(node2);
        CCASSERT(vec.getIndex(node1) == 0, "");
        CCASSERT(vec.getIndex(node2) == 1, "");
        
        auto node3 = Node::create();
        node3->setTag(3);
        vec.insert(1, node3);
        CCASSERT(vec.at(0)->getTag() == 1, "");
        CCASSERT(vec.at(1)->getTag() == 3, "");
        CCASSERT(vec.at(2)->getTag() == 2, "");

        //Test copy constructor
        Vector<Node*> vec2(vec);
        CCASSERT(vec2.size() == vec.size(), "");
        ssize_t size = vec.size();
        for(ssize_t i = 0; i < size; i++)
        {
            CCASSERT(vec2.at(i) == vec.at(i), "");
            CCASSERT(vec.at(i)->getReferenceCount() == 3, "");
            CCASSERT(vec2.at(i)->getReferenceCount() == 3, "");
        }
        
        //Test copy assignment operator
        Vector<Node*> vec3;
        vec3 = vec2;
        CCASSERT(vec3.size() == vec2.size(), "");
        size = vec3.size();
        for(ssize_t i = 0; i < size; ++i)
        {
            CCASSERT(vec3.at(i) == vec2.at(i), "");
            CCASSERT(vec3.at(i)->getReferenceCount() == 4, "");
            CCASSERT(vec2.at(i)->getReferenceCount() == 4, "");
            CCASSERT(vec.at(i)->getReferenceCount() == 4, "");
        }
        
        //Test move constructor
        auto createVector = [this](){
            Vector<Node*> ret;
            for(int i = 0; i < 20; i++)
            {
                ret.pushBack(Node::create());
            }
            int j = 1000;
            for(auto& child : ret)
            {
                child->setTag(j++);
            }
            return ret;
        };
        
        Vector<Node*> vec4(createVector());
        for(const auto& child : vec4)
        {
            CC_UNUSED_PARAM(child);
            CCASSERT(child->getReferenceCount() == 2, "");
        }
        
        //Test init Vector<T> with capacity
        Vector<Node*> vec5(10);
        CCASSERT(vec5.capacity() == 10, "");
        vec.reserve(20);//储备
        CCASSERT(vec.capacity() == 20, "");
        
        CCASSERT(vec5.size() == 0, "");
        CCASSERT(vec5.empty(), "");
        
        auto toRemovedNode = Node::create();
        vec5.pushBack(toRemovedNode);
        CCASSERT(toRemovedNode->getReferenceCount() == 2, "");
        
        //Test move assignment operator
        vec5 = createVector();
        CCASSERT(toRemovedNode->getReferenceCount() == 1, "");
        CCASSERT(vec5.size() == 20, "size should be 20");
        for(const auto& child : vec5)
        {
            CC_UNUSED_PARAM(child);
            CCASSERT(child->getReferenceCount() == 2, "");
        }
        
        //Test Vector<T>::find
        CCASSERT(vec.find(node3) == (vec.begin() + 1), "");
        CCASSERT(std::find(std::begin(vec),std::end(vec),node2) == (vec.begin() + 2), "");
        CCASSERT(vec.front()->getTag() == 1, "");
        CCASSERT(vec.back()->getTag() == 2, "");
        CCASSERT(vec.getRandomObject(), "");//随机获取对象
        CCASSERT(!vec.contains(Node::create()), "");
        CCASSERT(vec.contains(node1), "");//包含
        CCASSERT(vec.contains(node2), "");//包含
        CCASSERT(vec.contains(node3), "");//包含
        CCASSERT(vec.equals(vec2), "");
        CCASSERT(vec.equals(vec3), "");
        
        //Insert
        vec5.insert(2, node1);
        CCASSERT(vec5.at(2)->getTag() == 1, "");
        CCASSERT(vec5.size() == 21, "");
        vec5.back()->setTag(100);
        vec5.popBack();
        CCASSERT(vec5.size() == 20, "");
        CCASSERT(vec5.back()->getTag() != 100, "");
        
        //Erase and clear
        Vector<Node*> vec6 = createVector();
        Vector<Node*> vec7 = vec6;
        
        CCASSERT(vec6.size() == 20, "");
        vec6.erase(vec6.begin()+1);
        CCASSERT(vec6.size() == 19, "");
        CCASSERT((*(vec6.begin() + 1))->getTag() == 1002, "");
        vec6.erase(vec6.begin() + 2, vec6.begin() + 10);
        CCASSERT(vec6.size() == 11, "");
        CCASSERT(vec6.at(0)->getTag() == 1000, "");
        CCASSERT(vec6.at(1)->getTag() == 1002, "");
        CCASSERT(vec6.at(2)->getTag() == 1011, "");
        CCASSERT(vec6.at(3)->getTag() == 1012, "");
        vec6.erase(3);
        CCASSERT(vec6.at(3)->getTag() == 1013, "");
        vec6.eraseObject(vec6.at(2));
        CCASSERT(vec6.at(2)->getTag() == 1013, "");
        vec6.clear();

        auto objA = Node::create();
        auto objB = Node::create();
        auto objC = Node::create();
        {
            Vector<Node*> array1;
            Vector<Node*> array2;
            
            array1.pushBack(objA);
            array1.pushBack(objA);
            array1.pushBack(objA);
            
            array2.pushBack(objA);//retain count is 5
            array2.pushBack(objB);//retain count is 2
            array2.pushBack(objC);//retain count is 2
            
            for(auto obj : array1)
            {
                array2.eraseObject(obj);
            }
            CCASSERT(objA->getReferenceCount() == 4, "");
        }
        CCASSERT(objA->getReferenceCount() == 1, "");
        
        {
            Vector<Node*> array1;
            array1.pushBack(objA); // retain count is 2
            array1.pushBack(objA); // retain count is 3
            array1.pushBack(objA); // retain count is 4
            CCASSERT(objA->getReferenceCount() == 4, "");
            array1.eraseObject(objA, true); // Remove all occurrences in the Vector.
            CCASSERT(objA->getReferenceCount() == 1, "");
            
            array1.pushBack(objA); // retain count is 2
            array1.pushBack(objA); // retain count is 3
            array1.pushBack(objA); // retain count is 4
            
            array1.eraseObject(objA, false);
            CCASSERT(objA->getReferenceCount() == 3, ""); // Only remove the first occurrence in the Vector.
        }
        
        //Check the retain count in vec7
        CCASSERT(vec7.size() == 20, "");
        for(const auto &child : vec7)
        {
            CC_UNUSED_PARAM(child);
            CCASSERT(child->getReferenceCount() == 2, "");
        }
        
        //sort
        Vector<Node*> vecForSort = createVector();
        std::sort(vecForSort.begin(), vecForSort.end(),[](Node* a, Node* b){
            return a->getTag() >= b->getTag();
        });
        
        for(int i = 0; i < 20; i++)
        {
            CCASSERT(vecForSort.at(i)->getTag()-1000 == (19-i), "");
        }
        
        //Reverse
        vecForSort.reverse();
        for(int i =0; i < 20; i++)
        {
            CCASSERT(vecForSort.at(i)->getTag() - 1000 == i, "");
        }
        
        //Swap
        Vector<Node*> vecForSwap = createVector();
        vecForSwap.swap(2, 4);
        CCASSERT(vecForSwap.at(2)->getTag() == 1004, "");
        CCASSERT(vecForSwap.at(4)->getTag() == 1002, "");
        vecForSwap.swap(vecForSwap.at(2), vecForSwap.at(4));
        CCASSERT(vecForSwap.at(2)->getTag() == 1002, "");
        CCASSERT(vecForSwap.at(4)->getTag() == 1004, "");
        
        //shrinkToFit缩小以适应
        Vector<Node*> vecForShrink = createVector();
        vecForShrink.reserve(100);//储备100个空间
        CCASSERT(vecForShrink.capacity() == 100, "");
        vecForShrink.pushBack(Node::create());
        vecForShrink.shrinkToFit();
        CCASSERT(vecForShrink.capacity() == 21, "");
        
        //get random object
        //set the seed by time
        
        srand((unsigned)time(nullptr));
        Vector<Node*> vecForRandom = createVector();
        log("<------begin------>");
        for(int i = 0; i < vecForRandom.size(); i++)
        {
            log("Vector: random object tag = %d",vecForRandom.getRandomObject()->getTag());
        }
        log("<-------end-------->");
        
        //self assignment自动赋值
        Vector<Node*> vecSelfAssign = createVector();
        vecSelfAssign = vecSelfAssign;
        CCASSERT(vecSelfAssign.size() == 20, "");
        for(const auto &child : vecSelfAssign)
        {
            CC_UNUSED_PARAM(child);
            CCASSERT(child->getReferenceCount() == 2, "");
        }
        
        vecSelfAssign = std::move(vecSelfAssign);
        CCASSERT(vecSelfAssign.size() == 20, "");
        
        for (const auto& child : vecSelfAssign)
        {
            CC_UNUSED_PARAM(child);
            CCASSERT(child->getReferenceCount() == 2, "");
        }
        
        //const at
        Vector<Node*> vecConstAt = createVector();
        constFunc(vecConstAt);
        
        bRet = true;
    }while(0);
    
    return bRet;
}

void VectorUnitLayout::constFunc(const Vector<cocos2d::Node *> &vec) const
{
    log("vec[8] = %d",vec.at(8)->getTag());
}

相关文章推荐

cocos2d-x-3.1 数据结构之Vector (coco2d-x 学习笔记六)

v3.0 beta加入

Cocos2d-x3.1中使用万普平台实现Android平台广告添加

本文使用万普广告平台往Cocos2d-x3.1项目中添加广告。万普的SDK for Cocos2d-x功能挺全的,只是官方文档的使用说明中使用的Cocos2d-x引擎是2.0.1,平台是Windows...

Cocos2d-x3.1自定义字体库的使用

通常为了使游戏界面更为美观,我们会选用某种字体来达到效果。今天要介绍的便是如何在iOS中引入ttf字体库文件(Cocos2d-x 原理相同) 在iOS中使用字体库的步骤如下(Android无需下...

使用XCode6.0.1将Cocos2d-x3.1工程打包ipa(支持iOS8)

据说iOS8需要使用XCode6.1打包,因为还未升级,暂时研究了几天,先把笔记整理下,以备以后使用方便,由于本人没有iphone,没有苹果企业证书,所有的东西都是东拼西凑的,所以打包成的ipa没有真...

Cocos2d-x3.1中ClippingNode的使用实例

1、首先看源码

Cocos2d-x3.1中调度器的使用

继承关系 原理介绍 Cocos2d-x调度器为游戏提供定时事件和定时调用服务。所有Node对象都知道如何调度和取消调度事件,使用调度器有几个好处: 每当Node不再可见或已从...

Cocos2d-x3.1中ListView的使用

工程中添加UIListViewTest类,定义.h和.cpp文件。 //.h #ifndef __ListViewTest__UIListViewTest__ #define __ListVie...

Cocos2d-x3.1FileUtilsTest使用

Cocos2d-x3.1中FileUtils的使用:本使用教程是基于HelloWorld的,只需在HelloWorld的init()函数中添加如下代码 //头文件 #include "platfor...

Cocos2d-x3.1SpriteBatchNode使用

bool NewSpriteBatch::init() { auto bRet = false; do{ CC_BREAK_IF(!Layer::init()...

Cocos2d-x3.1中EditBox使用

EditBox用来完成登陆框的设计,但是一个坏处是bu源码如下:
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Cocos2d-x3.1Vector的使用
举报原因:
原因补充:

(最多只允许输入30个字)