Main Page   Class Hierarchy   Compound List   File List   Compound Members  

FIFOMap.h

00001 // Copyright (C) 2001 Kevin Duffy <kevin@buhbird.com>
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License, or (at your option) any later version.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 #ifndef FIFOMAP_H
00018 #define FIFOMAP_H
00019 
00020 #include "Map.h"
00021 #include "Writeable.h"
00022 
00024 
00027 template <class Key>
00028 class KeyPair : virtual public Writeable
00029 {
00030    public:
00031       KeyPair(const size_t newOrder = 0, 
00032               const Key& newKey = NULL_STRING)
00033       {
00034          order = newOrder; key = newKey;
00035       };
00036 
00037       operator Key() const { return key; };
00038       bool operator< (const KeyPair& rhs) const 
00039       { 
00040          return (order < rhs.order);
00041       };
00042 
00043       string ToString() const { return (string)key; };
00044 
00045       void SetKey(const Key& newKey) { key = newKey; };
00046       Key GetKey() const { return key; };
00047       void SetOrder(const size_t newOrder) { order = newOrder;};
00048       size_t GetOrder() const { return order; };
00049 
00050       bool operator== (const KeyPair& rhs) const 
00051       { 
00052          return ((order == rhs.order) && (key == rhs.key)); 
00053       };
00054 
00055       bool operator!= (const KeyPair& rhs) const { return (*this != rhs); };
00056 
00057    private:
00058       size_t order;
00059       Key key;
00060 };
00061 
00063 
00070 template <class Key,
00071           class Value,
00072           class KeyList = vector<Key>,
00073           class ValueList  = vector<Value>
00074          >
00075 class FIFOMap : virtual public Map<KeyPair<Key>, Value, KeyList, ValueList>
00076 {
00077    public:
00079       typedef Map<KeyPair<Key>, Value, KeyList, ValueList> KeyMap;
00080       typedef vector<Key> KeyVector;
00081 
00083 
00086       //{@
00087       Value& FIFOMap::operator[] (const Key& k);
00088       pair<iterator, bool>  insert(const pair<Key, Value>& newPair); 
00089       size_t erase(const Key& k); 
00091 
00093 
00096       //{@   
00097       Value Get(const Key& key) const;
00098       KeyVector GetWordsAsKeys() const;
00099       void Add(const Key& k, const Value& v);  
00100       void Set(const Key& k, const Value& v);
00102 
00104       KeyPair<Key> FindKeyPair(const Key& k) const;
00105 };
00106 
00107 template <class Key, class Value, class KeyList, class ValueList>
00108 Value& 
00109 
00110 FIFOMap<Key, Value, KeyList, ValueList>::operator[] (const Key& k)
00111 {
00112    return (*((KeyMap::insert(Pair(FindKeyPair(k), Value()))).first)).second;
00113 }
00114 
00115 template <class Key, class Value, class KeyList, class ValueList>
00116 pair<FIFOMap<Key, Value, KeyList, ValueList>::iterator, bool>  
00117 FIFOMap<Key, Value, KeyList, ValueList>::
00118 insert(const pair<Key, Value>& newPair)
00119 {
00120    KeyPair<Key> k(size(), newPair.first);
00121    
00122    return KeyMap::insert(Pair(k, newPair.second));
00123 }
00124 
00125 template <class Key, class Value, class KeyList, class ValueList>
00126 Value 
00127 
00128 FIFOMap<Key, Value, KeyList, ValueList>::Get(const Key& key) const
00129 {
00130    return KeyMap::Get(FindKeyPair(key));
00131 }
00132    
00133 template <class Key, class Value, class KeyList, class ValueList>
00134 FIFOMap<Key, Value, KeyList, ValueList>::KeyVector 
00135 FIFOMap<Key, Value, KeyList, ValueList>::
00136 GetWordsAsKeys() const
00137 {
00138    KeyVector kv;
00139    for (const_iterator p = begin(); p != end(); ++p)
00140       kv.push_back(p->first.GetKey());
00141    
00142    return kv;
00143 }
00144 
00145 template <class Key, class Value, class KeyList, class ValueList>
00146 void 
00147 
00148 FIFOMap<Key, Value, KeyList, ValueList>::Add(const Key& k, const Value& v)
00149 {
00150    KeyMap::Add(KeyPair<Key>(size(), k), v);
00151 }
00152 
00153 template <class Key, class Value, class KeyList, class ValueList>
00154 void 
00155 
00156 FIFOMap<Key, Value, KeyList, ValueList>::Set(const Key& k, const Value& v)
00157 {
00158    KeyMap::Set(KeyPair<Key>(size(), k), v);
00159 }
00160 
00161 template <class Key, class Value, class KeyList, class ValueList>
00162 KeyPair<Key> 
00163 
00164 FIFOMap<Key, Value, KeyList, ValueList>::FindKeyPair(const Key& k) const
00165 {
00166    for (const_iterator p = begin(); p != end(); ++p)
00167    {
00168       if (p->first.GetKey() == k)
00169          return p->first;
00170    }
00171 
00172    return KeyPair<Key>(size(), k);
00173 };
00174 
00175 template <class Key, class Value, class KeyList, class ValueList>
00176 size_t 
00177 FIFOMap<Key, Value, KeyList, ValueList>::
00178 erase(const Key& k)
00179 {
00180    return KeyMap::erase(FindKeyPair(k));
00181 };
00182 
00183 #endif

Generated at Wed May 2 00:24:41 2001 for API by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001