CASM  1.1.0
A Clusters Approach to Statistical Mechanics
algorithm.hh
Go to the documentation of this file.
1 #ifndef CASM_algorithm
2 #define CASM_algorithm
3 
4 #include <algorithm>
5 #include <vector>
6 
8 
9 namespace CASM {
10 
11 using std::find;
12 
15 template <typename Iterator, typename T, typename BinaryCompare>
16 Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q) {
17  return std::find_if(begin, end, [&value, q](const T &other) -> bool {
18  return q(value, other);
19  });
20 }
21 
23 template <typename Iterator, typename T>
24 Index find_index(Iterator begin, Iterator end, const T &value) {
25  return std::distance(begin, std::find(begin, end, value));
26 }
27 
29 template <typename Iterator, typename T, typename BinaryCompare>
30 Index find_index(Iterator begin, Iterator end, const T &value,
31  BinaryCompare q) {
32  return std::distance(begin, find(begin, end, value, q));
33 }
34 
37 template <typename Container, typename T, typename BinaryCompare>
38 Index find_index(const Container &container, const T &value, BinaryCompare q) {
39  return std::distance(container.begin(),
40  find(container.begin(), container.end(), value, q));
41 }
42 
45 template <typename Container, typename T>
46 Index find_index(const Container &container, const T &value) {
47  return std::distance(container.begin(),
48  std::find(container.begin(), container.end(), value));
49 }
50 
52 template <typename Iterator, typename UnaryPredicate>
53 Index find_index_if(Iterator begin, Iterator end, UnaryPredicate p) {
54  return std::distance(begin, std::find_if(begin, end, p));
55 }
56 
59 template <typename Container, typename UnaryPredicate>
60 Index find_index_if(const Container &container, UnaryPredicate p) {
61  return std::distance(container.begin(),
62  std::find_if(container.begin(), container.end(), p));
63 }
64 
66 template <typename Iterator, typename UnaryPredicate>
67 Index find_index_if_not(Iterator begin, Iterator end, UnaryPredicate q) {
68  // Please use -Werror=return-type in your compiler flags
69  return std::distance(begin, std::find_if_not(begin, end, q));
70 }
71 
74 template <typename Container, typename UnaryPredicate>
75 Index find_index_if_not(const Container &container, UnaryPredicate q) {
76  return std::distance(container.begin(),
77  std::find_if_not(container.begin(), container.end(), q));
78 }
79 
82 template <typename Container, typename T>
83 bool contains(const Container &container, const T &value) {
84  return container.end() !=
85  std::find(container.begin(), container.end(), value);
86 }
87 
90 template <typename Container, typename T, typename BinaryCompare>
91 bool contains(const Container &container, const T &value, BinaryCompare q) {
92  return container.end() != find(container.begin(), container.end(), value, q);
93 }
94 
97 template <typename Container, typename UnaryPredicate>
98 bool contains_if(const Container &container, UnaryPredicate p) {
99  return container.end() != std::find_if(container.begin(), container.end(), p);
100 }
101 
104 template <typename Container, typename UnaryPredicate>
105 bool contains_if_not(const Container &container, UnaryPredicate q) {
106  return container.end() !=
107  std::find_if_not(container.begin(), container.end(), q);
108 }
109 
111 template <typename Container1, typename Container2>
112 bool contains_all(const Container1 &container, const Container2 &values) {
113  for (auto const &v : values)
114  if (!contains(container, v)) return false;
115 
116  return true;
117 }
118 
121 template <typename Container1, typename Container2, typename BinaryCompare>
122 bool contains_all(const Container1 &container, const Container2 &values,
123  BinaryCompare q) {
124  for (auto const &v : values)
125  if (!contains(container, v, q)) return false;
126 
127  return true;
128 }
129 
130 template <typename Container>
131 typename Container::value_type sum(
132  const Container &container, typename Container::value_type init_val = 0) {
133  for (const auto &val : container) {
134  init_val += val;
135  }
136  return init_val;
137 }
138 
141 template <typename T>
142 T *end_ptr(std::vector<T> &container) {
143  return container.data() + container.size();
144 }
145 
148 template <typename T>
149 T const *end_ptr(std::vector<T> const &container) {
150  return container.data() + container.size();
151 }
152 
153 } // namespace CASM
154 
155 #endif
Main CASM namespace.
Definition: APICommand.hh:8
Index find_index(Iterator begin, Iterator end, const T &value)
Equivalent to std::distance(begin, std::find(begin, end, value))
Definition: algorithm.hh:24
Container::value_type sum(const Container &container, typename Container::value_type init_val=0)
Definition: algorithm.hh:131
Index find_index_if(Iterator begin, Iterator end, UnaryPredicate p)
Equivalent to std::distance(begin, std::find_if(begin, end, p))
Definition: algorithm.hh:53
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
Definition: algorithm.hh:16
bool contains_if_not(const Container &container, UnaryPredicate q)
Equivalent to container.end() != std::find_if_not(container.begin(), container.end(),...
Definition: algorithm.hh:105
bool contains(const Container &container, const T &value)
Equivalent to container.end() != std::find(container.begin(), container.end(), value)
Definition: algorithm.hh:83
T * end_ptr(std::vector< T > &container)
Return pointer one past end of vector. Equivalent to convainer.data()+container.size()
Definition: algorithm.hh:142
bool contains_if(const Container &container, UnaryPredicate p)
Equivalent to container.end() != std::find_if(container.begin(), container.end(), p)
Definition: algorithm.hh:98
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
bool contains_all(const Container1 &container, const Container2 &values)
Returns true if each elements of 'values' is contained in 'container'.
Definition: algorithm.hh:112
Index find_index_if_not(Iterator begin, Iterator end, UnaryPredicate q)
Equivalent to std::distance(begin, std::find_if_not(begin, end, q))
Definition: algorithm.hh:67