📄 array.hpp
字号:
//
// Array.hpp
//
// Copyright (C) 2006 Sophia Cradle Incorporated
#ifndef __SOPHIACRADLE_ARRAY_HPP
#define __SOPHIACRADLE_ARRAY_HPP
#include <SophiaFramework.hpp>
template <typename T>
class Array {
private:
SFXArray<T*> _array;
public:
explicit Array(Void);
explicit Array(const Array<T> &array);
explicit Array(UInt16 threshold, UInt16 cluster);
virtual ~Array(Void);
const Array<T> &operator=(const Array<T> &array);
SFCError Append(const T &element);
virtual Void Clear(Void);
const T &Get(SInt32 index) const;
SInt32 GetSize(Void) const;
Void Remove(SInt32 index);
SFCError Insert(SInt32 index, const T &element);
virtual SFCError Set(const Array<T> &array);
SFCError Set(SInt32 index, const T &element);
};
template <typename T>
inline Array<T>::Array(Void)
{
return;
}
template <typename T>
inline Array<T>::Array(const Array<T> &array)
{
Set(array);
return;
}
template <typename T>
inline Array<T>::~Array(Void)
{
Clear();
return;
}
template <typename T>
inline const Array<T> &Array<T>::operator=(const Array<T> &array)
{
Set(array);
return *this;
}
template <typename T>
inline SFCError Array<T>::Append(const T &element)
{
SFCError result(SFERR_NO_ERROR);
T* p;
if ((p = ::new T(element)) != null) {
if ((result = _array.Append(p)) != SFERR_NO_ERROR) {
::delete p;
}
}
else {
result = SFERR_NO_MEMORY;
}
return result;
}
template <typename T>
inline Void Array<T>::Clear(Void)
{
SInt32 i;
for (i = _array.GetSize() - 1; i >= 0; --i) {
::delete _array.Get(i);
}
_array.Clear();
return;
}
template <typename T>
inline const T &Array<T>::Get(SInt32 index) const
{
ASSERT(index >= 0 && index < GetSize());
return *(_array.Get(index));
}
template <typename T>
inline SInt32 Array<T>::GetSize(Void) const
{
return _array.GetSize();
}
template <typename T>
inline Void Array<T>::Remove(SInt32 index)
{
ASSERT(index >= 0 && index < GetSize());
::delete _array.Get(index);
_array.Remove(index);
return;
}
template <typename T>
inline SFCError Array<T>::Insert(SInt32 index, const T &element)
{
SFCError result(SFERR_NO_ERROR);
T* p;
ASSERT(index >= 0 && index <= GetSize());
if ((p = ::new T(element)) != null) {
if (index < GetSize()) {
result = _array.Insert(index, p);
}
else {
result = _array.Append(p);
}
if (result != SFERR_NO_ERROR) {
::delete p;
}
}
else {
result = SFERR_NO_MEMORY;
}
return result;
}
template <typename T>
inline SFCError Array<T>::Set(const Array<T> &array)
{
Clear();
SInt32Const size(array.GetSize());
SFCError result(SFERR_NO_ERROR);
SInt32 i;
for (i = 0; i < size; ++i) {
if ((result = Append(array.Get(i))) != SFERR_NO_ERROR) {
break;
}
}
return result;
}
template <typename T>
inline SFCError Array<T>::Set(SInt32 index, const T &element)
{
ASSERT(index >= 0 && index < GetSize());
*(_array.Get(index)) = element;
return SFERR_NO_ERROR;
}
#endif // __SOPHIACRADLE_ARRAY_HPP //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -