2014-12-23 14:34:21 +01:00
|
|
|
#ifndef containers_hh_INCLUDED
|
|
|
|
#define containers_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
#include <iterator>
|
2017-05-28 20:56:47 +02:00
|
|
|
#include <numeric>
|
2014-12-23 14:34:21 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Factory>
|
|
|
|
struct ContainerView { Factory factory; };
|
|
|
|
|
2016-03-14 10:56:08 +01:00
|
|
|
template<typename Factory>
|
|
|
|
ContainerView<Factory> make_view(Factory factory) { return {factory}; }
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container, typename Factory>
|
2016-03-14 10:56:08 +01:00
|
|
|
decltype(auto) operator| (Container&& container, ContainerView<Factory> view)
|
2016-03-08 22:35:56 +01:00
|
|
|
{
|
|
|
|
return view.factory(std::forward<Container>(container));
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
template<typename Container>
|
2016-03-08 22:35:56 +01:00
|
|
|
struct ReverseView
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
|
|
|
using iterator = decltype(std::declval<Container>().rbegin());
|
|
|
|
|
|
|
|
iterator begin() { return m_container.rbegin(); }
|
|
|
|
iterator end() { return m_container.rend(); }
|
|
|
|
|
2016-03-24 23:14:18 +01:00
|
|
|
Container m_container;
|
2014-12-23 14:34:21 +01:00
|
|
|
};
|
|
|
|
|
2016-03-24 23:14:18 +01:00
|
|
|
template<typename C>
|
|
|
|
using RemoveReference = typename std::remove_reference<C>::type;
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
struct ReverseFactory
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container>
|
2016-03-24 23:14:18 +01:00
|
|
|
ReverseView<RemoveReference<Container>> operator()(Container&& container) const
|
|
|
|
{
|
|
|
|
return {std::move(container)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
ReverseView<Container&> operator()(Container& container) const
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
return {container};
|
2014-12-23 14:34:21 +01:00
|
|
|
}
|
2016-03-08 22:35:56 +01:00
|
|
|
};
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
inline ContainerView<ReverseFactory> reverse() { return {}; }
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-25 01:14:56 +01:00
|
|
|
template<typename Container>
|
|
|
|
using IteratorOf = decltype(std::begin(std::declval<Container>()));
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
using ValueOf = typename Container::value_type;
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container, typename Filter>
|
|
|
|
struct FilterView
|
|
|
|
{
|
2016-03-25 01:14:56 +01:00
|
|
|
using ContainerIt = IteratorOf<Container>;
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
struct Iterator : std::iterator<std::forward_iterator_tag,
|
2017-01-02 06:13:58 +01:00
|
|
|
typename std::iterator_traits<ContainerIt>::value_type>
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
Iterator(const FilterView& view, ContainerIt it, ContainerIt end)
|
|
|
|
: m_it{std::move(it)}, m_end{std::move(end)}, m_view{view}
|
|
|
|
{
|
|
|
|
do_filter();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator*() -> decltype(*std::declval<ContainerIt>()) { return *m_it; }
|
|
|
|
Iterator& operator++() { ++m_it; do_filter(); return *this; }
|
|
|
|
Iterator operator++(int) { auto copy = *this; ++(*this); return copy; }
|
|
|
|
|
|
|
|
friend bool operator==(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return lhs.m_it == rhs.m_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContainerIt& base() const { return m_it; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void do_filter()
|
|
|
|
{
|
|
|
|
while (m_it != m_end and not m_view.m_filter(*m_it))
|
|
|
|
++m_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ContainerIt m_it;
|
|
|
|
ContainerIt m_end;
|
|
|
|
const FilterView& m_view;
|
|
|
|
};
|
|
|
|
|
2016-10-11 01:17:00 +02:00
|
|
|
Iterator begin() const { return {*this, std::begin(m_container), std::end(m_container)}; }
|
|
|
|
Iterator end() const { return {*this, std::end(m_container), std::end(m_container)}; }
|
2014-12-23 23:15:25 +01:00
|
|
|
|
2016-03-24 23:14:18 +01:00
|
|
|
Container m_container;
|
2016-12-14 14:25:55 +01:00
|
|
|
mutable Filter m_filter;
|
2014-12-23 14:34:21 +01:00
|
|
|
};
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Filter>
|
|
|
|
struct FilterFactory
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container>
|
2016-03-24 23:14:18 +01:00
|
|
|
FilterView<Container&, Filter> operator()(Container& container) const { return {container, std::move(m_filter)}; }
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
FilterView<RemoveReference<Container>, Filter> operator()(Container&& container) const { return {std::move(container), std::move(m_filter)}; }
|
2014-12-23 14:34:21 +01:00
|
|
|
|
|
|
|
Filter m_filter;
|
|
|
|
};
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Filter>
|
2016-03-09 00:22:32 +01:00
|
|
|
inline ContainerView<FilterFactory<Filter>> filter(Filter f) { return {{std::move(f)}}; }
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2015-02-27 20:32:27 +01:00
|
|
|
template<typename I, typename T>
|
|
|
|
using TransformedResult = decltype(std::declval<T>()(*std::declval<I>()));
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container, typename Transform>
|
|
|
|
struct TransformView
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-25 01:14:56 +01:00
|
|
|
using ContainerIt = IteratorOf<Container>;
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
struct Iterator : std::iterator<std::forward_iterator_tag,
|
2016-03-12 16:27:54 +01:00
|
|
|
std::remove_reference_t<TransformedResult<ContainerIt, Transform>>>
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
Iterator(const TransformView& view, ContainerIt it)
|
|
|
|
: m_it{std::move(it)}, m_view{view} {}
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
auto operator*() -> TransformedResult<ContainerIt, Transform> { return m_view.m_transform(*m_it); }
|
|
|
|
Iterator& operator++() { ++m_it; return *this; }
|
|
|
|
Iterator operator++(int) { auto copy = *this; ++m_it; return copy; }
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
friend bool operator==(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return lhs.m_it == rhs.m_it;
|
|
|
|
}
|
2014-12-23 23:15:25 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
ContainerIt base() const { return m_it; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ContainerIt m_it;
|
|
|
|
const TransformView& m_view;
|
|
|
|
};
|
|
|
|
|
2016-10-11 01:17:00 +02:00
|
|
|
Iterator begin() const { return {*this, std::begin(m_container)}; }
|
|
|
|
Iterator end() const { return {*this, std::end(m_container)}; }
|
2014-12-23 14:34:21 +01:00
|
|
|
|
2016-03-24 23:14:18 +01:00
|
|
|
Container m_container;
|
2016-12-14 14:25:55 +01:00
|
|
|
mutable Transform m_transform;
|
2014-12-23 14:34:21 +01:00
|
|
|
};
|
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Transform>
|
|
|
|
struct TransformFactory
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container>
|
2016-03-24 23:14:18 +01:00
|
|
|
TransformView<Container&, Transform> operator()(Container& container) const { return {container, std::move(m_transform)}; }
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
TransformView<RemoveReference<Container>, Transform> operator()(Container&& container) const { return {std::move(container), std::move(m_transform)}; }
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
Transform m_transform;
|
|
|
|
};
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Transform>
|
2016-03-09 00:22:32 +01:00
|
|
|
inline ContainerView<TransformFactory<Transform>> transform(Transform t) { return {{std::move(t)}}; }
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-25 01:14:56 +01:00
|
|
|
template<typename Container, typename Separator = ValueOf<Container>,
|
|
|
|
typename ValueTypeParam = void>
|
|
|
|
struct SplitView
|
|
|
|
{
|
|
|
|
using ContainerIt = IteratorOf<Container>;
|
|
|
|
using ValueType = typename std::conditional<std::is_same<void, ValueTypeParam>::value,
|
|
|
|
std::pair<IteratorOf<Container>, IteratorOf<Container>>,
|
|
|
|
ValueTypeParam>::type;
|
|
|
|
|
|
|
|
struct Iterator : std::iterator<std::forward_iterator_tag, ValueType>
|
|
|
|
{
|
|
|
|
Iterator(ContainerIt pos, ContainerIt end, char separator)
|
|
|
|
: pos(pos), sep(pos), end(end), separator(separator)
|
|
|
|
{
|
|
|
|
while (sep != end and *sep != separator)
|
|
|
|
++sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator& operator++() { advance(); return *this; }
|
|
|
|
Iterator operator++(int) { auto copy = *this; advance(); return copy; }
|
|
|
|
|
|
|
|
bool operator==(const Iterator& other) const { return pos == other.pos; }
|
|
|
|
bool operator!=(const Iterator& other) const { return pos != other.pos; }
|
|
|
|
|
|
|
|
ValueType operator*() { return {pos, sep}; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void advance()
|
|
|
|
{
|
|
|
|
if (sep == end)
|
|
|
|
{
|
|
|
|
pos = end;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = sep+1;
|
|
|
|
for (sep = pos; sep != end; ++sep)
|
|
|
|
{
|
|
|
|
if (*sep == separator)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContainerIt pos;
|
|
|
|
ContainerIt sep;
|
|
|
|
ContainerIt end;
|
|
|
|
Separator separator;
|
|
|
|
};
|
|
|
|
|
2016-10-11 01:17:00 +02:00
|
|
|
Iterator begin() const { return {std::begin(m_container), std::end(m_container), m_separator}; }
|
|
|
|
Iterator end() const { return {std::end(m_container), std::end(m_container), m_separator}; }
|
2016-03-25 01:14:56 +01:00
|
|
|
|
|
|
|
Container m_container;
|
|
|
|
Separator m_separator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ValueType, typename Separator>
|
|
|
|
struct SplitViewFactory
|
|
|
|
{
|
|
|
|
template<typename Container>
|
|
|
|
SplitView<RemoveReference<Container>, Separator, ValueType>
|
|
|
|
operator()(Container&& container) const { return {std::move(container), std::move(separator)}; }
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
SplitView<Container&, Separator, ValueType>
|
|
|
|
operator()(Container& container) const { return {container, std::move(separator)}; }
|
|
|
|
|
|
|
|
Separator separator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ValueType = void, typename Separator>
|
|
|
|
ContainerView<SplitViewFactory<ValueType, Separator>> split(Separator separator) { return {{std::move(separator)}}; }
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
template<typename Container1, typename Container2>
|
|
|
|
struct ConcatView
|
|
|
|
{
|
|
|
|
using ContainerIt1 = decltype(begin(std::declval<Container1>()));
|
|
|
|
using ContainerIt2 = decltype(begin(std::declval<Container2>()));
|
2017-01-02 06:13:58 +01:00
|
|
|
using ValueType = typename std::common_type<typename std::iterator_traits<ContainerIt1>::value_type,
|
|
|
|
typename std::iterator_traits<ContainerIt2>::value_type>::type;
|
2016-03-08 22:35:56 +01:00
|
|
|
|
|
|
|
struct Iterator : std::iterator<std::forward_iterator_tag, ValueType>
|
2016-03-08 14:56:37 +01:00
|
|
|
{
|
2017-01-02 06:13:58 +01:00
|
|
|
static_assert(std::is_convertible<typename std::iterator_traits<ContainerIt1>::value_type, ValueType>::value, "");
|
|
|
|
static_assert(std::is_convertible<typename std::iterator_traits<ContainerIt2>::value_type, ValueType>::value, "");
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
Iterator(ContainerIt1 it1, ContainerIt1 end1, ContainerIt2 it2)
|
|
|
|
: m_it1(std::move(it1)), m_end1(std::move(end1)),
|
|
|
|
m_it2(std::move(it2)) {}
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-10-11 00:44:18 +02:00
|
|
|
ValueType operator*() { return is2() ? *m_it2 : *m_it1; }
|
2016-03-08 22:35:56 +01:00
|
|
|
Iterator& operator++() { if (is2()) ++m_it2; else ++m_it1; return *this; }
|
|
|
|
Iterator operator++(int) { auto copy = *this; ++*this; return copy; }
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
friend bool operator==(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return lhs.m_it1 == rhs.m_it1 and lhs.m_end1 == rhs.m_end1 and
|
|
|
|
lhs.m_it2 == rhs.m_it2;
|
|
|
|
}
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
|
|
|
|
{
|
|
|
|
return not (lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is2() const { return m_it1 == m_end1; }
|
|
|
|
|
|
|
|
ContainerIt1 m_it1;
|
|
|
|
ContainerIt1 m_end1;
|
|
|
|
ContainerIt2 m_it2;
|
|
|
|
};
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
ConcatView(Container1& container1, Container2& container2)
|
2016-03-09 00:36:54 +01:00
|
|
|
: m_container1(container1), m_container2(container2) {}
|
2016-03-08 14:56:37 +01:00
|
|
|
|
2016-03-08 22:35:56 +01:00
|
|
|
Iterator begin() const { return {m_container1.begin(), m_container1.end(), m_container2.begin()}; }
|
|
|
|
Iterator end() const { return {m_container1.end(), m_container1.end(), m_container2.end()}; }
|
2016-03-08 14:56:37 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Container1& m_container1;
|
|
|
|
Container2& m_container2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Container1, typename Container2>
|
2016-03-08 22:35:56 +01:00
|
|
|
ConcatView<Container1, Container2> concatenated(Container1&& container1, Container2&& container2)
|
2016-03-08 14:56:37 +01:00
|
|
|
{
|
|
|
|
return {container1, container2};
|
2014-12-23 14:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container, typename T>
|
2016-03-14 10:56:08 +01:00
|
|
|
auto find(Container&& container, const T& value)
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-14 10:56:08 +01:00
|
|
|
using std::begin; using std::end;
|
2014-12-23 14:34:21 +01:00
|
|
|
return std::find(begin(container), end(container), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container, typename T>
|
2016-03-14 10:56:08 +01:00
|
|
|
auto find_if(Container&& container, T op)
|
2014-12-23 14:34:21 +01:00
|
|
|
{
|
2016-03-14 10:56:08 +01:00
|
|
|
using std::begin; using std::end;
|
2014-12-23 14:34:21 +01:00
|
|
|
return std::find_if(begin(container), end(container), op);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container, typename T>
|
|
|
|
bool contains(Container&& container, const T& value)
|
|
|
|
{
|
2016-03-14 10:56:08 +01:00
|
|
|
using std::end;
|
2014-12-23 14:34:21 +01:00
|
|
|
return find(container, value) != end(container);
|
|
|
|
}
|
|
|
|
|
2016-02-28 19:30:35 +01:00
|
|
|
template<typename Container, typename T>
|
|
|
|
bool contains_that(Container&& container, T op)
|
|
|
|
{
|
2016-03-14 10:56:08 +01:00
|
|
|
using std::end;
|
2016-02-28 19:30:35 +01:00
|
|
|
return find_if(container, op) != end(container);
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
template<typename Container, typename U>
|
|
|
|
void unordered_erase(Container&& vec, U&& value)
|
|
|
|
{
|
|
|
|
auto it = find(vec, std::forward<U>(value));
|
|
|
|
if (it != vec.end())
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(vec.back(), *it);
|
|
|
|
vec.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 20:56:47 +02:00
|
|
|
template<typename Container, typename Init, typename BinOp>
|
|
|
|
Init accumulate(Container&& c, Init&& init, BinOp&& op)
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(c), end(c), init, op);
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // containers_hh_INCLUDED
|