#ifndef SIMPLESET_H
#define SIMPLESET_H
#include "SingleList.h"

template<typename T>
class SimpleSetBase : protected SingleList<T> {
	const bool IsEmpty() const { return SingleList<T>::GetSize()==0; }
	int GetSize() const { return SingleList<T>::GetSize(); }
	const T &GetItem(int i) const { return SingleList<T>::operator[](i); }
};

template<typename T>
class SimpleSet : private SingleList<T> {
public:
	void Add(const T &x) {
		if (SingleList<T>::Find(x)==SingleList<T>::NotFound())
			SingleList<T>::Add(x);
	}
	bool Has(const T &x) const {
		return SingleList<T>::Find(x)!=SingleList<T>::NotFound();
	}
	void Remove(const T &x) {
		int pos = SingleList<T>::Find(x);
		if (pos!=SingleList<T>::NotFound())
			SingleList<T>::FastRemove(pos);
	}
	void Set(const T &x, bool in_the_set) {
		if (in_the_set) Add(x);
		else            Remove(x);
	}
	
	
};

template<typename TKey, typename TValue> 
struct SimpleMapItem {
	TKey key;
	TValue value;
	SimpleMapItem() {}
	SimpleMapItem(const TKey &k, const TValue &v) : key(k), value(v) {}
};

template<typename TKey, typename TValue>
class SimpleMap : private SingleList< SimpleMapItem<TKey,TValue> > {
	typedef SimpleMapItem<TKey,TValue> T;
	int Find(const TKey &key) const {
		for(int i=0;i<SingleList<T>::GetSize();i++)
			if (SingleList<T>::operator[](i).key==key) return i;
		return SingleList<T>::NotFound();
	}
public:
	void Set(const TKey &key, const TValue &value) {
		int pos = Find(key);
		if (pos==SingleList<T>::NotFound())
			SingleList<T>::Add(T(key,value));
		else
			SingleList<T>::operator[](pos).value=value;
	}
	bool Has(const TKey &key) const {
		return Find(key)!=SingleList<T>::NotFound();
	}
	void Remove(const TKey &key) {
		int pos = Find(key);
		if (pos!=SingleList<T>::NotFound())
			SingleList<T>::FastRemove(pos);
	}
	const TValue &Get(const TKey &key, const TValue &default_value) const {
		int pos = Find(key);
		if (pos==SingleList<T>::NotFound())
			return default_value;
		else
			return SingleList<T>::operator[](pos).value;
	}
	
	const bool IsEmpty() const { return SingleList<T>::GetSize()==0; }
	int GetSize() const { return SingleList<T>::GetSize(); }
	const T &GetItem(int i) const { return SingleList<T>::operator[](i); }
};

#endif

