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
18 changes: 18 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def test_methods_no_arg(
('__getitem__', 'int', [1, 2, 3], 1, {'index': 0}),
('__getitem__', 'string', ['foo', 'bar', 'baz'], 'foo', {'index': 0}),

('__getitem__', 'bool', [True, False, True],
[True, True], {'index': ul.IndexList([0, 2])}),
('__getitem__', 'float', [1.0, 2.0, 3.0], [1.0, 3.0],
{'index': ul.IndexList([0, 2])}),
('__getitem__', 'int', [1, 2, 3],
[1, 3], {'index': ul.IndexList([0, 2])}),
('__getitem__', 'string', ['foo', 'bar', 'baz'],
['foo', 'baz'], {'index': ul.IndexList([0, 2])}),

("apply", "bool", [True, False], [
False, True], {"fn": lambda x: x == False},), # noqa: E712
("apply", "float", [1.0, 2.0], [
Expand Down Expand Up @@ -197,6 +206,15 @@ def test_methods_no_arg(
('get', 'int', [1, 2, 3], 1, {'index': 0}),
('get', 'string', ['foo', 'bar', 'baz'], 'foo', {'index': 0}),

('get_by_indexes', 'bool', [True, False, True],
[True, True], {'indexes': ul.IndexList([0, 2])}),
('get_by_indexes', 'float', [1.0, 2.0, 3.0], [1.0, 3.0],
{'indexes': ul.IndexList([0, 2])}),
('get_by_indexes', 'int', [1, 2, 3],
[1, 3], {'indexes': ul.IndexList([0, 2])}),
('get_by_indexes', 'string', ['foo', 'bar', 'baz'],
['foo', 'baz'], {'indexes': ul.IndexList([0, 2])}),

("not_equal_scala", 'bool', [False, True, False], [
True, False, True], {"elem": True}),
("not_equal_scala", 'float', [1.0, 2.0, 3.0], [
Expand Down
25 changes: 19 additions & 6 deletions ulist/python/ulist/core.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations # To avoid circular import.
from typing import TYPE_CHECKING, Callable

from typing import TYPE_CHECKING, Callable, Union

from .typedef import ELEM, LIST_PY, LIST_RS, NUM, NUM_OR_LIST, COUNTER
from .ulist import BooleanList, FloatList, IntegerList, StringList, IndexList

from .typedef import COUNTER, ELEM, LIST_PY, LIST_RS, NUM
from .ulist import BooleanList, FloatList, IndexList, IntegerList, StringList

if TYPE_CHECKING: # To avoid circular import.
from .control_flow import CaseObject

NUM_OR_LIST = Union[NUM, "UltraFastList"]
ELEM_OR_LIST = Union[ELEM, "UltraFastList"]


class UltraFastList:
"""
Expand Down Expand Up @@ -59,9 +61,16 @@ def __eq__(self, other: int) -> "UltraFastList": # type: ignore
"""Return self == other."""
return self.equal_scala(other)

def __getitem__(self, index: int) -> ELEM:
def __getitem__(self, index: Union[int, IndexList]) -> ELEM_OR_LIST:
"""Return self[index]."""
return self._values.get(index)
if isinstance(index, int):
return self._values.get(index)
elif isinstance(index, IndexList):
return UltraFastList(self._values.get_by_indexes(index))
else:
raise TypeError(
"Parameter index should be int or IndexList type!"
)

def __ge__(self, other: int) -> "UltraFastList":
"""Return self >= other."""
Expand Down Expand Up @@ -305,6 +314,10 @@ def get(self, index: int) -> ELEM:
"""Return self[index]."""
return self._values.get(index)

def get_by_indexes(self, indexes: IndexList) -> UltraFastList:
"""Return self[indexes]."""
return UltraFastList(self._values.get_by_indexes(indexes))

def greater_than_or_equal_scala(self, elem: NUM) -> "UltraFastList":
"""Return self >= elem."""
assert not isinstance(self._values, (BooleanList, StringList))
Expand Down
2 changes: 0 additions & 2 deletions ulist/python/ulist/typedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
ELEM = Union[int, float, bool, str]
NUM = Union[int, float]
LIST_PY = Union[List[float], List[int], List[bool], List[str]]
NUM_LIST_PY = Union[List[float], List[int]]
LIST_RS = Union[FloatList, IntegerList, BooleanList, StringList]
NUM_LIST_RS = Union[FloatList, IntegerList]
NUM_OR_LIST = Union[NUM, NUM_LIST_RS]
COUNTER = Union[Dict[int, int], Dict[bool, int], Dict[str, int]]
4 changes: 4 additions & 0 deletions ulist/python/ulist/ulist.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BooleanList:
def equal_scala(self, elem: ELEM) -> BooleanList: ...
def filter(self, condition: BooleanList) -> BooleanList: ...
def get(self, index: int) -> bool: ...
def get_by_indexes(self, indexes: IndexList) -> BooleanList: ...
def not_(self) -> BooleanList: ...
def not_equal_scala(self, elem: ELEM) -> BooleanList: ...
def or_(self, other: BooleanList) -> BooleanList: ...
Expand Down Expand Up @@ -58,6 +59,7 @@ class FloatList:
def equal_scala(self, elem: NUM) -> BooleanList: ...
def filter(self, condition: BooleanList) -> FloatList: ...
def get(self, index: int) -> float: ...
def get_by_indexes(self, indexes: IndexList) -> FloatList: ...
def greater_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
def greater_than_scala(self, elem: NUM) -> BooleanList: ...
def less_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
Expand Down Expand Up @@ -104,6 +106,7 @@ class IntegerList:
def equal_scala(self, elem: NUM) -> BooleanList: ...
def filter(self, condition: BooleanList) -> IntegerList: ...
def get(self, index: int) -> int: ...
def get_by_indexes(self, indexes: IndexList) -> IntegerList: ...
def greater_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
def greater_than_scala(self, elem: NUM) -> BooleanList: ...
def less_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
Expand Down Expand Up @@ -145,6 +148,7 @@ class StringList:
def equal_scala(self, elem: ELEM) -> BooleanList: ...
def filter(self, condition: BooleanList) -> StringList: ...
def get(self, index: int) -> str: ...
def get_by_indexes(self, indexes: IndexList) -> StringList: ...
def not_equal_scala(self, elem: ELEM) -> BooleanList: ...
def pop(self): ...
@staticmethod
Expand Down
21 changes: 18 additions & 3 deletions ulist/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::boolean::BooleanList;
use crate::index::IndexList;
use std::cell::Ref;
use std::cell::RefMut;

Expand Down Expand Up @@ -44,14 +45,28 @@ where
List::_new(vec)
}

unsafe fn get(&self, index: usize) -> T {
if index < self.size() {
self.values().get_unchecked(index).clone()
fn get(&self, index: usize) -> T {
let vec = self.values();
let val = vec.get(index);
if let Some(result) = val {
return result.clone();
} else {
panic!("Index out of range!")
}
}

unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
if indexes.back() >= self.size() {
panic!("Index out of range!")
}
let vec = indexes
.values()
.iter()
.map(|&x| self.values().get_unchecked(x).clone())
.collect();
List::_new(vec)
}

fn not_equal_scala(&self, elem: T) -> BooleanList {
BooleanList::new(self._fn_scala(|x| x != &elem))
}
Expand Down
6 changes: 5 additions & 1 deletion ulist/src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ impl BooleanList {
List::filter(self, condition)
}

pub unsafe fn get(&self, index: usize) -> bool {
pub fn get(&self, index: usize) -> bool {
List::get(self, index)
}

pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
List::get_by_indexes(self, indexes)
}

pub fn not_(&self) -> Self {
let vec = self.values().iter().map(|&x| !x).collect();
BooleanList::new(vec)
Expand Down
7 changes: 6 additions & 1 deletion ulist/src/float.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::base::List;
use crate::boolean::BooleanList;
use crate::index::IndexList;
use crate::integer::IntegerList;
use crate::numerical::NumericalList;
use crate::string::StringList;
Expand Down Expand Up @@ -85,10 +86,14 @@ impl FloatList {
List::filter(self, condition)
}

pub unsafe fn get(&self, index: usize) -> f32 {
pub fn get(&self, index: usize) -> f32 {
List::get(self, index)
}

pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
List::get_by_indexes(self, indexes)
}

pub fn greater_than_or_equal_scala(&self, elem: f32) -> BooleanList {
NumericalList::greater_than_or_equal_scala(self, elem)
}
Expand Down
6 changes: 6 additions & 0 deletions ulist/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub struct IndexList {
_values: Vec<usize>,
}

impl IndexList {
pub fn values(&self) -> &Vec<usize> {
&self._values
}
}

#[pymethods]
impl IndexList {
// Arrange the following methods in alphabetical order.
Expand Down
7 changes: 6 additions & 1 deletion ulist/src/integer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::base::List;
use crate::boolean::BooleanList;
use crate::float::FloatList;
use crate::index::IndexList;
use crate::non_float::NonFloatList;
use crate::numerical::NumericalList;
use crate::string::StringList;
Expand Down Expand Up @@ -91,10 +92,14 @@ impl IntegerList {
List::filter(self, condition)
}

pub unsafe fn get(&self, index: usize) -> i32 {
pub fn get(&self, index: usize) -> i32 {
List::get(self, index)
}

pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
List::get_by_indexes(self, indexes)
}

pub fn greater_than_or_equal_scala(&self, elem: i32) -> BooleanList {
NumericalList::greater_than_or_equal_scala(self, elem)
}
Expand Down
7 changes: 6 additions & 1 deletion ulist/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::base::List;
use crate::boolean::BooleanList;
use crate::float::FloatList;
use crate::index::IndexList;
use crate::integer::IntegerList;
use crate::non_float::NonFloatList;
use crate::types::AsBooleanList;
Expand Down Expand Up @@ -69,10 +70,14 @@ impl StringList {
List::filter(self, condition)
}

pub unsafe fn get(&self, index: usize) -> String {
pub fn get(&self, index: usize) -> String {
List::get(self, index)
}

pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
List::get_by_indexes(self, indexes)
}

pub fn not_equal_scala(&self, elem: String) -> BooleanList {
List::not_equal_scala(self, elem)
}
Expand Down