| 1 | # Yada: Yet Another Double-Array |
| 2 | |
| 3 | [](https://crates.io/crates/yada) |
| 4 | [](https://docs.rs/yada) |
| 5 | |
| 6 | Yada is a yet another double-array trie library aiming for fast search and |
| 7 | compact data representation. |
| 8 | |
| 9 | ## Features |
| 10 | |
| 11 | - Build static double-array tries |
| 12 | - Yada adopts the compact binary representation of double-array nodes like |
| 13 | [Darts-clone](https://github.com/s-yata/darts-clone). |
| 14 | - Common prefix search |
| 15 | - The method returns an `Iterator` that is an effective way to find multiple |
| 16 | values without heap allocation. |
| 17 | - Exact match search |
| 18 | - The method finds a value associated with an exact match key as a `Option`. |
| 19 | |
| 20 | ## Requirements |
| 21 | |
| 22 | - Rust version >= 1.46.0 |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | See also [example code](examples/build_and_search.rs) for more details. |
| 27 | |
| 28 | ### Build a double-array trie |
| 29 | |
| 30 | ```rust |
| 31 | use yada::builder::DoubleArrayBuilder; |
| 32 | |
| 33 | // make a keyset which have key-value pairs |
| 34 | let keyset = &[ |
| 35 | ("a".as_bytes(), 0), |
| 36 | ("ab".as_bytes(), 1), |
| 37 | ("abc".as_bytes(), 2), |
| 38 | ("b".as_bytes(), 3), |
| 39 | ("bc".as_bytes(), 4), |
| 40 | ("c".as_bytes(), 5), |
| 41 | ]; |
| 42 | |
| 43 | // build a double-array trie binary |
| 44 | let da_bytes: Option<Vec<u8>> = DoubleArrayBuilder::build(keyset); |
| 45 | ``` |
| 46 | |
| 47 | ### Search entries by keys |
| 48 | |
| 49 | ```rust |
| 50 | use yada::DoubleArray; |
| 51 | |
| 52 | // create a double-array trie instance |
| 53 | let da = DoubleArray::new(da_bytes.unwrap()); |
| 54 | |
| 55 | // exact match search |
| 56 | for (key, value) in keyset { |
| 57 | assert_eq!(da.exact_match_search(key), Some(*value as u32)); |
| 58 | } |
| 59 | assert_eq!(da.exact_match_search("abc".as_bytes()), Some(2)); |
| 60 | assert_eq!(da.exact_match_search("abcd".as_bytes()), None); |
| 61 | |
| 62 | // common prefix search |
| 63 | assert_eq!( |
| 64 | da.common_prefix_search("abcd".as_bytes()) |
| 65 | .collect::<Vec<_>>(), |
| 66 | vec![(0, 1), (1, 2), (2, 3)] // match "a", "ab", "abc", value and key length |
| 67 | ); |
| 68 | assert_eq!( |
| 69 | da.common_prefix_search("d".as_bytes()).collect::<Vec<_>>(), |
| 70 | vec![] // don't match |
| 71 | ); |
| 72 | ``` |
| 73 | |
| 74 | ## Limitations |
| 75 | |
| 76 | - The value must be represented as a 31 bit unsigned integer, typed `u32`. |
| 77 | - Yada uses the most significant bit (MSB) as a flag to distinguish between a value node and others. |
| 78 | - The offset of an double-array node is 29 bits wide, so it can represent up to |
| 79 | ~536M nodes. |
| 80 | - It means this limitation results in the size upper bound ~2GB of double-arrays. |
| 81 | |
| 82 | ## License |
| 83 | |
| 84 | Licensed under either of |
| 85 | |
| 86 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE)) |
| 87 | - MIT license ([LICENSE-MIT](LICENSE-MIT)) |
| 88 | |
| 89 | at your option. |
| 90 | |
| 91 | ## Contribution |
| 92 | |
| 93 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 94 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 95 | dual licensed as above, without any additional terms or conditions. |
| 96 | |
| 97 | ## References |
| 98 | |
| 99 | - [Aoe, J. An Efficient Digital Search Algorithm by Using a Double-Array Structure. |
| 100 | IEEE Transactions on Software Engineering. Vol. 15, 9 (Sep 1989). pp. 1066-1077.](https://ieeexplore.ieee.org/document/31365) |
| 101 | - [Darts: Double ARray Trie System](http://chasen.org/~taku/software/darts/) |
| 102 | - [Darts-clone: A clone of Darts (Double-ARray Trie System)](https://github.com/s-yata/darts-clone) |