Spaces:
Running
Running
Download include/sincekit_compact.h from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/include/sincekit_compact.h
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/include/sincekit_compact.h
-
curl -L -o sincekit_compact.h https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/include/sincekit_compact.h
4.86 kB
| namespace sincekit { | |
| // A bounded chronological journal for dense slots. Each write appends one ID. | |
| // Every capacity writes, compact the preceding generations in place: O(capacity) | |
| // worst-case write, O(1) amortized. since(c) scans O(min(revision-c, capacity)) | |
| // entries, up to two generations. Hot overwrites can cost more reads than Index. | |
| // Fixed storage: two ID arrays, one 64-bit revision per slot and a deletion bitmap. | |
| // Serialize writers against all accesses; concurrent read-only traversals are safe. | |
| class CompactIndex { | |
| public: | |
| explicit CompactIndex(std::size_t capacity) | |
| : latest_(checked_capacity(capacity)), | |
| prefix_(capacity), tail_(capacity), | |
| deleted_(capacity / 64 + (capacity % 64 != 0)) {} | |
| CompactIndex(const CompactIndex&) = delete; | |
| CompactIndex& operator=(const CompactIndex&) = delete; | |
| std::size_t capacity() const noexcept { return latest_.size(); } | |
| std::uint64_t revision() const noexcept { return revision_; } | |
| std::uint64_t touch(std::uint32_t id) { return publish(id, false); } | |
| std::uint64_t erase(std::uint32_t id) { return publish(id, true); } | |
| template<class Fn> | |
| std::uint64_t since(std::uint64_t cursor, Fn&& callback) const { | |
| if (cursor > revision_) throw std::out_of_range("sincekit: cursor exceeds current revision"); | |
| if (cursor == revision_) return revision_; | |
| ReadScope reading(this); | |
| auto stamp = revision_; | |
| for (auto i = tail_size_; i > 0 && stamp > cursor; --i, --stamp) { | |
| const auto id = tail_[i - 1]; | |
| if (latest_[id] == stamp) emit(callback, id, stamp); | |
| } | |
| if (cursor < prefix_revision_) { | |
| for (auto i = prefix_size_; i > 0; --i) { | |
| const auto id = prefix_[i - 1]; | |
| const auto last = latest_[id]; | |
| if (last > prefix_revision_) continue; // Superseded in the tail. | |
| if (last <= cursor) break; | |
| emit(callback, id, last); | |
| } | |
| } | |
| return revision_; | |
| } | |
| private: | |
| struct ReadScope { | |
| inline static thread_local const ReadScope* current = nullptr; | |
| const CompactIndex* index; | |
| const ReadScope* previous; | |
| explicit ReadScope(const CompactIndex* owner) noexcept | |
| : index(owner), previous(current) { current = this; } | |
| ~ReadScope() { current = previous; } | |
| }; | |
| static std::size_t checked_capacity(std::size_t capacity) { | |
| if (capacity > (std::numeric_limits<std::uint32_t>::max)()) { | |
| throw std::length_error("sincekit: capacity exceeds uint32 slot limit"); | |
| } | |
| return capacity; | |
| } | |
| template<class Fn> | |
| void emit(Fn& callback, std::uint32_t id, std::uint64_t stamp) const { | |
| callback(std::uint32_t{id}, std::uint64_t{stamp}, | |
| (deleted_[id / 64] & (std::uint64_t{1} << (id % 64))) != 0); | |
| } | |
| void compact() noexcept { | |
| std::size_t out = 0; | |
| for (std::size_t i = 0; i < prefix_size_; ++i) { | |
| const auto id = prefix_[i]; | |
| if (latest_[id] <= prefix_revision_) prefix_[out++] = id; | |
| } | |
| auto stamp = prefix_revision_; | |
| for (std::size_t i = 0; i < tail_size_; ++i) { | |
| const auto id = tail_[i]; | |
| if (latest_[id] == ++stamp) prefix_[out++] = id; | |
| } | |
| // Each surviving ID occurs once, so the destination always fits. | |
| prefix_size_ = out; | |
| prefix_revision_ = revision_; | |
| tail_size_ = 0; | |
| } | |
| std::uint64_t publish(std::uint32_t id, bool deleted) { | |
| if (id >= capacity()) throw std::out_of_range("sincekit: slot outside capacity"); | |
| for (auto* scope = ReadScope::current; scope; scope = scope->previous) { | |
| if (scope->index == this) throw std::logic_error("sincekit: write during traversal"); | |
| } | |
| if (revision_ == (std::numeric_limits<std::uint64_t>::max)()) { | |
| throw std::overflow_error("sincekit: revision exhausted"); | |
| } | |
| if (tail_size_ == capacity()) compact(); | |
| tail_[tail_size_++] = id; | |
| auto& bits = deleted_[id / 64]; | |
| const auto mask = std::uint64_t{1} << (id % 64); | |
| if (deleted) bits |= mask; | |
| else bits &= ~mask; | |
| latest_[id] = ++revision_; | |
| return revision_; | |
| } | |
| std::vector<std::uint64_t> latest_; | |
| std::vector<std::uint32_t> prefix_; | |
| std::vector<std::uint32_t> tail_; | |
| std::vector<std::uint64_t> deleted_; | |
| std::size_t prefix_size_ = 0; | |
| std::size_t tail_size_ = 0; | |
| std::uint64_t revision_ = 0; | |
| std::uint64_t prefix_revision_ = 0; | |
| }; | |
| } // namespace sincekit | |