| 1 | extern crate yadarust-analyzer cargo yada 0.5.1 yada/; |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 5 | use yadarust-analyzer cargo yada 0.5.1 crate/::builderrust-analyzer cargo yada 0.5.1 builder/::DoubleArrayBuilderrust-analyzer cargo yada 0.5.1 builder/DoubleArrayBuilder#; |
| 6 | use yadarust-analyzer cargo yada 0.5.1 crate/::DoubleArrayrust-analyzer cargo yada 0.5.1 DoubleArray#; |
| 7 | |
| 8 | fn mainrust-analyzer cargo yada 0.5.1 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> { |
| 9 | // filename to save and load |
| 10 | let filename = "load_from_file_example.da"; |
| 11 | |
| 12 | // make a keyset which have key-value pairs |
| 13 | let keyset = &[ |
| 14 | ("a".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), 0), |
| 15 | ("aa".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), 1), |
| 16 | ("aaa".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), 2), |
| 17 | ("b".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), 3), |
| 18 | ("bcd".as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), 4), |
| 19 | ]; |
| 20 | |
| 21 | // build a double-array trie binary |
| 22 | let da_bytes = DoubleArrayBuilderrust-analyzer cargo yada 0.5.1 builder/DoubleArrayBuilder#::buildrust-analyzer cargo yada 0.5.1 builder/impl#[DoubleArrayBuilder]build().(keyset); |
| 23 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(da_bytes.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 24 | |
| 25 | // create a double-array trie instance |
| 26 | let da = DoubleArrayrust-analyzer cargo yada 0.5.1 DoubleArray#::newrust-analyzer cargo yada 0.5.1 impl#[`DoubleArray<T>`]new().(da_bytes.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 27 | |
| 28 | // save to file |
| 29 | let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(filename)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 30 | file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(da.0rust-analyzer cargo yada 0.5.1 DoubleArray#0..as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 31 | file.flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Write]flush().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 32 | |
| 33 | // load from file |
| 34 | let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(filename)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 35 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 36 | let _ = file.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_end().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 37 | let da = DoubleArrayrust-analyzer cargo yada 0.5.1 DoubleArray#::newrust-analyzer cargo yada 0.5.1 impl#[`DoubleArray<T>`]new().(buf); |
| 38 | |
| 39 | // test search |
| 40 | for (key, value) in keyset.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 41 | let v = da.exact_match_searchrust-analyzer cargo yada 0.5.1 impl#[`DoubleArray<T>`]exact_match_search().(key).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 42 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(v, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().value); |
| 43 | } |
| 44 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 45 | da.common_prefix_searchrust-analyzer cargo yada 0.5.1 impl#[`DoubleArray<T>`]common_prefix_search().(&"aaaa").collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 46 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(0, 1), (1, 2), (2, 3)] |
| 47 | ); |
| 48 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 49 | da.common_prefix_searchrust-analyzer cargo yada 0.5.1 impl#[`DoubleArray<T>`]common_prefix_search().(&"bcde").collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(), |
| 50 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(3, 1), (4, 3)] |
| 51 | ); |
| 52 | |
| 53 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 54 | } |