Algorithms.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_ALGORITHMS_HPP
30 #define AURORA_ALGORITHMS_HPP
31 
32 #include <Aurora/Tools/Swap.hpp>
33 
34 #include <algorithm>
35 #include <cassert>
36 
37 
38 namespace aurora
39 {
40 
43 
47 template <typename T>
48 bool equivalent(const T& lhs, const T& rhs)
49 {
50  return !(lhs < rhs) && !(rhs < lhs);
51 }
52 
58 template <typename ForwardIterator, typename T>
59 ForwardIterator binarySearch(ForwardIterator first, ForwardIterator last, const T& value)
60 {
61  // Note: std::lower_bound() has O(log n) on random access iterators
62  ForwardIterator result = std::lower_bound(first, last, value);
63 
64  // std::lower_bound() returns iterator to first element >= value, which can be inequal to value
65  if (result == last || !equivalent(*result, value))
66  return last;
67  else
68  return result;
69 }
70 
75 template <typename Container, typename Iterator>
76 void eraseUnordered(Container& c, Iterator itr)
77 {
78  adlSwap(*itr, c.back());
79  c.pop_back();
80 }
81 
84 template <typename Container, typename Value>
85 void remove(Container& c, const Value& v)
86 {
87  auto begin = std::remove(c.begin(), c.end(), v);
88  c.erase(begin, c.end());
89 }
90 
93 template <typename Container, typename Predicate>
94 void removeIf(Container& c, const Predicate& p)
95 {
96  auto begin = std::remove_if(c.begin(), c.end(), p);
97  c.erase(begin, c.end());
98 }
99 
102 template <typename Queue>
103 typename Queue::value_type pop(Queue& q)
104 {
105  auto value = std::move(q.front());
106  q.pop();
107  return value;
108 }
109 
112 template <typename Queue>
113 void clearQueue(Queue& q)
114 {
115  while (!q.empty())
116  q.pop();
117 }
118 
123 template <typename AssocContainer, typename Key>
124 typename AssocContainer::mapped_type& mapAt(AssocContainer& map, const Key& k)
125 {
126  auto itr = map.find(k);
127  assert(itr != map.end());
128  return itr->second;
129 }
130 
131 // const overload
132 template <typename AssocContainer, typename Key>
133 const typename AssocContainer::mapped_type& mapAt(const AssocContainer& map, const Key& k)
134 {
135  auto itr = map.find(k);
136  assert(itr != map.end());
137  return itr->second;
138 }
139 
141 
142 } // namespace aurora
143 
144 #endif // AURORA_ALGORITHMS_HPP
void remove(Container &c, const Value &v)
Erase-remove idiom.
Definition: Algorithms.hpp:85
void clearQueue(Queue &q)
Clear std::queue.
Definition: Algorithms.hpp:113
bool equivalent(const T &lhs, const T &rhs)
Determines whether two values are considered equivalent in sorting.
Definition: Algorithms.hpp:48
Queue::value_type pop(Queue &q)
Pop from queue with return value.
Definition: Algorithms.hpp:103
void eraseUnordered(Container &c, Iterator itr)
Erase element using swap-and-pop_back idiom.
Definition: Algorithms.hpp:76
Helpers to declare and invoke swap() functions.
void removeIf(Container &c, const Predicate &p)
Erase-remove-if idiom.
Definition: Algorithms.hpp:94
void adlSwap(T &lhs, T &rhs)
swap() function with argument-dependent lookup
Definition: Swap.hpp:49
ForwardIterator binarySearch(ForwardIterator first, ForwardIterator last, const T &value)
Binary search for value in iterator range.
Definition: Algorithms.hpp:59
AssocContainer::mapped_type & mapAt(AssocContainer &map, const Key &k)
Returns the value type for a specific key.
Definition: Algorithms.hpp:124
Definition: DispatchTraits.hpp:39