Crates.io | ironstorm_lookup |
lib.rs | ironstorm_lookup |
version | 1.0.4 |
source | src |
created_at | 2016-04-27 19:14:16.294721 |
updated_at | 2017-01-13 22:34:08.554601 |
description | Lightning fast lookup table for auto completion, type ahead, suggestion engines. |
homepage | https://github.com/forgemo/ironstorm_lookup/blob/master/README.md |
repository | https://github.com/forgemo/ironstorm_lookup |
max_upload_size | |
id | 4879 |
size | 17,569 |
This library contains the internal data structure used by the ironstorm project
To learn more about ironstorm_lookup, read this README.md and the Crate Documentation
It compiles only with the nightly version of rust due to usage of unstable features.
LookupTable
for multiple gigabytes of data can take a few minutesLookupTable
can not be modified, only recreatedMovie
structLookup
trait for your custom type.Iterator
that will iterate over all the elements you would like to put into the LookupTable
LookupTable
by calling LookupTable::from_iter(myMoviesIterator)
myMoviesLookupTable.find("hero")
to get an lazy 'Iterator' over all matching elementsLet's build a LookupTable
to find restaurants by name.
use std::iter::FromIterator;
use ironstorm_lookup::{LookupTable, Lookup, Bucket};
// 1. Create a custom struct representing a restaurant
struct Restaurant<'a> {
name: &'a str,
cuisine: &'a str
}
// 2. Implement the `Lookup` trait for `Restaurant` references
impl <'a> Lookup for &'a Restaurant<'a> {
// Make the restaurant name searchable
fn searchable_text(&self) -> String {
self.name.to_string()
}
// Decide, based on cuisine, to which `Bucket` a restaurant belongs.
// `Bucket` is just a type alias for an unsigned integer aka usize.
// Matches in lower buckets will be returned before matches in higher buckets.
fn bucket(&self) -> Bucket {
match self.cuisine {
"italian" => 0,
"german" => 0,
"chinese" => 1,
_ => 5
}
}
}
// 3. Create some restaurants and the according iterator
let restaurants = vec![
Restaurant{name:"India Man", cuisine:"indian"},
Restaurant{name:"Ami Guy", cuisine:"american"},
Restaurant{name:"Italiano Pizza", cuisine:"italian"},
Restaurant{name:"Sushi House", cuisine:"chinese"},
Restaurant{name:"Brezel Hut", cuisine:"german"}
];
let iter = restaurants.iter();
// 4. Create the `LookupTable`
let lookup_table = ironstorm_lookup::LookupTable::from_iter(iter);
// 5. Find restaurants containing `i`
let mut result_iter = lookup_table.find("i");
// two times 'Italiano pizza', because it's in the lowest bucket
// two times because it has two lower case `i` in the name
assert_eq!(result_iter.next().unwrap().name, "Italiano Pizza");
assert_eq!(result_iter.next().unwrap().name, "Italiano Pizza");
// 'Sushi House', because it's in the second lowest bucket
assert_eq!(result_iter.next().unwrap().name, "Sushi House");
// 'Ami Guy' or ' India Man'
// They are in the same bucket and there is no order within the same bucket
let indian_or_american_1 = result_iter.next().unwrap().name;
assert!(indian_or_american_1=="India Man" || indian_or_american_1=="Ami Guy");
// The other one of 'Ami Guy' or ' India Man'
let indian_or_american_2 = result_iter.next().unwrap().name;
assert!(indian_or_american_2=="India Man" || indian_or_american_2=="Ami Guy");
assert!(indian_or_american_1 != indian_or_american_2);
// No more matches
// "Brezel Hut" doesn't contain an "i" and was not part of the result.
assert!(result_iter.next().is_none());
Licensed under either of
at your option
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.