5#include <portals/collection.hpp>
6#include <portals/model_position.hpp>
8namespace portals::ui::detail {
11 std::uint64_t revision;
12 std::uint64_t beforeCount;
13 PositionChange operation;
16class OwnedHistory final :
public PositionChangeHistory {
18 OwnedHistory(std::uint64_t revision, std::uint64_t count, PositionMapOptions options)
19 : revision_(revision), proofFloor_(revision), count_(count), options_(options) {
22 OwnedHistory(
const OwnedHistory &previous, std::uint64_t beforeCount, std::optional<PositionChange> operation)
23 : revision_(previous.revision_ + 1), proofFloor_(previous.proofFloor_), count_(beforeCount),
24 options_(previous.options_) {
27 const auto skip = operation && previous.changes_.size() >= options_.maxChanges
28 ? previous.changes_.size() - options_.maxChanges + 1
31 changes_.reserve(previous.changes_.size() - skip + (operation ? 1 : 0));
32 changes_.insert(changes_.end(), previous.changes_.begin() + skip, previous.changes_.end());
35 proofFloor_ = previous.changes_[skip - 1].revision + 1;
39 changes_.push_back({previous.revision_, beforeCount, *operation});
41 if (operation->kind == PositionChangeKind::SPLICE) {
42 count_ = beforeCount - operation->removed + operation->inserted;
47 std::expected<PositionHistoryReply, SourceError> changesFrom(std::uint64_t revision)
const override {
48 if (!revision || revision > revision_) {
49 return std::unexpected(SourceError::REVISION_CHANGED);
51 const auto floor = proofFloor_;
52 if (revision < floor) {
53 return PositionHistoryReply{{}, floor, PositionResolution::UNAVAILABLE};
55 if (revision == revision_) {
56 return PositionHistoryReply{{}, floor, PositionResolution::EXACT};
58 const auto first = std::ranges::lower_bound(changes_, revision, {}, &OwnedChange::revision);
61 std::vector<PositionChange> suffix(0);
62 suffix.reserve(
static_cast<std::size_t
>(changes_.end() - first));
63 for (
auto entry = first; entry != changes_.end(); ++entry) {
64 suffix.push_back(entry->operation);
66 const auto beforeCount = first == changes_.end() ? count_ : first->beforeCount;
68 auto proof = PositionChangeMap::create(revision, revision_, beforeCount, suffix, options_);
69 return proof ? std::expected<PositionHistoryReply, SourceError>(
70 PositionHistoryReply{std::move(*proof), floor, PositionResolution::EXACT})
71 : std::unexpected(proof.error());
72 }
catch (
const std::bad_alloc &) {
73 return std::unexpected(SourceError::NO_MEMORY);
75 return std::unexpected(SourceError::CALLBACK_FAILED);
80 std::vector<OwnedChange> changes_ = std::vector<OwnedChange>(0);
81 std::uint64_t revision_;
82 std::uint64_t proofFloor_;
84 PositionMapOptions options_;
87template <
class T>
class OwnedRows final :
public ModelSnapshot<T> {
89 OwnedRows(
const std::vector<ModelRow<T>> &rows, std::uint64_t revision, std::shared_ptr<const OwnedHistory> history)
90 : ModelSnapshot<T>(revision, rows.size()), rows_(rows), history_(std::move(history)) {
91 index_.reserve(rows_.size());
92 for (std::size_t offset = 0; offset < rows_.size(); ++offset) {
93 index_.emplace_back(rows_[offset].
id.value, offset);
95 std::ranges::sort(index_);
98 ModelRows<T> read(RowRange range)
const override {
99 return {RangeState::READY, std::span(rows_).subspan(
static_cast<std::size_t
>(range.first), range.count), {}};
102 RowLookup locateRow(RowId
id)
const override {
103 const auto found = std::ranges::lower_bound(index_,
id.value, {}, &std::pair<std::uint64_t, std::uint64_t>::first);
104 return id.generation == 1 &&
id.value && found != index_.end() && found->first ==
id.value
105 ? RowLookup{RowLookupState::FOUND, found->second}
106 : RowLookup{RowLookupState::NOT_FOUND, 0};
109 std::shared_ptr<const PositionChangeHistory> positionHistory()
const override {
113 std::span<const ModelRow<T>> rows() const noexcept {
117 const OwnedHistory &history() const noexcept {
122 std::vector<ModelRow<T>> rows_;
123 std::vector<std::pair<std::uint64_t, std::uint64_t>> index_ = std::vector<std::pair<std::uint64_t, std::uint64_t>>(0);
124 std::shared_ptr<const OwnedHistory> history_;
127struct OwnedEditVersion {
132template <
class T>
struct OwnedSequence {
133 std::shared_ptr<const OwnedRows<T>> current;
134 std::size_t capacity{CollectionLimits{}.maxRows};
135 PositionMapOptions history;
136 std::uint64_t nextId{1};
138 std::shared_ptr<std::vector<std::weak_ptr<OwnedEditVersion>>> editVersions;
Version version() noexcept
Return the version of the linked library by value.