Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Jan 17, 2020
1 parent 546a3c8 commit 478bc67
Show file tree
Hide file tree
Showing 21 changed files with 984 additions and 946 deletions.
5 changes: 4 additions & 1 deletion benches/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ fn get_words() -> Vec<String> {
}

fn get_words_outputs() -> Vec<(String, u64)> {
WORDS.lines().map(|s| (s.to_owned(), s.len() as u64)).collect()
WORDS
.lines()
.map(|s| (s.to_owned(), s.len() as u64))
.collect()
}

#[bench]
Expand Down
20 changes: 7 additions & 13 deletions benches/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

extern crate fnv;
extern crate tantivy_fst;
#[macro_use] extern crate lazy_static;
#[macro_use]
extern crate lazy_static;
extern crate test;

const STR_WORDS: &'static str = include_str!("./../data/words-100000");
Expand Down Expand Up @@ -46,10 +47,9 @@ macro_rules! search {
})
}


#[bench]
fn fst_streams(b: &mut Bencher) {
use tantivy_fst::{Streamer, IntoStreamer};
use tantivy_fst::{IntoStreamer, Streamer};
lazy_static! {
static ref FST: Fst = {
let mut bfst = Builder::memory();
Expand All @@ -76,9 +76,7 @@ macro_rules! search {
fn hash_fnv_contains(b: &mut Bencher) {
type Fnv = BuildHasherDefault<FnvHasher>;
lazy_static! {
static ref SET: HashSet<String, Fnv> = {
$keys.clone().into_iter().collect()
};
static ref SET: HashSet<String, Fnv> = { $keys.clone().into_iter().collect() };
}
let mut i = 0;
b.iter(|| {
Expand All @@ -90,9 +88,7 @@ macro_rules! search {
#[bench]
fn hash_sip_contains(b: &mut Bencher) {
lazy_static! {
static ref SET: HashSet<String> = {
$keys.clone().into_iter().collect()
};
static ref SET: HashSet<String> = { $keys.clone().into_iter().collect() };
}
let mut i = 0;
b.iter(|| {
Expand All @@ -104,9 +100,7 @@ macro_rules! search {
#[bench]
fn btree_contains(b: &mut Bencher) {
lazy_static! {
static ref SET: BTreeSet<String> = {
$keys.clone().into_iter().collect()
};
static ref SET: BTreeSet<String> = { $keys.clone().into_iter().collect() };
}
let mut i = 0;
b.iter(|| {
Expand All @@ -115,7 +109,7 @@ macro_rules! search {
})
}
}
}
};
}

search!(words, ::WORDS);
Expand Down
142 changes: 72 additions & 70 deletions src/automaton/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate levenshtein_automata;

use self::StartsWithStateInternal::*;
use self::levenshtein_automata::Distance;
use self::StartsWithStateInternal::*;

/// Automaton describes types that behave as a finite automaton.
///
Expand Down Expand Up @@ -69,31 +69,37 @@ pub trait Automaton {

/// Returns an automaton that matches the strings that start with something
/// this automaton matches.
fn starts_with(self) -> StartsWith<Self> where Self: Sized {
fn starts_with(self) -> StartsWith<Self>
where
Self: Sized,
{
StartsWith(self)
}

/// Returns an automaton that matches the strings matched by either this or
/// the other automaton.
fn union<Rhs: Automaton>(
self,
rhs: Rhs,
) -> Union<Self, Rhs> where Self: Sized {
fn union<Rhs: Automaton>(self, rhs: Rhs) -> Union<Self, Rhs>
where
Self: Sized,
{
Union(self, rhs)
}

/// Returns an automaton that matches the strings matched by both this and
/// the other automaton.
fn intersection<Rhs: Automaton>(
self,
rhs: Rhs,
) -> Intersection<Self, Rhs> where Self: Sized {
fn intersection<Rhs: Automaton>(self, rhs: Rhs) -> Intersection<Self, Rhs>
where
Self: Sized,
{
Intersection(self, rhs)
}

/// Returns an automaton that matches the strings not matched by this
/// automaton.
fn complement(self) -> Complement<Self> where Self: Sized {
fn complement(self) -> Complement<Self>
where
Self: Sized,
{
Complement(self)
}
}
Expand Down Expand Up @@ -132,7 +138,7 @@ impl Automaton for levenshtein_automata::DFA {
fn is_match(&self, state: &Self::State) -> bool {
match self.distance(*state) {
Distance::Exact(_) => true,
Distance::AtLeast(_) => false
Distance::AtLeast(_) => false,
}
}

Expand All @@ -144,31 +150,37 @@ impl Automaton for levenshtein_automata::DFA {
/// An automaton that matches if the input contains a specific subsequence.
#[derive(Clone, Debug)]
pub struct Subsequence<'a> {
subseq: &'a [u8]
subseq: &'a [u8],
}

impl<'a> Subsequence<'a> {
/// Constructs automaton that matches input containing the
/// specified subsequence.
#[inline]
pub fn new(subsequence: &'a str) -> Subsequence<'a> {
Subsequence { subseq: subsequence.as_bytes() }
Subsequence {
subseq: subsequence.as_bytes(),
}
}
}

impl<'a> Automaton for Subsequence<'a> {
type State = usize;

#[inline]
fn start(&self) -> usize { 0 }
fn start(&self) -> usize {
0
}

#[inline]
fn is_match(&self, &state: &usize) -> bool {
state == self.subseq.len()
}

#[inline]
fn can_match(&self, _: &usize) -> bool { true }
fn can_match(&self, _: &usize) -> bool {
true
}

#[inline]
fn will_always_match(&self, &state: &usize) -> bool {
Expand All @@ -177,7 +189,9 @@ impl<'a> Automaton for Subsequence<'a> {

#[inline]
fn accept(&self, &state: &usize, byte: u8) -> usize {
if state == self.subseq.len() { return state; }
if state == self.subseq.len() {
return state;
}
state + (byte == self.subseq[state]) as usize
}
}
Expand All @@ -192,11 +206,26 @@ pub struct AlwaysMatch;
impl Automaton for AlwaysMatch {
type State = ();

#[inline] fn start(&self) -> () { () }
#[inline] fn is_match(&self, _: &()) -> bool { true }
#[inline] fn can_match(&self, _: &()) -> bool { true }
#[inline] fn will_always_match(&self, _: &()) -> bool { true }
#[inline] fn accept(&self, _: &(), _: u8) -> () { () }
#[inline]
fn start(&self) -> () {
()
}
#[inline]
fn is_match(&self, _: &()) -> bool {
true
}
#[inline]
fn can_match(&self, _: &()) -> bool {
true
}
#[inline]
fn will_always_match(&self, _: &()) -> bool {
true
}
#[inline]
fn accept(&self, _: &(), _: u8) -> () {
()
}
}

/// An automaton that matches a string that begins with something that the
Expand All @@ -209,7 +238,7 @@ pub struct StartsWithState<A: Automaton>(StartsWithStateInternal<A>);

enum StartsWithStateInternal<A: Automaton> {
Done,
Running(A::State)
Running(A::State),
}

impl<A: Automaton> Automaton for StartsWith<A> {
Expand All @@ -229,42 +258,36 @@ impl<A: Automaton> Automaton for StartsWith<A> {
fn is_match(&self, state: &StartsWithState<A>) -> bool {
match state.0 {
Done => true,
Running(_) => false
Running(_) => false,
}
}

fn can_match(&self, state: &StartsWithState<A>) -> bool {
match state.0 {
Done => true,
Running(ref inner) => self.0.can_match(inner)
Running(ref inner) => self.0.can_match(inner),
}
}

fn will_always_match(&self, state: &StartsWithState<A>) -> bool {
match state.0 {
Done => true,
Running(_) => false
Running(_) => false,
}
}

fn accept(
&self,
state: &StartsWithState<A>,
byte: u8,
) -> StartsWithState<A> {
StartsWithState(
match state.0 {
Done => Done,
Running(ref inner) => {
let next_inner = self.0.accept(inner, byte);
if self.0.is_match(&next_inner) {
Done
} else {
Running(next_inner)
}
fn accept(&self, state: &StartsWithState<A>, byte: u8) -> StartsWithState<A> {
StartsWithState(match state.0 {
Done => Done,
Running(ref inner) => {
let next_inner = self.0.accept(inner, byte);
if self.0.is_match(&next_inner) {
Done
} else {
Running(next_inner)
}
}
)
})
}
}

Expand All @@ -279,10 +302,7 @@ impl<A: Automaton, B: Automaton> Automaton for Union<A, B> {
type State = UnionState<A, B>;

fn start(&self) -> UnionState<A, B> {
UnionState(
self.0.start(),
self.1.start()
)
UnionState(self.0.start(), self.1.start())
}

fn is_match(&self, state: &UnionState<A, B>) -> bool {
Expand All @@ -294,15 +314,11 @@ impl<A: Automaton, B: Automaton> Automaton for Union<A, B> {
}

fn will_always_match(&self, state: &UnionState<A, B>) -> bool {
self.0.will_always_match(&state.0)
|| self.1.will_always_match(&state.1)
self.0.will_always_match(&state.0) || self.1.will_always_match(&state.1)
}

fn accept(&self, state: &UnionState<A, B>, byte: u8) -> UnionState<A, B> {
UnionState(
self.0.accept(&state.0, byte),
self.1.accept(&state.1, byte)
)
UnionState(self.0.accept(&state.0, byte), self.1.accept(&state.1, byte))
}
}

Expand All @@ -317,10 +333,7 @@ impl<A: Automaton, B: Automaton> Automaton for Intersection<A, B> {
type State = IntersectionState<A, B>;

fn start(&self) -> IntersectionState<A, B> {
IntersectionState(
self.0.start(),
self.1.start()
)
IntersectionState(self.0.start(), self.1.start())
}

fn is_match(&self, state: &IntersectionState<A, B>) -> bool {
Expand All @@ -335,15 +348,8 @@ impl<A: Automaton, B: Automaton> Automaton for Intersection<A, B> {
self.0.will_always_match(&state.0) && self.1.will_always_match(&state.1)
}

fn accept(
&self,
state: &IntersectionState<A, B>,
byte: u8,
) -> IntersectionState<A, B> {
IntersectionState(
self.0.accept(&state.0, byte),
self.1.accept(&state.1, byte)
)
fn accept(&self, state: &IntersectionState<A, B>, byte: u8) -> IntersectionState<A, B> {
IntersectionState(self.0.accept(&state.0, byte), self.1.accept(&state.1, byte))
}
}

Expand Down Expand Up @@ -373,11 +379,7 @@ impl<A: Automaton> Automaton for Complement<A> {
!self.0.can_match(&state.0)
}

fn accept(
&self,
state: &ComplementState<A>,
byte: u8,
) -> ComplementState<A> {
fn accept(&self, state: &ComplementState<A>, byte: u8) -> ComplementState<A> {
ComplementState(self.0.accept(&state.0, byte))
}
}
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#![warn(missing_docs)]

extern crate byteorder;
#[cfg(test)] extern crate quickcheck;
#[cfg(test)] extern crate rand;
#[cfg(test)] extern crate proptest;
#[cfg(test)]
extern crate proptest;
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
extern crate rand;

pub use automaton::Automaton;
pub use error::{Error, Result};
Expand All @@ -20,9 +23,9 @@ mod regex;

pub use self::regex::Regex;

mod error;
#[path = "automaton/mod.rs"]
mod inner_automaton;
mod error;
#[path = "map.rs"]
mod inner_map;
pub mod raw;
Expand Down
Loading

0 comments on commit 478bc67

Please sign in to comment.