generated from eigerco/beerus
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathspan_ext.cairo
229 lines (191 loc) · 6.94 KB
/
span_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use core::clone::Clone;
use core::cmp::min;
use core::num::traits::CheckedSub;
use super::array_ext::ArrayTraitExt;
pub trait SpanTraitExt<T> {
/// Removes up to `n` elements from the front of `self` and returns them in a new span.
fn pop_front_n(ref self: Span<T>, n: usize) -> Span<T>;
/// Removes up to `n` elements from the back of `self` and returns them in a new span.
fn pop_back_n(ref self: Span<T>, n: usize) -> Span<T>;
/// Removes up to `n` elements from the front of `self`.
fn remove_front_n(ref self: Span<T>, n: usize);
/// Removes up to `n` elements from the back of `self`.
fn remove_back_n(ref self: Span<T>, n: usize);
/// Clones and appends all the elements of `self` and then `other` in a single new array.
fn concat(self: Span<T>, other: Span<T>) -> Array<T>;
/// Return a new array containing the elements of `self` in a reversed order.
fn reversed(self: Span<T>) -> Array<T>;
/// Returns `true` if the span contains an element with the given value.
fn contains<+PartialEq<T>>(self: Span<T>, item: @T) -> bool;
/// Searches for an element the span, returning its index.
fn position<+PartialEq<T>>(self: Span<T>, item: @T) -> Option<usize>;
/// Returns the number of elements in the span with the given value.
fn occurrences<+PartialEq<T>>(self: Span<T>, item: @T) -> usize;
/// Returns the minimum element of a span.
fn min<+PartialOrd<@T>>(self: Span<T>) -> Option<T>;
/// Returns the position of the minimum element of a span.
fn min_position<+PartialOrd<@T>>(self: Span<T>) -> Option<usize>;
/// Returns the maximum element of a span.
fn max<+PartialOrd<@T>>(self: Span<T>) -> Option<T>;
/// Returns the position of the maximum element of a span.
fn max_position<+PartialOrd<@T>>(self: Span<T>) -> Option<usize>;
/// Returns a new array, cloned from `self` but removes consecutive repeated elements.
/// If the span is sorted, this removes all duplicates.
fn dedup<+PartialEq<T>>(self: Span<T>) -> Array<T>;
/// Returns a new array, cloned from `self` but without any duplicate.
fn unique<+PartialEq<T>>(self: Span<T>) -> Array<T>;
}
impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
fn pop_front_n(ref self: Span<T>, mut n: usize) -> Span<T> {
let span_len = self.len();
let separator = min(n, span_len);
let res = self.slice(0, separator);
self = self.slice(separator, span_len - separator);
res
}
fn pop_back_n(ref self: Span<T>, n: usize) -> Span<T> {
let span_len = self.len();
// Saturating substraction
let separator = span_len.checked_sub(n).unwrap_or(0);
let res = self.slice(separator, span_len - separator);
self = self.slice(0, separator);
res
}
fn remove_front_n(ref self: Span<T>, mut n: usize) {
let span_len = self.len();
let separator = min(n, span_len);
self = self.slice(separator, span_len - separator);
}
fn remove_back_n(ref self: Span<T>, mut n: usize) {
let span_len = self.len();
// Saturating substraction
let separator = span_len.checked_sub(n).unwrap_or(0);
self = self.slice(0, separator);
}
fn concat(mut self: Span<T>, mut other: Span<T>) -> Array<T> {
let mut ret = array![];
ret.extend_from_span(self);
ret.extend_from_span(other);
ret
}
fn reversed(mut self: Span<T>) -> Array<T> {
let mut res = array![];
while let Option::Some(v) = self.pop_back() {
res.append(v.clone());
};
res
}
fn contains<+PartialEq<T>>(mut self: Span<T>, item: @T) -> bool {
loop {
match self.pop_front() {
Option::Some(v) => { if v == item {
break true;
} },
Option::None => { break false; },
};
}
}
fn position<+PartialEq<T>>(mut self: Span<T>, item: @T) -> Option<usize> {
let mut index = 0_usize;
loop {
match self.pop_front() {
Option::Some(v) => {
if v == item {
break Option::Some(index);
}
index += 1;
},
Option::None => { break Option::None; },
};
}
}
fn occurrences<+PartialEq<T>>(mut self: Span<T>, item: @T) -> usize {
let mut count = 0_usize;
for v in self {
if v == item {
count += 1;
}
};
count
}
fn min<+PartialOrd<@T>>(mut self: Span<T>) -> Option<T> {
let mut min = match self.pop_front() {
Option::Some(item) => item,
Option::None => { return Option::None; },
};
for item in self {
if item < min {
min = item
}
};
Option::Some(min.clone())
}
fn min_position<+PartialOrd<@T>>(mut self: Span<T>) -> Option<usize> {
let mut index = 0;
let mut min_position = 0;
let mut min = match self.pop_front() {
Option::Some(item) => item,
Option::None => { return Option::None; },
};
for item in self {
if item < min {
min_position = index + 1;
min = item;
}
index += 1;
};
Option::Some(min_position)
}
fn max<+PartialOrd<@T>>(mut self: Span<T>) -> Option<T> {
let mut max = match self.pop_front() {
Option::Some(item) => item,
Option::None => { return Option::None; },
};
for item in self {
if item > max {
max = item
}
};
Option::Some(max.clone())
}
fn max_position<+PartialOrd<@T>>(mut self: Span<T>) -> Option<usize> {
let mut index = 0;
let mut max_position = 0;
let mut max = match self.pop_front() {
Option::Some(item) => item,
Option::None => { return Option::None; },
};
for item in self {
if item > max {
max_position = index + 1;
max = item
}
index += 1;
};
Option::Some(max_position)
}
fn dedup<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
if self.len() == 0 {
return array![];
}
// Safe to unwrap because we checked for empty vec
let mut last_value = self.pop_front().unwrap();
let mut ret = array![last_value.clone()];
for v in self {
if (last_value != v) {
last_value = v;
ret.append(v.clone());
}
};
ret
}
fn unique<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
let mut ret = array![];
for v in self {
if !ret.span().contains(v) {
ret.append(v.clone());
}
};
ret
}
}