ironstorm_lookup

Build Status license: Apache2+MIT rurst: nightly

Overview

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.

Design goals

Accepted drawbacks

Basic Usage

  1. Create a custom type for the data you want to seacrh for, e.g.: a Movie struct
  2. Implement the Lookup trait for your custom type.
  3. Create an Iterator that will iterate over all the elements you would like to put into the LookupTable
  4. Create a new LookupTable by calling LookupTable::from_iter(myMoviesIterator)
  5. Call myMoviesLookupTable.find("hero") to get an lazy 'Iterator' over all matching elements

Example

Let's build a LookupTable to find restaurants by name.

```rust 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 searchabletext(&self) -> String { self.name.tostring() } // 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 lookuptable = ironstormlookup::LookupTable::from_iter(iter);

// 5. Find restaurants containing i let mut resultiter = lookuptable.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 asserteq!(resultiter.next().unwrap().name, "Italiano Pizza"); asserteq!(resultiter.next().unwrap().name, "Italiano Pizza");

// 'Sushi House', because it's in the second lowest bucket asserteq!(resultiter.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 indianoramerican1 = resultiter.next().unwrap().name; assert!(indianoramerican1=="India Man" || indianoramerican1=="Ami Guy");

// The other one of 'Ami Guy' or ' India Man' let indianoramerican2 = resultiter.next().unwrap().name; assert!(indianoramerican2=="India Man" || indianoramerican2=="Ami Guy"); assert!(indianoramerican1 != indianoramerican2);

// No more matches // "Brezel Hut" doesn't contain an "i" and was not part of the result. assert!(resultiter.next().isnone()); ```

License

Licensed under either of

at your option

Contribution

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.