LibMusicXML 3.22
bimap.h
1/*
2 MusicXML Library
3 Copyright (C) Grame 2006-2013
4
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
10 research@grame.fr
11*/
12
13#ifndef __bimap__
14#define __bimap__
15
16#include <map>
17#include <iterator>
18#include <utility>
19using namespace std;
20
27template <typename T1, typename T2>
28class bimap {
29 public:
30 bimap() {}
31 bimap(const T1 tbl1[], const T2 tbl2[], int n);
32 virtual ~bimap() {}
33
35 const T2 operator[] (const T1 key) { return fT1Map[key]; }
37 const T1 operator[] (const T2 key) { return fT2Map[key]; }
39 long size() { return fT1Map.size(); }
40
42 bimap& add (const T1& v1, const T2& v2)
43 { fT1Map[v1]=v2; fT2Map[v2]=v1; return *this; }
44
45 private:
46 map<T1, T2> fT1Map;
47 map<T2, T1> fT2Map;
48};
49
50template <typename T1, typename T2>
51bimap<T1, T2>::bimap(const T1 tbl1[], const T2 tbl2[], int n)
52{
53 for (int i=0; i<n; i++) {
54 add(tbl1[i], tbl2[i]);
55 }
56}
57
61template <typename T1, typename T2>
62class pairmap : public multimap<T1,T2> {
63 public:
64 pairmap() {}
65 virtual ~pairmap() {}
66
67 typedef typename pairmap<T1,T2>::iterator iterator;
68 typedef typename pairmap<T1,T2>::const_iterator const_iterator;
69
70 iterator insert (const pair<T1,T2>& x)
71 { return exist (x) ? this->end() : multimap<T1,T2>::insert(x); }
72 iterator insert (iterator position, const pair<T1,T2>& x)
73 { return exist (x) ? this->end() : multimap<T1,T2>::insert(position, x); }
74
75 private:
76 // the method is private so that pairmap and multimap remains fully interchangeable
77 bool exist (const pair<T1,T2>& x) const {
78 for (const_iterator i=find(x.first); (i!=this->end()) && (i->first==x.first); i++)
79 if (i->second == x.second) return true;
80 return false;
81 }
82
83};
84
85
86#endif
87
const T2 operator[](const T1 key)
returns the second type value indexed by the first type
Definition bimap.h:35
long size()
returns the map size
Definition bimap.h:39
bimap & add(const T1 &v1, const T2 &v2)
adds a pair of values
Definition bimap.h:42