现在的位置: 首页 > 综合 > 正文

vc ArrayList

2013年06月28日 ⁄ 综合 ⁄ 共 5278字 ⁄ 字号 评论关闭

我们在VC本地代码的时候有时候需要动态数组,普通的数组肯定是不行的。托管的ArrayList又是托管的。那本地的怎么办?无非就是自己写一个。

 

 

#ifndef CARRAYLIST_H_H
#define CARRAYLIST_H_H
#include <string>
using namespace std;

#define IncreateSize (20)

/**
*@autho Rocky
*/ //20 increase when you need more space
// Define a class to implement a array list.
template <typename Object>

class ArrayList
{
public:     // Construcotrs
    // Initialize the instance of the ArrayList class.
    ArrayList()
    {
        _list = NULL;
        _supplySpace=_size = 0;
    }
    // Initialize the instance of the ArrayList class with a special count.
    ArrayList(int count)
    {
        _supplySpace=_size = count;
        _list = InitArray(_supplySpace);
    }
public:     // Destructors
    // Uninitialize the instance of the ArrayList class.
    ~ArrayList()
    {
        if (_list != NULL)
            delete[] _list;
        _list = NULL;
        _size = 0;
        _supplySpace=0;
    }
public:     // Methods
    // Gets the size of the array.
    int Size()
    {
        return _size;
    }
    void ToTrimSize()
    {
        if(_size<=0)
        {
            delete []_list;
            _list=NULL;
            _supplySpace=_size=0;
        }
        else
        {
            if(_supplySpace==_size)//不需要精简
                return;

            Object *newPointer=InitArray(_size);
            memcpy(newPointer,_list,_size*sizeof(Object));//拷贝过来实际的数量值           
            delete []_list;
            ///最后的精简数据,刚刚合适
            _list=newPointer;
            _supplySpace=_size;
        }
    }
    // Sets the value at specail index.
    bool SetItem(int index, Object value)
    {
        // index out of range
        if (index < 0 || index >= _size)
            return false;
        // set value
        _list[index] = value;
        return true;
    }
    // Gets the value at special index.
    Object GetItem(int index)
    {
       /* if ( index < 0 || index >= _size )
        {
            Object o;
            return o;
        }*/
        return _list[index];
    }
    // add a item at the tail of array with a special value.
    void Add(Object value)
    {
        if (_list != NULL)
        {
            if(_size<_supplySpace)
            {
                _list[_size]=value;
                _size++;
            }
            else
            {    //空间不够,需要增加
                _supplySpace=_supplySpace+IncreateSize;//每次增加IncreateSize

                Object *tmpList = InitArray(_supplySpace);
                // copy the data from _list to temp array memory.
                memcpy(tmpList, _list, _size*sizeof(Object));
                // set special value at the end of array.
                tmpList[_size] = value;
                // clear old array memory.
                delete[] _list;
                // point _list to tmpList's memory.
                _list = tmpList;
                // modify the size of the current array.
                _size += 1;
            }          
        }
        else // array has not been created.
        {                       
            _supplySpace=_supplySpace+IncreateSize;//每次增加IncreateSize

            _list = InitArray(_supplySpace);
            // set value.
            _list[0] = value;
            // set array size.
            _size = 1;
        }
    }
    // Remove the item from special index.
    bool RemoveAt(int index)
    {
        return RemoveRange(index, 1);
    }
    // Insert a item at special index.
    bool InsertAt(int index, Object value)
    {
        bool succeed = true;
        // insert a empty item to array.
        succeed = InsertRange(index, 1);
        // if insert failed, return false.
        if (!succeed)
            return false;
        // set value
        _list[index] = value;
        return true;
    }
    // Insert a continue space into array at special index.
    bool InsertRange(int index, int count)
    {
        if (index < 0 || index > _size)
            return false;
       
        if (_list != NULL)
        {
            _supplySpace=_size + count;//当前的实际需求总量

            Object *tmpList = InitArray(__supplySpace);
            if (index == _size)
            {//仅仅需要拷贝一次
                // if index == _count, add the range to the tail of array
                memcpy(tmpList, _list, _size*sizeof(Object));
            }
            else
            {//需要多次拷贝
                // if index >= 0 || index < _count, insert the range to array
                memcpy(tmpList, _list, index*sizeof(Object));
                memcpy(tmpList + index + count,
                        _list + index,
                        (_size - index) * sizeof(Object));
            }
            // clear the memory of the old array.
            delete[] _list;
            // point the _list to the new memory of current array.
            _list = tmpList;
            // modify the array count.
            _size=_supplySpace;

        }
        else
        {
             _size =_supplySpace= count;
            _list = InitArray(_supplySpace);          
        }
        return true;

    }
    //
    //删除从Index开始的count数量删除
    //
    bool RemoveRange(int index, int count)
    {

        if (_list == 0 || index < 0 || index >= _size)
            return false;

        if(index+count>=_size)
            _supplySpace=index;
        else
            _supplySpace=_size - count;

         Object *tmpList=InitArray(_supplySpace);//分配需要的大小的控件

         memcpy(tmpList, _list, index*sizeof(Object));//把Index前面的所有全部保存起来

         if(index+count<_size)
             memcpy(tmpList+index,_list+index+count,(_size-index-count)*sizeof(Object));
       
         delete []_list;

         _list=tmpList;
         _size=_supplySpace;
         
         return true;

    }
private:   
    // Instance Data
    // define the pointer which point to memory of array.
    Object *_list;
    // save the size of array.
    int _size;            //实际大小
    int _supplySpace;    //实际申请的空间
private:   
    // Methods
    // create a new array which type is Object and initialize each item with 0.
    Object* InitArray(int count)
    {
        // craete new array
        Object *newarray = new Object[count];
        memset(newarray,0,sizeof(Object)*count);

        return newarray;
    }
};

#endif

抱歉!评论已关闭.