|
| 1 | +// Copyright 2024 PISA developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <concepts> |
| 18 | +#include <cstddef> |
| 19 | +#include <cstdint> |
| 20 | +#include <memory> |
| 21 | +#include <optional> |
| 22 | +#include <ostream> |
| 23 | +#include <span> |
| 24 | + |
| 25 | +namespace pisa::lt { |
| 26 | + |
| 27 | +namespace detail { |
| 28 | + |
| 29 | + class BaseLookupTable { |
| 30 | + public: |
| 31 | + virtual ~BaseLookupTable() = default; |
| 32 | + [[nodiscard]] virtual auto size() const noexcept -> std::size_t = 0; |
| 33 | + [[nodiscard]] virtual auto operator[](std::size_t idx) const |
| 34 | + -> std::span<std::byte const> = 0; |
| 35 | + [[nodiscard]] virtual auto find(std::span<std::byte const> value) const noexcept |
| 36 | + -> std::optional<std::size_t> = 0; |
| 37 | + |
| 38 | + [[nodiscard]] virtual auto clone() -> std::unique_ptr<BaseLookupTable> = 0; |
| 39 | + }; |
| 40 | + |
| 41 | + class BaseLookupTableEncoder { |
| 42 | + public: |
| 43 | + virtual ~BaseLookupTableEncoder() = default; |
| 44 | + void virtual insert(std::span<std::byte const> payload) = 0; |
| 45 | + void virtual encode(std::ostream& out) = 0; |
| 46 | + }; |
| 47 | + |
| 48 | +} // namespace detail |
| 49 | + |
| 50 | +namespace v1 { |
| 51 | + |
| 52 | + class Flags { |
| 53 | + private: |
| 54 | + std::uint8_t flags = 0; |
| 55 | + |
| 56 | + public: |
| 57 | + constexpr Flags() = default; |
| 58 | + explicit constexpr Flags(std::uint8_t bitset) : flags(bitset) {} |
| 59 | + |
| 60 | + [[nodiscard]] auto sorted() const noexcept -> bool; |
| 61 | + [[nodiscard]] auto wide_offsets() const noexcept -> bool; |
| 62 | + [[nodiscard]] auto bits() const noexcept -> std::uint8_t; |
| 63 | + }; |
| 64 | + |
| 65 | + namespace flags { |
| 66 | + inline constexpr std::uint8_t SORTED = 0b001; |
| 67 | + inline constexpr std::uint8_t WIDE_OFFSETS = 0b010; |
| 68 | + } // namespace flags |
| 69 | + |
| 70 | +}; // namespace v1 |
| 71 | + |
| 72 | +} // namespace pisa::lt |
| 73 | + |
| 74 | +namespace pisa { |
| 75 | + |
| 76 | +/** |
| 77 | + * Lookup table mapping integers from a range [0, N) to binary payloads. |
| 78 | + * |
| 79 | + * This table assigns each _unique_ value (duplicates are not allowed) to an index in [0, N), where |
| 80 | + * N is the size of the table. Thus, this structure is equivalent to a sequence of binary values. |
| 81 | + * The difference between `LookupTable` and, say, `std::vector` is that its encoding supports |
| 82 | + * reading the values without fully parsing the entire binary representation of the table. As such, |
| 83 | + * it supports quickly initializing the structure from an external device (with random access), |
| 84 | + * e.g., via mmap, and performing a lookup without loading the entire structure to main memory. |
| 85 | + * This is especially useful for short-lived programs that must perform a lookup without the |
| 86 | + * unnecessary overhead of loading it to memory. |
| 87 | + * |
| 88 | + * If the values are sorted, and the appropriate flag is toggled in the header, a quick binary |
| 89 | + * search lookup can be performed to find an index of a value. If the values are not sorted, then a |
| 90 | + * linear scan will be used; therefore, one should consider having values sorted if such lookups are |
| 91 | + * important. Getting the value at a given index is a constant-time operation, though if using |
| 92 | + * memory mapping, each such operation may need to load multiple pages to memory. |
| 93 | + */ |
| 94 | +class LookupTable { |
| 95 | + private: |
| 96 | + std::unique_ptr<::pisa::lt::detail::BaseLookupTable> m_impl; |
| 97 | + |
| 98 | + explicit LookupTable(std::unique_ptr<::pisa::lt::detail::BaseLookupTable> impl); |
| 99 | + |
| 100 | + [[nodiscard]] static auto v1(std::span<const std::byte> bytes) -> LookupTable; |
| 101 | + |
| 102 | + public: |
| 103 | + LookupTable(LookupTable const&); |
| 104 | + LookupTable(LookupTable&&); |
| 105 | + LookupTable& operator=(LookupTable const&); |
| 106 | + LookupTable& operator=(LookupTable&&); |
| 107 | + ~LookupTable(); |
| 108 | + |
| 109 | + /** |
| 110 | + * The number of elements in the table. |
| 111 | + */ |
| 112 | + [[nodiscard]] auto size() const noexcept -> std::size_t; |
| 113 | + |
| 114 | + /** |
| 115 | + * Retrieves the value at index `idx`. |
| 116 | + * |
| 117 | + * If `idx < size()`, then `std::out_of_range` exception is thrown. See `at()` if you want to |
| 118 | + * conveniently cast the memory span to another type. |
| 119 | + */ |
| 120 | + [[nodiscard]] auto operator[](std::size_t idx) const -> std::span<std::byte const>; |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the position of `value` in the table or `std::nullopt` if the value does not exist. |
| 124 | + * |
| 125 | + * See the templated version of this function if you want to automatically cast from another |
| 126 | + * type to byte span. |
| 127 | + */ |
| 128 | + [[nodiscard]] auto find(std::span<std::byte const> value) const noexcept |
| 129 | + -> std::optional<std::size_t>; |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the value at index `idx` cast to type `T`. |
| 133 | + * |
| 134 | + * The type `T` must define `T::value_type` that resolves to a byte-wide type, as well as a |
| 135 | + * constructor that takes `T::value_type const*` (pointer to the first byte) and `std::size_t` |
| 136 | + * (the total number of bytes). If `T::value_type` is longer than 1 byte, this operation results |
| 137 | + * in **undefined behavior**. |
| 138 | + * |
| 139 | + * Examples of types that can be used are: `std::string_view` or `std::span<const char>`. |
| 140 | + */ |
| 141 | + template <typename T> |
| 142 | + [[nodiscard]] auto at(std::size_t idx) const -> T { |
| 143 | + auto bytes = this->operator[](idx); |
| 144 | + return T(reinterpret_cast<typename T::value_type const*>(bytes.data()), bytes.size()); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the position of `value` in the table or `std::nullopt` if the value does not exist. |
| 149 | + * |
| 150 | + * The type `T` of the value must be such that `std:span<typename T::value_type const>` is |
| 151 | + * constructible from `T`. |
| 152 | + */ |
| 153 | + template <typename T> |
| 154 | + requires(std::constructible_from<std::span<typename T::value_type const>, T>) |
| 155 | + [[nodiscard]] auto find(T value) const noexcept -> std::optional<std::size_t> { |
| 156 | + return find(std::as_bytes(std::span<typename T::value_type const>(value))); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Constructs a lookup table from the encoded sequence of bytes. |
| 161 | + */ |
| 162 | + [[nodiscard]] static auto from_bytes(std::span<std::byte const> bytes) -> LookupTable; |
| 163 | +}; |
| 164 | + |
| 165 | +/** |
| 166 | + * Lookup table encoder. |
| 167 | + * |
| 168 | + * This class builds and encodes a sequence of values to the binary format of lookup table. |
| 169 | + * See `LookupTable` for more details. |
| 170 | + * |
| 171 | + * Note that all encoded data is accumulated in memory and only flushed to the output stream when |
| 172 | + * `encode()` member function is called. |
| 173 | + */ |
| 174 | +class LookupTableEncoder { |
| 175 | + std::unique_ptr<::pisa::lt::detail::BaseLookupTableEncoder> m_impl; |
| 176 | + |
| 177 | + explicit LookupTableEncoder(std::unique_ptr<::pisa::lt::detail::BaseLookupTableEncoder> impl); |
| 178 | + |
| 179 | + public: |
| 180 | + /** |
| 181 | + * Constructs an encoder for a lookup table in v1 format, with the given flag options. |
| 182 | + * |
| 183 | + * If sorted flag is _not_ set, then an additional hash set will be produced to keep track of |
| 184 | + * duplicates. This will increase the memory footprint at build time. |
| 185 | + */ |
| 186 | + static LookupTableEncoder v1(::pisa::lt::v1::Flags flags); |
| 187 | + |
| 188 | + /** |
| 189 | + * Inserts payload. |
| 190 | + * |
| 191 | + * If sorted flag was set at construction time, it will throw if the given payload is not |
| 192 | + * lexicographically greater than the previously inserted payload. If sorted flag was _not_ set |
| 193 | + * and the given payload has already been inserted, it will throw as well. |
| 194 | + */ |
| 195 | + auto insert(std::span<std::byte const> payload) -> LookupTableEncoder&; |
| 196 | + |
| 197 | + /** |
| 198 | + * Writes the encoded table to the output stream. |
| 199 | + */ |
| 200 | + auto encode(std::ostream& out) -> LookupTableEncoder&; |
| 201 | + |
| 202 | + /** |
| 203 | + * Inserts a payload of type `Payload`. |
| 204 | + * |
| 205 | + * `std::span<typename Payload::value_type const>` must be constructible from `Payload`, which |
| 206 | + * in turn will be cast as byte span before calling the non-templated version of `insert()`. |
| 207 | + */ |
| 208 | + template <typename Payload> |
| 209 | + requires(std::constructible_from<std::span<typename Payload::value_type const>, Payload>) |
| 210 | + auto insert(Payload const& payload) -> LookupTableEncoder& { |
| 211 | + insert(std::as_bytes(std::span(payload))); |
| 212 | + return *this; |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Inserts all payloads in the given span. |
| 217 | + * |
| 218 | + * It calls `insert()` for each element in the span. See `insert()` for more details. |
| 219 | + */ |
| 220 | + template <typename Payload> |
| 221 | + auto insert_span(std::span<Payload const> payloads) -> LookupTableEncoder& { |
| 222 | + for (auto const& payload: payloads) { |
| 223 | + insert(payload); |
| 224 | + } |
| 225 | + return *this; |
| 226 | + } |
| 227 | +}; |
| 228 | + |
| 229 | +} // namespace pisa |
0 commit comments