Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Map.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 
00018 #ifndef MAPCLASS_H
00019 #define MAPCLASS_H
00020 
00021 #include <vector>
00022 #include <map>
00023 #include <list>
00024 using namespace std;
00025 
00027 
00032 template <class Word, 
00033           class Definition,
00034           class WordList = vector<Word>,
00035           class DefList  = vector<Definition>
00036          >
00037 class Map : virtual public map<Word, Definition>
00038 {
00039    public:
00041       typedef map<Word, Definition>::value_type Pair;
00042 
00044 
00045 
00046       virtual Definition Get(const Word& word) const;
00047    
00049       virtual WordList GetWords() const;
00050 
00052       virtual DefList GetDefs() const;
00054 
00056 
00057 
00058       virtual void Add(const Word& word, const Definition& definition);
00059 
00060    /* I'm waiting for gcc to accept this
00061       virtual void Add(const WordList& wordList, const DefList& defList);
00062    */
00064       virtual void Add(const Pair& newPair);
00065 
00067       virtual void Set(const Word& word, const Definition& definition);
00068 
00069 /* I'm waiting for gcc to accept this
00070       virtual void Set(const DefList& defList);
00071    */
00073 
00075       bool Find(const Word& word) const;
00076 
00078       virtual ~Map() {};
00079 };
00080 
00081 template <class Word, class Definition, class WordList, class DefList>
00082 inline Definition 
00083 
00084 Map<Word, Definition, WordList, DefList>::Get(const Word& word) const
00085 {
00086    const_iterator p;
00087    return  ((p = find(word)) != end()) ? p->second : Definition();
00088 }
00089 
00090 template <class Word, class Definition, class WordList, class DefList>
00091 inline void 
00092 
00093 Map<Word, Definition, WordList, DefList>::Add(const Word& word, const Definition& definition)
00094 {
00095    Find(word) ? Set(word, definition) : insert(end(), Pair(word, definition));
00096 }
00097 
00098 template <class Word, class Definition, class WordList, class DefList>
00099 inline void 
00100 
00101 Map<Word, Definition, WordList, DefList>::Add(const Pair& newPair)
00102 {
00103    Add(newPair.first, newPair.second);
00104 }
00105 
00106 /*
00107 template <class Word, class Definition, class WordList, class DefList>
00108 inline void 
00109 Map<Word, Definition, WordList, DefList>::
00110 Add(const WordList& wordList, const DefList& defList)
00111 {
00112    typename Map<Word, Definition, WordList, DefList>::WordList::const_iterator;
00113    WordList::const_iterator p = wordList.begin();
00114    DefList::const_iterator dp = defList.begin();
00115    for (p, dp; 
00116         p != wordList.end() && dp != defList.end(); 
00117         ++p, ++dp)
00118    {
00119       Add(*p, *dp);
00120    }
00121 }
00122 */
00123 
00124 template <class Word, class Definition, class WordList, class DefList>
00125 inline void
00126 
00127 Map<Word, Definition, WordList, DefList>::Set(const Word& word, const Definition& definition)
00128 {
00129    (*this)[word] = definition;
00130 }
00131 
00132 /*
00133 template <class Word, class Definition, class WordList,class DefList>
00134 inline void
00135 Map<Word, Definition, WordList, DefList>::
00136 Set(const DefList& defList)
00137 {
00138    Add(GetWords(), defList);
00139 }
00140 */
00141 
00142 template <class Word, class Definition,class WordList,class DefList>
00143 inline bool
00144 
00145 Map<Word, Definition, WordList, DefList>::Find(const Word& word) const
00146 {
00147    return (find(word) != end());
00148 }
00149 
00150 template <class Word, class Definition,class WordList,class DefList>
00151 inline WordList
00152 
00153 Map<Word, Definition, WordList, DefList>::GetWords() const
00154 { 
00155    WordList wl;
00156    for (const_iterator p = begin(); p != end(); ++p)
00157       wl.push_back(p->first);
00158    return wl; 
00159 }
00160 
00161 template <class Word, class Definition,class WordList,class DefList>
00162 inline DefList
00163 
00164 Map<Word, Definition, WordList, DefList>::GetDefs() const
00165 {
00166    DefList dl;
00167    for (const_iterator p = begin(); p != end(); ++p)
00168       dl.push_back(p->second);
00169    return dl; 
00170 }     
00171 
00172 #endif

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