Loading...
Searching...
No Matches
commonUtils.tpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_COMMON_UTILS_TPP__
26#define __BITPIT_COMMON_UTILS_TPP__
27
30namespace bitpit {
31
32namespace utils {
33
53template <typename T, typename Comparator>
54bool addToOrderedVector(const T &value, std::vector<T> &list, Comparator comparator)
55{
56 if (list.empty()) {
57 list.push_back(value);
58 return true;
59 }
60
61 typename std::vector<T>::iterator itr = std::lower_bound(list.begin(), list.end(), value, comparator);
62 if (itr == list.end() || *itr != value) {
63 list.insert(itr, value);
64 return true;
65 } else {
66 return false;
67 }
68};
69
89template <typename T, typename Comparator>
90typename std::vector<T>::const_iterator findInOrderedVector(const T &value, const std::vector<T> &list, Comparator comparator)
91{
92 typename std::vector<T>::const_iterator itr = std::lower_bound(list.begin(), list.end(), value, comparator);
93 if (itr == list.end() || *itr != value) {
94 return list.end();
95 }
96
97 return itr;
98};
99
111template<typename T>
112void reorderVector(std::vector<size_t> &order, std::vector<T> &v, std::size_t size)
113{
114 reorderContainer(order, v, size);
115}
116
129template<typename OrderContainer, typename DataContainer>
130void reorderContainer(OrderContainer &order, DataContainer &v, std::size_t size)
131{
132 for (std::size_t i = 0; i < size; i++) {
133 std::size_t j;
134 while (i != (j = order[i])) {
135 std::size_t k = order[j];
136
137 swapValue(v, j, k);
138 std::swap(order[i], order[j]);
139 }
140 }
141}
142
154template<typename Container, typename Index>
155void swapValue(Container &v, Index i, Index j)
156{
157 std::swap(v[i], v[j]);
158}
159
169template<class T>
170void eraseValue(std::vector<T> &vec, const T &value)
171{
172 typename std::vector<T>::iterator it = find(vec.begin(), vec.end(), value);
173 if (it != vec.end()) {
174 vec.erase(it);
175 }
176}
177
187template<class T>
188std::vector<T> intersectionVector(const std::vector<T> &vec_1, const std::vector<T> &vec_2)
189{
190 std::vector<T> intersect;
191 std::map<T, bool> storage;
192
193 intersect.reserve(std::min(vec_1.size(), vec_2.size()));
194 for (auto cit_ = vec_2.begin(); cit_ != vec_2.end(); ++cit_) {
195 storage[*cit_] = true;
196 }
197
198 for (auto it_ = vec_1.begin(); it_ != vec_1.end(); ++it_) {
199 if (storage[*it_]) {
200 intersect.push_back(*it_);
201 }
202 }
203
204 return intersect;
205}
206
212template <typename T>
213auto is_iterable_impl(int) -> decltype (
214 std::begin(std::declval<T&>()) != std::end(std::declval<T&>()), // begin/end and operator !=
215 ++std::declval<decltype(std::begin(std::declval<T&>()))&>(), // operator ++
216 *std::begin(std::declval<T&>()), // operator*
217 std::true_type{}
218);
219
220template <typename T>
221std::false_type is_iterable_impl(...);
222
223template <typename T>
224using is_iterable = decltype(is_iterable_impl<T>(0));
225
226}
227
228}
229
230#endif
void swapValue(std::vector< bool > &v, std::size_t i, std::size_t j)
std::vector< T > intersectionVector(const std::vector< T > &, const std::vector< T > &)
void reorderContainer(OrderContainer &order, DataContainer &v, std::size_t size)
auto is_iterable_impl(int) -> decltype(std::begin(std::declval< T & >()) !=std::end(std::declval< T & >()),++std::declval< decltype(std::begin(std::declval< T & >()))& >(), *std::begin(std::declval< T & >()), std::true_type{})
void reorderVector(std::vector< size_t > &order, std::vector< T > &v, std::size_t size)
void eraseValue(std::vector< T > &, const T &)
--- layout: doxygen_footer ---