forked from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_ext.cairo
119 lines (100 loc) · 4.26 KB
/
array_ext.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use super::span_ext::SpanTraitExt;
pub trait ArrayTraitExt<T> {
/// Moves all the elements of `other` into `self`, leaving `other` empty.
fn append_all(ref self: Array<T>, ref other: Array<T>);
/// Clones and appends all the elements of `other` into `self`.
fn extend_from_span<+Drop<T>>(ref self: Array<T>, other: Span<T>);
/// Removes up to `n` elements from the front of `self` and returns them in a new array.
fn pop_front_n(ref self: Array<T>, n: usize) -> Array<T>;
/// Removes up to `n` elements from the front of `self`.
fn remove_front_n(ref self: Array<T>, n: usize);
/// Clones and appends all the elements of `self` and then `other` in a single new array.
fn concat(self: @Array<T>, other: @Array<T>) -> Array<T>;
/// Return a new array containing the elements of `self` in a reversed order.
fn reversed(self: @Array<T>) -> Array<T>;
/// Returns `true` if the array contains an element with the given value.
fn contains<+PartialEq<T>>(self: @Array<T>, item: @T) -> bool;
/// Searches for an element in the array, returning its index.
fn position<+PartialEq<T>>(self: @Array<T>, item: @T) -> Option<usize>;
/// Returns the number of elements in the array with the given value.
fn occurrences<+PartialEq<T>>(self: @Array<T>, item: @T) -> usize;
/// Returns the minimum element of an array.
fn min<+PartialOrd<T>>(self: @Array<T>) -> Option<T>;
/// Returns the position of the minimum element of an array.
fn min_position<+PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
/// Returns the maximum element of an array.
fn max<+PartialOrd<T>>(self: @Array<T>) -> Option<T>;
/// Returns the position of the maximum element of an array.
fn max_position<+PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
/// Returns a new array, cloned from `self` but removes consecutive repeated elements.
/// If the array is sorted, this removes all duplicates.
fn dedup<+PartialEq<T>>(self: @Array<T>) -> Array<T>;
/// Returns a new array, cloned from `self` but without any duplicate.
fn unique<+PartialEq<T>>(self: @Array<T>) -> Array<T>;
}
impl ArrayImpl<T, +Clone<T>, +Drop<T>> of ArrayTraitExt<T> {
fn append_all(ref self: Array<T>, ref other: Array<T>) {
while let Option::Some(elem) = other.pop_front() {
self.append(elem);
}
}
fn extend_from_span<+Destruct<T>>(ref self: Array<T>, mut other: Span<T>) {
while let Option::Some(elem) = other.pop_front() {
self.append(elem.clone());
}
}
fn pop_front_n(ref self: Array<T>, mut n: usize) -> Array<T> {
let mut res = array![];
while (n != 0) {
match self.pop_front() {
Option::Some(e) => {
res.append(e);
n -= 1;
},
Option::None => { break; },
};
};
res
}
fn remove_front_n(ref self: Array<T>, mut n: usize) {
while (n != 0) {
match self.pop_front() {
Option::Some(_) => { n -= 1; },
Option::None => { break; },
};
}
}
fn concat(self: @Array<T>, other: @Array<T>) -> Array<T> {
self.span().concat(other.span())
}
fn reversed(self: @Array<T>) -> Array<T> {
self.span().reversed()
}
fn contains<+PartialEq<T>>(self: @Array<T>, item: @T) -> bool {
self.span().contains(item)
}
fn position<+PartialEq<T>>(self: @Array<T>, item: @T) -> Option<usize> {
self.span().position(item)
}
fn occurrences<+PartialEq<T>>(self: @Array<T>, item: @T) -> usize {
self.span().occurrences(item)
}
fn min<+PartialOrd<@T>>(self: @Array<T>) -> Option<T> {
self.span().min()
}
fn min_position<+PartialOrd<@T>>(self: @Array<T>) -> Option<usize> {
self.span().min_position()
}
fn max<+PartialOrd<@T>>(self: @Array<T>) -> Option<T> {
self.span().max()
}
fn max_position<+PartialOrd<@T>>(mut self: @Array<T>) -> Option<usize> {
self.span().max_position()
}
fn dedup<+PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
self.span().dedup()
}
fn unique<+PartialEq<T>>(mut self: @Array<T>) -> Array<T> {
self.span().unique()
}
}