15 #include <boost/iterator/transform_iterator.hpp>
25 template<
typename type1,
typename type2>
34 std::map<type1, int> domain;
35 std::map<type2, int> codomain;
41 typedef std::pair<typename std::map<type1, int>::iterator,
42 typename std::map<type2, int>::iterator>
45 std::map<int, dir_entry> directory;
51 struct select1st :
public std::unary_function<T, typename T::first_type> {
52 typename T::first_type operator()(
const T& p)
const {
return p.first; };
55 select1st<std::pair<type1, int>> get_domain_elt;
56 select1st<std::pair<type2, int>> get_codomain_elt;
63 typedef boost::transform_iterator<select1st<std::pair<domain_type, int>>,
64 typename std::map<domain_type, int>::const_iterator>
66 typedef boost::transform_iterator<select1st<std::pair<codomain_type, int>>,
67 typename std::map<codomain_type, int>::const_iterator>
76 void insert(
const type1& elt1,
const type2& elt2);
78 const codomain_type&
get_image(
const domain_type& elt)
const;
79 const domain_type&
get_preimage(
const codomain_type& elt)
const;
91 template<
typename type1,
typename type2>
97 template<
typename type1,
typename type2>
99 codomain(
std::move(b.codomain)),
100 directory(
std::move(b.directory)),
101 dir_entries(b.dir_entries) {}
103 template<
typename type1,
typename type2>
106 auto ret1 = domain.
insert(std::make_pair(elt1, dir_entries));
107 auto ret2 = codomain.insert(std::make_pair(elt2, dir_entries));
110 if(!ret1.second or !ret2.second)
111 throw std::domain_error(
"Either or both of the element already exists");
114 directory[dir_entries] = std::make_pair(ret1.first, ret2.first);
119 template<
typename type1,
typename type2>
122 auto it = domain.find(elt);
123 if(it == domain.end())
124 throw std::out_of_range(
"The key doesn't exist");
126 return (directory.at(it->second).second)->first;
129 template<
typename type1,
typename type2>
132 auto it = codomain.find(elt);
133 if(it == codomain.end())
134 throw std::out_of_range(
"The key doesn't exist");
136 return (directory.at(it->second).first)->first;
140 template<
typename type1,
typename type2>
143 auto domain_it = domain.find(elt);
144 if(domain_it == domain.end())
145 throw std::out_of_range(
"element doesn't exist");
147 auto dir_entry = *directory[domain_it->second];
148 auto it_pair = dir_entry->second;
151 domain.erase(it_pair->first);
152 codomain.erase(it_pair->second);
155 directory.erase(dir_entry);
159 template<
typename type1,
typename type2>
162 auto codomain_it = codomain.find(elt);
163 if(codomain_it == codomain.end())
164 throw std::out_of_range(
"element doesn't exist");
166 auto dir_entry = *directory[codomain_it->second];
167 auto it_pair = dir_entry->second;
170 domain.erase(it_pair->first);
171 codomain.erase(it_pair->second);
174 directory.erase(dir_entry);
180 template<
typename type1,
typename type2>
183 return boost::make_transform_iterator(domain.begin(), get_domain_elt);
186 template<
typename type1,
typename type2>
189 return boost::make_transform_iterator(domain.end(), get_domain_elt);
192 template<
typename type1,
typename type2>
195 return boost::make_transform_iterator(codomain.begin(), get_codomain_elt);
198 template<
typename type1,
typename type2>
201 return boost::make_transform_iterator(codomain.end(), get_codomain_elt);
boost::transform_iterator< select1st< std::pair< domain_type, int > >, typename std::map< domain_type, int >::const_iterator > domain_iterator
Definition: bimap.h:65
boost::transform_iterator< select1st< std::pair< codomain_type, int > >, typename std::map< codomain_type, int >::const_iterator > codomain_iterator
Definition: bimap.h:68
bimap()
Definition: bimap.h:92
type2 codomain_type
Definition: bimap.h:62
codomain_iterator codomain_begin() const
Definition: bimap.h:194
void insert(const type1 &elt1, const type2 &elt2)
Definition: bimap.h:104
void remove_domain_elt(const domain_type &elt)
Definition: bimap.h:141
const codomain_type & get_image(const domain_type &elt) const
Definition: bimap.h:120
domain_iterator domain_end() const
Definition: bimap.h:188
codomain_iterator codomain_end() const
Definition: bimap.h:200
void remove_codomain_elt(const codomain_type &elt)
Definition: bimap.h:160
const domain_type & get_preimage(const codomain_type &elt) const
Definition: bimap.h:130
type1 domain_type
Definition: bimap.h:61
domain_iterator domain_begin() const
Definition: bimap.h:182