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
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
- name: Install Python packages
run: |
pip install -r ./requirements/dev.txt
- name: Cargo clippy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use Rust Lints here.

run: |
cd ulist
cargo clippy
- name: Build ulist
run: |
maturin build --out dist -m ulist/Cargo.toml
Expand Down
20 changes: 8 additions & 12 deletions ulist/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::cell::Ref;
use std::cell::RefMut;
use std::collections::HashSet;

pub fn _fill_na<T: Clone>(vec: &mut Vec<T>, na_indexes: Ref<HashSet<usize>>, na_value: T) {
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) };
Expand Down Expand Up @@ -38,7 +38,7 @@ where
}

fn _fn_scala<U>(&self, func: impl Fn(&T) -> U) -> Vec<U> {
self.values().iter().map(|x| func(x)).collect()
self.values().iter().map(func).collect()
}

fn _sort(&self) {
Expand Down Expand Up @@ -77,7 +77,7 @@ where
return false;
}
}
return true;
true
}

fn append(&self, elem: Option<T>) {
Expand All @@ -98,8 +98,8 @@ where
self.na_indexes().len()
}

fn cycle(vec: &Vec<T>, size: usize) -> Self {
let v = vec.iter().cycle().take(size).map(|x| x.clone()).collect();
fn cycle(vec: &[T], size: usize) -> Self {
let v: Vec<_> = vec.iter().cycle().take(size).cloned().collect();
List::_new(v, HashSet::new())
}

Expand Down Expand Up @@ -165,13 +165,11 @@ where
// TODO: use get_unchecked instead.
let mut vec: Vec<T> = Vec::new();
let mut hset: HashSet<usize> = HashSet::new();
let mut i: usize = 0;
for j in indexes.values().iter() {
for (i, j) in indexes.values().iter().enumerate() {
vec.push(self.values()[*j].clone());
if self.na_indexes().contains(j) {
hset.insert(i);
}
i += 1;
}
Ok(List::_new(vec, hset))
}
Expand Down Expand Up @@ -207,10 +205,8 @@ where
} else {
self.replace_by_na(_old)
}
} else {
if let Some(_new) = new {
self.replace_na(_new)
}
} else if let Some(_new) = new {
self.replace_na(_new)
}
}

Expand Down
8 changes: 4 additions & 4 deletions ulist/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl BooleanList {
}

pub fn and_(&self, other: &Self) -> PyResult<Self> {
_logical_operate(&self, &other, |x, y| x && y)
_logical_operate(self, other, |x, y| x && y)
}

pub fn any(&self) -> bool {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl BooleanList {
}

pub fn not_(&self) -> Self {
let mut vec = self.values().iter().map(|&x| !x).collect();
let mut vec: Vec<_> = self.values().iter().map(|&x| !x).collect();
_fill_na(&mut vec, self.na_indexes(), false);
let hset = self.na_indexes().clone();
BooleanList::new(vec, hset)
Expand All @@ -121,7 +121,7 @@ impl BooleanList {
}

pub fn or_(&self, other: &Self) -> PyResult<Self> {
_logical_operate(&self, &other, |x, y| x || y)
_logical_operate(self, other, |x, y| x || y)
}

pub fn pop(&self) {
Expand Down Expand Up @@ -159,7 +159,7 @@ impl BooleanList {
.iter()
.enumerate()
.filter(|(_, y)| **y)
.map(|(x, _)| x.clone())
.map(|(x, _)| x)
.collect();
IndexList::new(vec)
}
Expand Down
9 changes: 5 additions & 4 deletions ulist/src/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::collections::HashSet;

fn select<T, U>(
py: Python,
conditions: &Vec<Py<BooleanList>>,
choices: &Vec<T>,
conditions: &[Py<BooleanList>],
choices: &[T],
default: T,
) -> PyResult<U>
where
Expand All @@ -34,11 +34,12 @@ where
}

let mut vec = vec![default; cond[0].size()];
for j in 0..cond[0].size() {
// for j in 0..cond[0].size() {
for (j, item) in vec.iter_mut().enumerate().take(cond[0].size()) {
for i in 0..cond.len() {
// TODO: Improve the benchmark.
if cond[i].get(j).unwrap().unwrap() {
vec[j] = choices[i].clone();
*item = choices[i].clone();
break;
}
}
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 @@ -384,7 +384,7 @@ impl AsStringList for FloatList64 {
}
}

fn _sort(vec: &mut Vec<f64>, ascending: bool) {
fn _sort(vec: &mut [f64], ascending: bool) {
if ascending {
vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
} else {
Expand Down
2 changes: 1 addition & 1 deletion ulist/src/integers/int32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl IntegerList32 {
.na_indexes()
.iter()
.chain(other.na_indexes().iter())
.map(|x| x.clone())
.copied()
.collect();
Ok(FloatList64::new(NumericalList::div(self, other)?, hset))
}
Expand Down
2 changes: 1 addition & 1 deletion ulist/src/integers/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl IntegerList64 {
.na_indexes()
.iter()
.chain(other.na_indexes().iter())
.map(|x| x.clone())
.copied()
.collect();
Ok(FloatList64::new(NumericalList::div(self, other)?, hset))
}
Expand Down
41 changes: 21 additions & 20 deletions ulist/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use crate::boolean::BooleanList;
use crate::floatings::FloatList64;
use crate::integers::IntegerList64;
use crate::string::StringList;
// use crate::boolean::BooleanList;
// use crate::floatings::FloatList64;
// use crate::integers::IntegerList64;
// use crate::string::StringList;
use pyo3::prelude::*;
use std::collections::HashSet;
use std::iter::FromIterator;
// use std::collections::HashSet;
// use std::iter::FromIterator;

#[pyfunction]
pub fn read_csv(py: Python) -> Vec<PyObject> {
pub fn read_csv(_py: Python) -> Vec<PyObject> {
// This is an example implementation of `read_csv` function, which will return
// PyList[BooleanList[True, False, None], IntegerList64[2, 3, None],
// FloatList64[2.0, 3.0, None], StringList['foo', 'bar', None]]
let blist = BooleanList::new(vec![true, false, false], HashSet::from_iter(vec![2]));
let ilist = IntegerList64::new(vec![2, 3, 0], HashSet::from_iter(vec![2]));
let flist = FloatList64::new(vec![2.0, 3.0, 0.0], HashSet::from_iter(vec![2]));
let slist = StringList::new(
vec!["foo".to_string(), "bar".to_string(), "".to_string()],
HashSet::from_iter(vec![2]),
);
let mut result: Vec<PyObject> = Vec::new();
result.push(blist.into_py(py));
result.push(ilist.into_py(py));
result.push(flist.into_py(py));
result.push(slist.into_py(py));
return result;
// let blist = BooleanList::new(vec![true, false, false], HashSet::from_iter(vec![2]));
// let ilist = IntegerList64::new(vec![2, 3, 0], HashSet::from_iter(vec![2]));
// let flist = FloatList64::new(vec![2.0, 3.0, 0.0], HashSet::from_iter(vec![2]));
// let slist = StringList::new(
// vec!["foo".to_string(), "bar".to_string(), "".to_string()],
// HashSet::from_iter(vec![2]),
// );
// let mut result: Vec<PyObject> = Vec::new();
// result.push(blist.into_py(py));
// result.push(ilist.into_py(py));
// result.push(flist.into_py(py));
// result.push(slist.into_py(py));
// return result;
todo!()
}
4 changes: 2 additions & 2 deletions ulist/src/numerical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
}

fn _fn_num<W: Clone>(&self, func: impl Fn(T) -> W, default: W) -> Vec<W> {
let mut vec = self.values().iter().map(|&x| func(x)).collect();
let mut vec: Vec<_> = self.values().iter().map(|&x| func(x)).collect();
_fill_na(&mut vec, self.na_indexes(), default);
vec
}
Expand All @@ -44,7 +44,7 @@ where
.na_indexes()
.iter()
.chain(other.na_indexes().iter())
.map(|x| x.clone())
.copied()
.collect();
let result: Self = List::_new(vec, hset);
_fill_na(
Expand Down
8 changes: 4 additions & 4 deletions ulist/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl StringList {
}

pub fn contains(&self, elem: &str) -> BooleanList {
let mut vec = self.values().iter().map(|x| x.contains(elem)).collect();
let mut vec: Vec<_> = self.values().iter().map(|x| x.contains(elem)).collect();
_fill_na(&mut vec, self.na_indexes(), false);
BooleanList::new(vec, HashSet::new())
}
Expand All @@ -87,7 +87,7 @@ impl StringList {
}

pub fn ends_with(&self, elem: &str) -> BooleanList {
let mut vec = self.values().iter().map(|x| x.ends_with(elem)).collect();
let mut vec: Vec<_> = self.values().iter().map(|x| x.ends_with(elem)).collect();
_fill_na(&mut vec, self.na_indexes(), false);
BooleanList::new(vec, HashSet::new())
}
Expand Down Expand Up @@ -138,13 +138,13 @@ impl StringList {
}

pub fn starts_with(&self, elem: &str) -> BooleanList {
let mut vec = self.values().iter().map(|x| x.starts_with(elem)).collect();
let mut vec: Vec<_> = self.values().iter().map(|x| x.starts_with(elem)).collect();
_fill_na(&mut vec, self.na_indexes(), false);
BooleanList::new(vec, HashSet::new())
}

pub fn str_len(&self) -> IntegerList64 {
let mut vec = self.values().iter().map(|x| x.len() as i64).collect();
let mut vec: Vec<_> = self.values().iter().map(|x| x.len() as i64).collect();
_fill_na(&mut vec, self.na_indexes(), 0);
IntegerList64::new(vec, self.na_indexes().clone())
}
Expand Down