Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_model_sequence.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/collection.hpp>
6#include <portals/model_position.hpp>
7
8namespace portals::ui::detail {
10struct OwnedChange {
11 std::uint64_t revision;
12 std::uint64_t beforeCount;
13 PositionChange operation;
14};
15
16class OwnedHistory final : public PositionChangeHistory {
17public:
18 OwnedHistory(std::uint64_t revision, std::uint64_t count, PositionMapOptions options)
19 : revision_(revision), proofFloor_(revision), count_(count), options_(options) {
20 }
21
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_) {
25 // Identity revisions retain every structural proof. Dropping an operation only
26 // expires revisions through that operation; the following identity gap stays exact.
27 const auto skip = operation && previous.changes_.size() >= options_.maxChanges
28 ? previous.changes_.size() - options_.maxChanges + 1
29 : 0;
30
31 changes_.reserve(previous.changes_.size() - skip + (operation ? 1 : 0));
32 changes_.insert(changes_.end(), previous.changes_.begin() + skip, previous.changes_.end());
33
34 if (skip) {
35 proofFloor_ = previous.changes_[skip - 1].revision + 1;
36 }
37
38 if (operation) {
39 changes_.push_back({previous.revision_, beforeCount, *operation});
40
41 if (operation->kind == PositionChangeKind::SPLICE) {
42 count_ = beforeCount - operation->removed + operation->inserted;
43 }
44 }
45 }
46
47 std::expected<PositionHistoryReply, SourceError> changesFrom(std::uint64_t revision) const override {
48 if (!revision || revision > revision_) {
49 return std::unexpected(SourceError::REVISION_CHANGED);
50 }
51 const auto floor = proofFloor_;
52 if (revision < floor) {
53 return PositionHistoryReply{{}, floor, PositionResolution::UNAVAILABLE};
54 }
55 if (revision == revision_) {
56 return PositionHistoryReply{{}, floor, PositionResolution::EXACT};
57 }
58 const auto first = std::ranges::lower_bound(changes_, revision, {}, &OwnedChange::revision);
59
60 try {
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);
65 }
66 const auto beforeCount = first == changes_.end() ? count_ : first->beforeCount;
67
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);
74 } catch (...) {
75 return std::unexpected(SourceError::CALLBACK_FAILED);
76 }
77 }
78
79private:
80 std::vector<OwnedChange> changes_ = std::vector<OwnedChange>(0);
81 std::uint64_t revision_;
82 std::uint64_t proofFloor_;
83 std::uint64_t count_;
84 PositionMapOptions options_;
85};
86
87template <class T> class OwnedRows final : public ModelSnapshot<T> {
88public:
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);
94 }
95 std::ranges::sort(index_);
96 }
97
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), {}};
100 }
101
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};
107 }
108
109 std::shared_ptr<const PositionChangeHistory> positionHistory() const override {
110 return history_;
111 }
112
113 std::span<const ModelRow<T>> rows() const noexcept {
114 return rows_;
115 }
116
117 const OwnedHistory &history() const noexcept {
118 return *history_;
119 }
120
121private:
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_;
125};
126
127struct OwnedEditVersion {
128 RowId row;
129 std::uint64_t version{1};
130};
131
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};
137 bool editing{};
138 std::shared_ptr<std::vector<std::weak_ptr<OwnedEditVersion>>> editVersions;
139};
140
142} // namespace portals::ui::detail
Version version() noexcept
Return the version of the linked library by value.