8#include <pixie/permutations/permutation.h>
38template <
class T = std::uint64_t,
39 class Compare = std::less<>,
40 class Index = std::uint64_t,
41 std::size_t StorageBits = 2048,
42 std::size_t Fanout = 8,
45 static_assert(std::is_object_v<T> && !std::is_array_v<T> &&
46 std::same_as<T, std::remove_cv_t<T>> &&
47 !std::same_as<T, bool>,
48 "SortedPermutationVector requires a non-bool object type");
66 : compare_(std::move(compare)) {}
88 template <std::ranges::input_range Range>
90 constructible_from<T, std::ranges::range_rvalue_reference_t<Range>>
92 Compare compare = {}) {
94 auto it = std::ranges::begin(range);
95 const auto end = std::ranges::end(range);
96 for (; it != end; ++it) {
97 result.insert(T(std::ranges::iter_move(it)));
105 bool empty() const noexcept {
return permutation_.empty(); }
115 return values_[permutation_[position]];
138 return permutation_.lower_bound(
139 [&](Index idx) {
return !compare_(values_[idx], key); });
152 return permutation_.lower_bound(
153 [&](Index idx) {
return compare_(key, values_[idx]); });
172 if (values_.size() > std::numeric_limits<Index>::max()) {
173 throw std::length_error(
"SortedPermutationVector: index domain");
175 values_.push_back(std::move(value));
177 permutation_.insert_at(position);
193 const size_type maximum = std::numeric_limits<size_type>::max();
194 const size_type payload = values_.capacity() > maximum /
sizeof(T)
196 : values_.capacity() *
sizeof(T);
198 permutation_.memory_usage_bytes() -
sizeof(permutation_);
200 payload > maximum -
sizeof(*this) ? maximum :
sizeof(*this) + payload;
201 return permutation > maximum - base ? maximum : base + permutation;
205 std::vector<T> values_;
207 [[no_unique_address]] Compare compare_;
Exclusively owned permutation of the integers in [0, size()).
Definition permutation.h:45
Duplicate-preserving sorted view over owned vector payload and order.
Definition sorted_vector.h:44
SortedPermutationVector & operator=(SortedPermutationVector &&)=default
Replace vector payload, permutation, and comparator by move.
SortedPermutationVector & operator=(const SortedPermutationVector &)=delete
Exclusive ownership disallows copy assignment.
SortedPermutationVector(SortedPermutationVector &&)=default
Move vector payload, permutation, and comparator state.
const T & const_reference
Immutable read result borrowing the owned vector.
Definition sorted_vector.h:56
Permutation< Index, StorageBits, Fanout, Layout > permutation_type
Underlying order-index type.
Definition sorted_vector.h:52
const_reference operator[](size_type position) const
Read one sorted zero-based value.
Definition sorted_vector.h:114
T value_type
Stored value type.
Definition sorted_vector.h:54
size_type insert(T value)
Insert a value before existing equivalent values.
Definition sorted_vector.h:170
size_type memory_usage_bytes() const noexcept
Return explicit requested bytes owned by this wrapper.
Definition sorted_vector.h:192
SortedPermutationVector(Compare compare={})
Construct empty with a comparator.
Definition sorted_vector.h:65
size_type size() const noexcept
Return the number of sorted values.
Definition sorted_vector.h:103
bool empty() const noexcept
Return whether this owner has no values.
Definition sorted_vector.h:105
SortedPermutationVector(const SortedPermutationVector &)=delete
Exclusive ownership disallows copying.
size_type upper_bound_index(const Key &key) const
Return the first position whose value is ordered after key.
Definition sorted_vector.h:151
size_type lower_bound_index(const Key &key) const
Return the first position whose value is not ordered before key.
Definition sorted_vector.h:137
std::size_t size_type
Count and position type.
Definition sorted_vector.h:58
static SortedPermutationVector from_range(Range &&range, Compare compare={})
Build a sorted owner by consuming values one at a time.
Definition sorted_vector.h:91
void reserve(size_type count)
Reserve append-only payload capacity.
Definition sorted_vector.h:126
LengthLayout
Representation of tree child lengths.
Definition sequence_options.h:14