Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 17 additions & 41 deletions ulist/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ use std::collections::HashSet;

pub fn _fill_na<T: Clone>(vec: &mut [T], na_indexes: Ref<HashSet<usize>>, na_value: T) {
for i in na_indexes.iter() {
// TODO: Use get_unchecked_mut instead.
// let ptr = unsafe { vec.get_unchecked_mut(*i) };
// *ptr = na_value.clone();
vec[*i] = na_value.clone();
let ptr = unsafe { vec.get_unchecked_mut(*i) };
*ptr = na_value.clone();
}
}

Expand Down Expand Up @@ -150,9 +148,6 @@ where
let mut i = 0;
for ((j, x), cond) in self.values().iter().enumerate().zip(cond.iter()) {
if *cond {
// TODO: Use get_unchecked_mut instead
// let ptr = unsafe { vec.get_unchecked_mut(i) };
// *ptr = x.clone();
vec.push(x.clone());
if self.na_indexes().contains(&j) {
hset.insert(i);
Expand All @@ -179,20 +174,19 @@ where
}

fn get_by_indexes(&self, indexes: &IndexList) -> PyResult<Self> {
// TODO: Put this kind of check
// where there is unsafe block.
if indexes.back() >= self.size() {
return Err(PyIndexError::new_err("Index out of range!"));
}
// TODO: use get_unchecked instead.
let mut vec: Vec<T> = Vec::new();
let mut vec: Vec<T> = Vec::with_capacity(indexes.size());
let mut hset: HashSet<usize> = HashSet::new();
for (i, j) in indexes.values().iter().enumerate() {
vec.push(self.values()[*j].clone());
let elem = unsafe { self.values().get_unchecked(*j).clone() };
vec.push(elem);
if self.na_indexes().contains(j) {
hset.insert(i);
}
}
hset.shrink_to_fit();
Ok(List::_new(vec, hset))
}

Expand Down Expand Up @@ -238,14 +232,9 @@ where
let n = self.size();
let mut vec = self.values_mut();
for i in 0..n {
// TODO: Use get_unchecked_mut instead.
// let ptr = unsafe { vec.get_unchecked_mut(i) };
// if *ptr == old {
// *ptr = self.na_value();
// self.na_indexes_mut().insert(i);
// }
if vec[i] == old {
vec[i] = self.na_value();
let ptr = unsafe{ vec.get_unchecked_mut(i)};
if *ptr == old {
*ptr = self.na_value();
self.na_indexes_mut().insert(i);
}
}
Expand All @@ -255,24 +244,18 @@ where
let n = self.size();
let mut vec = self.values_mut();
for i in 0..n {
// TODO: Use get_unchecked_mut instead.
// let ptr = unsafe { vec.get_unchecked_mut(i) };
// if *ptr == old {
// *ptr = new.clone();
// }
if vec[i] == old {
vec[i] = new.clone();
let ptr = unsafe { vec.get_unchecked_mut(i) };
if *ptr == old {
*ptr = new.clone();
}
}
}

fn replace_na(&self, new: T) {
let mut vec = self.values_mut();
for i in self.na_indexes().iter() {
// TODO: Use get_unchecked_mut instead.
// let ptr = unsafe { vec.get_unchecked_mut(*i) };
// *ptr = new.clone();
vec[*i] = new.clone();
let ptr = unsafe { vec.get_unchecked_mut(*i) };
*ptr = new.clone();
}
self.na_indexes_mut().clear();
}
Expand All @@ -282,19 +265,12 @@ where
return Err(PyIndexError::new_err("Index out of range!"));
}
let mut vec = self.values_mut();
// TODO: Use get_unchecked_mut instead.
// let ptr = unsafe { vec.get_unchecked_mut(index) };
// if let Some(i) = elem {
// *ptr = i;
// } else {
// *ptr = self.na_value();
// self.na_indexes_mut().insert(index);
// }
let ptr = unsafe{vec.get_unchecked_mut(index)};
if let Some(i) = elem {
vec[index] = i;
*ptr = i;
self.na_indexes_mut().remove(&index);
} else {
vec[index] = self.na_value();
*ptr = self.na_value();
self.na_indexes_mut().insert(index);
}
Ok(())
Expand Down
7 changes: 5 additions & 2 deletions ulist/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::cell::RefMut;
use std::collections::HashMap;
use std::collections::HashSet;
use std::ops::Fn;
use std::cmp::max;

/// List with boolean type elements.
#[pyclass]
Expand Down Expand Up @@ -57,7 +58,7 @@ impl BooleanList {
self._check_len_eq(other)?;
let hset1 = self.na_indexes();
let hset2 = other.na_indexes();
let mut hset: HashSet<usize> = HashSet::new();
let mut hset: HashSet<usize> = HashSet::with_capacity(max(hset1.len(), hset2.len()));
let vec = self
.values()
.iter()
Expand All @@ -83,6 +84,7 @@ impl BooleanList {
}
})
.collect();
hset.shrink_to_fit();
Ok(BooleanList::new(vec, hset))
}

Expand Down Expand Up @@ -179,7 +181,7 @@ impl BooleanList {
self._check_len_eq(other)?;
let hset1 = self.na_indexes();
let hset2 = other.na_indexes();
let mut hset: HashSet<usize> = HashSet::new();
let mut hset: HashSet<usize> = HashSet::with_capacity(max(hset1.len(), hset2.len()));
let vec = self
.values()
.iter()
Expand All @@ -205,6 +207,7 @@ impl BooleanList {
}
})
.collect();
hset.shrink_to_fit();
Ok(BooleanList::new(vec, hset))
}

Expand Down
11 changes: 6 additions & 5 deletions ulist/src/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ where
}

let mut vec = vec![default; cond[0].size()];
// for j in 0..cond[0].size() {
for (j, item) in vec.iter_mut().enumerate().take(cond[0].size()) {
for j in 0..vec.len() {
for i in 0..cond.len() {
// TODO: Improve the benchmark.
if cond[i].get(j).unwrap().unwrap() {
*item = choices[i].clone();
let ptr = unsafe { cond.get_unchecked(i) };
let choice = unsafe { choices.get_unchecked(i) };
// TODO: implement iter() for BooleanList
if ptr.get(j).unwrap().unwrap() {
vec[j] = choice.clone();
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ulist/src/floatings/float32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl FloatList32 {
vec.push(self.na_value());
}
// Construct List.
let mut hset = HashSet::new();
let mut hset = HashSet::with_capacity(1);
if self.count_na() > 0 {
hset.insert(vec.len() - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion ulist/src/floatings/float64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl FloatList64 {
vec.push(self.na_value());
}
// Construct List.
let mut hset = HashSet::new();
let mut hset = HashSet::with_capacity(1);
if self.count_na() > 0 {
hset.insert(vec.len() - 1);
}
Expand Down
4 changes: 4 additions & 0 deletions ulist/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ impl IndexList {
pub fn to_list(&self) -> Vec<usize> {
self._values.clone()
}

pub fn size(&self) -> usize {
self._values.len()
}
}
2 changes: 1 addition & 1 deletion ulist/src/non_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
}
};
// Construct List.
let mut hset = HashSet::new();
let mut hset = HashSet::with_capacity(1);
if self.count_na() > 0 {
hset.insert(vec_dedup.len() - 1);
}
Expand Down