Array.h
#ifndef __ARRAY_H
#define __ARRAY_H
#include "Object.h"
#include "Exception.h"
namespace DTLib
{
template <typename T>
class Array :public Object
{
protected:
T* m_array;
public:
virtual bool set(int i, const T& e)
{
bool ret = ((0 <= i) && (i < length()));
if (ret)
{
m_array[i] = e;
}
return ret;
}
virtual bool get(int i, T& e) const
{
bool ret = ((0 <= i) && (i < length()));
if (ret)
{
e = m_array[i];
}
return ret;
}
//数组访问操作符
T& operator[] (int i)
{
if ((0 <= i) && (i < length()))
{
return m_array[i];
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
}
T operator[] (int i) const
{
return (const_cast<Array<T>&>(*this))[i];
}
virtual int length() const = 0;
};
}
#endif // !__ARRAY_H
StaticArray.h
#ifndef __STATICARRAY_H
#define __STATICARRAY_H
#include "Array.h"
namespace DTLib {
template <typename T,int N>
class StaticArray :public Array<T>
{
protected:
T m_space[N];
public:
StaticArray()
{
this->m_array = m_space;
}
StaticArray(const StaticArray<T, N>& obj)
{
this->m_array = m_space;
for (int i = 0; i < N; i++)
{
m_space[i] = obj.m_space[i];
}
}
StaticArray<T, N>& operator= (const StaticArray<T, N>& obj)
{
if (this != &obj)
{
for (int i = 0; i < N; i++)
{
m_space[i] = obj.m_space[i];
}
}
else
{
return *this;
}
}
int length() const
{
return N;
}
};
}
#endif // ! __STATICARRAY_H
测试main.cpp
#include <iostream>
//#include "SmartPointer.h"
//#include"Exception.h"
//#include"Object.h"
//#include "StaticList.h"
//#include "DynamicList.h"
#include "StaticArray.h"
using namespace std;
using namespace DTLib;
int main()
{
StaticArray<int, 5>s1;
for (int i = 0; i < s1.length(); i++)
{
s1[i] = i * i;
}
for (int i = 0; i < s1.length(); i++)
{
cout << s1[i] << endl;
}
StaticArray<int,5>s2;
//赋值测试
s2 = s1;
for(int i = 0; i < s2.length(); i++)
{
cout << s2[i] << endl;
}
//异常测试
//s2[6] = 100;
return 0;
}
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include "Array.h"
#include "Exception.h"
namespace DTLib
{
template < typename T >
class DynamicArray : public Array<T>
{
protected:
int m_length;
T* copy(T* array, int len, int newLen) // O(min(len, newLen)) ==> O(n)
{
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 length) // O(1)
{
if (array != NULL)
{
T* temp = this->m_array;
this->m_array = array;
this->m_length = length;
delete[] temp;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to update DynamicArray object ...");
}
}
void init(T* array, int length) // O(1)
{
if (array != NULL)
{
this->m_array = array;
this->m_length = length;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create DynamicArray object ...");
}
}
public:
DynamicArray(int length = 0) // O(1)
{
init(new T[length], length);
}
DynamicArray(const DynamicArray<T>& obj) // O(n)
{
init(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
}
DynamicArray<T>& operator= (const DynamicArray<T>& obj) // O(n)
{
if (this != &obj)
{
update(copy(obj.m_array, obj.m_length, obj.m_length), obj.m_length);
}
return *this;
}
int length() const // O(1)
{
return m_length;
}
void resize(int length) // O(n)
{
if (length != m_length)
{
update(copy(this->m_array, m_length, length), length);
}
}
~DynamicArray() // O(1)
{
delete[] this->m_array;
}
};
}
#endif // DYNAMICARRAY_H