Skip to content

Commit

Permalink
Formatting: Fixes formatting with rust-fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
aserebryakov committed Mar 2, 2018
1 parent 1bfe690 commit 57b8acf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
69 changes: 38 additions & 31 deletions benches/basic_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate test;
use test::Bencher;
use std::collections::HashMap;


fn generate_keys() -> Vec<String> {
let mut keys = Vec::new();

Expand All @@ -22,7 +21,6 @@ fn generate_keys() -> Vec<String> {
keys
}


#[bench]
fn trie_match(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -34,7 +32,6 @@ fn trie_match(b: &mut Bencher) {
})
}


#[bench]
fn trie_mismatch(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -46,18 +43,18 @@ fn trie_mismatch(b: &mut Bencher) {
})
}


#[bench]
fn hash_map_match(b: &mut Bencher) {
let mut h = HashMap::new();
let key = String::from("test");

h.insert(key.clone(), true);

b.iter(|| { h.get(&key); })
b.iter(|| {
h.get(&key);
})
}


#[bench]
fn hash_map_mismatch(b: &mut Bencher) {
let mut h = HashMap::new();
Expand All @@ -66,10 +63,11 @@ fn hash_map_mismatch(b: &mut Bencher) {

h.insert(key, true);

b.iter(|| { h.get(&notkey); })
b.iter(|| {
h.get(&notkey);
})
}


#[bench]
fn trie_massive_match(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -79,12 +77,13 @@ fn trie_massive_match(b: &mut Bencher) {
t.insert(key.chars(), key.clone());
}

b.iter(|| for key in &keys {
assert_eq!(t.contains_key(key.chars()), true);
b.iter(|| {
for key in &keys {
assert_eq!(t.contains_key(key.chars()), true);
}
})
}


#[bench]
fn trie_massive_mismatch_on_0(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -95,12 +94,13 @@ fn trie_massive_mismatch_on_0(b: &mut Bencher) {
t.insert(key.chars(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
}
})
}


#[bench]
fn trie_massive_mismatch_on_1(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -111,12 +111,13 @@ fn trie_massive_mismatch_on_1(b: &mut Bencher) {
t.insert(key.chars(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
}
})
}


#[bench]
fn trie_massive_mismatch_on_2(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -127,12 +128,13 @@ fn trie_massive_mismatch_on_2(b: &mut Bencher) {
t.insert(key.chars(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
}
})
}


#[bench]
fn trie_massive_mismatch_on_3(b: &mut Bencher) {
let mut t = gtrie::Trie::new();
Expand All @@ -143,12 +145,13 @@ fn trie_massive_mismatch_on_3(b: &mut Bencher) {
t.insert(key.chars(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(t.contains_key(mismatching.chars()), false);
}
})
}


#[bench]
fn hash_map_massive_match(b: &mut Bencher) {
let mut h = HashMap::new();
Expand All @@ -158,12 +161,13 @@ fn hash_map_massive_match(b: &mut Bencher) {
h.insert(key.clone(), key.clone());
}

b.iter(|| for key in &keys {
assert_eq!(h.contains_key(key), true);
b.iter(|| {
for key in &keys {
assert_eq!(h.contains_key(key), true);
}
})
}


#[bench]
fn hash_map_massive_mismatch_on_0(b: &mut Bencher) {
let mut h = HashMap::new();
Expand All @@ -174,12 +178,13 @@ fn hash_map_massive_mismatch_on_0(b: &mut Bencher) {
h.insert(key.clone(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(h.contains_key(&mismatching), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(h.contains_key(&mismatching), false);
}
})
}


#[bench]
fn hash_map_massive_mismatch_on_0_one_symbol_key(b: &mut Bencher) {
let mut h = HashMap::new();
Expand All @@ -190,7 +195,9 @@ fn hash_map_massive_mismatch_on_0_one_symbol_key(b: &mut Bencher) {
h.insert(key.clone(), key.clone());
}

b.iter(|| for _ in 0..keys.len() {
assert_eq!(h.contains_key(&mismatching), false);
b.iter(|| {
for _ in 0..keys.len() {
assert_eq!(h.contains_key(&mismatching), false);
}
})
}
13 changes: 3 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ use std::cmp::{Eq, Ord};
use std::clone::Clone;
use trie_node::TrieNode;


/// Prefix tree object
pub struct Trie<T, U> {
/// Root of the prefix tree
root: Rc<RefCell<TrieNode<T, U>>>,
}


impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
/// Creates a new `Trie` object
///
Expand All @@ -86,10 +84,11 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
/// let t = Trie::<char, String>::new();
/// ```
pub fn new() -> Trie<T, U> {
Trie { root: Rc::new(RefCell::new(TrieNode::new(None))) }
Trie {
root: Rc::new(RefCell::new(TrieNode::new(None))),
}
}


/// Checks that trie is empty
///
/// # Example
Expand All @@ -104,7 +103,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
self.root.borrow().children.is_empty()
}


/// Adds a new key to the trie
///
/// # Example
Expand All @@ -128,7 +126,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
(*node).borrow_mut().set_value(value);
}


/// Clears the trie
///
/// # Example
Expand All @@ -147,7 +144,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
(*self.root).borrow_mut().children.clear();
}


/// Looks for the key in trie
///
/// # Example
Expand Down Expand Up @@ -178,7 +174,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
}
}


/// Gets the value from the tree by key
///
/// # Example
Expand All @@ -202,7 +197,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
}
}


/// Sets the value pointed by a key
///
/// # Example
Expand Down Expand Up @@ -231,7 +225,6 @@ impl<T: Eq + Ord + Clone, U: Clone> Trie<T, U> {
}
}


/// Finds the node in the trie by the key
///
/// Internal API
Expand Down
7 changes: 0 additions & 7 deletions src/trie_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ use std::cell::RefCell;
use std::cmp::{Eq, Ord};
use std::clone::Clone;


pub struct TrieNode<T, U> {
pub value: Option<U>,
pub children: Vec<(T, Rc<RefCell<TrieNode<T, U>>>)>,
}


impl<T: Eq + Ord + Clone, U: Clone> TrieNode<T, U> {
pub fn new(value: Option<U>) -> TrieNode<T, U> {
TrieNode {
Expand All @@ -40,7 +38,6 @@ impl<T: Eq + Ord + Clone, U: Clone> TrieNode<T, U> {
}
}


pub fn find(&self, key: &T) -> Option<Rc<RefCell<TrieNode<T, U>>>> {
if let Ok(idx) = self.children.binary_search_by(|x| x.0.cmp(key)) {
return Some(self.children[idx].1.clone());
Expand All @@ -49,7 +46,6 @@ impl<T: Eq + Ord + Clone, U: Clone> TrieNode<T, U> {
None
}


pub fn insert(&mut self, key: &T) -> Rc<RefCell<TrieNode<T, U>>> {
match self.find(key) {
None => {
Expand All @@ -62,17 +58,14 @@ impl<T: Eq + Ord + Clone, U: Clone> TrieNode<T, U> {
}
}


pub fn set_value(&mut self, value: U) {
self.value = Some(value);
}


pub fn get_value(&self) -> Option<U> {
self.value.clone()
}


pub fn may_be_leaf(&self) -> bool {
self.value.is_some()
}
Expand Down
5 changes: 0 additions & 5 deletions tests/api_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate gtrie;


#[cfg(test)]
mod tests {
use gtrie::Trie;
Expand All @@ -10,7 +9,6 @@ mod tests {
assert_eq!(Trie::<char, String>::new().is_empty(), true);
}


#[test]
fn add_word_to_trie() {
let mut t = Trie::new();
Expand All @@ -19,7 +17,6 @@ mod tests {
assert_eq!(t.is_empty(), false);
}


#[test]
fn contains_key_test() {
let mut t = Trie::new();
Expand All @@ -35,7 +32,6 @@ mod tests {
assert_eq!(t.contains_key(notintest), false);
}


#[test]
fn contains_key_sub_path_test() {
let mut t = Trie::new();
Expand All @@ -52,7 +48,6 @@ mod tests {
assert_eq!(t.contains_key(notintest), false);
}


#[test]
fn clear_test() {
let mut t = Trie::new();
Expand Down

0 comments on commit 57b8acf

Please sign in to comment.