Skip to content

Commit cfbbd73

Browse files
authored
Merge pull request #140 from tushushu/wip-get-by-index
Wip get by index
2 parents 38d2bc6 + bafe884 commit cfbbd73

File tree

10 files changed

+88
-15
lines changed

10 files changed

+88
-15
lines changed

tests/test_base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def test_methods_no_arg(
133133
('__getitem__', 'int', [1, 2, 3], 1, {'index': 0}),
134134
('__getitem__', 'string', ['foo', 'bar', 'baz'], 'foo', {'index': 0}),
135135
136+
('__getitem__', 'bool', [True, False, True],
137+
[True, True], {'index': ul.IndexList([0, 2])}),
138+
('__getitem__', 'float', [1.0, 2.0, 3.0], [1.0, 3.0],
139+
{'index': ul.IndexList([0, 2])}),
140+
('__getitem__', 'int', [1, 2, 3],
141+
[1, 3], {'index': ul.IndexList([0, 2])}),
142+
('__getitem__', 'string', ['foo', 'bar', 'baz'],
143+
['foo', 'baz'], {'index': ul.IndexList([0, 2])}),
144+
136145
("apply", "bool", [True, False], [
137146
False, True], {"fn": lambda x: x == False},), # noqa: E712
138147
("apply", "float", [1.0, 2.0], [
@@ -197,6 +206,15 @@ def test_methods_no_arg(
197206
('get', 'int', [1, 2, 3], 1, {'index': 0}),
198207
('get', 'string', ['foo', 'bar', 'baz'], 'foo', {'index': 0}),
199208
209+
('get_by_indexes', 'bool', [True, False, True],
210+
[True, True], {'indexes': ul.IndexList([0, 2])}),
211+
('get_by_indexes', 'float', [1.0, 2.0, 3.0], [1.0, 3.0],
212+
{'indexes': ul.IndexList([0, 2])}),
213+
('get_by_indexes', 'int', [1, 2, 3],
214+
[1, 3], {'indexes': ul.IndexList([0, 2])}),
215+
('get_by_indexes', 'string', ['foo', 'bar', 'baz'],
216+
['foo', 'baz'], {'indexes': ul.IndexList([0, 2])}),
217+
200218
("not_equal_scala", 'bool', [False, True, False], [
201219
True, False, True], {"elem": True}),
202220
("not_equal_scala", 'float', [1.0, 2.0, 3.0], [

ulist/python/ulist/core.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from __future__ import annotations # To avoid circular import.
2-
from typing import TYPE_CHECKING, Callable
32

3+
from typing import TYPE_CHECKING, Callable, Union
44

5-
from .typedef import ELEM, LIST_PY, LIST_RS, NUM, NUM_OR_LIST, COUNTER
6-
from .ulist import BooleanList, FloatList, IntegerList, StringList, IndexList
7-
5+
from .typedef import COUNTER, ELEM, LIST_PY, LIST_RS, NUM
6+
from .ulist import BooleanList, FloatList, IndexList, IntegerList, StringList
87

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

11+
NUM_OR_LIST = Union[NUM, "UltraFastList"]
12+
ELEM_OR_LIST = Union[ELEM, "UltraFastList"]
13+
1214

1315
class UltraFastList:
1416
"""
@@ -59,9 +61,16 @@ def __eq__(self, other: int) -> "UltraFastList": # type: ignore
5961
"""Return self == other."""
6062
return self.equal_scala(other)
6163

62-
def __getitem__(self, index: int) -> ELEM:
64+
def __getitem__(self, index: Union[int, IndexList]) -> ELEM_OR_LIST:
6365
"""Return self[index]."""
64-
return self._values.get(index)
66+
if isinstance(index, int):
67+
return self._values.get(index)
68+
elif isinstance(index, IndexList):
69+
return UltraFastList(self._values.get_by_indexes(index))
70+
else:
71+
raise TypeError(
72+
"Parameter index should be int or IndexList type!"
73+
)
6574

6675
def __ge__(self, other: int) -> "UltraFastList":
6776
"""Return self >= other."""
@@ -305,6 +314,10 @@ def get(self, index: int) -> ELEM:
305314
"""Return self[index]."""
306315
return self._values.get(index)
307316

317+
def get_by_indexes(self, indexes: IndexList) -> UltraFastList:
318+
"""Return self[indexes]."""
319+
return UltraFastList(self._values.get_by_indexes(indexes))
320+
308321
def greater_than_or_equal_scala(self, elem: NUM) -> "UltraFastList":
309322
"""Return self >= elem."""
310323
assert not isinstance(self._values, (BooleanList, StringList))

ulist/python/ulist/typedef.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
ELEM = Union[int, float, bool, str]
66
NUM = Union[int, float]
77
LIST_PY = Union[List[float], List[int], List[bool], List[str]]
8-
NUM_LIST_PY = Union[List[float], List[int]]
98
LIST_RS = Union[FloatList, IntegerList, BooleanList, StringList]
109
NUM_LIST_RS = Union[FloatList, IntegerList]
11-
NUM_OR_LIST = Union[NUM, NUM_LIST_RS]
1210
COUNTER = Union[Dict[int, int], Dict[bool, int], Dict[str, int]]

ulist/python/ulist/ulist.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BooleanList:
2121
def equal_scala(self, elem: ELEM) -> BooleanList: ...
2222
def filter(self, condition: BooleanList) -> BooleanList: ...
2323
def get(self, index: int) -> bool: ...
24+
def get_by_indexes(self, indexes: IndexList) -> BooleanList: ...
2425
def not_(self) -> BooleanList: ...
2526
def not_equal_scala(self, elem: ELEM) -> BooleanList: ...
2627
def or_(self, other: BooleanList) -> BooleanList: ...
@@ -58,6 +59,7 @@ class FloatList:
5859
def equal_scala(self, elem: NUM) -> BooleanList: ...
5960
def filter(self, condition: BooleanList) -> FloatList: ...
6061
def get(self, index: int) -> float: ...
62+
def get_by_indexes(self, indexes: IndexList) -> FloatList: ...
6163
def greater_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
6264
def greater_than_scala(self, elem: NUM) -> BooleanList: ...
6365
def less_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
@@ -104,6 +106,7 @@ class IntegerList:
104106
def equal_scala(self, elem: NUM) -> BooleanList: ...
105107
def filter(self, condition: BooleanList) -> IntegerList: ...
106108
def get(self, index: int) -> int: ...
109+
def get_by_indexes(self, indexes: IndexList) -> IntegerList: ...
107110
def greater_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
108111
def greater_than_scala(self, elem: NUM) -> BooleanList: ...
109112
def less_than_or_equal_scala(self, elem: NUM) -> BooleanList: ...
@@ -145,6 +148,7 @@ class StringList:
145148
def equal_scala(self, elem: ELEM) -> BooleanList: ...
146149
def filter(self, condition: BooleanList) -> StringList: ...
147150
def get(self, index: int) -> str: ...
151+
def get_by_indexes(self, indexes: IndexList) -> StringList: ...
148152
def not_equal_scala(self, elem: ELEM) -> BooleanList: ...
149153
def pop(self): ...
150154
@staticmethod

ulist/src/base.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::boolean::BooleanList;
2+
use crate::index::IndexList;
23
use std::cell::Ref;
34
use std::cell::RefMut;
45

@@ -44,14 +45,28 @@ where
4445
List::_new(vec)
4546
}
4647

47-
unsafe fn get(&self, index: usize) -> T {
48-
if index < self.size() {
49-
self.values().get_unchecked(index).clone()
48+
fn get(&self, index: usize) -> T {
49+
let vec = self.values();
50+
let val = vec.get(index);
51+
if let Some(result) = val {
52+
return result.clone();
5053
} else {
5154
panic!("Index out of range!")
5255
}
5356
}
5457

58+
unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
59+
if indexes.back() >= self.size() {
60+
panic!("Index out of range!")
61+
}
62+
let vec = indexes
63+
.values()
64+
.iter()
65+
.map(|&x| self.values().get_unchecked(x).clone())
66+
.collect();
67+
List::_new(vec)
68+
}
69+
5570
fn not_equal_scala(&self, elem: T) -> BooleanList {
5671
BooleanList::new(self._fn_scala(|x| x != &elem))
5772
}

ulist/src/boolean.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ impl BooleanList {
7878
List::filter(self, condition)
7979
}
8080

81-
pub unsafe fn get(&self, index: usize) -> bool {
81+
pub fn get(&self, index: usize) -> bool {
8282
List::get(self, index)
8383
}
8484

85+
pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
86+
List::get_by_indexes(self, indexes)
87+
}
88+
8589
pub fn not_(&self) -> Self {
8690
let vec = self.values().iter().map(|&x| !x).collect();
8791
BooleanList::new(vec)

ulist/src/float.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::base::List;
22
use crate::boolean::BooleanList;
3+
use crate::index::IndexList;
34
use crate::integer::IntegerList;
45
use crate::numerical::NumericalList;
56
use crate::string::StringList;
@@ -85,10 +86,14 @@ impl FloatList {
8586
List::filter(self, condition)
8687
}
8788

88-
pub unsafe fn get(&self, index: usize) -> f32 {
89+
pub fn get(&self, index: usize) -> f32 {
8990
List::get(self, index)
9091
}
9192

93+
pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
94+
List::get_by_indexes(self, indexes)
95+
}
96+
9297
pub fn greater_than_or_equal_scala(&self, elem: f32) -> BooleanList {
9398
NumericalList::greater_than_or_equal_scala(self, elem)
9499
}

ulist/src/index.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ pub struct IndexList {
55
_values: Vec<usize>,
66
}
77

8+
impl IndexList {
9+
pub fn values(&self) -> &Vec<usize> {
10+
&self._values
11+
}
12+
}
13+
814
#[pymethods]
915
impl IndexList {
1016
// Arrange the following methods in alphabetical order.

ulist/src/integer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::base::List;
22
use crate::boolean::BooleanList;
33
use crate::float::FloatList;
4+
use crate::index::IndexList;
45
use crate::non_float::NonFloatList;
56
use crate::numerical::NumericalList;
67
use crate::string::StringList;
@@ -91,10 +92,14 @@ impl IntegerList {
9192
List::filter(self, condition)
9293
}
9394

94-
pub unsafe fn get(&self, index: usize) -> i32 {
95+
pub fn get(&self, index: usize) -> i32 {
9596
List::get(self, index)
9697
}
9798

99+
pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
100+
List::get_by_indexes(self, indexes)
101+
}
102+
98103
pub fn greater_than_or_equal_scala(&self, elem: i32) -> BooleanList {
99104
NumericalList::greater_than_or_equal_scala(self, elem)
100105
}

ulist/src/string.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::base::List;
22
use crate::boolean::BooleanList;
33
use crate::float::FloatList;
4+
use crate::index::IndexList;
45
use crate::integer::IntegerList;
56
use crate::non_float::NonFloatList;
67
use crate::types::AsBooleanList;
@@ -69,10 +70,14 @@ impl StringList {
6970
List::filter(self, condition)
7071
}
7172

72-
pub unsafe fn get(&self, index: usize) -> String {
73+
pub fn get(&self, index: usize) -> String {
7374
List::get(self, index)
7475
}
7576

77+
pub unsafe fn get_by_indexes(&self, indexes: &IndexList) -> Self {
78+
List::get_by_indexes(self, indexes)
79+
}
80+
7681
pub fn not_equal_scala(&self, elem: String) -> BooleanList {
7782
List::not_equal_scala(self, elem)
7883
}

0 commit comments

Comments
 (0)