1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#ifndef UTIL_CONTAINER_H_2020227_
#define UTIL_CONTAINER_H_2020227_
#include <algorithm>
#include <array>
#include <type_traits>
namespace util {
template <class T, size_t N, class U = T>
constexpr void shl(std::array<T, N> &buffer, U value = T()) {
static_assert(std::is_convertible<T, U>::value, "U must be convertable to the type contained in the array!");
std::rotate(buffer.begin(), buffer.begin() + 1, buffer.end());
buffer[N - 1] = value;
}
template <class T, size_t N, class U = T>
constexpr void shr(std::array<T, N> &buffer, U value = T()) {
static_assert(std::is_convertible<T, U>::value, "U must be convertable to the type contained in the array!");
std::rotate(buffer.rbegin(), buffer.rbegin() + 1, buffer.rend());
buffer[0] = value;
}
template <class T, size_t N>
constexpr void rol(std::array<T, N> &buffer) {
std::rotate(buffer.begin(), buffer.begin() + 1, buffer.end());
}
template <class T, size_t N>
constexpr void ror(std::array<T, N> &buffer) {
std::rotate(buffer.rbegin(), buffer.rbegin() + 1, buffer.rend());
}
template <typename T, typename... Tail>
constexpr auto make_array(T head, Tail... tail) -> std::array<T, 1 + sizeof...(Tail)> {
return std::array<T, 1 + sizeof...(Tail)>{head, tail...};
}
template <typename Container, typename Element>
bool contains(const Container &container, const Element &element) {
return std::find(std::begin(container), std::end(container), element) != std::end(container);
}
template <typename Container, typename Pred>
bool contains_if(const Container &container, Pred pred) {
return std::find_if(std::begin(container), std::end(container), pred) != std::end(container);
}
}
#endif
|