Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CAVector, CAList, CADeque, CAMap (Data Container)


May 21, 2021 CrossApp



CAVector, CAList, CADeque, CAMap are used consistently in the same way as the STL provided by STL in C + , but when CAVector, CAList, CADeque, CAMap adds and removes elements, follow CrossApp's memory management principles, and object reference counts are used to add elements 1, when removed, Object's reference count -1.


Let's look at the different descriptions of them in C.
In the standard library, both container vector and list can be used to hold a set of data of the same type. A nd one thing that differs from arrays is that they support dynamic growth. But they still have a few differences

(1) Vector is a sequential table that represents a continuous piece of memory in which elements are stored sequentially, and lists are two-way connected tables that are not necessarily continuous in memory.

(2) When there is not enough numeric memory, vector reapplys for a piece of continuous memory large enough to copy the original data into the new memory;

(3) List can only access elements through pointers, random access elements are particularly inefficient, when the need for frequent random access elements, the use of vector is more appropriate.

(4) When inserting or deleting an element from vector, it is necessary to copy all elements that move to the right of the element to be inserted;

Let me take a look at the description of the deque pair

deque is a continuous storage space for bidirectional openings. Although it is continuous storage space, but this continuity is only superficial, in fact, its memory is dynamically allocated, it allocates a piece of dynamic storage on the heap, each piece of dynamic storage itself is continuous, deque's own mechanism to virtually connect this piece of storage.

It inserts an element for the first time and dynamically allocates 512 bytes of space by default, and when that 512 bytes of space is used up, it dynamically allocates its own additional 512 bytes of space and then virtually connects them together. D eque's design makes it a much more complex architecture, algorithm, and iterator design than vector. I ts performance loss ratio is several orders of magnitude difference. So, deque should be used with caution.

The map container in C++ provides a key-to-container, and the map allows only one key-to-value.

CrossApp follows similar design principles when designing them, so we use them in a similar way.

Our operations on data containers are generally: add, delete, check, traverse

Here we explain the common functions of CAVector, CAList, CADeque, and CAMap:

CAVector<CAObject*> ca_vector;
//增
ca_vector.insert(size_t index, CrossApp::CAObject *object);//插入一个元素到指定位置
ca_vector.pushBack(CrossApp::CAObject *object);//在list末尾插入一个元素
    
//删
ca_vector.erase(ca_vector.begin());//删除指定位置的元素
ca_vector.erase(ca_vector.begin(),ca_vector.end());//删除指定范围的元素
ca_vector.popBack();//删除最后一个元素
ca_vector.clear();//删除所有元素
     
//查
ca_vector.empty();//是否含有元素
ca_vector.size();//返回vector中的元素个数
ca_vector.end();//返回末尾的迭代器
ca_vector.begin();//返回指向第一个元素的迭代器
ca_vector.front();//返回第一个元素
ca_vector.back();//返回最后一个元素
  
//遍历
for (int i = 0; i < ca_vector.size(); i++) {
    CAObject* obj = ca_vector.at(i);
}
CAList<CAObject*> ca_list;
//增
ca_list.insert(size_t index, CrossApp::CAObject *object);//插入一个元素到指定位置
ca_list.pushBack(CrossApp::CAObject *object);//在list末尾插入一个元素
ca_list.pushFront(CrossApp::CAObject *object);//在list头部添加一个元素
//删
ca_list.erase(ca_list.begin());//删除指定位置的元素
ca_list.erase(ca_list.begin(),ca_list.end());//删除指定范围的元素
ca_list.popBack();//删除最后一个元素
ca_list.popFront();//删除第一个元素
ca_list.clear();//删除所有元素
//查
ca_list.size();//返回list中的元素个数
ca_list.begin();//返回指向第一个元素的迭代器
ca_list.end();//返回末尾的迭代器
ca_list.front();//返回第一个元素
ca_list.back();//返回最后一个元素
//遍历
std::list<CAObject*>::iterator it;
for (it = ca_list.begin(); it != ca_list.end(); it++) {
    CAObject* obj = (CAObject*)*it;
}
CADeque<CAObject*> ca_deque;
//增
ca_deque.insert(size_t index, CrossApp::CAObject *object);//插入一个元素到指定位置
ca_deque.pushBack(CrossApp::CAObject *object);//在list末尾插入一个元素
ca_deque.pushFront(CrossApp::CAObject *object);//在list头部添加一个元素
     
//删
ca_deque.erase(size_t index);//删除指定位置的元素
ca_deque.erase(ca_deque.begin()+1, ca_deque.end()-2);//删除指定范围的元素
ca_deque.popBack();//删除最后一个元素
ca_deque.popFront();//删除第一个元素
ca_deque.clear();//删除所有元素
    
//查
ca_deque.size();//返回deque中的元素个数
ca_deque.begin();//返回指向第一个元素的迭代器
ca_deque.end();//返回末尾的迭代器
ca_deque.front();//返回第一个元素
ca_deque.back();//返回最后一个元素
ca_deque.at(size_t index);//返指定位置的元素
     
//遍历
for (int i = 0; i < ca_deque.size(); i++) {
    CAObject* obj = ca_deque.at(i);
}
CAMap<int, CAObject*> ca_map;
//增
ca_map.insert(int key, CrossApp::CAObject *object);//增加一个元素键值对
     
//删
ca_map.erase(int key);//通过key删除元素
ca_map.clear();//删除所有元素
   
//查
ca_map.empty();//判断mpa是否是空
ca_map.contains(int key);//是否有这个key返回bool
ca_map.getValue(int key);//根据key返回对应的Value
std::vector<int> vec = ca_map.getKeys();//返回包含所有key的vector
     
//遍历
std::map<int, CAObject*>::iterator it;
for (it = ca_map.begin(); it != ca_map.end(); ++it) {
    int key = it->first;
    CAObject* obj = it->second;
}
Let's do a memory management experiment with CAVector as an example to see if the reference count of CAObject has changed as elements are added and removed.
CAObject* obj = new CAObject();
     
CAVector<CAObject*> ca_vector;
     
//打印引用计数
CCLog("count:%d",obj->retainCount());
     
//把obj添加到vector尾部
ca_vector.pushBack(obj);
     
//打印引用计数
CCLog("count:%d",obj->retainCount());
     
//把obj移除
ca_vector.eraseObject(obj);
     
//打印引用计数
CCLog("count:%d",obj->retainCount());