c++版Pair實現

寫在前面:小生純業餘選手,開此博僅僅是爲了積累,純當筆記來用。如有看官光臨小生博客,請不要相信我的代碼就是正確的。如果您發現了錯誤也懇請耽誤您一點時間,請您在下面指出來,不勝感激!


這裏的pair與SGI STL裏邊的pair一樣都是鍵值對,有的書上也叫“字典”(dictionary),不管這種結構叫什麼,它都是爲了更好地管理數據。在實現的時候遇到了各種各樣的麻煩,最後不得不搬出書上的代碼。這裏的實現要包含list代碼文件,鍵值對是作爲一個單元放在某一容器中,下面代碼選擇的是list容器。


#include "list.h"
using namespace std;

template<typename Key,typename E>
class Dictionary
{
private:
	void operator=(const Dictionary&){}
	Dictionary(const Dictionary&){}
public:
	Dictionary(){}
	~Dictionary(){}

	virtual void clear() = 0;
	virtual void insert(const Key& k,const E& e) = 0;
	virtual E remove(const Key& k) = 0;
	virtual E removeAny() = 0;
	virtual E find(const Key&)const = 0;
	virtual int size() = 0;
};

template<typename Key,typename E>
class KVpair
{
private:
	Key k;
	E e;
public:
	KVpair(){}
	KVpair(Key kval,E eval):k(kval),e(eval){}
	KVpair(const KVpair& o):k(o.k),e(o.e){}

	void operator =(const KVpair& o)
	{
		k = o.k;
		e = o.e;
	}

	Key key()
	{
		return k;
	}
	void setKey(Key ink)
	{
		k = ink;
	}

	E value()
	{
		return e;
	}
};

template<typename Key,typename E>
class UALdict : public Dictionary<Key,E>
{
private:
	AList<KVpair<Key,E> >* list;
public:
	UALdict(int size = 10)
	{
		list = new AList<KVpair<Key,E> >(size);
	}
	~UALdict()
	{
		delete list;
	}
	void clear()
	{
		list->clear();
	}
	void insert(const Key& k,const E& e)
	{
		KVpair<Key,E> temp(k,e);
		list->append(temp);
	}
	E remove(const Key &k)
	{
		E temp = find(k);
		if(temp != NULL)
			list->remove();
		return temp;
	}
	E removeAny()
	{
		assert(size() != 0);
		list->moveToEnd();
		list->prev();
		KVpair<Key,E> e = list->remove();
		return e.value();
	}
	E find(const Key& k)const
	{
		for(list->moveToStart();list->currPos() < list->length();list->next())
		{
			KVpair<Key,E> temp = list->getValue();
			if(k == temp.key())
				return temp.value();
		}
		return NULL;
	}
	int size()
	{
		return list->length();
	}
};

template<typename Key,typename E>
class SAList : protected AList<KVpair<Key,E> >
{
public:
	SAList(int size = 10):AList<KVpair<Key,E> >(size){}
	~SAList(){}
	void insert(KVpair<Key,E>& it)
	{
		KVpair<Key,E> curr;
		for(moveToStart();currPos() < length();next())
		{
			curr = getValue();
			if(curr.key()>it.key())
				break;
		}
		AList<KVpair<Key,E> >::insert(it);
	}

	AList<KVpair<Key,E> >::clear();
	AList<KVpair<Key,E> >::remove();
	AList<KVpair<Key,E> >::moveToStart();
	AList<KVpair<Key,E> >::moveToEnd();
	AList<KVpair<Key,E> >::prev();
	AList<KVpair<Key,E> >::next();
	AList<KVpair<Key,E> >::length();
	AList<KVpair<Key,E> >::currPos();
	AList<KVpair<Key,E> >::moveToPos();
	AList<KVpair<Key,E> >::getValue();
};

template<typename Key,typename E>
class SALdict : public Dictionary<Key,E>
{
private:
	SAList<Key,E>* list;
public:
	SALdict(int size = 10)
	{
		list = new SAList<Key,E>(size);
	}
	~SALdict(){}
	void clear()
	{
		list->clear();
	}
	void insert(const Key& k,const E& e)
	{
		KVpair<Key,E> temp(k,e);
		list->insert(temp);
	}
	E remove(const Key& k)
	{
		E temp = find(k);
		if(temp != NULL)
			list->remove();
		return temp;
	}
	E removeAny()
	{
		assert(size() != 0);
		list->moveToEnd();
		list->prev();
		KVpair<Key,E> e = list->remove();
		return e.value();
	}
	E find(const Key& k)const
	{
		int l = -1;
		int r = list->length();
		while (l+1 != r)
		{
			int i = (l+r)/2;
			list->moveToPos(i);
			KVpair<Key,E> temp = list->getValue();
			if(k < temp.key())
				r = i;
			if(k == temp.key())
				return temp.value();
			if(k > temp.key())
				l = i;
		}
		return NULL;
	}
	int size()
	{
		return list->length();
	}
};







發佈了25 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章