DynamicArray

1 DynamicArray设计要点

类模板

  • 动态确定内部数组空间的大小
  • 实现函数返回数组长度
  • 构造拷贝和赋值操作

2 继承关系图和接口实现

继承关系图
在这里插入图片描述

接口实现
在这里插入图片描述

template < typename T >
class DynamicArray : public Array<T>
{
protected:
    int m_length;

public:
    DynamicArray(int length);

    DynamicArray(const DynamicArray<T>& obj);
    DynamicArray<T>& operator= (const DynamicArray<T>& obj);

    void resize(int length);
    int length() const;

    ~DynamicArray();
};

3 代码实现

DynamicArray.h

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include "Array.h"
#include "Exception.h"

namespace LemonLib {
template < typename T >
class DynamicArray : public Array<T>
{
protected:
    int m_length;

public:
    DynamicArray(int length = 0)
    {
        this->m_array = new T[length];

        if (this->m_array)
        {
            m_length = length;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to create DynamicArray obj...");
        }
    }

    DynamicArray(const DynamicArray<T>& obj)
    {
        this->m_array = new T[obj.m_length];

        if (this->m_array)
        {
            for (int i=0; i<obj.m_length; i++)
            {
                this->m_array[i] = obj.m_array[i];
            }

            m_length = obj.m_length;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to copy DynamicArray obj...");
        }
    }

    DynamicArray<T>& operator= (const DynamicArray<T>& obj)
    {
        if (this != &obj)
        {
            T* array = new T[obj.m_length];

            if (array)
            {
                for (int i=0; i<obj.m_length; i++)
                {
                    array[i] = obj.m_array[i];
                }

                T* tmp = this->m_array;

                this->m_array = array;
                m_length = obj.m_length;

                delete[] tmp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to copy DynamicArray obj...");
            }
        }


        return *this;
    }

    void resize(int length)
    {
        if (length != m_length)
        {
            T* array = new T[length];

            if (array)
            {
                int len = (m_length < length) ? m_length : length;

                for (int i=0; i<len; i++)
                {
                    array[i] = this->m_array[i];
                }

                T* tmp = this->m_array;

                this->m_array = array;
                m_length = length;

                delete[] tmp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to resize DynamicArray obj...");
            }
        }
    }

    int length() const
    {
        return m_length;
    }

    ~DynamicArray()
    {
        delete[] this->m_array;
    }
};
}

#endif // DYNAMICARRAY_H

main.cpp

#include <iostream>
#include "Object.h"
#include "Exception.h"
#include "List.h"
#include "Seqlist.h"
#include "Staticlist.h"
#include "Dynamiclist.h"
#include "Staticarray.h"
#include "DynamicArray.h"

using namespace std;
using namespace LemonLib;

int main()
{
    DynamicArray<int> da(5);

    for (int i=0; i<da.length(); i++)
    {
        da[i] = i * i;
    }

    for (int i=0; i<da.length(); i++)
    {
        cout << da[i] << endl;
    }

    DynamicArray<int> da1 = da;

    for (int i=0; i<da1.length(); i++)
    {
        cout << da[i] << endl;
    }

    DynamicArray<int> da2(10);
    da2 = da1;

    for (int i=0; i<da2.length(); i++)
    {
        cout << da2[i] << endl;
    }

    da2.resize(10);
    for (int i=0; i<da2.length(); i++)
    {
        cout << da2[i] << endl;
    }

    da2.resize(3);
    for (int i=0; i<da2.length(); i++)
    {
        cout << da2[i] << endl;
    }
    
    return 0;
}


4 代码优化

可以看出DynamicArray类中函数实现存在重复的逻辑,如何进行代码优化呢?

重复代码逻辑的抽象

  • init:对象构造时的初始化操作。
  • copy:在堆空间中申请新的内存,并执行拷贝操作。
  • update:将指定的堆空间作为内部存储数组使用。
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include "Array.h"
#include "Exception.h"

namespace LemonLib {
template < typename T >
class DynamicArray : public Array<T>
{
protected:
    int m_length;

    T* copy(T* array, int len, int newLen)
    {
        T* ret = new T[newLen];

        if (ret != NULL)
        {
            int size = (len < newLen) ? len : newLen;

            for (int i=0; i<size; i++)
            {
                ret[i] = array[i];
            }
        }

        return ret;
    }

    void update(T* array, int len)
    {
        if (array != NULL)
        {
            T* tmp = this->m_array;

            this->m_array = array;
            m_length = len;

            delete[] tmp;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to update DynamicArray obj...");
        }
    }

    void init(T* array, int len)
    {
        if (array != NULL)
        {
            this->m_array = array;
            m_length = len;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to init DynamicArray obj...");
        }
    }

public:
    DynamicArray(int length)
    {
        init(new T[length], length);
    }

    DynamicArray(const DynamicArray<T>& obj)
    {
        init(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
    }

    DynamicArray<T>& operator= (const DynamicArray<T>& obj)
    {
        if (this != &obj)
        {
            update(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
        }

        return *this;
    }

    void resize(int length)
    {
        if (length != m_length)
        {
            update(copy(this->m_array, m_length, length), length);
        }
    }

    int length() const
    {
        return m_length;
    }

    ~DynamicArray()
    {
        delete[] this->m_array;
    }
};
}

#endif // DYNAMICARRAY_H

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值