4 #include <boost/iterator/iterator_adaptor.hpp> 5 #include <boost/type_traits/remove_cv.hpp> 6 #include <boost/variant/get.hpp> 22 template <
typename Range,
typename Func>
23 auto sum(Range&& r, Func f);
31 template <
typename Iter>
33 return std::make_move_iterator(it);
39 template <
typename Iter>
45 template <
typename ContainerT>
52 void operator()(std::vector<T>& c,
size_t size)
const { c.reserve(size); }
55 template <
typename VectorT>
62 template <
typename VectorT,
typename ContainerT>
65 const auto size =
sum(c, [](
auto& elem) {
return elem.size(); });
68 for (
auto& elem : c) {
69 conced.insert(conced.end(), move(elem.begin()), move(elem.end()));
74 template <
typename VectorT,
typename ContainerT,
typename Func>
78 for (
auto& elem : c) {
80 conced.insert(conced.end(), move(value.begin()), move(value.end()));
85 template <
typename ContainerT,
typename Func>
87 auto size =
sum(c, [f](
auto&& elem) {
91 using RetT =
typename decltype(f(*c.begin()).first)::value_type;
92 std::vector<RetT> ret;
95 for (
auto&& elem : c) {
97 ret.insert(ret.end(), move(range.first), move(range.second));
102 template <
typename ContainerT,
typename Func,
typename AllocatorT>
104 auto size =
sum(c, [f](
auto&& elem) {
105 auto range = f(elem);
108 using RetT =
typename decltype(f(*c.begin()).first)::value_type;
109 std::vector<RetT, AllocatorT> ret(alloc);
112 for (
auto&& elem : c) {
113 auto range = f(elem);
114 ret.insert(ret.end(), move(range.first), move(range.second));
119 template <
typename Container,
typename Func>
121 using RetT = std::decay_t<decltype(f(*c.begin()))>;
122 std::vector<RetT> transformed;
123 transformed.reserve(c.size());
124 std::transform(c.begin(), c.end(), std::back_inserter(transformed), f);
132 template <
typename PrimitiveT>
133 constexpr
bool idLess(
const PrimitiveT&
p1,
const PrimitiveT&
p2) {
134 return p1.id() < p2.id();
138 template <
typename T>
143 template <
typename T1,
typename T2>
145 return (first.id() < second.id()) ? std::make_pair(first, second) : std::make_pair(second, first);
154 std::shared_ptr<const T>
addConst(std::shared_ptr<T> t) {
155 return std::const_pointer_cast<
const T>(t);
160 return std::const_pointer_cast<T>(t);
165 return const_cast<T&
>(t);
168 template <
typename Container>
169 Container
invert(
const Container& cont) {
171 std::reverse_copy(cont.begin(), cont.end(), std::back_inserter(cOut));
175 template <
typename Iterator,
typename Func>
176 auto transform(Iterator begin, Iterator end,
const Func f) {
177 using ValueT = decltype(*begin);
178 using RetT = std::decay_t<decltype(f(*begin))>;
179 std::vector<RetT> transformed;
181 std::transform(begin, end, std::back_inserter(transformed), [&f](ValueT elem) {
return f(elem); });
185 template <
typename ContainerT,
typename ValueT>
186 auto find(ContainerT&& c,
const ValueT& val) {
188 auto it =
std::find(std::begin(c), std::end(c), val);
189 if (it != std::end(c)) {
195 template <
typename ContainerT,
typename Func>
198 auto it = std::find_if(std::begin(c), std::end(c), f);
199 if (it != std::end(c)) {
205 template <
typename Container,
typename Func>
210 template <
typename T,
typename Func>
215 template <
typename Container,
typename Predicate>
216 bool anyOf(
const Container& c, Predicate&&
p) {
217 return std::any_of(c.begin(), c.end(),
p);
220 template <
typename Container,
typename Value>
221 bool contains(
const Container& c,
const Value& v) {
222 return std::find(c.begin(), c.end(), v) != c.end();
225 template <
typename Container,
typename Func>
227 std::for_each(c.begin(), c.end(), std::forward<Func>(f));
230 template <
typename OutT,
typename InT>
232 std::vector<std::shared_ptr<OutT>> transformed;
233 transformed.reserve(v.size());
234 for (
const auto& elem : v) {
235 auto cast = std::dynamic_pointer_cast<OutT>(elem);
237 transformed.push_back(std::move(cast));
247 template <
typename Range,
typename Func>
248 auto sum(Range&& r, Func f) {
249 using ValT = decltype(f(*std::begin(r)));
250 return std::accumulate(std::begin(r), std::end(r), ValT(), [&f](
auto v,
auto&& elem) {
return v + f(elem); });
259 template <
typename ContainerT>
261 using VectorT = std::decay_t<decltype(*std::begin(c))>;
262 return detail::concatenate<VectorT>(std::forward<ContainerT>(c));
265 template <
typename T>
267 using VectorT = std::decay_t<decltype(*std::begin(c))>;
268 return detail::concatenate<VectorT>(std::move(c));
275 template <
typename ContainerT,
typename Func>
277 using VectorT = std::decay_t<decltype(f(*c.begin()))>;
278 return detail::concatenate<VectorT>(std::forward<ContainerT>(c), f);
281 template <
typename T,
typename Func>
283 using VectorT = std::decay_t<decltype(f(*c.begin()))>;
284 return detail::concatenate<VectorT>(std::move(c), f);
294 template <
typename ContainerT,
typename Func>
299 template <
typename T,
typename Func>
305 template <
typename ContainerT,
typename Func,
typename AllocatorT>
310 template <
typename T,
typename Func,
typename AllocatorT>
315 template <
typename T,
typename Variant>
319 for (
const auto& e : v) {
320 const auto* t = boost::get<typename T::MutableType>(&e);
332 constexpr
bool strequal(
char const* lhs,
char const* rhs) {
333 while (*lhs != 0 && *rhs != 0) {
334 if (*lhs++ != *rhs++) {
338 return *lhs == 0 && *rhs == 0;
346 template <
typename WeakT>
348 std::vector<decltype(v.front().lock())> s;
350 for (
const auto& e : v) {
352 s.push_back(e.lock());
359 inline bool nearZero(
double d) {
return std::abs(d) < 1e-8; }
366 template <
typename Container,
typename ConstIterator>
367 typename Container::iterator
removeConst(Container& c, ConstIterator it) {
368 return c.erase(it, it);
VectorT createReserved(size_t size)
auto find(ContainerT &&c, const ValueT &val)
auto orderedPair(T1 &first, T2 &second)
constexpr bool strequal(char const *lhs, char const *rhs)
compares two c-strings at compile time. Do not use this for run time.
VectorT concatenate(ContainerT &&c)
auto concatenateRange(ContainerT &&c, Func f)
auto strong(std::vector< WeakT > v)
transforms a vector of weak_ptrs to a vector of shared_ptrs
Compares ids of primitives. Can be used for some sorted stl containers.
auto transformSharedPtr(const std::vector< std::shared_ptr< InT >> &v)
auto transform(std::initializer_list< T > &&c, Func f)
void forEach(Container &&c, Func &&f)
std::vector< T > getVariant(const std::vector< Variant > &v)
boost::optional< T > Optional
constexpr bool operator()(const T &a, const T &b)
void operator()(ContainerT &, size_t) const
std::shared_ptr< T > removeConst(std::shared_ptr< const T > t)
Optional< double > distance
auto transform(Container &&c, Func f)
constexpr bool idLess(const PrimitiveT &p1, const PrimitiveT &p2)
Compares ids of primitives. Can be used for some stl algorithms.
bool contains(const Container &c, const Value &v)
auto findIf(ContainerT &&c, Func f)
auto sum(Range &&r, Func f)
Wrapper around std::accumulate for summing up the result of Func over a range.
void operator()(std::vector< T > &c, size_t size) const
bool anyOf(const Container &c, Predicate &&p)
bool nearZero(double d)
Tests if a double is close to zero within a small margin.
Container invert(const Container &cont)