正文
增加 529 行删除 697 行
12343 行未改动89101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794创作原因(2025/8/26 11:34):> @[\_Kagamine\_Rin\_](https://www.luogu.com.cn/user/260985) : 会慢得跟屎一样,最关键的是我要封装 || @[AKPC](https://www.luogu.com.cn/user/540363) : 用数组手写双端队列 || @[\_Kagamine\_Rin\_](https://www.luogu.com.cn/user/260985) : vector 在首尾添加很慢,deque 随机访问很慢,那能不能把这两个 STL 结合起来??我认为这个容器需要达到下列要求:3 行未改动3. `sizeof(veque) <= 40`4. 代码长度 $\le 10240 \texttt{ Bytes}$5. 最低能够在 `C++11 (GCC 7)` 的编译条件下运行### 当前版本: 0.9.0-beta该版本已经达成了前三个目标。3. `sizeof(veque) == 32`4. 代码长度有 29k,未达成5. 目前至少需要 `C++17 (GCC 7)`,未达成注意:由于我把 `iterator` 全部换成了 `iter`,所以迭代器请使用 `veque::veque<int>::iter a = veq.begin();````cpp#ifndef VEQUE_HPP#define VEQUE_HPP#include <algorithm>#include <cstddef>#include <cstring>#include <iterator>#include <limits>#include <ratio>#include <string>#include <type_traits>#include <stdexcept>#include <utility>#define if_constexpr if constexprnamespace veque {template<class T>inline const T& _clamp(const T& v, const T& lo, const T& hi) { return (v < lo) ? lo : (hi < v ? hi : v); }struct frt {using abf = std::ratio<1>;using aab = std::ratio<1>;static constexpr auto rfcs = true;};struct vcrt {using abf = std::ratio<1>;using aab = std::ratio<1>;static constexpr auto rfcs = false;};struct svt {using abf = std::ratio<0>;using aab = std::ratio<1>;static constexpr auto rfcs = false;};struct nrt {using abf = std::ratio<0>;using aab = std::ratio<0>;static constexpr auto rfcs = false;};template< typename T, typename RT = frt, typename Allocator = std::allocator<T> >class veque {public:using allocator_T = Allocator;using alloc_traits = std::allocator_traits<allocator_T>;using value_T = T;using ref = T &;using const_ref = const T &;using ptr = T *;using const_ptr = const T *;using iter = T *;using citer = const T *;using reverse_iter = std::reverse_iterator<iter>;using const_reverse_iter = std::reverse_iterator<citer>;using difference_T = std::ptrdiff_t;using size_T = std::size_t;using ssize_T = std::ptrdiff_t;// Common member functionsveque() noexcept(noexcept(Allocator())) : veque(Allocator()) {}explicit veque(const Allocator& alloc) noexcept : _data { 0, alloc } {}explicit veque(size_T n, const Allocator& alloc = Allocator()): veque(_aut{}, n, alloc){ _value_construct_range(begin(), end()); }veque(size_T n, const T &value, const Allocator& alloc = Allocator()): veque(_aut{}, n, alloc){ _value_construct_range(begin(), end(), value); }template< typename InputIt, typename ItCat = typename std::iterator_traits<InputIt>::iter_category >veque(InputIt b, InputIt e, const Allocator& alloc = Allocator()): veque(b, e, alloc, ItCat{}){ }veque(std::initializer_list<T> lst, const Allocator& alloc = Allocator()): veque(_aut{}, lst.size(), alloc){ _copy_construct_range(lst.begin(), lst.end(), begin()); }veque(const veque & rhs): veque(_aut{}, rhs.size(), alloc_traits::select_on_container_copy_construction(rhs._allocator())){ _copy_construct_range(rhs.begin(), rhs.end(), begin()); }template< typename rhsRT >veque(const veque<T,rhsRT,Allocator> & rhs): veque(_aut{}, rhs.size(), alloc_traits::select_on_container_copy_construction(rhs._allocator())){ _copy_construct_range(rhs.begin(), rhs.end(), begin()); }template< typename rhsRT >veque(const veque<T,rhsRT,Allocator> & rhs, const Allocator & alloc): veque(_aut{}, rhs.size(), alloc){ _copy_construct_range(rhs.begin(), rhs.end(), begin()); }veque(veque && rhs) noexcept{ _swap_with_allocator(std::move(rhs)); }template< typename rhsRT >veque(veque<T,rhsRT,Allocator> && rhs) noexcept{ _swap_with_allocator(std::move(rhs)); }template< typename rhsRT >veque(veque<T,rhsRT,Allocator> && rhs, const Allocator & alloc) noexcept: veque(alloc){if_constexpr (!alloc_traits::is_always_equal::value)if (alloc != rhs._allocator()) {// Incompatible allocators. Allocate new storage.auto replacement = veque(_aut{}, rhs.size(), alloc);_nothrow_move_construct_range(rhs.begin(), rhs.end(), replacement.begin());_swap_without_allocator(std::move(replacement));return;}_swap_without_allocator(std::move(rhs));}~veque() { _destroy(begin(), end()); }veque & operator=(const veque & rhs) { return _copy_assignment(rhs); }template< typename rhsRT >veque & operator=(const veque<T,rhsRT,Allocator> & rhs){ return _copy_assignment(rhs); }veque & operator=(veque && rhs) noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value || alloc_traits::is_always_equal::value)){ return _move_assignment(std::move(rhs)); }template< typename rhsRT >veque & operator=(veque<T,rhsRT,Allocator> && rhs) noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value || alloc_traits::is_always_equal::value)){ return _move_assignment(std::move(rhs)); }veque & operator=(std::initializer_list<T> lst){ _assign(lst.begin(), lst.end()); return *this; }void assign(size_T count, const T &value) {if (count > capacity_full()) _swap_without_allocator(veque(count, value, _allocator()));else _reassign_existing_storage(count, value);}template< typename InputIt, typename ItCat = typename std::iterator_traits<InputIt>::iter_category >void assign(InputIt b, InputIt e){ _assign(b, e, ItCat{}); }void assign(std::initializer_list<T> lst){ _assign(lst.begin(), lst.end()); }allocator_T get_allocator() const { return _allocator(); }// Element accessref at(size_T idx) { return (*this)[idx]; }const_ref at(size_T idx) const { return (*this)[idx]; }ref operator[](size_T idx) { return *(begin() + idx); }const_ref operator[](size_T idx) const { return *(begin() + idx); }ref front() { return (*this)[0]; }const_ref front() const { return (*this)[0]; }ref back() { return (*this)[size() - 1];}const_ref back() const { return (*this)[size() - 1]; }T * data() noexcept { return begin(); }const T * data() const noexcept { return begin(); }// Iteratorsciter cbegin() const noexcept { return _storage_begin() + _offset; }iter begin() noexcept { return _storage_begin() + _offset; }citer begin() const noexcept { return cbegin(); }citer cend() const noexcept { return _storage_begin() + _offset + size(); }iter end() noexcept { return _storage_begin() + _offset + size(); }citer end() const noexcept { return cend(); }const_reverse_iter crbegin() const noexcept { return const_reverse_iter(cend()); }reverse_iter rbegin() noexcept { return reverse_iter(end()); }const_reverse_iter rbegin() const noexcept { return crbegin(); }const_reverse_iter crend() const noexcept { return const_reverse_iter(cbegin()); }reverse_iter rend() noexcept { return reverse_iter(begin()); }const_reverse_iter rend() const noexcept{ return crend(); }// Capacity[[nodiscard]] bool empty() const noexcept { return size() == 0; }size_T size() const noexcept { return _size; }ssize_T ssize() const noexcept { return _size; }size_T max_size() const noexcept {constexpr auto compile_time_limit = std::min(std::numeric_limits<ssize_T>::max() / sizeof(T),std::numeric_limits<size_T>::max() / _fur::num);// The allocator's ceilingauto runtime_limit = alloc_traits::max_size(_allocator());return std::min(compile_time_limit, runtime_limit);}// Reserve front and back capacity, in one operation.void reserve(size_T front, size_T back) {if (front > capacity_front() || back > capacity_back()) {auto allocated_before_begin = std::max(capacity_front(), front) - size();auto allocated_after_begin = std::max(capacity_back(), back);auto new_full_capacity = allocated_before_begin + allocated_after_begin;_reallocate(new_full_capacity, allocated_before_begin);}}void reserve_front(size_T count) { reserve(count, 0); }void reserve_back(size_T count) { reserve(0, count); }void reserve(size_T count) { reserve(count, count); }size_T capacity_front() const noexcept { return _offset + size(); }size_T capacity_back() const noexcept { return capacity_full() - _offset; }size_T capacity_full() const noexcept { return _data._allocated; }size_T capacity() const noexcept { return capacity_back(); }void shrink_to_fit() { if (size() < capacity_full()) _reallocate(size(), 0); }// Modifiersvoid clear() noexcept {_destroy(begin(), end());_size = 0;_offset = 0;if_constexpr (std::ratio_greater<_ur, std::ratio<0>>::value) {using unused_front_ratio = std::ratio_divide<_fr,_ur>;_offset = capacity_full() * unused_front_ratio::num / unused_front_ratio::den;}}iter insert(citer it, const T & value) { return emplace(it, value); }iter insert(citer it, T && value) { return emplace(it, std::move(value)); }iter insert(citer it, size_T count, const T & value) {auto res = _insert_storage(it, count);_value_construct_range(res, res + count, value);return res;}template< typename InputIt, typename ItCat = typename std::iterator_traits<InputIt>::iter_category >iter insert(citer it, InputIt b, InputIt e) { return _insert(it, b, e, ItCat{}); }iter insert(citer it, std::initializer_list<T> lst) { return insert(it, lst.begin(), lst.end()); }template< typename ...Args >iter emplace(citer it, Args && ... args) {auto res = _insert_storage(it, 1);alloc_traits::construct(_allocator(), res, std::forward<Args>(args)...);return res;}iter erase(citer it) { return erase(it, std::next(it)); }iter erase(citer b, citer e) {auto count = std::distance(b, e);if_constexpr (_rfcs) {auto elements_before = std::distance(cbegin(), b);auto elements_after = std::distance(e, cend());if ( elements_before < elements_after) {_shift_back(begin(), b, count);_move_begin(count);return _mutable_iter(e);}}_shift_front(e, end(), count);_move_end(-count);return _mutable_iter(b);}void push_back(const T & value) { emplace_back(value); }void push_back(T && value) { emplace_back(std::move(value)); }void push_front(const T & value) { emplace_front(value); }void push_front(T && value) { emplace_front(std::move(value)); }template< typename ... Args>ref emplace_back(Args && ...args) {if (size() == capacity_back()) _reallocate_space_at_back(size() + 1);alloc_traits::construct(_allocator(), end(), std::forward<Args>(args)...);_move_end(1);return back();}template< typename ... Args>ref emplace_front(Args && ...args) {if (size() == capacity_front()) _reallocate_space_at_front(size() + 1);alloc_traits::construct(_allocator(), begin()-1, std::forward<Args>(args)...);_move_begin(-1);return front();}void pop_back() { alloc_traits::destroy(_allocator(), &back()); _move_end(-1); }void pop_front() { alloc_traits::destroy(_allocator(), &front()); _move_begin(1); }T pop_back_element() { auto res(_nothrow_construct_move(back())); pop_back(); return res; }T pop_front_element() { auto res(_nothrow_construct_move(front())); pop_front(); return res; }void resize_front(size_T count) { _resize_front(count); }void resize_front(size_T count, const T & value) { _resize_front(count, value); }void resize_back(size_T count) { _resize_back(count); }void resize_back(size_T count, const T & value) { _resize_back(count, value); }void resize(size_T count) { _resize_back(count); }void resize(size_T count, const T & value) { _resize_back(count, value); }template< typename rhsRT >void swap(veque<T,rhsRT,Allocator> & rhs) noexcept(noexcept(alloc_traits::propagate_on_container_swap::value || alloc_traits::is_always_equal::value)){if_constexpr (alloc_traits::propagate_on_container_swap::value) _swap_with_allocator(std::move(rhs));else {if (_allocator() == rhs._allocator()) _swap_without_allocator(std::move(rhs));else {auto new_this = veque(_aut{}, rhs.size(), _allocator());_nothrow_move_construct_range(rhs.begin(), rhs.end(), new_this.begin());auto new_rhs = veque(_aut{}, size(), rhs._allocator());_nothrow_move_construct_range(begin(), end(), new_rhs.begin());_swap_without_allocator(std::move(new_this));rhs._swap_without_allocator(std::move(new_rhs));}}}private:using _fr = typename RT::abf::type;using _br = typename RT::aab::type;using _ur = std::ratio_add< _fr, _br >;using _fur = std::ratio_add< std::ratio<1>, _ur >;static constexpr auto _rfcs = RT::rfcs;static_assert(_fr::den > 0 );static_assert(_br::den > 0 );static_assert(std::ratio_greater_equal<_fr,std::ratio<0>>::value, "Reserving negative space is not well-defined");static_assert(std::ratio_greater_equal<_br,std::ratio<0>>::value, "Reserving negative space is not well-defined");static_assert(std::ratio_greater_equal<_ur,std::ratio<0>>::value, "Reserving negative space is not well-defined");static constexpr auto _cdcd = std::is_same<allocator_T,std::allocator<T>>::value;static constexpr auto _cccd = std::is_same<allocator_T,std::allocator<T>>::value;static constexpr auto _cdd =std::is_same<allocator_T,std::allocator<T>>::value;size_T _size = 0;size_T _offset = 0;struct Data : Allocator {T *_storage = nullptr;size_T _allocated = 0;Data() = default;Data(size_T size, const Allocator & alloc): Allocator{alloc}, _storage{size ? std::allocator_traits<Allocator>::allocate(allocator(), size) : nullptr}, _allocated{size}{ }Data(const Data&) = delete;Data(Data && rhs){ *this = std::move(rhs); }~Data() { if (_storage) std::allocator_traits<Allocator>::deallocate(allocator(), _storage, _allocated); }Data& operator=(const Data &) = delete;Data& operator=(Data && rhs) {using std::swap;if_constexpr(! std::is_empty<Allocator>::value) swap(allocator(), rhs.allocator());swap(_allocated, rhs._allocated);swap(_storage, rhs._storage);return *this;}Allocator& allocator() { return *this; }const Allocator& allocator() const { return *this; }} _data;template< typename InputIt >veque(InputIt b, InputIt e, const Allocator & alloc, std::input_iterator_tag): veque{alloc}{ for (; b != e; ++b) push_back(*b); }template< typename InputIt >veque(InputIt b, InputIt e, const Allocator & alloc, std::forward_iterator_tag): veque(_aut{}, std::distance(b, e), alloc){ _copy_construct_range(b, e, begin()); }struct _aut {};struct _rut {};veque(_aut, size_T size, size_T allocated, size_T offset, const Allocator & alloc): _size{ size } , _offset{ offset } , _data { allocated, alloc }{ }veque(_aut, size_T size, const Allocator & alloc): veque(_aut{}, size, size, 0, alloc){ }veque(_rut, size_T size, const Allocator & alloc): veque(_aut{}, size, _calc_reallocation(size), _calc_offset(size), alloc){ }static constexpr size_T _calc_reallocation(size_T size) { return size * _fur::num / _fur::den; }static constexpr size_T _calc_offset(size_T size) { return size * _fr::num / _fr::den; }// Acquire AllocatorAllocator& _allocator() noexcept { return _data.allocator(); }const Allocator& _allocator() const noexcept { return _data.allocator(); }void _destroy(citer b, citer e) {if_constexpr (std::is_trivially_destructible<T>::value && _cdd) {(void)b; (void)e; // Unused}else {auto start = _mutable_iter(b);for (auto i = start; i != e; ++i) alloc_traits::destroy(_allocator(), i);}}template< typename rhsRT >veque & _copy_assignment(const veque<T,rhsRT,Allocator> & rhs) {if_constexpr (alloc_traits::propagate_on_container_copy_assignment::value)if_constexpr (!alloc_traits::is_always_equal::value)if (rhs._allocator() != _allocator() || rhs.size() > capacity_full()) {_swap_with_allocator(veque(rhs, rhs._allocator()));return *this;}if (rhs.size() > capacity_full()) _swap_without_allocator(veque(rhs, _allocator()));else _reassign_existing_storage(rhs.begin(), rhs.end());return *this;}template< typename rhsRT >veque & _move_assignment(veque<T,rhsRT,Allocator> && rhs) noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value || alloc_traits::is_always_equal::value)){if_constexpr (!alloc_traits::is_always_equal::value)if (_allocator() != rhs._allocator()) {if_constexpr (alloc_traits::propagate_on_container_move_assignment::value)_swap_with_allocator(std::move(rhs));else {if (rhs.size() > capacity_full()) _swap_without_allocator(veque(std::move(rhs), _allocator()));else _reassign_existing_storage(std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));}return *this;}_swap_without_allocator(std::move(rhs));return *this;}template< typename ...Args >void _value_construct_range(citer b, citer e, const Args & ...args) {static_assert(sizeof...(args) <= 1, "This is for default- or copy-constructing");if_constexpr (std::is_trivially_copy_constructible<T>::value && _cdcd) {if_constexpr (sizeof...(args) == 0) std::memset(_mutable_iter(b), 0, std::distance(b, e) * sizeof(T));else std::fill(_mutable_iter(b), _mutable_iter(e), args...);} elsefor (auto dest = _mutable_iter(b); dest != e; ++dest) alloc_traits::construct(_allocator(), dest, args...);}template< typename It >void _copy_construct_range(It b, It e, citer dest) {static_assert(std::is_convertible<typename std::iterator_traits<It>::iter_category,std::forward_iterator_tag>::value);if_constexpr (std::is_trivially_copy_constructible<T>::value && _cccd) std::memcpy(_mutable_iter(dest), b, std::distance(b, e) * sizeof(T));else for (; b != e; ++dest, ++b) alloc_traits::construct(_allocator(), dest, *b);}template< typename It >void _assign(It b, It e) {static_assert(std::is_convertible<typename std::iterator_traits<It>::iter_category,std::forward_iterator_tag>::value);if (std::distance(b, e) > static_cast<difference_T>(capacity_full())) _swap_without_allocator(veque(b, e, _allocator()));else _reassign_existing_storage(b, e);}template< typename It >void _assign(It b, It e, std::forward_iterator_tag) { _assign(b, e); }template< typename It >void _assign(It b, It e, std::input_iterator_tag) {clear();for (; b != e; ++b) push_back(*b);}template< typename It >iter _insert(citer it, It b, It e) {static_assert(std::is_convertible<typename std::iterator_traits<It>::iter_category,std::forward_iterator_tag>::value);auto res = _insert_storage(it, std::distance(b, e));_copy_construct_range(b, e, res);return res;}template< typename It >iter _insert(citer it, It b, It e, std::forward_iterator_tag) { return _insert(it, b, e); }template< typename It >iter _insert(citer it, It b, It e, std::input_iterator_tag) {auto allocated = veque(b, e);_insert(it, allocated.begin(), allocated.end());}template< typename rhsRT >void _swap_with_allocator(veque<T,rhsRT,Allocator> && rhs) noexcept {std::swap(_size, rhs._size);std::swap(_offset, rhs._offset);std::swap(_data, rhs._data);}template< typename rhsRT >void _swap_without_allocator(veque<T,rhsRT,Allocator> && rhs) noexcept {std::swap(_size, rhs._size);std::swap(_offset, rhs._offset);std::swap(_data._allocated, rhs._data._allocated);std::swap(_data._storage, rhs._data._storage);}template< typename ...Args >void _resize_front(size_T count, const Args & ...args) {difference_T delta = count - size();if (delta > 0) {if (count > capacity_front()) _reallocate_space_at_front(count);_value_construct_range(begin() - delta, begin(), args...);}else _destroy(begin(), begin() - delta);_move_begin(-delta);}template< typename ...Args >void _resize_back(size_T count, const Args & ...args) {difference_T delta = count - size();if (delta > 0) {if (count > capacity_back()) _reallocate_space_at_back(count);_value_construct_range(end(), end() + delta, args...);}else _destroy(end() + delta, end());_move_end(delta);}void _reallocate_space_at_back(size_T count) {auto storage_needed = _calc_reallocation(count);auto current_capacity = capacity_full();auto new_offset = _calc_offset(count);if (storage_needed <= current_capacity) {auto distance = _offset - new_offset;_shift_front(begin(), end(), distance );_move_begin(-distance);_move_end(-distance);} else _reallocate(storage_needed, new_offset);}void _reallocate_space_at_front(size_T count) {auto storage_needed = _calc_reallocation(count);auto current_capacity = capacity_full();auto new_offset = count - size() + _calc_offset(count);if (storage_needed <= current_capacity) {auto distance = new_offset - _offset;_shift_back(begin(), end(), distance);_move_begin(distance);_move_end(distance);} else _reallocate(storage_needed, new_offset);}void _reallocate(size_T allocated, size_T offset) {auto replacement = veque(_aut{}, size(), allocated, offset, _allocator());_nothrow_move_construct_range(begin(), end(), replacement.begin());_swap_without_allocator(std::move(replacement));}iter _insert_storage(citer it, size_T count) {auto required_size = size() + count;auto can_shift_back = capacity_back() >= required_size;if_constexpr (std::ratio_greater<_fr,std::ratio<0>>::value)if (can_shift_back && it == begin())can_shift_back = false;if_constexpr (_rfcs) {auto can_shift_front = capacity_front() >= required_size;if_constexpr (std::ratio_greater<_br,std::ratio<0>>::value)if (can_shift_front && it == end())can_shift_front = false;if (can_shift_back && can_shift_front) {auto index = std::distance(cbegin(), it);if (index <= ssize() / 2) can_shift_back = false;else can_shift_front = false;}if (can_shift_front) {_shift_front(begin(), it, count);_move_begin(-count);return _mutable_iter(it) - count;}}if (can_shift_back) {_shift_back(it, end(), count);_move_end(count);return _mutable_iter(it);}auto replacement = veque(_rut{}, required_size, _allocator());auto index = std::distance(cbegin(), it);auto insertion_point = begin() + index;_nothrow_move_construct_range(begin(), insertion_point, replacement.begin());_nothrow_move_construct_range(insertion_point, end(), replacement.begin() + index + count);_swap_with_allocator(std::move(replacement));return begin() + index;}void _shift_front(citer b, citer e, size_T count) {if (e == begin()) return;auto element_count = std::distance(b, e);auto start = _mutable_iter(b);if (element_count > 0) {auto dest = start - count;if_constexpr (std::is_trivially_copyable<T>::value && std::is_trivially_copy_constructible<T>::value && _cccd)std::memmove(dest, start, element_count * sizeof(T));else {auto src = start;auto dest_construct_end = std::min(begin(), _mutable_iter(e) - count);for (; dest < dest_construct_end; ++src, ++dest) _nothrow_move_construct(dest, src);for (; src != e; ++src, ++dest) _nothrow_move_assign(dest, src);}}_destroy(std::max(cbegin(), e - count), e);}void _shift_back(citer b, citer e, size_T count) {auto start = _mutable_iter(b);if (b == end()) return;auto element_count = std::distance(b, e);if (element_count > 0) {if_constexpr (std::is_trivially_copyable<T>::value && std::is_trivially_copy_constructible<T>::value && _cccd)std::memmove(start + count, start, element_count * sizeof(T));else {auto src = _mutable_iter(e-1);auto dest = src + count;auto dest_construct_end = std::max(end()-1, dest - element_count);for (; dest > dest_construct_end; --src, --dest) _nothrow_move_construct(dest, src);for (; src != b-1; --src, --dest) _nothrow_move_assign(dest, src);}}_destroy(b, std::min(cend(), b + count));}template< typename It >void _reassign_existing_storage(It b, It e) {static_assert(std::is_convertible<typename std::iterator_traits<It>::iter_category,std::forward_iterator_tag>::value);auto count = std::distance(b, e);auto size_delta = static_cast<difference_T>(count - size());auto ideal_begin = _storage_begin() + (capacity_full() - count) / 2;if (size() == 0)_copy_construct_range(b, e, ideal_begin);else if (size_delta == 0) {std::copy(b, e, begin());return;} else if (size_delta < 0) {ideal_begin = _clamp(ideal_begin, begin(), end() - count);_destroy(begin(), ideal_begin);auto ideal_end = std::copy(b, e, ideal_begin);_destroy(ideal_end, end());}else {ideal_begin = _clamp(ideal_begin, end() - count, begin());auto src = b;auto copy_src = src + std::distance(ideal_begin, begin());_copy_construct_range(src, copy_src, ideal_begin);std::copy(copy_src, copy_src + ssize(), begin());_copy_construct_range(copy_src + ssize(), e, end());}_move_begin(std::distance(begin(), ideal_begin));_move_end(std::distance(end(), ideal_begin + count));}void _reassign_existing_storage(size_T count, const T & value) {auto size_delta = static_cast<difference_T>(count - size());auto ideal_begin = _storage_begin();if_constexpr (std::ratio_greater<_ur, std::ratio<0>>::value) {using ideal_begin_ratio = std::ratio_divide<_fr, _ur >;ideal_begin += (capacity_full() - count) * ideal_begin_ratio::num / ideal_begin_ratio::den;}if (size() == 0)_value_construct_range(ideal_begin, ideal_begin + count, value);else if (size_delta == 0) {std::fill(begin(), end(), value);return;} else if (size_delta < 0) {ideal_begin = _clamp(ideal_begin, begin(), end() - count);_destroy(begin(), ideal_begin);std::fill(ideal_begin, ideal_begin + count, value);_destroy(ideal_begin + count, end());} else {ideal_begin += _calc_offset(capacity_full() - count) / 2;_value_construct_range(ideal_begin, begin(), value);std::fill(begin(), end(), value);_value_construct_range(end(), ideal_begin + count, value);}_move_begin(std::distance(begin(), ideal_begin));_move_end(std::distance(end(), ideal_begin + count));}static decltype(auto) _nothrow_construct_move_impl(T & t, std::true_type) { return std::move(t); }static decltype(auto) _nothrow_construct_move_impl(T & t, std::false_type) { return t; }static decltype(auto) _nothrow_construct_move(T & t) { return _nothrow_construct_move_impl(t, std::is_nothrow_move_constructible<T>{}); }void _nothrow_move_construct(iter dest, iter src) {if_constexpr (std::is_trivially_copy_constructible<T>::value && _cccd) *dest = *src;else alloc_traits::construct(_allocator(), dest, _nothrow_construct_move(*src));}void _nothrow_move_construct_range(iter b, iter e, iter dest) {auto size = std::distance(b, e);if (size) {if_constexpr (std::is_trivially_copy_constructible<T>::value && _cccd)std::memcpy(dest, b, size * sizeof(T));elsefor (; b != e; ++dest, ++b) _nothrow_move_construct(dest, b);}}static void _nothrow_move_assign(iter dest, iter src) {if_constexpr (std::is_nothrow_move_assignable<T>::value) *dest = std::move(*src);else *dest = *src;}static void _nothrow_move_assign_range(iter b, iter e, iter src){ for (auto dest = b; dest != e; ++dest, ++src) _nothrow_move_assign(dest, src); }void _move_begin(difference_T count) noexcept { _size -= count; _offset += count; }void _move_end(difference_T count) noexcept { _size += count; }iter _mutable_iter(citer i) { return begin() + std::distance(cbegin(), i); }citer _storage_begin() const noexcept { return _data._storage; }iter _storage_begin() noexcept { return _data._storage; }};#define TPIB template< typename T, typename LRT, typename LAlloc, typename RRT, typename RA >TPIB operator==(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); }TPIB operator!=(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return !(lhs == rhs); }TPIB operator<(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); }TPIB operator<=(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return !(rhs < lhs); }TPIB operator>(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return (rhs < lhs); }TPIB operator>=(const veque<T,LRT,LAlloc> &lhs, const veque<T,RRT,RA> &rhs) { return !(lhs < rhs); }template< typename T, typename RT, typename Alloc >inline void swap(veque<T,RT,Alloc> & lhs, veque<T,RT,Alloc> & rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); }#undef TPIB}namespace std {template< typename T, typename RT, typename Alloc >struct hash<veque::veque<T,RT,Alloc>> {size_t operator()(const veque::veque<T,RT,Alloc> & v) const {size_t hash = 0;auto hasher = std::hash<T>();for (auto && val : v)hash ^= hasher(val) + 0x9e3779b9 + (hash<<6) + (hash>>2);return hash;}};}#endif#include <vector>#include <deque>#include <iostream>using namespace std;veque::veque<int> a;vector<int> b;deque<int> c;#define test(...) {clock_t _t = clock(); __VA_ARGS__ cout << (clock() - _t) << "\n"; }int main() {test( for (int i = 1; i <= 100000000; ++ i) a.emplace_back(i); )test( for (int i = 1; i <= 100000000; ++ i) b.emplace_back(i); )test( for (int i = 1; i <= 100000000; ++ i) c.emplace_back(i); )}```Note:这个版本已经让一些变量名简化,但仍然有 29k。欢迎查错,可在评论区或私信提出。1234563 行未改动101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626考虑到审核的原因,该文章不一定会实时更新。建议前往 [luogu.me](https://www.luogu.me/article/irm6y17k) 观看。创作原因(2025/8/26 11:34):> @[\_Kagamine\_Rin\_](https://www.luogu.com.cn/user/260985) : 会慢得跟屎一样,最关键的是我要封装 || @[AKPC](https://www.luogu.com.cn/user/540363) : 用数组手写双端队列 || @[\_Kagamine\_Rin\_](https://www.luogu.com.cn/user/260985) : vector 在首尾添加很慢,deque 随机访问很慢,那能不能把这两个 STL 结合起来??我认为这个容器需要达到下列要求:3 行未改动3. `sizeof(veque) <= 40`4. 代码长度 $\le 10240 \texttt{ Bytes}$5. 最低能够在 `C++11 (GCC 7)` 的编译条件下运行[历史版本 0.9.0-beta](https://www.luogu.me/paste/mayt4zm2)### 当前版本 0.9.1-beta该版本已经达成了前三个目标。3. `sizeof(veque) == 32`4. 代码长度缩短至 24k,仍然未达成5. 目前至少需要 C++17 (GCC 7),未达成更新内容:1. 移除了原先与很多 STL 不兼容的情况。2. 仅对 `private` 部分的函数或变量进行缩写,不影响使用。```cpp#ifndef VEQUE_HEADER_GUARD#define VEQUE_HEADER_GUARD#include<algorithm>#include<cstddef>#include<cstring>#include<iterator>#include<limits>#include<ratio>#include<string>#include<type_traits>#include<stdexcept>#include<utility>//namespace veque{struct fast_resize_traits{using allocation_before_front=std::ratio<1>;using allocation_after_back=std::ratio<1>;static constexpr auto resize_from_closest_side=true;};struct vector_compatible_resize_traits{using allocation_before_front=std::ratio<1>;using allocation_after_back=std::ratio<1>;static constexpr auto resize_from_closest_side=false;};struct std_vector_traits{using allocation_before_front=std::ratio<0>;using allocation_after_back=std::ratio<1>;static constexpr auto resize_from_closest_side=false;};struct no_reserve_traits{using allocation_before_front=std::ratio<0>;using allocation_after_back=std::ratio<0>;static constexpr auto resize_from_closest_side=false;};template<typename T,typename RT=fast_resize_traits,typename Alc=std::allocator<T>>class veque{public:using allocator_type=Alc;using alloc_traits=std::allocator_traits<allocator_type>;using value_type=T;using reference=T&;using const_reference=const T&;using pointer=T*;using const_pointer=const T*;using iterator=T*;using const_iterator=const T*;using reverse_iterator=std::reverse_iterator<iterator>;using const_reverse_iterator=std::reverse_iterator<const_iterator>;using difference_type=std::ptrdiff_t;using size_type=std::size_t;using ssize_type=std::ptrdiff_t;veque()noexcept(noexcept(Alc())):veque(Alc()){}explicit veque(const Alc&a)noexcept:_data{0,a}{}explicit veque(size_type n,const Alc&a=Alc()):veque(_aut{},n,a){_vcr(begin(),end());}veque(size_type n,const T&value,const Alc&a=Alc()):veque(_aut{},n,a){_vcr(begin(),end(),value);}template<typename It,typename ItCat=typename std::iterator_traits<It>::iterator_category>veque(It b,It e,const Alc&a=Alc()):veque(b,e,a,ItCat{}){}veque(std::initializer_list<T>lst,const Alc&a=Alc()):veque(_aut{},lst.size(),a){_ccr(lst.begin(),lst.end(),begin());}veque(const veque&o):veque(_aut{},o.size(),alloc_traits::select_on_container_copy_construction(o._al())){_ccr(o.begin(),o.end(),begin());}template<typename ORT>veque(const veque<T,ORT,Alc>&o):veque(_aut{},o.size(),alloc_traits::select_on_container_copy_construction(o._al())){_ccr(o.begin(),o.end(),begin());}template<typename ORT>veque(const veque<T,ORT,Alc>&o,const Alc&a):veque(_aut{},o.size(),a){_ccr(o.begin(),o.end(),begin());}veque(veque&&o)noexcept{_swa(std::move(o));}template<typename ORT>veque(veque<T,ORT,Alc>&&o)noexcept{_swa(std::move(o));}template<typename ORT>veque(veque<T,ORT,Alc>&&o,const Alc&a)noexcept:veque(a){if constexpr(!alloc_traits::is_always_equal::value)if(a!=o._al()){auto _=veque(_aut{},o.size(),a);_nmcr(o.begin(),o.end(),_.begin());_swoa(std::move(_));return;}_swoa(std::move(o));}~veque(){_d(begin(),end());}veque&operator=(const veque&o){return _ca(o);}template<typename ORT>veque&operator=(const veque<T,ORT,Alc>&o){return _ca(o);}veque&operator=(veque&&o)noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value||alloc_traits::is_always_equal::value)){return _ma(std::move(o));}template<typename ORT>veque&operator=(veque<T,ORT,Alc>&&o)noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value||alloc_traits::is_always_equal::value)){return _ma(std::move(o));}veque&operator=(std::initializer_list<T>lst){_a(lst.begin(),lst.end());return*this;}void assign(size_type cnt,const T&value){if(cnt>capacity_full())_swoa(veque(cnt,value,_al()));else _res(cnt,value);}template<typename It,typename ItCat=typename std::iterator_traits<It>::iterator_category>void assign(It b,It e){_a(b,e,ItCat{});}void assign(std::initializer_list<T>lst){_a(lst.begin(),lst.end());}allocator_type get_allocator()const{return _al();}reference at(size_type idx){if(idx>=size()){throw std::out_of_range("veque<T,ResizeTraits,Alloc>::at("+std::to_string(idx)+") out of range");}return(*this)[idx];}const_reference at(size_type idx)const{if(idx>=size()){throw std::out_of_range("veque<T,ResizeTraits,Alloc>::at("+std::to_string(idx)+") out of range");}return(*this)[idx];}reference operator[](size_type idx){return*(begin()+idx);}const_reference operator[](size_type idx)const{return*(begin()+idx);}reference front(){return(*this)[0];}const_reference front()const{return(*this)[0];}reference back(){return(*this)[size()-1];}const_reference back()const{return(*this)[size()-1];}T*data()noexcept{return begin();}const T*data()const noexcept{return begin();}const_iterator cbegin()const noexcept{return _sb()+_offset;}iterator begin()noexcept{return _sb()+_offset;}const_iterator begin()const noexcept{return cbegin();}const_iterator cend()const noexcept{return _sb()+_offset+size();}iterator end()noexcept{return _sb()+_offset+size();}const_iterator end()const noexcept{return cend();}const_reverse_iterator crbegin()const noexcept{return const_reverse_iterator(cend());}reverse_iterator rbegin()noexcept{return reverse_iterator(end());}const_reverse_iterator rbegin()const noexcept{return crbegin();}const_reverse_iterator crend()const noexcept{return const_reverse_iterator(cbegin());}reverse_iterator rend()noexcept{return reverse_iterator(begin());}const_reverse_iterator rend()const noexcept{return crend();}[[nodiscard]]bool empty()const noexcept{return size()==0;}size_type size()const noexcept{return _size;}ssize_type ssize()const noexcept{return _size;}size_type max_size()const noexcept{constexpr auto compile_time_limit=std::min(std::numeric_limits<ssize_type>::max()/sizeof(T),std::numeric_limits<size_type>::max()/_full_realloc::num);auto runtime_limit=alloc_traits::max_size(_al());return std::min(compile_time_limit,runtime_limit);}void reserve(size_type front,size_type back){if(front>capacity_front() || back>capacity_back()){auto allocated_before_begin=std::max(capacity_front(),front)-size();auto allocated_after_begin=std::max(capacity_back(),back);auto new_full_capacity=allocated_before_begin+allocated_after_begin;if(new_full_capacity>max_size())throw std::length_error("veque<T,ResizeTraits,Alloc>::reserve("+std::to_string(front)+","+std::to_string(back)+") exceeds max_size()");_re(new_full_capacity,allocated_before_begin);}}void reserve_front(size_type cnt){reserve(cnt,0);}void reserve_back(size_type cnt){reserve(0,cnt);}void reserve(size_type cnt){reserve(cnt,cnt);}size_type capacity_front()const noexcept{return _offset+size();}size_type capacity_back()const noexcept{return capacity_full()-_offset;}size_type capacity_full()const noexcept{return _data._allocated;}size_type capacity()const noexcept{return capacity_back();}void shrink_to_fit(){if(size()<capacity_full())_re(size(),0);}void clear() noexcept{_d(begin(),end());_size=0;_offset=0;if constexpr(std::ratio_greater_v<_unused_realloc,std::ratio<0>>){using unused_front_ratio=std::ratio_divide<_front_realloc,_unused_realloc>;_offset=capacity_full()*unused_front_ratio::num/unused_front_ratio::den;}}iterator insert(const_iterator it,const T&value){return emplace(it,value);}iterator insert(const_iterator it,T&&value){return emplace(it,std::move(value));}iterator insert(const_iterator it,size_type cnt,const T&value){auto res=_is(it,cnt);_vcr(res,res+cnt,value);return res;}template<typename It,typename ItCat=typename std::iterator_traits<It>::iterator_category>iterator insert(const_iterator it,It b,It e){return _i(it,b,e,ItCat{});}iterator insert(const_iterator it,std::initializer_list<T>lst){return insert(it,lst.begin(),lst.end());}template<typename...Args>iterator emplace(const_iterator it,Args&&...args){auto res=_is(it,1);alloc_traits::construct(_al(),res,std::forward<Args>(args)...);return res;}iterator erase(const_iterator it){return erase(it,std::next(it));}iterator erase(const_iterator b,const_iterator e){auto cnt=std::distance(b,e);if constexpr(_rfcs){auto elements_before=std::distance(cbegin(),b);auto elements_after=std::distance(e,cend());if( elements_before<elements_after){_shb(begin(),b,cnt);_mb(cnt);return _mi(e);}}_shf(e,end(),cnt);_me(-cnt);return _mi(b);}void push_back(const T&value){emplace_back(value);}void push_back(T&&value){emplace_back(std::move(value));}template<typename...Args>reference emplace_back(Args&&...args){if(size()==capacity_back())_resab(size()+1);alloc_traits::construct(_al(),end(),std::forward<Args>(args)...);_me(1);return back();}void push_front(const T&value){emplace_front(value);}void push_front(T&&value){emplace_front(std::move(value));}template<typename...Args>reference emplace_front(Args&&...args){if(size()==capacity_front())_resaf(size()+1);alloc_traits::construct(_al(),begin()-1,std::forward<Args>(args)...);_mb(-1);return front();}void pop_back(){alloc_traits::destroy(_al(),&back());_me(-1);}T pop_back_element(){auto res(_ncm(back()));pop_back();return res;}void pop_front(){alloc_traits::destroy(_al(),&front());_mb(1);}T pop_front_element(){auto res(_ncm(front()));pop_front();return res;}void resize_front(size_type cnt){_rf(cnt);}void resize_front(size_type cnt,const T&value){_rf(cnt,value);}void resize_back(size_type cnt){_rb(cnt);}void resize_back(size_type cnt,const T&value){_rb(cnt,value);}void resize(size_type cnt){_rb(cnt);}void resize(size_type cnt,const T&value){_rb(cnt,value);}template<typename ORT>void swap(veque<T,ORT,Alc>&o)noexcept(noexcept(alloc_traits::propagate_on_container_swap::value||alloc_traits::is_always_equal::value)){if constexpr(alloc_traits::propagate_on_container_swap::value)_swa(std::move(o));else{if(_al()==o._al())_swoa(std::move(o));else{auto new_this=veque(_aut{},o.size(),_al());_nmcr(o.begin(),o.end(),new_this.begin());auto new_o=veque(_aut{},size(),o._al());_nmcr(begin(),end(),new_o.begin());_swoa(std::move(new_this));o._swoa(std::move(new_o));}}}private:using _front_realloc=typename RT::allocation_before_front::type;using _back_realloc=typename RT::allocation_after_back::type;using _unused_realloc=std::ratio_add<_front_realloc,_back_realloc>;using _full_realloc=std::ratio_add<std::ratio<1>,_unused_realloc>;static constexpr auto _rfcs=RT::resize_from_closest_side;static_assert(_front_realloc::den>0 );static_assert(_back_realloc::den>0 );static_assert(std::ratio_greater_equal_v<_front_realloc,std::ratio<0>>,"Reserving negative space is not well-defined");static_assert(std::ratio_greater_equal_v<_back_realloc,std::ratio<0>>,"Reserving negative space is not well-defined");static_assert(std::ratio_greater_equal_v<_unused_realloc,std::ratio<0>>,"Reserving negative space is not well-defined");static constexpr auto _cdcd=std::is_same_v<allocator_type,std::allocator<T>>;static constexpr auto _cccd=std::is_same_v<allocator_type,std::allocator<T>>;static constexpr auto _cdd=std::is_same_v<allocator_type,std::allocator<T>>;size_type _size=0;size_type _offset=0;struct Data:Alc{T*_storage=nullptr;size_type _allocated=0;Data()=default;Data(size_type size,const Alc&a):Alc{a},_storage{size?std::allocator_traits<Alc>::allocate(allocator(),size):nullptr},_allocated{size}{}Data(const Data&)=delete;Data(Data&&o){*this=std::move(o);}~Data(){if(_storage)std::allocator_traits<Alc>::deallocate(allocator(),_storage,_allocated);}Data&operator=(const Data&)=delete;Data&operator=(Data&&o){using std::swap;if constexpr(!std::is_empty_v<Alc>)swap(allocator(),o.allocator());swap(_allocated,o._allocated);swap(_storage,o._storage);return*this;}Alc&allocator(){return*this;}const Alc&allocator()const{return*this;}} _data;template<typename It>veque(It b,It e,const Alc&a,std::input_iterator_tag):veque{a}{for(;b!=e;++b){push_back(*b);}}template<typename It>veque(It b,It e,const Alc&a,std::forward_iterator_tag):veque(_aut{},std::distance(b,e),a){_ccr(b,e,begin());}struct _aut{};struct _rut{};veque(_aut,size_type size,size_type allocated,size_type offset,const Alc&a):_size{size},_offset{offset},_data{allocated,a}{}veque(_aut,size_type size,const Alc&a):veque(_aut{},size,size,0,a){}veque(_rut,size_type size,const Alc&a):veque(_aut{},size,_cr(size),_co(size),a){}static constexpr size_type _cr(size_type size){return size*_full_realloc::num/_full_realloc::den;}static constexpr size_type _co(size_type size){return size*_front_realloc::num/_front_realloc::den;}Alc&_al()noexcept{return _data.allocator();}const Alc&_al()const noexcept{return _data.allocator();}void _d(const_iterator b,const_iterator e){if constexpr(std::is_trivially_destructible_v<T>&&_cdd){(void)b;(void)e;}else{auto start=_mi(b);for(auto i=start;i!=e;++i)alloc_traits::destroy(_al(),i);}}template<typename ORT>veque&_ca(const veque<T,ORT,Alc>&o){if constexpr(alloc_traits::propagate_on_container_copy_assignment::value)if constexpr(!alloc_traits::is_always_equal::value)if(o._al()!=_al() || o.size()>capacity_full()){_swa(veque(o,o._al()));return *this;}if(o.size()>capacity_full())_swoa(veque(o,_al()));else _res(o.begin(),o.end());return *this;}template<typename ORT>veque&_ma(veque<T,ORT,Alc>&&o)noexcept(noexcept(alloc_traits::propagate_on_container_move_assignment::value||alloc_traits::is_always_equal::value)){if constexpr(!alloc_traits::is_always_equal::value){if(_al()!=o._al()){if constexpr(alloc_traits::propagate_on_container_move_assignment::value)_swa(std::move(o));else{if(o.size()>capacity_full())_swoa(veque(std::move(o),_al()));else _res(std::move_iterator(o.begin()),std::move_iterator(o.end()));}return *this;}}_swoa(std::move(o));return *this;}template<typename...Args>void _vcr(const_iterator b,const_iterator e,const Args&...args){static_assert(sizeof...(args)<=1,"This is for default-or copy-constructing");if constexpr(std::is_trivially_copy_constructible_v<T>&&_cdcd){if constexpr(sizeof...(args)==0)std::memset(_mi(b),0,std::distance(b,e)*sizeof(T));else std::fill(_mi(b),_mi(e),args...);}else for(auto dest=_mi(b);dest!=e;++dest) alloc_traits::construct(_al(),dest,args...);}template<typename It>void _ccr(It b,It e,const_iterator dest){static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>);if constexpr(std::is_trivially_copy_constructible_v<T>&&_cccd)std::memcpy(_mi(dest),b,std::distance(b,e)*sizeof(T));else for(;b!=e;++dest,++b)alloc_traits::construct(_al(),dest,*b);}template<typename It>void _a(It b,It e){static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>);if(std::distance(b,e)>static_cast<difference_type>(capacity_full()))_swoa(veque(b,e,_al()));else _res(b,e);}template<typename It>void _a(It b,It e,std::forward_iterator_tag){_a(b,e);}template<typename It>void _a(It b,It e,std::input_iterator_tag){clear();for(;b!=e;++b)push_back(*b);}template<typename It>iterator _i(const_iterator it,It b,It e){static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>);auto res=_is(it,std::distance(b,e));_ccr(b,e,res);return res;}template<typename It>iterator _i(const_iterator it,It b,It e,std::forward_iterator_tag){return _i(it,b,e);}template<typename It>iterator _i(const_iterator it,It b,It e,std::input_iterator_tag){auto allocated=veque(b,e);_i(it,allocated.begin(),allocated.end());}template<typename ORT>void _swa(veque<T,ORT,Alc>&&o)noexcept{std::swap(_size,o._size);std::swap(_offset,o._offset);std::swap(_data,o._data);}template<typename ORT>void _swoa(veque<T,ORT,Alc>&&o)noexcept{std::swap(_size,o._size);std::swap(_offset,o._offset);std::swap(_data._allocated,o._data._allocated);std::swap(_data._storage,o._data._storage);}template<typename...Args>void _rf(size_type cnt,const Args&...args){difference_type delta=cnt-size();if(delta>0){if(cnt>capacity_front())_resaf(cnt);_vcr(begin()-delta,begin(),args...);}else _d(begin(),begin()-delta);_mb(-delta);}template<typename...Args>void _rb(size_type cnt,const Args&...args){difference_type delta=cnt-size();if(delta>0){if(cnt>capacity_back())_resab(cnt);_vcr(end(),end()+delta,args...);}else _d(end()+delta,end());_me(delta);}void _resab(size_type cnt){auto storage_needed=_cr(cnt);auto current_capacity=capacity_full();auto new_offset=_co(cnt);if(storage_needed<=current_capacity){auto distance=_offset-new_offset;_shf(begin(),end(),distance );_mb(-distance);_me(-distance);}else _re(storage_needed,new_offset);}void _resaf(size_type cnt){auto storage_needed=_cr(cnt);auto current_capacity=capacity_full();auto new_offset=cnt-size()+_co(cnt);if(storage_needed<=current_capacity){auto distance=new_offset-_offset;_shb(begin(),end(),distance);_mb(distance);_me(distance);}else _re(storage_needed,new_offset);}void _re(size_type allocated,size_type offset){auto tmp=veque(_aut{},size(),allocated,offset,_al());_nmcr(begin(),end(),tmp.begin());_swoa(std::move(tmp));}iterator _is(const_iterator it,size_type cnt){auto required_size=size()+cnt;auto can_shift_back=capacity_back()>=required_size;if constexpr(std::ratio_greater_v<_front_realloc,std::ratio<0>>)if(can_shift_back&&it==begin())can_shift_back=false;if constexpr(_rfcs){auto can_shift_front=capacity_front()>=required_size;if constexpr(std::ratio_greater_v<_back_realloc,std::ratio<0>>)if(can_shift_front&&it==end())can_shift_front=false;if(can_shift_back&&can_shift_front){auto index=std::distance(cbegin(),it);if(index<=ssize()/2)can_shift_back=false;else can_shift_front=false;}if(can_shift_front){_shf(begin(),it,cnt);_mb(-cnt);return _mi(it)-cnt;}}if(can_shift_back){_shb(it,end(),cnt);_me(cnt);return _mi(it);}auto tmp=veque(_rut{},required_size,_al());auto index=std::distance(cbegin(),it);auto insertion_point=begin()+index;_nmcr(begin(),insertion_point,tmp.begin());_nmcr(insertion_point,end(),tmp.begin()+index+cnt);_swa(std::move(tmp));return begin()+index;}void _shf(const_iterator b,const_iterator e,size_type cnt){if(e==begin())return;auto element_cnt=std::distance(b,e);auto start=_mi(b);if(element_cnt>0){auto dest=start-cnt;if constexpr(std::is_trivially_copyable_v<T>&&std::is_trivially_copy_constructible_v<T>&&_cccd)std::memmove(dest,start,element_cnt*sizeof(T));else{auto src=start;auto dest_construct_end=std::min(begin(),_mi(e)-cnt);for(;dest<dest_construct_end;++src,++dest)_nmc(dest,src);for(;src!=e;++src,++dest)_nma(dest,src);}}_d(std::max(cbegin(),e-cnt),e);}void _shb(const_iterator b,const_iterator e,size_type cnt){auto start=_mi(b);if(b==end())return;auto element_cnt=std::distance(b,e);if(element_cnt>0){if constexpr(std::is_trivially_copyable_v<T>&&std::is_trivially_copy_constructible_v<T>&&_cccd)std::memmove(start+cnt,start,element_cnt*sizeof(T));else{auto src=_mi(e-1);auto dest=src+cnt;auto dest_construct_end=std::max(end()-1,dest-element_cnt);for(;dest>dest_construct_end;--src,--dest)_nmc(dest,src);for(;src!=b-1;--src,--dest)_nma(dest,src);}}_d(b,std::min(cend(),b+cnt));}template<typename It>void _res(It b,It e){static_assert(std::is_convertible_v<typename std::iterator_traits<It>::iterator_category,std::forward_iterator_tag>);auto cnt=std::distance(b,e);auto size_delta=static_cast<difference_type>(cnt-size());auto ib=_sb()+(capacity_full()-cnt)/2;if(size()==0)_ccr(b,e,ib);else if(size_delta==0){std::copy(b,e,begin());return;}else if(size_delta<0){ib=std::clamp(ib,begin(),end()-cnt);_d(begin(),ib);auto ideal_end=std::copy(b,e,ib);_d(ideal_end,end());}else{ib=std::clamp(ib,end()-cnt,begin());auto src=b;auto copy_src=src+std::distance(ib,begin());_ccr(src,copy_src,ib);std::copy(copy_src,copy_src+ssize(),begin());_ccr(copy_src+ssize(),e,end());}_mb(std::distance(begin(),ib));_me(std::distance(end(),ib+cnt));}void _res(size_type cnt,const T&value){auto size_delta=static_cast<difference_type>(cnt-size());auto ib=_sb();if constexpr(std::ratio_greater_v<_unused_realloc,std::ratio<0>>){using ideal_begin_ratio=std::ratio_divide<_front_realloc,_unused_realloc>;ib+=(capacity_full()-cnt)*ideal_begin_ratio::num/ideal_begin_ratio::den;}if(size()==0)_vcr(ib,ib+cnt,value);else if(size_delta==0){std::fill(begin(),end(),value);return;}else if(size_delta<0){ib=std::clamp(ib,begin(),end()-cnt);_d(begin(),ib);std::fill(ib,ib+cnt,value);_d(ib+cnt,end());}else{ib+=_co(capacity_full()-cnt)/2;_vcr(ib,begin(),value);std::fill(begin(),end(),value);_vcr(end(),ib+cnt,value);}_mb(std::distance(begin(),ib));_me(std::distance(end(),ib+cnt));}static decltype(auto)_ncm(T&t){if constexpr(std::is_nothrow_move_constructible_v<T>)return std::move(t);else return t;}void _nmc(iterator dest,iterator src){if constexpr(std::is_trivially_copy_constructible_v<T>&&_cccd)*dest=*src;else alloc_traits::construct(_al(),dest,_ncm(*src));}void _nmcr(iterator b,iterator e,iterator dest){auto size=std::distance(b,e);if(size){if constexpr(std::is_trivially_copy_constructible_v<T>&&_cccd)std::memcpy(dest,b,size*sizeof(T));else for(;b!=e;++dest,++b)_nmc(dest,b);}}static void _nma(iterator dest,iterator src){if constexpr(std::is_nothrow_move_assignable_v<T>)*dest=std::move(*src);else*dest=*src;}static void _nmar(iterator b,iterator e,iterator src){for(auto dest=b;dest!=e;++dest,++src)_nma(dest,src);}void _mb(difference_type cnt)noexcept{_size-=cnt;_offset+=cnt;}void _me(difference_type cnt)noexcept{_size+=cnt;}iterator _mi(const_iterator i){return begin()+std::distance(cbegin(),i);}const_iterator _sb()const noexcept{return _data._storage;}iterator _sb()noexcept{return _data._storage;}};#define tp template<typename T,typename LRT,typename LA,typename RRT,typename RA>#define arg const veque<T,LRT,LA>&lhs,const veque<T,RRT,RA>&rhstp inline bool operator==(arg){return std::equal(lhs.begin(),lhs.end(),rhs.begin(),rhs.end());}tp inline bool operator!=(arg){return!(lhs==rhs);}tp inline bool operator<(arg){return std::lexicographical_compare(lhs.begin(),lhs.end(),rhs.begin(),rhs.end());}tp inline bool operator<=(arg){return!(rhs<lhs);}tp inline bool operator>(arg){return(rhs<lhs);}tp inline bool operator>=(arg){return!(lhs<rhs);}#undef tp#undef argtemplate<typename T,typename RT,typename A>inline void swap(veque<T,RT,A>&lhs,veque<T,RT,A>&rhs)noexcept(noexcept(lhs.swap(rhs))){lhs.swap(rhs);}template<typename It,typename A=std::allocator<typename std::iterator_traits<It>::value_type>>veque(It,It,A=A())->veque<typename std::iterator_traits<It>::value_type,fast_resize_traits,A>;}namespace std{template<typename T,typename RT,typename A>struct hash<veque::veque<T,RT,A>>{size_t operator()(const veque::veque<T,RT,A>&v)const{size_t hash=0; auto hasher=std::hash<T>();for(auto&&val:v) hash^=hasher(val)+0x9e3779b9+(hash<<6)+(hash>>2);return hash;}};}#endifint main(){}/*被更改的变量名InputIt->It(L/R)Alloc->AAllocator->Alcalloc->a(L/R/Other)ResizeTraits->RTideal_begin->ibcount->cnt_allocate_uninitialized_tag->_aut_reallocate_uninitialized_tag->_rutreplacement->tmpOther->Oother->o_storage_begin->_sb_mutable_iterator->_mi_move_end->_me_move_begin->_mb_nothrow_move_assign_range->_nmar_nothrow_move_assign->_nma_nothrow_move_construct_range->_nmcr_nothrow_move_construct->_nmc_nothrow_construct_move->_ncm_reassign_existing_storage->_res_shift_back->_shb_shift_front->_shf_insert_storage->_is_reallocate->_re_reallocate_space_at_front->_resaf_reallocate_space_at_back->_resab_resize_back->_rb_resize_front->_rf_swap_without_allocator->_swoa_swap_with_allocator->_swa_insert->_i_assign->_a_copy_construct_range->_ccr_value_construct_range->_vcr_move_assignment->_ma_copy_assignment->_ca_destroy->_d_allocator->_al_calc_offset->_co_calc_reallocation->_cr_calls_destructor_directly->_cdd_calls_copy_constructor_directly->_cccd_calls_default_constructor_directly->_cdcd_resize_from_closest_side->_rfcs*/```