#ifndef BASE_STL_UTIL_H_
#define BASE_STL_UTIL_H_
#include <algorithm>
#include <iterator>
#include "base/check.h"
namespace base {
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 ResultType, typename Arg1, typename Arg2>
ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
DCHECK(std::ranges::is_sorted(a1));
DCHECK(std::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(std::ranges::is_sorted(a1));
DCHECK(std::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(std::ranges::is_sorted(a1));
DCHECK(std::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)
: it_(collection.begin()), end_(collection.end()) {}
bool operator()(const typename Collection::value_type& x) {
while (it_ != end_ && *it_ < x) {
++it_;
}
if (it_ == end_ || *it_ != x) {
return true;
}
++it_;
return false;
}
private:
typename Collection::const_iterator it_;
const typename Collection::const_iterator end_;
};
}
#endif