写在前面:小生纯业余选手,开此博仅仅是为了积累,纯当笔记来用。如有看官光临小生博客,请不要相信我的代码就是正确的。如果您发现了错误也恳请耽误您一点时间,请您在下面指出来,不胜感激!
这里的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();
}
};