LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
map.hxx
Go to the documentation of this file.
1 
6 #ifndef MISC_MAP_HXX
7 # define MISC_MAP_HXX
8 
9 # include <iostream>
10 # include <sstream>
11 
12 # include <boost/lexical_cast.hpp>
13 
14 # include <misc/indent.hh>
15 # include <misc/contract.hh>
16 # include <misc/map.hh>
17 
18 namespace misc
19 {
20 
21  template <class T, class N>
23  {
24  }
25 
26 
27  template <class T, class N>
28  map<T, N>*
30  {
31  return new map<T, N>(*this);
32  }
33 
34  template <class T, class N>
36  {
37  }
38 
39  template <class T, class N>
41  map<T, N>::find(const T& t) const
42  {
43  return map_.find(t);
44  }
45 
46  template <class T, class N>
47  typename map<T, N>::iterator
48  map<T, N>::find(const T& t)
49  {
50  return map_.find(t);
51  }
52 
53  template <class T, class N>
54  typename map<T, N>::iterator
55  map<T, N>::xfind(const T& t)
56  {
57  iterator ires = find(t);
58  if (ires != map_.end())
59  return ires;
60  else
61  throw std::range_error(std::string("map: no mapping for ")
62  + boost::lexical_cast<std::string>(t));
63  }
64 
65  template <class T, class N>
67  map<T, N>::xfind(const T& t) const
68  {
69  const_iterator ires = find(t);
70  if (ires != map_.end())
71  return ires;
72  else
73  throw std::range_error(std::string("map: no mapping for ")
74  + boost::lexical_cast<std::string>(t));
75  }
76 
77  template <class T, class N>
78  typename map<T, N>::key_compare
80  {
81  return map_.key_comp();
82  }
83 
84  template <class T, class N>
85  N
86  map<T, N>::operator()(const T& t) const
87  {
88  return xfind(t)->second;
89  }
90 
91  template <class T, class N>
92  std::list<N>
93  map<T, N>::operator()(const std::list<T>& ts) const
94  {
95  std::list<N> res;
96  for (const T& t : ts)
97  res.push_back(operator()(t));
98  return res;
99  }
100 
101  template <class T, class N>
102  std::ostream&
103  map<T, N>::print(std::ostream& ostr) const
104  {
105  for (const value_type& p : *this)
106  ostr << p.first << " -> " << p.second << misc::iendl;
107  return ostr;
108  }
109 
110  template <class T, class N>
111  N
112  map<T, N>::operator[](const T& t) const
113  {
114  return operator()(t);
115  }
116 
117  template <class T, class N>
118  N&
120  {
121  return map_[t];
122  }
123 
124  template <class T, class N>
125  typename map<T, N>::iterator
127  {
128  return map_.begin();
129  }
130 
131  template <class T, class N>
132  typename map<T, N>::iterator
134  {
135  return map_.end();
136  }
137 
138  template <class T, class N>
141  {
142  return map_.begin();
143  }
144 
145  template <class T, class N>
148  {
149  return map_.end();
150  }
151 
152 
153  template <class T, class N>
154  typename map<T, N>::iterator
156  {
157  return map_.lower_bound(k);
158  }
159 
160  template <class T, class N>
162  map<T, N>::lower_bound(const T& k) const
163  {
164  return map_.lower_bound(k);
165  }
166 
167 
168  template <class T, class N>
169  std::pair<typename map<T, N>::iterator, bool>
170  map<T, N>::insert(const std::pair<const T, N>& x)
171  {
172  return map_.insert(x);
173  }
174 
175  template <class T, class N>
176  void
177  map<T, N>::insert(const map& other)
178  {
179  map_.insert(other.begin(), other.end());
180  }
181 
182  template <class T, class N>
183  template <class... Args>
184  std::pair<typename map<T, N>::iterator, bool>
185  map<T, N>::emplace(Args&&... args)
186  {
187  return map_.emplace(std::forward<Args>(args)...);
188  }
189 
190  template <class T, class N>
191  bool
193  {
194  return map_.empty();
195  }
196 
197  template <class T, class N>
198  size_t
200  {
201  return map_.size();
202  }
203 
204  template <class T, class N>
205  N
206  map<T, N>::take(const T& t)
207  {
208  typename map_type::iterator ires = xfind(t);
209  N elt = ires->second;
210  map_.erase(ires);
211  return elt;
212  }
213 
214  template <class T, class N>
215  void
217  {
218  map_.clear();
219  }
220 
221  template <class T, class N>
222  std::ostream&
223  operator<<(std::ostream& ostr, const map<T, N>& m)
224  {
225  return m.print(ostr);
226  }
227 
228 }
229 
230 #endif // !MISC_MAP_HXX