Skip to content

Commit d8c643b

Browse files
authored
Merge pull request #181 from Rust-Data-Science/wip-equal-all
Wip equal all
2 parents 91ad6fd + 9be7ace commit d8c643b

File tree

10 files changed

+156
-21
lines changed

10 files changed

+156
-21
lines changed

tests/test_base.py

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,121 @@
2929
[True, False] * 50,
3030
"UltraFastList([True, False, True, ..., False, True, False])",
3131
),
32-
('__repr__', 'bool', [True, False], 'UltraFastList([True, False])'),
33-
('__repr__', 'float', [1.0, 2.0], 'UltraFastList([1.0, 2.0])'),
34-
('__repr__', 'float', range(0, 100),
35-
'UltraFastList([0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0])'),
36-
('__repr__', 'int', [1, 2], 'UltraFastList([1, 2])'),
37-
('__repr__', 'int', range(0, 100),
38-
'UltraFastList([0, 1, 2, ..., 97, 98, 99])'),
32+
(
33+
'__repr__',
34+
'bool',
35+
[True, False],
36+
'UltraFastList([True, False])',
37+
),
38+
(
39+
'__repr__',
40+
'float',
41+
[1.0, 2.0],
42+
'UltraFastList([1.0, 2.0])',
43+
),
44+
(
45+
'__repr__',
46+
'float',
47+
range(0, 100),
48+
'UltraFastList([0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0])',
49+
),
50+
(
51+
'__repr__',
52+
'int',
53+
[1, 2],
54+
'UltraFastList([1, 2])',
55+
),
56+
(
57+
'__repr__',
58+
'int',
59+
range(0, 100),
60+
'UltraFastList([0, 1, 2, ..., 97, 98, 99])',
61+
),
3962
(
4063
"__repr__",
4164
"string",
4265
['foo', 'bar'] * 50,
4366
"UltraFastList(['foo', 'bar', 'foo', ..., 'bar', 'foo', 'bar'])",
4467
),
45-
('__repr__', 'string', ['foo', 'bar'],
46-
"UltraFastList(['foo', 'bar'])"),
68+
(
69+
'__repr__',
70+
'string',
71+
['foo', 'bar'],
72+
"UltraFastList(['foo', 'bar'])",
73+
),
4774
(
4875
"__repr__",
4976
"string",
5077
['foo', None] * 50,
5178
"UltraFastList(['foo', None, 'foo', ..., None, 'foo', None])",
5279
),
53-
('__repr__', 'string', ['foo', None],
54-
"UltraFastList(['foo', None])"),
80+
(
81+
'__repr__',
82+
'string',
83+
['foo', None],
84+
"UltraFastList(['foo', None])",
85+
),
5586
5687
(
5788
"__str__",
5889
"bool",
5990
[True, False] * 50,
6091
"[True, False, True, ..., False, True, False]",
6192
),
62-
('__str__', 'bool', [True, False], '[True, False]'),
63-
('__str__', 'float', [1.0, 2.0], '[1.0, 2.0]'),
64-
('__str__', 'float', range(0, 100),
65-
'[0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0]'),
66-
('__str__', 'int', [1, 2], '[1, 2]'),
67-
('__str__', 'int', range(0, 100), '[0, 1, 2, ..., 97, 98, 99]'),
93+
(
94+
'__str__',
95+
'bool',
96+
[True, False],
97+
'[True, False]',
98+
),
99+
(
100+
'__str__',
101+
'float',
102+
[1.0, 2.0],
103+
'[1.0, 2.0]',
104+
),
105+
(
106+
'__str__',
107+
'float',
108+
range(0, 100),
109+
'[0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0]',
110+
),
111+
(
112+
'__str__',
113+
'int',
114+
[1, 2],
115+
'[1, 2]',
116+
),
117+
(
118+
'__str__',
119+
'int',
120+
range(0, 100),
121+
'[0, 1, 2, ..., 97, 98, 99]',
122+
),
68123
(
69124
"__str__",
70125
"string",
71126
['foo', 'bar'] * 50,
72127
"['foo', 'bar', 'foo', ..., 'bar', 'foo', 'bar']",
73128
),
74-
('__str__', 'string', ['foo', 'bar'], "['foo', 'bar']"),
129+
(
130+
'__str__',
131+
'string',
132+
['foo', 'bar'],
133+
"['foo', 'bar']",
134+
),
75135
(
76136
"__str__",
77137
"string",
78138
['foo', None] * 50,
79139
"['foo', None, 'foo', ..., None, 'foo', None]",
80140
),
81-
('__str__', 'string', ['foo', None], "['foo', None]"),
141+
(
142+
'__str__',
143+
'string',
144+
['foo', None],
145+
"['foo', None]",
146+
),
82147
83148
('copy', 'bool', [True, False], [True, False]),
84149
('copy', 'float', [1.0, 2.0], [1.0, 2.0]),
@@ -150,6 +215,24 @@ def test_methods_no_arg(
150215
('__getitem__', 'string', ['foo', 'bar', 'baz'],
151216
['foo', 'baz'], {'index': ul.IndexList([0, 2])}),
152217
218+
("all_equal", 'bool', [True, False], True, {"other": [True, False]}),
219+
("all_equal", 'bool', [True, False], False, {"other": [True, True]}),
220+
("all_equal", 'bool', [True, False], False, {"other": [False, True]}),
221+
("all_equal", 'bool', [True, False], False, {"other": [False, False]}),
222+
("all_equal", 'bool', [True, False], False, {"other": [True, None]}),
223+
("all_equal", 'bool', [True, None], False, {"other": [True, None]}),
224+
("all_equal", 'bool', [True, None], False, {"other": [True, False]}),
225+
("all_equal", 'bool', [True, False], False, {"other": [True]}),
226+
("all_equal", 'bool', [True, False], False, {"other": [None]}),
227+
("all_equal", 'float', [1.0, 0.0], True, {"other": [1.0, 0.0]}),
228+
("all_equal", 'float', [1.0, 0.0], False, {"other": [1.0, 1.0]}),
229+
("all_equal", 'int', [1, 0], True, {"other": [1, 0]}),
230+
("all_equal", 'int', [1, 0], False, {"other": [1, 1]}),
231+
("all_equal", 'string', ['foo', 'bar'],
232+
True, {"other": ['foo', 'bar']}),
233+
("all_equal", 'string', ['foo', 'bar'],
234+
False, {"other": ['foo', 'foo']}),
235+
153236
("apply", "bool", [True, False], [
154237
False, True], {"fn": lambda x: x == False},), # noqa: E712
155238
("apply", "float", [1.0, 2.0], [
@@ -160,7 +243,6 @@ def test_methods_no_arg(
160243
("apply", "string", ['foo', 'bar', None], [
161244
True, False, True], {"fn": lambda x: x != 'bar'},),
162245
163-
164246
("equal_scala", 'bool', [True, False], [False, True], {"elem": False}),
165247
("equal_scala", 'float', [1.0, 2.0, 3.0],
166248
[False, True, False], {"elem": 2.0}),
@@ -274,7 +356,11 @@ def test_methods_with_args(
274356
kwargs: dict,
275357
) -> None:
276358
arr = ul.from_seq(nums, dtype)
277-
result = getattr(arr, test_method)(**kwargs)
359+
fn = getattr(arr, test_method)
360+
if kwargs.get("other") and isinstance(kwargs["other"], list):
361+
result = fn(ul.from_seq(kwargs["other"], dtype))
362+
else:
363+
result = fn(**kwargs)
278364
check_test_result(dtype, test_method, result, expected_value)
279365

280366

ulist/python/ulist/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ def all(self) -> bool:
169169
assert isinstance(self._values, BooleanList)
170170
return self._values.all()
171171

172+
def all_equal(self, other: "UltraFastList") -> bool:
173+
"""
174+
Whether all elements in the same position of self and other
175+
are equal.
176+
"""
177+
return self._values.all_equal(other._values)
178+
172179
def and_(self, other: "UltraFastList") -> "UltraFastList":
173180
"""Return self & other."""
174181
assert isinstance(self._values, BooleanList)

ulist/python/ulist/ulist.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BooleanList:
99

1010
def __init__(self, vec: Sequence[bool], hset: Set[int]) -> None: ...
1111
def all(self) -> bool: ...
12+
def all_equal(self, other: LIST_RS) -> bool: ...
1213
def and_(self, other: BooleanList) -> BooleanList: ...
1314
def any(self) -> bool: ...
1415
def append(self, elem: ELEM_OPT) -> None: ...
@@ -49,6 +50,7 @@ class FloatList32:
4950
def __init__(self, vec: Sequence[float], hset: Set[int]) -> None: ...
5051
def add(self, other: NUM_LIST_RS) -> FloatList32: ...
5152
def add_scala(self, elem: NUM) -> FloatList32: ...
53+
def all_equal(self, other: LIST_RS) -> bool: ...
5254
def append(self, elem: ELEM_OPT) -> None: ...
5355
def argmax(self) -> int: ...
5456
def argmin(self) -> int: ...
@@ -98,6 +100,7 @@ class FloatList64:
98100
def __init__(self, vec: Sequence[float], hset: Set[int]) -> None: ...
99101
def add(self, other: NUM_LIST_RS) -> FloatList64: ...
100102
def add_scala(self, elem: NUM) -> FloatList64: ...
103+
def all_equal(self, other: LIST_RS) -> bool: ...
101104
def append(self, elem: ELEM_OPT) -> None: ...
102105
def argmax(self) -> int: ...
103106
def argmin(self) -> int: ...
@@ -147,6 +150,7 @@ class IntegerList32:
147150
def __init__(self, vec: Sequence[int], hset: Set[int]) -> None: ...
148151
def add(self, other: NUM_LIST_RS) -> IntegerList32: ...
149152
def add_scala(self, elem: NUM) -> IntegerList32: ...
153+
def all_equal(self, other: LIST_RS) -> bool: ...
150154
def append(self, elem: ELEM_OPT) -> None: ...
151155
def argmax(self) -> int: ...
152156
def argmin(self) -> int: ...
@@ -197,6 +201,7 @@ class IntegerList64:
197201
def __init__(self, vec: Sequence[int], hset: Set[int]) -> None: ...
198202
def add(self, other: NUM_LIST_RS) -> IntegerList64: ...
199203
def add_scala(self, elem: NUM) -> IntegerList64: ...
204+
def all_equal(self, other: LIST_RS) -> bool: ...
200205
def append(self, elem: ELEM_OPT) -> None: ...
201206
def argmax(self) -> int: ...
202207
def argmin(self) -> int: ...
@@ -245,6 +250,7 @@ class StringList:
245250
# Arrange the following methods in alphabetical order.
246251

247252
def __init__(self, vec: Sequence[str], hset: Set[int]) -> None: ...
253+
def all_equal(self, other: LIST_RS) -> bool: ...
248254
def append(self, elem: ELEM_OPT) -> None: ...
249255
def as_bool(self) -> BooleanList: ...
250256
def as_float32(self) -> FloatList32: ...

ulist/src/base.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ where
6262
}
6363
}
6464

65+
fn all_equal(&self, other: &Self) -> bool {
66+
if self.size() != other.size() || self.count_na() > 0 || other.count_na() > 0 {
67+
return false;
68+
};
69+
for (x, y) in self.values().iter().zip(other.values().iter()) {
70+
if x != y {
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
6577
fn append(&self, elem: Option<T>) {
6678
if let Some(i) = elem {
6779
self.values_mut().push(i);

ulist/src/boolean.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ impl BooleanList {
4040
self.values().iter().all(|&x| x)
4141
}
4242

43+
pub fn all_equal(&self, other: &Self) -> bool {
44+
List::all_equal(self, other)
45+
}
46+
4347
pub fn and_(&self, other: &Self) -> Self {
4448
_logical_operate(&self, &other, |x, y| x && y)
4549
}

ulist/src/floatings/float32.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl FloatList32 {
4141
NumericalList::add_scala(self, elem)
4242
}
4343

44+
pub fn all_equal(&self, other: &Self) -> bool {
45+
List::all_equal(self, other)
46+
}
47+
4448
pub fn append(&self, elem: Option<f32>) {
4549
List::append(self, elem)
4650
}

ulist/src/floatings/float64.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl FloatList64 {
4141
NumericalList::add_scala(self, elem)
4242
}
4343

44+
pub fn all_equal(&self, other: &Self) -> bool {
45+
List::all_equal(self, other)
46+
}
47+
4448
pub fn append(&self, elem: Option<f64>) {
4549
List::append(self, elem)
4650
}

ulist/src/integers/int32.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl IntegerList32 {
4343
NumericalList::add_scala(self, elem)
4444
}
4545

46+
pub fn all_equal(&self, other: &Self) -> bool {
47+
List::all_equal(self, other)
48+
}
49+
4650
pub fn append(&self, elem: Option<i32>) {
4751
List::append(self, elem)
4852
}

ulist/src/integers/int64.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ impl IntegerList64 {
4545
NumericalList::add_scala(self, elem)
4646
}
4747

48+
pub fn all_equal(&self, other: &Self) -> bool {
49+
List::all_equal(self, other)
50+
}
51+
4852
pub fn append(&self, elem: Option<i64>) {
4953
List::append(self, elem)
5054
}

ulist/src/string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ impl StringList {
3535
List::_new(vec, hset)
3636
}
3737

38+
pub fn all_equal(&self, other: &Self) -> bool {
39+
List::all_equal(self, other)
40+
}
41+
3842
pub fn append(&self, elem: Option<String>) {
3943
List::append(self, elem)
4044
}

0 commit comments

Comments
 (0)