-
Notifications
You must be signed in to change notification settings - Fork 122
/
vec.dl
185 lines (163 loc) · 5.64 KB
/
vec.dl
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
/*
Copyright (c) 2021 VMware, Inc.
SPDX-License-Identifier: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* Functions for working with vectors (see basic vector operations defined in
* `ddlog_std.dl`).
*/
/* Applies closure `f` to each element of the vector. */
function map(v: Vec<'A>, f: function('A): 'B): Vec<'B> {
var res = vec_with_capacity(v.len());
for (x in v) {
res.push(f(x))
};
res
}
/* Applies closure `f` to each element of the vector and concatenates the
* resulting vectors, returning a flat vector. */
function flatmap(v: Vec<'A>, f: function('A): Vec<'B>): Vec<'B> {
var res = vec_empty();
for (x in v) {
res.append(f(x))
};
res
}
/* Sorts the vector with a key extraction function, but may not preserve the
* order of equal elements. */
function sort_by(v: mut Vec<'A>, f: function('A): 'B) {
vec_sort_by(v, f)
}
/* Returns the element that gives the minimum value from the specified function.
* If several elements are equally minimum, the first element is returned.
* If the vector is empty, `None` is returned. */
function arg_min(v: Vec<'A>, f: function('A): 'B): Option<'A> {
vec_arg_min(v, f)
}
/* Returns the element that gives the maximum value from the specified function.
* If several elements are equally maximum, the first element is returned.
* If the vector is empty, `None` is returned. */
function arg_max(v: Vec<'A>, f: function('A): 'B): Option<'A> {
vec_arg_max(v, f)
}
/* Returns the first element of the vector that satisfies predicate `f` or
* `None` if none of the elements satisfy the predicate. */
function find(v: Vec<'A>, f: function('A): bool): Option<'A> {
for (x in v) {
if (f(x)) {
return Some{x}
}
};
None
}
/* Returns a vector containing only those elements in `v` that satisfy predicate `f`,
* preserving the order of the elements in the original vector. */
function filter(v: Vec<'A>, f: function('A): bool): Vec<'A> {
var res = vec_empty();
for (x in v) {
if (f(x)) {
res.push(x)
}
};
res
}
/* Both filters and maps the vector.
*
* Calls the closure on each element of the vector. If the closure returns
* `Some{element}`, then that element is returned. */
function filter_map(v: Vec<'A>, f: function('A): Option<'B>): Vec<'B> {
var res = vec_empty();
for (x in v) {
match (f(x)) {
None -> (),
Some{y} -> res.push(y)
}
};
res
}
/* Retains only the elements specified by the predicate.
* In other words, remove all elements `e` such that `f(e)` returns `false`.
* This method operates in place, visiting each element exactly once in the
* original order, and preserves the order of the retained elements. */
function retain(v: mut Vec<'A>, f: function('A): bool) {
var del: s64 = 0;
var len = v.len();
for (i in range_vec(0, (len as isize), 1)) {
var x = v.nth(i as usize).unwrap_or_default();
if (not f(x)) {
del = del + 1
} else if (del > 0) {
v.update_nth((i - del) as usize, x);
}
};
if (del > 0) {
v.truncate(len - (del as usize));
}
}
/* Returns `true` iff all elements of the vector satisfy predicate `f`. */
function all(v: Vec<'A>, f: function('A): bool): bool {
for (x in v) {
if (not f(x)) {
return false
}
};
true
}
/* Returns `true` iff at least one element of the vector satisfies predicate `f`. */
function any(v: Vec<'A>, f: function('A): bool): bool {
for (x in v) {
if (f(x)) {
return true
}
};
false
}
/* Counts the number of elements in `v` that satisfy predicate `f`. */
function count(v: Vec<'A>, f: function('A): bool): usize {
var cnt = 0;
for (x in v) {
if (f(x)) {
cnt = cnt + 1
}
};
cnt
}
/* Iterates over the vector, aggregating its contents using `f`.
*
* `f` - takes the previous value of the accumulator and the next element in the
* vector and returns the new value of the accumulator.
*
* `initializer` - initial value of the accumulator. */
function fold(v: Vec<'A>, f: function('B, 'A): 'B, initializer: 'B): 'B {
var res = initializer;
for (x in v) {
res = f(res, x)
};
res
}
// Gets the first element of a vector if it exists
function first(vec: Vec<'T>): Option<'T> {
vec.nth(0)
}
// Gets the last element of a vector if it exists
function last(vec: Vec<'T>): Option<'T> {
vec.nth(vec.len() - 1)
}
extern function vec_sort_by(v: mut Vec<'A>, f: function('A): 'B)
extern function vec_arg_min(v: Vec<'A>, f: function('A): 'B): Option<'A>
extern function vec_arg_max(v: Vec<'A>, f: function('A): 'B): Option<'A>