感覺完成要花1200行
testmain檔
#include "ListPool.hpp"
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
ListPool<int> myPool;
ListPool<int>::iterator a;
int ary[5];
int i;
for (i=1;i<6;i++)
{
myPool.AddBack(i);
myPool.AddFront(i);
myPool.AddFront(i);
}
for (i=1,a=myPool.begin();i<6;i++)
{
a = myPool.Remove(a);
++a;
}
for (i=1;i<6;i++)
{
myPool.AddBack(i);
myPool.AddFront(i);
}
for (a = myPool.begin();a != myPool.end();a++)
{
cout << *a << " ";
}
system("pause");
return 0;
}
hpp檔
#ifndef ListPool_I69536f4dm12490dbe5c9mm677c_H
#define ListPool_I69536f4dm12490dbe5c9mm677c_H
#include <memory.h>
#include <xutility>
template <class T,int size = 1000> class ListPool;
template <class T>
struct element
{
T me;
element<T> *prev,*next;
};
template <class T>
struct _iterator:public std::iterator<std::forward_iterator_tag,T>
{
element<T> *lpme;
_iterator():lpme(NULL)
{};
_iterator(element<T> *data)
{
lpme = data;
};
_iterator& operator++()
{
lpme = lpme->next;
return *this;
};
_iterator& operator--()
{
lpme = lpme->prev;
return *this;
};
_iterator operator++(int)
{
_iterator result = *this;
lpme = lpme->next;
return result;
};
_iterator operator--(int)
{
_iterator result = *this;
lpme = lpme->prev;
return result;
};
T& operator*() const
{
return lpme->me;
};
T* operator&() const
{
return &lpme->me;
};
bool operator==(const _iterator<T> &rhs) const
{
return lpme == rhs.lpme;
};
bool operator!=(const _iterator<T> &rhs) const
{
return lpme != rhs.lpme;
};
void apotosis()
{
//me = NULL;
};
};
template <class T,int size>
class ListPool
{
public:
typedef _iterator<T> iterator;
ListPool():max(size),front(0),rear(0),queuesNum(0),nowSize(0),head(0)
{
datas = new element<T>[size];
queue = new element<T>*[size];
memset(datas,0,sizeof(element<T>)*size);
memset(queue,0,sizeof(element<T>*)*size);
last = GetNew();
first = datas;
};
ListPool(const ListPool<T> &oldObj)
{
max = oldObj.max;
front = oldObj.front;
rear = oldObj.rear;
queuesNum = oldObj.queuesNum;
nowSize = oldObj.nowSize;
first = oldObj.first;
last = oldObj.last;
head = oldObj.head;
datas = new element<T>[max];
queue = new element<T>*[max];
memcpy(datas,&(oldObj.datas),sizeof(element<T>)*max);
memcpy(queue,&(oldObj.queue),sizeof(element<T>*)*max);
};
~ListPool()
{
delete[] datas;
delete[] queue;
};
iterator begin()
{
return iterator(first);
};
iterator end()
{
return iterator(last);
};
bool AddBack( const T &data )
{
last->me = data;
last = GetNew();
if (last == NULL)
return false;
return true;
};
bool AddFront( const T &data )
{
if (queuesNum != 0)
{
queue[front]->me = data;
--queuesNum;
if (nowSize != 0)
{
first->prev = queue[front];
queue[front]->next = first;
}
first = queue[front];
++front;
++nowSize;
if (front >= max) //超過最後一個就到0
front = 0;
return true;
}
else if (nowSize < max)
{
if (head == max) return false; //超過max就加入失敗
datas[head].me = data;
if (nowSize != 0)
{
first->prev = &datas[head];
datas[head].next = first;
}
first = &datas[head];
++head;
++nowSize;
return true;
}
return false;
};
void Insert(iterator iter,const T &data)
{
element<T> *next = iter.lpme->next;
element<T> *newData = GetNew();
newData->me = data; //save data
iter.lpme->next = newData;
newData->prev = iter.lpme;
newData->next = next;
next->prev = newData;
};
void Clear()
{
head = front = front = queuesNum = nowSize = 0;
first = last = datas;
};
element<T> * GetNew() //from last get new element
{
element<T> * result = NULL;
if (queuesNum != 0)
{
result = queue[front];
--queuesNum;
if (nowSize != 0)
{
last->next = queue[front];
queue[front]->prev = last;
}
++front;
++nowSize;
if (front >= max) //超過最後一個就到0
front = 0;
}
else if (nowSize < max)
{
if (head == max) return result; //超過max就加入失敗
result = &datas[head];
if (nowSize != 0)
{
last->next = &datas[head];
datas[head].prev = last;
}
++head;
++nowSize;
}
return result;
};
T* GetBackNew()
{
T* result = NULL;
if (queuesNum != 0)
{
result = &queue[front].me;
--queuesNum;
if (nowSize != 0)
{
last->next = queue[front];
queue[front].prev = last;
}
last = &queue[front];
++front;
++nowSize;
if (front >= max) //超過最後一個就到0
front = 0;
}
else (nowSize < max)
{
if (head == max) return result; //超過max就加入失敗
result = &datas[head].me;
if (nowSize != 0)
{
last->next = &datas[head];
datas[head].prev = last;
}
last = &datas[head];
++head;
++nowSize;
}
return result;
};
T* GetFrontNew()
{
T* result = NULL;
if (queuesNum != 0)
{
result = &queue[front].me;
--queuesNum;
if (nowSize != 0)
{
first->prev = &queue[front];
queue[front].next = first;
}
first = &queue[front];
++front;
++nowSize;
if (front >= max) //超過最後一個就到0
front = 0;
}
else (nowSize < max)
{
if (head == max) return false; //超過max就加入失敗
result = &datas[head].me;
if (nowSize != 0)
{
first->prev = &queue[front];
queue[front].next = first;
}
first = &datas[head];
++head;
++nowSize;
}
return result;
}
void SetSize(int newSize)
{
element* newDatas = new element[newSize];
element** newQueue = new element*[newSize];
memset(newDatas, 0,sizeof(element)*newSize);
memset(newQueue, 0,sizeof(element*)*newSize);
element* iter = first;
for (int i =0;;i++)
{
newDatas[i] = *iter;
if (iter->next != NULL)
iter = iter->next;
else
{
head = i+1;
break;
}
}
front = front = queuesNum = 0;
first = newDatas;
last = iter;
}
iterator Remove(const iterator &iter) //return next iterator
{
if (iter == begin())
{
addDeleteElement(first);
first = first->next;
first->prev = NULL;
return begin();
}
else if (iter != --end())
{
element<T> *next = iter.lpme->next;
element<T> *prev = iter.lpme->prev;
if (next != NULL && prev != NULL)
{
prev->next = next;
next->prev = prev;
}
}
else if (iter == end())
{
return NULL;
}
iterator result = iter;
return ++result;
};
void Remove_if()
{
};
void Sort() //sort all element
{
};
void addDeleteElement(element<T> * unuseData)
{
queue[rear] = unuseData;
if (++rear >= max) //超過最後一個就到0
rear = 0;
++queuesNum;
};
element<T> * getUnuseElement()
{
element<T> * result = NULL;
if (queuesNum == 0) return NULL; //if none then return
--queuesNum;
result = queue[front];
++front;
++nowSize;
if (front >= max) //超過最後一個就到0
front = 0;
return result;
}
bool empty()
{
return nowSize == 0;
};
private:
int head,max,front,rear,queuesNum,nowSize;
element<T> *datas,*last,*first,*lastElement;
element<T> **queue;
};
#endif // ListPool_I69536f4dm12490dbe5c9mm677c_H
公告版位
星落的瞬間!放棄的後悔是永遠!
- Nov 08 Sun 2009 03:00
目前的物件池進度(差兩個自定義函式與n個stl相容函式)
close
全站熱搜
留言列表