跳到正文

剪贴板

剪贴板 pnparijs

stripe_python
原文
剪贴板信息

剪贴板信息

发布
收录
更新
C++
#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 Unique>
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;
	std::vector<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 ;
	void                      erase (size_t, size_t)          ;
public:
	_spn_set_base() : cmp(), _size(0) {}
	template <class It> _spn_set_base(const It& _begin, const It& _end) 
	: cmp(), _size(0) {extend(_begin, _end);}
	
	class iterator;
	iterator begin ();
	iterator end   ();
	iterator get   (size_t);
	
	size_t size             ()                     const noexcept ;
	bool   empty            ()                     const noexcept ;
	void   reserve          (size_t)                              ;
	void   clear            ()                     noexcept       ;
	void   shrink_to_fit    ()                     noexcept       ;
	template <class It> 
	void   extend           (const It&, const It&)                ; 
	template <class It> 
	void   from_sorted      (const It&, const It&)                ;
	bool   insert           (const T&)                            ;
	bool   erase            (const T&)                            ;
	void   erase            (const iterator&)                     ;
	T      kth              (size_t)                              ;
	T      operator[]       (size_t)                              ;
	T      back             ()                     const          ;
	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&)                            ;
	T      strict_precursor (const T&)                            ;
	T      strict_successor (const T&)                            ;
	void   swap             (_spn_set_base&)                      ;
	bool   operator==       (const _spn_set_base&)                ;
	bool   operator!=       (const _spn_set_base&)                ;
};

template <class T, class Cmp, bool Unique>
class _spn_set_base<T, Cmp, Unique>::iterator {
private:
	_spn_set_base<T, Cmp, Unique> *set;
	size_t                         pos, idx;
	iterator() : set(nullptr), pos(-1), idx(-1) {}
	iterator(_spn_set_base<T, Cmp, Unique> *_set, size_t _pos, size_t _idx)
	: set(_set), pos(_pos), idx(_idx) {}
public:
	operator size_t () {return set->loc(pos, idx);}
	T operator* () {return set->lists[pos][idx];}
	iterator& operator++ () {
		if (++idx >= set->lists[pos].size()) pos++, idx = 0;
		return *this;
	}
	iterator& operator-- () {
		if (--idx == (size_t) -1) pos--, idx = set->lists[pos].size() - 1;
		return *this;
	}
	iterator operator++ (int) {return ++iterator(*this);}
	iterator operator-- (int) {return --iterator(*this);}
};

template <class T, class Cmp, bool Unique>
typename _spn_set_base<T, Cmp, Unique>::iterator _spn_set_base<T, Cmp, Unique>::begin() {
	return typename _spn_set_base<T, Cmp, Unique>::iterator(this, 0, 0);
}
template <class T, class Cmp, bool Unique>
typename _spn_set_base<T, Cmp, Unique>::iterator _spn_set_base<T, Cmp, Unique>::end() {
	return typename _spn_set_base<T, Cmp, Unique>::iterator(this, lists.size(), 0);
}
template <class T, class Cmp, bool Unique>
typename _spn_set_base<T, Cmp, Unique>::iterator _spn_set_base<T, Cmp, Unique>::get(size_t idx) {
	if (idx >= _size) throw std::out_of_range("index out of range");
	const auto& [a, b] = pos(idx);
	return iterator(this, a, b);
}

template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::build() noexcept {
	size_t n = lists.size();
	index.resize(n, 0);
	for (size_t i = 1; i < n; i++) {
		index[i] += lists[i - 1].size();
		size_t x = i + (i & -i);
		if (x < n) index[x] += index[i];
	}
}
template <class T, class Cmp, bool Unique>
std::pair<size_t, size_t> _spn_set_base<T, Cmp, Unique>::pos(size_t idx) noexcept {
	if (idx < lists[0].size()) return {0, idx};
	if (index.empty()) build();
	size_t n = index.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};
}
template <class T, class Cmp, bool Unique>
size_t _spn_set_base<T, Cmp, Unique>::loc(size_t pos, size_t idx) noexcept {
	if (pos == 0) return idx;
	if (index.empty()) build();
	while (pos > 0) idx += index[pos], pos &= pos - 1;
	return idx;
}

template <class T, class Cmp, bool Unique>
size_t _spn_set_base<T, Cmp, Unique>::size() const noexcept {return _size;}
template <class T, class Cmp, bool Unique>
bool _spn_set_base<T, Cmp, Unique>::empty() const noexcept {return maxes.empty();}
template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::clear() noexcept
{_size = 0, lists.clear(), maxes.clear(), index.clear();}
template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::shrink_to_fit() noexcept
{lists.shrink_to_fit(), maxes.shrink_to_fit(), index.shrink_to_fit();}
template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::reserve(size_t len) 
{lists.reserve(len / _load + 1), maxes.resize(len / _load + 1);}

template <class T, class Cmp, bool Unique>
bool _spn_set_base<T, Cmp, Unique>::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();
#define _check(val0) do {if (!cmp(val, val0) && !cmp(val0, val)) return false;} while (0)
	if (pos == maxes.size()) {
		if (Unique) _check(maxes.back());
		pos--, lists[pos].emplace_back(val), maxes[pos] = val;
	} else {
		auto it = std::upper_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
		if (Unique) {
			if (it != lists[pos].begin()) _check(*std::prev(it));
			else if (pos > 0) _check(lists[pos - 1].back());
		}
		lists[pos].insert(it, val);
	}
#undef _check
	_size++, split(pos);
	return true;
}
template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::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.clear();
	} else if (!index.empty()) {
		size_t n = index.size();
		for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]++;
	}
}
template <class T, class Cmp, bool Unique> template <class It>
void _spn_set_base<T, Cmp, Unique>::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());
	}
}
template <class T, class Cmp, bool Unique> template <class It>
void _spn_set_base<T, Cmp, Unique>::from_sorted(const It& _begin, const It& _end) {
	std::vector<T> a(_begin, _end);
	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());
	}
}

template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::erase(size_t pos, size_t idx) {
	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.empty()) {
			size_t n = index.size();
			for (size_t i = pos + 1; i < n; i += (i & -i)) index[i]--;
		}
	} else if (lists.size() > 1) {
		if (pos == 0) pos++;
		merge(pos), split(pos - 1);
	} else if (n > 0) {
		maxes[pos] = lists[pos].back();	
	} else {
		lists.erase(lists.begin() + pos), maxes.erase(maxes.begin() + pos), index.clear();
	}
}

template <class T, class Cmp, bool Unique>
bool _spn_set_base<T, Cmp, Unique>::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;
	erase(pos, idx);
	return true;
}
template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::erase(const iterator& it) {erase(it.pos, it.idx);}

template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::kth(size_t k) {
	if (k >= _size) throw std::out_of_range("index out of range");
	const auto& [a, b] = pos(k);
	return lists[a][b];
}

template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::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.clear();
}

template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::operator[] (size_t k) {return kth(k);}
template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::back() const {
	if (maxes.empty()) throw std::out_of_range("index out of range");
	return maxes.back();
}

template <class T, class Cmp, bool Unique>
size_t _spn_set_base<T, Cmp, Unique>::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());
}
template <class T, class Cmp, bool Unique>
size_t _spn_set_base<T, Cmp, Unique>::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());
}
template <class T, class Cmp, bool Unique>
bool _spn_set_base<T, Cmp, Unique>::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);
}
template <class T, class Cmp, bool Unique>
size_t _spn_set_base<T, Cmp, Unique>::count(const T& val) noexcept {
	if (Unique) 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);
}

template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::precursor(const T& val) {
	if (maxes.empty()) throw std::out_of_range("precursor not found");
	size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
	if (pos == maxes.size()) return maxes.back();
	auto it = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
	if (it != lists[pos].end() && !cmp(val, *it) && !cmp(*it, val)) return *it;
	if (it != lists[pos].begin()) return *std::prev(it);
	if (pos == 0) throw std::out_of_range("precursor not found");
	return maxes[pos - 1];
}
template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::successor(const T& val) {
	if (maxes.empty()) throw std::out_of_range("successor not found");
	size_t pos = std::lower_bound(maxes.begin(), maxes.end(), val, cmp) - maxes.begin();
	if (pos == maxes.size()) throw std::out_of_range("successor not found");
	auto it = std::lower_bound(lists[pos].begin(), lists[pos].end(), val, cmp);
	if (it != lists[pos].end()) return *it;
	if (pos + 1 == maxes.size()) throw std::out_of_range("successor not found");
	return lists[pos + 1][0];
}

template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::strict_precursor(const T& val) {
	if (maxes.empty()) throw std::out_of_range("precursor not found");
	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];
}
template <class T, class Cmp, bool Unique>
T _spn_set_base<T, Cmp, Unique>::strict_successor(const T& val) {
	if (maxes.empty()) throw std::out_of_range("successor not found");
	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;
}

template <class T, class Cmp, bool Unique>
void _spn_set_base<T, Cmp, Unique>::swap(_spn_set_base& other) {
	std::swap(_size, other._size);
	lists.swap(other.lists), maxes.swap(other.maxes), index.swap(other.index);
}

template <class T, class Cmp, bool Unique> 
bool _spn_set_base<T, Cmp, Unique>::operator== (const _spn_set_base& other) { 
	if (_size != other._size) return false; 
	size_t pos1 = 0, idx1 = 0, pos2 = 0, idx2 = 0; 
	while (pos1 < lists.size() && pos2 < other.lists.size()) { 
		if (!cmp(lists[pos1][idx1], lists[pos2][idx2]) && !cmp(lists[pos2][idx2], lists[pos1][idx1])) return false; 
		idx1++, idx2++; 
		if (idx1 >= lists[pos1].size()) pos1++, idx1 = 0; 
		if (idx2 >= other.lists[pos2].size()) pos2++, idx2 = 0; 
	} 
	return true; 
}
template <class T, class Cmp, bool Unique> 
bool _spn_set_base<T, Cmp, Unique>::operator!= (const _spn_set_base& other) {return !(*this == other);}

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