aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/fuzzy/sparse_state.h
blob: e023f4d3de272662f8c7b94b27c05446f908b399 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/config.h>
#include <vespa/vespalib/stllike/hash_fun.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <ostream>
#include <span>

namespace vespalib::fuzzy {

// Sentinel U32 char for state stepping that cannot match any target string characters
constexpr const uint32_t WILDCARD = UINT32_MAX;

/**
 * diag(n) is the width of the diagonal of the cost matrix that can possibly be
 * within k edits. This means that for a fixed k, it suffices to maintain state
 * for up to and including diag(k) consecutive cells for any given matrix row.
 */
constexpr inline uint8_t diag(uint8_t k) noexcept {
    return k*2 + 1;
}

template <uint8_t MaxEdits>
struct FixedSparseState {
private:
    static_assert(MaxEdits > 0 && MaxEdits <= UINT8_MAX/2);

    std::array<uint32_t, diag(MaxEdits)> indices;
    std::array<uint8_t,  diag(MaxEdits)> costs; // elems are 1-1 with indices vector
    uint8_t sz;
public:
    constexpr FixedSparseState() noexcept : indices(), costs(), sz(0) {}

    [[nodiscard]] constexpr bool empty() const noexcept {
        return (sz == 0);
    }

    [[nodiscard]] constexpr uint32_t size() const noexcept {
        return sz;
    }

    [[nodiscard]] constexpr uint32_t index(uint32_t entry_idx) const noexcept {
        return indices[entry_idx];
    }

    [[nodiscard]] constexpr uint8_t cost(uint32_t entry_idx) const noexcept {
        return costs[entry_idx];
    }

    // Precondition: !empty()
    [[nodiscard]] constexpr uint32_t last_index() const noexcept {
        return indices[sz - 1];
    }

    // Precondition: !empty()
    [[nodiscard]] constexpr uint8_t last_cost() const noexcept {
        return costs[sz - 1];
    }

    void append(uint32_t index, uint8_t cost) noexcept {
        assert(sz < diag(MaxEdits));
        indices[sz] = index;
        costs[sz] = cost;
        ++sz;
    }

    constexpr bool operator==(const FixedSparseState& rhs) const noexcept {
        if (sz != rhs.sz) {
            return false;
        }
        return (std::equal(indices.begin(), indices.begin() + sz, rhs.indices.begin()) &&
                std::equal(costs.begin(),   costs.begin()   + sz, rhs.costs.begin()));
    }

    struct hash {
        size_t operator()(const FixedSparseState& s) const noexcept {
            static_assert(std::is_same_v<uint32_t, std::decay_t<decltype(s.indices[0])>>);
            static_assert(std::is_same_v<uint8_t,  std::decay_t<decltype(s.costs[0])>>);
            return (xxhash::xxh3_64(s.indices.data(), s.sz * sizeof(uint32_t)) ^
                    xxhash::xxh3_64(s.costs.data(), s.sz));
        }
    };
};

/**
 * Prints sparse states as a single matrix row. Columns prior to any state index
 * are printed explicitly as '-' characters to make states line up when printed.
 *
 * Example output for the state (2:1, 3:1):
 *
 *   [-, -, 1, 1]
 *
 * Only meant as a debugging aid during development, as states with high indices
 * will emit very large strings.
 */
template <uint8_t MaxEdits> [[maybe_unused]]
std::ostream& operator<<(std::ostream& os, const FixedSparseState<MaxEdits>& s) {
    os << "[";
    size_t last_idx = 0;
    for (size_t i = 0; i < s.size(); ++i) {
        if (i != 0) {
            os << ", ";
        }
        for (size_t j = last_idx; j < s.index(i); ++j) {
            os << "-, ";
        }
        last_idx = s.index(i) + 1;
        os << static_cast<uint32_t>(s.cost(i));
    }
    os << "]";
    return os;
}

template <uint8_t MaxEdits>
struct FixedMaxEditsTransitions {
    static_assert(MaxEdits > 0 && MaxEdits <= UINT8_MAX/2);

    std::array<uint32_t, diag(MaxEdits)> out_u32_chars;
    uint8_t size;

    constexpr FixedMaxEditsTransitions() noexcept : out_u32_chars(), size(0) {}

    [[nodiscard]] constexpr bool has_char(uint32_t u32ch) const noexcept {
        for (uint8_t i = 0; i < size; ++i) {
            if (out_u32_chars[i] == u32ch) {
                return true;
            }
        }
        return false;
    }

    void add_char(uint32_t u32ch) noexcept {
        if (!has_char(u32ch)) {
            assert(size < diag(MaxEdits));
            out_u32_chars[size] = u32ch;
            ++size;
        }
    }

    constexpr std::span<const uint32_t> u32_chars() const noexcept {
        return {out_u32_chars.begin(), out_u32_chars.begin() + size};
    }

    constexpr std::span<uint32_t> u32_chars() noexcept {
        return {out_u32_chars.begin(), out_u32_chars.begin() + size};
    }

    void sort() noexcept {
        // TODO use custom sorting networks for fixed array sizes <= 5?
        // FIXME GCC 12.2 worse-than-useless(tm) warning false positives :I
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
        std::sort(out_u32_chars.begin(), out_u32_chars.begin() + size);
#pragma GCC diagnostic pop
    }
};

template <uint8_t MaxEdits>
struct FixedMaxEditDistanceTraits {
    static_assert(MaxEdits > 0 && MaxEdits <= UINT8_MAX/2);
    using StateType       = FixedSparseState<MaxEdits>;
    using TransitionsType = FixedMaxEditsTransitions<MaxEdits>;
    constexpr static uint8_t max_edits() noexcept {
        return MaxEdits;
    }
};

}