Main Page | Modules | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | Related Pages

bimap.h

00001 /*
00002 
00003   MusicXML Library
00004   Copyright (C) 2003  Grame
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
00021   grame@grame.fr
00022 
00023 */
00024 
00025 #ifndef __bimap__
00026 #define __bimap__
00027 
00028 #include <map>
00029 #include <iterator>
00030 #include <utility>
00031 using namespace std;
00032 
00039 template <typename T1, typename T2> 
00040 class bimap {
00041     public:
00042         bimap() {}
00043         bimap(const T1 tbl1[], const T2 tbl2[], int n);
00044         virtual ~bimap() {}
00045         
00047         const T2 operator[] (const T1 key)      { return fT1Map[key]; }
00049         const T1 operator[] (const T2 key)      { return fT2Map[key]; }
00051         long size()     { return fT1Map.size(); }
00052 
00054         bimap& add (const T1& v1, const T2& v2) 
00055             { fT1Map[v1]=v2; fT2Map[v2]=v1; return *this; }
00056 
00057     private:
00058         map<T1, T2> fT1Map;
00059         map<T2, T1> fT2Map;
00060 };
00061 
00062 template <typename T1, typename T2>
00063 bimap<T1, T2>::bimap(const T1 tbl1[], const T2 tbl2[], int n)
00064 { 
00065     for (int i=0; i<n; i++) {
00066         add(tbl1[i], tbl2[i]);
00067     }
00068 }
00069 
00073 template <typename T1, typename T2> 
00074 class pairmap : public multimap<T1,T2> {
00075     public:
00076         pairmap() {}
00077         virtual ~pairmap() {}
00078 
00079 #ifdef __MWERKS__ // this is for Code Warrior version 8.3
00080         multimap<T1,T2>::iterator insert (const pair<T1,T2>& x)  
00081             { return exist (x) ? end() : multimap<T1,T2>::insert(x); }
00082 
00083         multimap<T1,T2>::iterator insert (multimap<T1,T2>::iterator position, const pair<T1,T2>& x)  
00084             { return exist (x) ? end() : multimap<T1,T2>::insert(position, x); }
00085 #else
00086         typedef typename pairmap<T1,T2>::iterator iterator;
00087         typedef typename pairmap<T1,T2>::const_iterator const_iterator;
00088 
00089         iterator insert (const pair<T1,T2>& x)  
00090             { return exist (x) ? end() : multimap<T1,T2>::insert(x); }
00091 
00092         iterator insert (iterator position, const pair<T1,T2>& x)  
00093             { return exist (x) ? end() : multimap<T1,T2>::insert(position, x); }
00094 #endif
00095 
00096     private:
00097         // the method is private so that pairmap and multimap remains fully interchangeable
00098         bool exist (const pair<T1,T2>& x) const { 
00099                 for (const_iterator i=find(x.first); (i!=end()) && (i->first==x.first); i++)
00100                     if (i->second == x.second) return true;
00101                 return false;
00102             }
00103 
00104 };
00105 
00106 
00107 #endif
00108 

Generated on Tue Mar 23 09:49:43 2004 for LibMusicXML by doxygen 1.3.3