列表类和字典类增加赋值构造函数
This commit is contained in:
parent
812cb9c658
commit
fd2d5c7230
|
@ -10,6 +10,37 @@ IDictionary::IDictionary(IComparer comparer)
|
|||
_Keys.Comparer = comparer;
|
||||
}
|
||||
|
||||
IDictionary::IDictionary(const IDictionary& dic)
|
||||
{
|
||||
operator=(dic);
|
||||
}
|
||||
|
||||
IDictionary::IDictionary(IDictionary&& dic)
|
||||
{
|
||||
move(dic);
|
||||
}
|
||||
|
||||
IDictionary& IDictionary::operator=(const IDictionary& dic)
|
||||
{
|
||||
_Keys = dic._Keys;
|
||||
_Values = dic._Values;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
IDictionary& IDictionary::operator=(IDictionary&& dic)
|
||||
{
|
||||
move(dic);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void IDictionary::move(IDictionary& dic)
|
||||
{
|
||||
_Keys = dic._Keys;
|
||||
_Values = dic._Values;
|
||||
}
|
||||
|
||||
//int IDictionary::Count() const { return _Keys.Count(); }
|
||||
//const IList& IDictionary::Keys() const { return _Keys; }
|
||||
//const IList& IDictionary::Values() const { return _Values; }
|
||||
|
|
|
@ -9,6 +9,11 @@ class IDictionary
|
|||
typedef void* PValue;
|
||||
public:
|
||||
IDictionary(IComparer comparer = nullptr);
|
||||
IDictionary(const IDictionary& dic);
|
||||
IDictionary(IDictionary&& dic);
|
||||
|
||||
IDictionary& operator=(const IDictionary& dic);
|
||||
IDictionary& operator=(IDictionary&& dic);
|
||||
|
||||
inline int Count() const { return _Keys.Count(); }
|
||||
inline const IList& Keys() const { return _Keys; }
|
||||
|
@ -41,6 +46,8 @@ public:
|
|||
private:
|
||||
IList _Keys;
|
||||
IList _Values;
|
||||
|
||||
void move(IDictionary& dic);
|
||||
};
|
||||
|
||||
template<typename TKey, typename TValue>
|
||||
|
|
|
@ -11,34 +11,14 @@ IList::IList(const IList& list)
|
|||
{
|
||||
Init();
|
||||
|
||||
// 如果list的缓冲区是自己的,则拷贝过来
|
||||
// 如果不是自己的,则直接拿过来用
|
||||
if(list._Arr != list.Arr) CheckCapacity(list._Count);
|
||||
|
||||
_Count = list._Count;
|
||||
Comparer = list.Comparer;
|
||||
|
||||
Buffer(_Arr, _Count << 2) = list._Arr;
|
||||
operator=(list);
|
||||
}
|
||||
|
||||
IList::IList(IList&& list)
|
||||
{
|
||||
_Count = list._Count;
|
||||
_Capacity = list._Capacity;
|
||||
Comparer = list.Comparer;
|
||||
Init();
|
||||
|
||||
// 如果list的缓冲区是自己的,则拷贝过来
|
||||
// 如果不是自己的,则直接拿过来用
|
||||
if(list._Arr == list.Arr)
|
||||
{
|
||||
_Arr = Arr;
|
||||
Buffer(_Arr, _Count << 2) = list._Arr;
|
||||
}
|
||||
else
|
||||
{
|
||||
_Arr = list._Arr;
|
||||
list.Init();
|
||||
}
|
||||
move(list);
|
||||
}
|
||||
|
||||
IList::~IList()
|
||||
|
@ -55,6 +35,51 @@ void IList::Init()
|
|||
Comparer = nullptr;
|
||||
}
|
||||
|
||||
IList& IList::operator=(const IList& list)
|
||||
{
|
||||
if(this != &list)
|
||||
{
|
||||
_Count = list._Count;
|
||||
Comparer = list.Comparer;
|
||||
|
||||
if(list._Arr != list.Arr) CheckCapacity(list._Count);
|
||||
|
||||
Buffer(_Arr, _Count << 2) = list._Arr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
IList& IList::operator=(IList&& list)
|
||||
{
|
||||
if(this != &list) move(list);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void IList::move(IList& list)
|
||||
{
|
||||
_Count = list._Count;
|
||||
_Capacity = list._Capacity;
|
||||
Comparer = list.Comparer;
|
||||
|
||||
// 如果list的缓冲区是自己的,则拷贝过来
|
||||
// 如果不是自己的,则直接拿过来用
|
||||
if(list._Arr == list.Arr)
|
||||
{
|
||||
_Arr = Arr;
|
||||
Buffer(_Arr, _Count << 2) = list._Arr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果已有数据区,则释放
|
||||
if(_Arr && _Arr != Arr) delete _Arr;
|
||||
|
||||
_Arr = list._Arr;
|
||||
list.Init();
|
||||
}
|
||||
}
|
||||
|
||||
//int IList::Count() const { return _Count; }
|
||||
|
||||
// 添加单个元素
|
||||
|
|
|
@ -14,6 +14,9 @@ public:
|
|||
IList(IList&& list);
|
||||
virtual ~IList();
|
||||
|
||||
IList& operator=(const IList& list);
|
||||
IList& operator=(IList&& list);
|
||||
|
||||
inline int Count() const { return _Count; }
|
||||
|
||||
// 添加单个元素
|
||||
|
@ -53,6 +56,7 @@ private:
|
|||
|
||||
void Init();
|
||||
bool CheckCapacity(int count);
|
||||
void move(IList& list);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
|
Loading…
Reference in New Issue