Compact immutable byte-string sets and maps, ported from Rust fst 0.4.7
moon add starAndHonor/fst@0.1.0name = "demo/consumer"
import {
"starAndHonor/fst@0.1.0",
}import {
"starAndHonor/fst" @fst,
"starAndHonor/fst/automaton" @automaton,
} for "test"test {
// 便捷构造器复制、排序输入;Set 自动合并重复键。
let words = @fst.FstSet::from_strings(["月亮", "月光", "太阳", "月亮"])
assert_eq(words.len(), 3UL)
assert_eq(words.prefix_str("月").strings(), ["月亮", "月光"])
// 编辑距离按 Unicode scalar value 计算。
let fuzzy = @automaton.levenshtein("月亮", 1)
assert_eq(words.search(fuzzy).strings(), ["月亮", "月光"])
// Map 默认拒绝重复键;UInt64 不经过浮点数转换。
let counts = @fst.FstMap::from_strings([
("apple", 10UL),
("apricot", 20UL),
("banana", 18446744073709551615UL),
])
assert_eq(counts.get_str("banana"), Some(18446744073709551615UL))
let restored = @fst.FstMap::from_bytes(counts.to_bytes())
assert_eq(restored.get_str("apple"), Some(10UL))
// 已有序的数据可以增量写入,避免先复制并排序所有键。
let builder = @fst.Builder::map()
builder.insert(b"a", 1UL)
builder.insert(b"ab", 2UL)
builder.insert(b"b", 3UL)
let built = builder.into_map()
assert_eq(built.get(b"ab"), Some(2UL))
let index = built.as_fst().index()
assert_eq(index.rank(b"ab"), (1UL, true))
assert_eq(index.prefix_count(b"a"), 2UL)
assert_eq(index.select(2UL).unwrap().key, b"b")
// 对匹配结果直接计数、定位和分页,无须收集所有命中键。
let matching = index.search_index(@automaton.prefix_bytes(b"a"))
assert_eq(matching.len(), 2UL)
assert_eq(matching.select(1UL).unwrap().key, b"ab")
assert_eq(index.page_reverse(0UL, 1)[0].key, b"b")
let statistics = index.prefix_value_statistics(b"a")
assert_eq(statistics.sum, Some(3UL))
assert_eq(index.top(1)[0].key, b"b")
let cursor = built.as_fst().cursor()
assert_true(cursor.advance_bytes(b"ab"))
assert_eq(cursor.value(), Some(2UL))
}members = [
"./moonfst",
"./consumer",
]moon run examples/dictionary --target wasm-gcpython3 scripts/benchmark.py --output benchmarks/results/my-runpython3 scripts/workload_benchmark.py \
--sizes 10000 100000 200000 --orders sorted shuffled \
--samples 7 --warmups 2 --rounds 3 --memory-rounds 3 \
--output benchmarks/results/my-real-runmoon fmt --check
moon test --target native --deny-warn
moon test --target js --deny-warn
moon test --target wasm --deny-warn
moon test --target wasm-gc --deny-warncargo test --locked --manifest-path oracle/Cargo.toml
python3 scripts/generate_fixtures.py --check
python3 scripts/verify_oracle.py
python3 scripts/interop.py --target native --target js --target wasm-gc
python3 scripts/random_interop.py --target nativepub(all) suberror FstError {
InvalidArgument(String)
OutOfOrder(Bytes, Bytes)
DuplicateKey(Bytes)
BuilderClosed
InvalidFormat(String)
InvalidUtf8
Overflow
ResourceLimit(String)
} derive(Debug)pub struct ArraySource {
// private fields
}impl EntrySource for ArraySourcepub struct Builder {
// private fields
}pub struct CheckedSource {
// private fields
}impl EntrySource for CheckedSourcepub struct Cursor {
// private fields
}pub struct FilterSource {
// private fields
}impl EntrySource for FilterSourcepub struct Fst {
// private fields
}pub struct FstIndex {
// private fields
}fn FstIndex::search_index(self : FstIndex, matcher : &Automaton, max_states? : Int) -> SearchIndex raise FstErrorfn FstIndex::value_statistics_at(self : FstIndex, lower : UInt64, upper : UInt64) -> ValueStatistics raise FstErrorpub struct FstMap {
// private fields
}fn FstMap::from_strings(entries : Array[(String, UInt64)], duplicates? : DuplicatePolicy, registry_capacity? : Int) -> FstMap raise FstErrorpub struct FstSet {
// private fields
}pub struct MapSource {
// private fields
}impl EntrySource for MapSourcepub struct MergeStream {
// private fields
}fn MergeStream::into_map(self : MergeStream, policy : DuplicatePolicy, registry_capacity? : Int) -> FstMap raise FstErrorpub struct ReverseStream {
// private fields
}pub struct SearchIndex {
// private fields
}fn SearchIndex::page(self : SearchIndex, offset : UInt64, limit : Int, range? : KeyRange) -> Array[Entry] raise FstErrorfn SearchIndex::page_reverse(self : SearchIndex, offset : UInt64, limit : Int, range? : KeyRange) -> Array[Entry] raise FstErrorpub struct Stream {
// private fields
}impl EntrySource for Streampub struct WindowSource {
// private fields
}impl EntrySource for WindowSourcefn merge_sources(sources : Array[&EntrySource], operation : Operation) -> MergeStream raise FstErrorfn prefix_successor(prefix : Bytes) -> Bytes?Install
Download zipCompact immutable byte-string sets and maps, ported from Rust fst 0.4.7