Remove needless num dependency from mentat_db and optimize remove_every.

This implementation of `remove_every` is O(n) and not O(n^2) like it was before.
This commit is contained in:
Thom Chiovoloni 2018-06-27 19:45:10 -07:00 committed by Nick Alexander
parent ad2b646700
commit dcc0770ca4
3 changed files with 9 additions and 16 deletions

View file

@ -14,7 +14,6 @@ indexmap = "1"
itertools = "0.7"
lazy_static = "0.2"
log = "0.4"
num = "0.1"
ordered-float = "0.5"
time = "0.1"
petgraph = "0.4.12"

View file

@ -76,8 +76,6 @@ use failure::{
ResultExt,
};
use num;
use rusqlite;
use mentat_core::{
@ -128,17 +126,10 @@ trait Remove<T> where T: PartialEq {
impl<T> Remove<T> for Vec<T> where T: PartialEq {
/// Remove all occurrences from a vector in-place, by equality.
/// Eventually replace with unstable feature: #40062.
fn remove_every(&mut self, item: &T) -> usize {
let mut removed = 0;
let range = num::range_step_inclusive(self.len() as isize - 1, 0, -1);
for i in range {
if self.get(i as usize).map_or(false, |x| x == item) {
self.remove(i as usize);
removed += 1;
}
}
removed
let initial_len = self.len();
self.retain(|v| v != item);
initial_len - self.len()
}
}
@ -187,10 +178,14 @@ impl<K, V> ExtendByAbsorbing for BTreeMap<K, V> where K: Ord, V: Absorb {
#[test]
fn test_vec_remove_item() {
let mut v = vec![1, 2, 3, 4, 5, 4, 3];
v.remove_every(&3);
let count = v.remove_every(&3);
assert_eq!(v, vec![1, 2, 4, 5, 4]);
v.remove_every(&4);
assert_eq!(count, 2);
let count = v.remove_every(&4);
assert_eq!(v, vec![1, 2, 5]);
assert_eq!(count, 2);
let count = v.remove_every(&9);
assert_eq!(count, 0);
}
//

View file

@ -15,7 +15,6 @@ extern crate itertools;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate log;
extern crate num;
extern crate petgraph;
extern crate rusqlite;
extern crate tabwriter;