LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
algorithm.hxx
Go to the documentation of this file.
1 
6 #ifndef MISC_ALGORITHM_HXX
7 # define MISC_ALGORITHM_HXX
8 
9 # include <ostream>
10 # include <algorithm>
11 
12 # include <misc/algorithm.hh>
13 
14 namespace misc
15 {
16 
17  template <typename Container>
18  void
19  deep_clear(Container& c)
20  {
21  for (typename Container::value_type& x : c)
22  delete x;
23  c.clear();
24  }
25 
26 
27  // Find \a v in the whole \a c.
28  template <typename Container>
29  inline typename Container::const_iterator
30  find(const Container& c, const typename Container::value_type& v)
31  {
32  return std::find(c.begin(), c.end(), v);
33  }
34 
35  // Find \a v in the whole \a c.
36  template <typename Container>
37  inline typename Container::iterator
38  find(Container& c, const typename Container::value_type& v)
39  {
40  return std::find(c.begin(), c.end(), v);
41  }
42 
43 
44  // Apply \a f to all the members of \a c, and return it.
45  template <typename Container, typename Functor>
46  inline Functor&
47  for_each(Container& c, Functor& f)
48  {
49  typename Container::const_iterator end = c.end();
50  for (typename Container::const_iterator i = c.begin(); i != end; ++i)
51  f(*i);
52  return f;
53  }
54 
55 
56  // Is \a v member of \a c?
57  template <typename Container>
58  inline bool
59  has(const Container& c, const typename Container::value_type& v)
60  {
61  // We specify the instance to solve a conflict between the
62  // two finds above, that compete against each other because
63  // the parameter Container can embed a "const".
64  return find<Container>(c, v) != c.end();
65  }
66 
67 
68  template <typename Map>
69  typename Map::iterator
70  put(Map& map,
71  const typename Map::key_type& key,
72  const typename Map::mapped_type& value)
73  {
74  // See ``Efficient STL''.
75  typename Map::iterator i = map.lower_bound(key);
76 
77  if (i != map.end() && !(map.key_comp()(key, i->first)))
78  {
79  // Update.
80  i->second = value;
81  return i;
82  }
83  else
84  {
85  // First insertion.
86  return map.emplace(key, value).first;
87  }
88  }
89 
90 }
91 
92 #endif // !MISC_ALGORITHM_HXX