#ifndef BASE_STL_UTIL_H_
#define BASE_STL_UTIL_H_
#include <algorithm>
#include <forward_list>
#include <iterator>
#include <type_traits>
#include "base/check.h"
#include "base/ranges/algorithm.h"
namespace base {
namespace internal {
template <typename Iter>
constexpr bool IsRandomAccessIter =
std::is_same<typename std::iterator_traits<Iter>::iterator_category,
std::random_access_iterator_tag>::value;
}
template <class A>
const typename A::container_type& GetUnderlyingContainer(const A& adapter) {
struct ExposedAdapter : A {
using A::c;
};
return adapter.*&ExposedAdapter::c;
}
template<class T>
void STLClearObject(T* obj) {
T tmp;
tmp.swap(*obj);
obj->reserve(0);
}
template <typename Container,
typename ConstIter,
std::enable_if_t<!internal::IsRandomAccessIter<ConstIter>>* = nullptr>
constexpr auto ConstCastIterator(Container& c, ConstIter it) {
return c.erase(it, it);
}
template <typename T, typename Allocator>
constexpr auto ConstCastIterator(
std::forward_list<T, Allocator>& c,
typename std::forward_list<T, Allocator>::const_iterator it) {
#if defined(__GLIBCXX__)
return c.insert_after(it, {});
#else
return c.erase_after(it, it);
#endif
}
template <typename Container,
typename ConstIter,
std::enable_if_t<internal::IsRandomAccessIter<ConstIter>>* = nullptr>
constexpr auto ConstCastIterator(Container& c, ConstIter it) {
using std::begin;
using std::cbegin;
return begin(c) + (it - cbegin(c));
}
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType difference;
std::set_difference(a1.begin(), a1.end(),
a2.begin(), a2.end(),
std::inserter(difference, difference.end()));
return difference;
}
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) {
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType result;
std::set_union(a1.begin(), a1.end(),
a2.begin(), a2.end(),
std::inserter(result, result.end()));
return result;
}
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) {
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType result;
std::set_intersection(a1.begin(), a1.end(),
a2.begin(), a2.end(),
std::inserter(result, result.end()));
return result;
}
template <class Collection>
class IsNotIn {
public:
explicit IsNotIn(const Collection& collection)
: i_(collection.begin()), end_(collection.end()) {}
bool operator()(const typename Collection::value_type& x) {
while (i_ != end_ && *i_ < x)
++i_;
if (i_ == end_)
return true;
if (*i_ == x) {
++i_;
return false;
}
return true;
}
private:
typename Collection::const_iterator i_;
const typename Collection::const_iterator end_;
};
}
#endif