#ifndef __SUIF_INDEXED_LIST_H__
#define __SUIF_INDEXED_LIST_H__
#include "suif_list.h"
template <class Domain,class Range> class indexed_list
{
public:
class pair
{
friend class indexed_list<Domain,Range>;
public:
Domain first;
Range second;
pair(const Domain &key,const Range &value) : first(key),second(value) {}
static void* constructorFunction(void* address) {
return new (address) pair;
}
protected:
pair() {}
};
typedef list<pair> pair_list;
typedef typename pair_list::iterator iterator;
typedef typename pair_list::const_iterator const_iterator;
//typedef const iterator const_iterator;
typedef pair value_type;
void push_back(const Domain &key,const Range &value)
{
the_list.push_back(pair(key,value));
}
pair &back() {return the_list.back();}
iterator insert(const iterator& pos, const pair& x)
{
return the_list.insert(pos,x);
}
void clear_list() {
the_list.clear_list();
}
iterator find(const Domain &key) {
iterator iter = the_list.begin();
while (iter != the_list.end())
{
if ((*iter).first == key)
break;
iter ++;
}
return iter;;
}
const_iterator find(const Domain &key) const {
const_iterator iter = the_list.begin();
while (iter != the_list.end())
{
if ((*iter).first == key)
break;
iter ++;
}
return iter;;
}
int num_with_key(const Domain &key) {
int count = 0;
iterator iter = the_list.begin();
while (iter != the_list.end())
{
if ((*iter).first == key)
count ++;
iter ++;
}
return count;
}
iterator find(const Domain &key,int no) {
iterator iter = the_list.begin();
while (iter != the_list.end())
{
if ((*iter).first == key)
{
no --;
if (no <= 0)
break;
}
iter ++;
}
return iter;;
}
const_iterator find(const Domain &key,int no) const {
const_iterator iter = the_list.begin();
while (iter != the_list.end())
{
if ((*iter).first == key)
{
no --;
if (no <= 0)
break;
}
iter ++;
}
return iter;;
}
bool is_member(const Domain &key) const {
return find(key) != the_list.end();
}
Range remove(iterator iter) {
Range x = (*iter).second;
the_list.erase(iter);
return x;
}
bool remove(Domain &key) {
iterator iter = find(key);
if (iter == the_list.end())
return false;
the_list.erase(iter);
return true;
}
iterator begin() { return the_list.begin();}
const_iterator begin() const { return the_list.begin();}
iterator end() { return the_list.end();}
const_iterator end() const { return the_list.end();}
unsigned length() const { return the_list.length(); }
unsigned size() const { return the_list.size(); }
pair & operator [](int i) const {
return the_list[i];
}
private:
pair_list the_list;
};
template <class Domain> class searchable_list : public list<Domain>
{
public:
typedef typename list<Domain>::iterator iterator;
/* Find an entry in the list */
iterator find(const Domain &key) {
iterator iter = begin();
while (iter != end())
{
if ((*iter) == key)
break;
iter ++;
}
return iter;;
}
const_iterator find(const Domain &key) const {
const_iterator iter = begin();
while (iter != end())
{
if ((*iter) == key)
break;
iter ++;
}
return iter;;
}
bool is_member(const Domain &key) const {
return find(key) != end();
}
bool remove(const Domain &key) {
iterator iter = find(key);
if (iter == end())
return false;
erase(iter);
return true;
}
};
#endif