Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
search_index.hpp
Go to the documentation of this file.
1#pragma once
2
3// SearchIndex — ranked substring / prefix / fuzzy search over a fixed list of
4// strings.
5//
6// This is the concrete "index and filter a large library" workload behind the
7// off-UI-thread query service (see core/view/.../query_service.hpp). It is a
8// pure data structure with no threads of its own: `build()` does the one-time
9// normalization work and `query()` is a `const` scan, so callers are free to
10// run either on a background thread and marshal the result back themselves.
11//
12// Ranking, highest first: exact match > prefix match > substring match >
13// subsequence ("fuzzy") match. Within a tier, shorter items and earlier match
14// positions rank higher; ties break on the item's original index so results
15// are fully deterministic. An empty query returns every item in original
16// order.
17//
18// Matching is ASCII-case-insensitive by default. Unicode is compared
19// byte-for-byte (no case folding beyond ASCII) — deliberate, to keep results
20// deterministic and locale-independent.
21
23
24#include <cstddef>
25#include <string>
26#include <string_view>
27#include <vector>
28
29namespace pulp::runtime {
30
33 std::size_t max_results = 0;
34
37 bool fuzzy = true;
38
40 bool case_sensitive = false;
41};
42
44public:
45 struct Match {
46 std::size_t index;
47 int score;
48 };
49
50 SearchIndex() = default;
51
56 void build(std::vector<std::string> items, const CancelToken& cancel = {});
57
58 std::size_t size() const { return items_.size(); }
59 bool empty() const { return items_.empty(); }
60
62 const std::string& item(std::size_t i) const { return items_[i]; }
63
68 std::vector<Match> query(std::string_view query, const SearchQueryOptions& options = {},
69 const CancelToken& cancel = {}) const;
70
71 private:
72 std::vector<std::string> items_;
73 std::vector<std::string> normalized_;
74};
75
76} // namespace pulp::runtime
Definition cancel.hpp:77
const std::string & item(std::size_t i) const
The original text of item i. Caller must ensure i < size().
Definition search_index.hpp:62
bool empty() const
Definition search_index.hpp:59
std::vector< Match > query(std::string_view query, const SearchQueryOptions &options={}, const CancelToken &cancel={}) const
Rank every item against query.
void build(std::vector< std::string > items, const CancelToken &cancel={})
Replace the indexed set.
std::size_t size() const
Definition search_index.hpp:58
Definition generated_event_source.hpp:33
Definition search_index.hpp:45
std::size_t index
position in the original items list
Definition search_index.hpp:46
int score
higher is a better match
Definition search_index.hpp:47
Definition search_index.hpp:31
std::size_t max_results
Cap on the number of matches returned. 0 means unlimited.
Definition search_index.hpp:33
bool fuzzy
Allow non-contiguous subsequence ("fuzzy") matches.
Definition search_index.hpp:37
bool case_sensitive
Match with ASCII case sensitivity. Defaults to case-insensitive.
Definition search_index.hpp:40