SPN Set
stripe_python · · 个人记录
#ifndef SPN_SET_H
#define SPN_SET_H
#include <vector>
#include <utility>
#include <algorithm>
#include <stdexcept>
#include <functional>
template <class T, class Cmp, const bool Diff>
class _spn_set_base {
private:
static constexpr size_t _load = 150;
Cmp cmp;
size_t _size;
std::vector<std::vector<T>> lists;
std::vector<T> maxes;
size_t* index;
void build () noexcept;
std::pair<size_t, size_t> pos (size_t) noexcept;
size_t loc (size_t, size_t) noexcept;
void split (size_t) noexcept;
void merge (size_t) noexcept;
public:
_spn_set_base() : cmp(), _size(0), index(nullptr) {}
template <class It> _spn_set_base(const It& _begin, const It& _end)
: cmp(), _size(0), index(nullptr) {extend(_begin, _end);}
template <class It>
void build_from_sorted(const It& _begin, const It& _end) noexcept;
size_t size () const noexcept;
bool empty () const noexcept;
void clear () noexcept;
void shrink_to_fit () noexcept;
template <class It>
void extend (const It&, const It&) ;
bool insert (const T&) ;
bool erase (const T&) ;
T kth (size_t) ;
T operator[] (size_t) ;
T front () const noexcept;
T back () const noexcept;
size_t lower_bound (const T&) noexcept;
size_t upper_bound (const T&) noexcept;
size_t count (const T&) noexcept;
bool contains (const T&) noexcept;
T precursor (const T&) ;
T successor (const T&) ;
void swap (_spn_set_base&) noexcept;
struct iterator;
iterator begin () const noexcept;
iterator end () const noexcept;
iterator find (size_t) ;
};
#define SET_TEMP template <class T, class Cmp, const bool Diff>
#define SET_BASE _spn_set_base<T, Cmp, Diff>
SET_TEMP template <class It>
void SET_BASE::build_from_sorted(const It& _begin, const It& _end) noexcept {
clear(), _size = std::distance(_begin, _end);
for (size_t i = 0; i < _size; i += _load) {
lists.emplace_back(_begin + i, _begin + std::min(_size, i + _load));
maxes.emplace_back(lists.back().back());
}
};
SET_TEMP void SET_BASE::build() noexcept {
size_t n = lists.size();
index = new size_t[n];
std::fill(index, index + n, 0);
for (size_t i = 1; i < n; i++) {
index[i] += lists[i - 1].size();
if (i + (i & -i) < n) index[i + (i & -i)] += index[i];
}
}
SET_TEMP std::pair<size_t, size_t> SET_BASE::pos(size_t idx) noexcept {
if (idx < lists[0].size()) return {0, idx};
if (!index) build();
size_t n = lists.size(), p = 0;
for (size_t i = std::__lg(n); i != (size_t) -1; i--) {
size_t x = p + (1 << i);
if (x < n && idx >= index[x]) idx -= index[x], p = x;
}
return {p, idx};
}
SET_TEMP size_t SET_BASE::loc(size_t pos, size_t idx) noexcept {
if (pos == 0) return idx;
if (!index) build();
while (pos > 0) idx += index[pos], pos &= pos - 1;
return idx;
}
SET_TEMP size_t SET_BASE::size() const noexcept {return _size;}
SET_TEMP bool SET_BASE::empty() const noexcept {return maxes.empty();}
SET_TEMP void SET_BASE::clear() noexcept
{_size = 0, lists.clear(), maxes.clear(), index = nullptr;}
SET_TEMP void SET_BASE::shrink_to_fit() noexcept
{lists.shrink_to_fit(), maxes.shrink_to_fit();}
SET_TEMP bool SET_BASE::insert(const T& val) {
if (maxes.empty()) {
lists.emplace_back(1, val), maxes.emplace_back(val), _size++;
return true;
}
size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) {
pos--, lists[pos].emplace_back(val), maxes[pos] = val;
} else {
auto it = std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
if (Diff && it != lists[pos].begin()) {
T val0 = *std::prev(it);
if (!cmp(val, val0) && !cmp(val0, val)) return false;
}
lists[pos].insert(it, val);
}
_size++, split(pos);
return true;
}
SET_TEMP void SET_BASE::split(size_t pos) noexcept {
if (lists[pos].size() > (_load << 1)) {
std::vector<T> half(lists[pos].begin() + _load, lists[pos].end());
lists[pos].erase(lists[pos].begin() + _load, lists[pos].end());
maxes[pos] = lists[pos].back();
lists.insert(lists.begin() + pos + 1, half);
maxes.insert(maxes.begin() + pos + 1, half.back());
index = nullptr;
} else if (index) {
size_t n = lists.size();
for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]++;
}
}
SET_TEMP template <class It>
void SET_BASE::extend(const It& _begin, const It& _end) {
ptrdiff_t len = std::distance(_begin, _end);
if ((len << 2) < (ptrdiff_t) _size) {
for (It it = _begin; it != _end; it++) insert(*it);
return;
}
std::vector<T> a(_begin, _end);
for (const std::vector<T>& vec : lists) a.insert(a.end(), vec.begin(), vec.end());
std::sort(a.begin(), a.end(), cmp);
clear(), _size = a.size();
for (size_t i = 0; i < _size; i += _load) {
lists.emplace_back(a.begin() + i, a.begin() + std::min(_size, i + _load));
maxes.emplace_back(lists.back().back());
}
}
SET_TEMP bool SET_BASE::erase(const T& val) {
if (maxes.empty()) return false;
size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) return false;
size_t idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin();
if (cmp(lists[pos][idx], val) || cmp(val, lists[pos][idx])) return false;
lists[pos].erase(lists[pos].begin() + idx), _size--;
size_t n = lists[pos].size();
if (n > (_load >> 1)) {
maxes[pos] = lists[pos].back();
if (index) {
size_t n = lists.size();
for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]--;
}
return true;
}
if (lists.size() > 1) {
if (pos == 0) pos++;
merge(pos), split(pos - 1);
return true;
}
if (n > 0) maxes[pos] = lists[pos].back();
else lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos), index = nullptr;
return true;
}
SET_TEMP void SET_BASE::merge(size_t pos) noexcept {
lists[pos - 1].insert(lists[pos - 1].end(), lists[pos].begin(), lists[pos].end());
maxes[pos - 1] = lists[pos - 1].back();
lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos), index = nullptr;
}
SET_TEMP T SET_BASE::kth(size_t k) {
if (k >= _size) throw std::out_of_range("index out of range");
auto [a, b] = pos(k); return lists[a][b];
}
SET_TEMP T SET_BASE::operator[] (size_t k) {return kth(k);}
SET_TEMP T SET_BASE::front() const noexcept {return lists[0][0];}
SET_TEMP T SET_BASE::back() const noexcept {return maxes.back();}
SET_TEMP size_t SET_BASE::lower_bound(const T& val) noexcept {
if (maxes.empty()) return 0;
size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) return _size;
return loc(pos, std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin());
}
SET_TEMP size_t SET_BASE::upper_bound(const T& val) noexcept {
if (maxes.empty()) return 0;
size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) return _size;
return loc(pos, std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin());
}
SET_TEMP bool SET_BASE::contains(const T& val) noexcept {
if (maxes.empty()) return false;
size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) return false;
size_t idx = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp) - lists[pos].begin();
return !cmp(val, lists[pos][idx]) && !cmp(lists[pos][idx], val);
}
SET_TEMP size_t SET_BASE::count(const T& val) noexcept {
if (Diff) return contains(val);
if (maxes.empty()) return 0;
size_t l = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (l == maxes.size()) return 0;
size_t r = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
size_t x = std::lower_bound(lists[l].begin(), lists[l].end(), val, cmp) - lists[l].begin();
if (r == maxes.size()) return _size - loc(l, x);
size_t y = std::upper_bound(lists[r].begin(), lists[r].end(), val, cmp) - lists[r].begin();
if (l == r) return y - x;
return loc(r, y) - loc(l, x);
}
SET_TEMP T SET_BASE::precursor(const T& val) {
if (cmp(maxes.back(), val)) return maxes.back();
size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) throw std::out_of_range("precursor not found");
auto it = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
if (it != lists[pos].begin()) return *std::prev(it);
if (pos == 0) throw std::out_of_range("precursor not found");
return maxes[pos - 1];
}
SET_TEMP T SET_BASE::successor(const T& val){
size_t pos = std::upper_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
if (pos == maxes.size()) throw std::out_of_range("successor not found");
auto it = std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
if (it == lists[pos].end()) throw std::out_of_range("successor not found");
return *it;
}
SET_TEMP void SET_BASE::swap(SET_BASE& o) noexcept {
std::swap(_size, o._size), std::swap(index, o.index);
lists.swap(o.lists), maxes.swap(o.maxes);
}
SET_TEMP struct SET_BASE::iterator {
private:
friend SET_BASE;
const SET_BASE* set;
size_t pos, idx;
iterator(const SET_BASE* st, size_t a, size_t b) : set(st), pos(a), idx(b) {}
public:
iterator() = delete;
T operator* () const {return set->lists[pos][idx];}
iterator operator++ () {
if (++idx == set->lists[pos].size()) pos++, idx = 0;
return *this;
}
iterator operator-- () {
if (--idx == -1) pos--, idx = set->lists[pos].size() - 1;
return *this;
}
iterator operator++ (int) const {iterator tmp = *this; return ++(*this), tmp;}
iterator operator-- (int) const {iterator tmp = *this; return --(*this), tmp;}
bool check() const {return pos < set->lists.size() && idx < set->lists[pos].size();}
size_t index() const {return set->loc(pos, idx);}
bool operator== (const iterator& o) const {return pos == o.pos && idx == o.idx;}
bool operator!= (const iterator& o) const {return pos != o.pos || idx != o.idx;}
};
SET_TEMP typename SET_BASE::iterator SET_BASE::begin() const noexcept
{return iterator(this, 0, 0);}
SET_TEMP typename SET_BASE::iterator SET_BASE::end() const noexcept
{return iterator(this, lists.size(), 0);}
SET_TEMP typename SET_BASE::iterator SET_BASE::find(size_t k)
{auto [a, b] = pos(k); return iterator(this, a, b);}
#undef SET_TEMP
#undef SET_BASE
template <class T, class Cmp = std::less<T>>
using spn_set = _spn_set_base<T, Cmp, true>;
template <class T, class Cmp = std::less<T>>
using spn_multiset = _spn_set_base<T, Cmp, false>;
#endif //SPN_SET_H