-
Notifications
You must be signed in to change notification settings - Fork 122
/
group.dl
170 lines (153 loc) · 5.14 KB
/
group.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
/*
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 groups (see basic group operations defined in
* `ddlog_std.dl`).
*/
/* We implement `map`, `flatmap`, `sort_by`, etc., as group-to-vector
* rather than group-to-group transformations, since the output of
* these methods can potentially be empty, thus violating the invariant
* that a group always has at least one element. */
/* Applies closure `f` to each element of the group. */
function map(g: Group<'K, 'V1>, f: function('V1): 'V2): Vec<'V2> {
var res = vec_with_capacity(g.count_distinct());
for ((x, _) in g) {
res.push(f(x))
};
res
}
/* Applies closure `f` to each element of the group and concatenates the
* resulting vectors, returning a flat vector. */
function flatmap(g: Group<'K, 'V1>, f: function('V1): Vec<'V2>): Vec<'V2> {
var res = vec_empty();
for ((x, _) in g) {
res.append(f(x))
};
res
}
/* Returns the element that gives the minimum value from the specified function.
* If several elements are equally minimum, the first element is returned. */
function arg_min(g: Group<'K, 'V>, f: function('V): 'A): 'V {
var min_arg = g.first();
var min_val = f(g.first());
for ((x, _) in g) {
var v = f(x);
if (v < min_val) {
min_val = v;
min_arg = x;
}
};
min_arg
}
/* Returns the element that gives the maximum value from the specified function.
* If several elements are equally maximum, the first element is returned. */
function arg_max(g: Group<'K, 'V>, f: function('V): 'A): 'V {
var max_arg = g.first();
var max_val = f(g.first());
for ((x, _) in g) {
var v = f(x);
if (v > max_val) {
max_val = v;
max_arg = x;
}
};
max_arg
}
/* Returns the first element of the group that satisfies predicate `f` or
* `None` if none of the elements satisfy the predicate. */
function find(g: Group<'K, 'V>, f: function('V): bool): Option<'V> {
for ((x, _) in g) {
if (f(x)) {
return Some{x}
}
};
None
}
/* Returns a vector containing only those elements in `g` that satisfy predicate `f`,
* preserving the order of the elements in the original group. */
function filter(g: Group<'K, 'V>, f: function('V): bool): Vec<'V> {
var res = vec_empty();
for ((x, _) in g) {
if (f(x)) {
res.push(x)
}
};
res
}
/* Both filters and maps the group.
*
* Calls the closure on each element of the group. If the closure returns
* `Some{element}`, then that element is returned. */
function filter_map(g: Group<'K, 'V1>, f: function('V1): Option<'V2>): Vec<'V2> {
var res = vec_empty();
for ((x, _) in g) {
match (f(x)) {
None -> (),
Some{y} -> res.push(y)
}
};
res
}
/* Returns `true` iff all elements of the group satisfy predicate `f`. */
function all(g: Group<'K, 'V>, f: function('V): bool): bool {
for ((x, _) in g) {
if (not f(x)) {
return false
}
};
true
}
/* Returns `true` iff at least one element of the group satisfies predicate `f`. */
function any(g: Group<'K, 'V>, f: function('V): bool): bool {
for ((x, _) in g) {
if (f(x)) {
return true
}
};
false
}
/* Counts the number of elements in `g` that satisfy predicate `f`. */
function count(g: Group<'K, 'V>, f: function('V): bool): usize {
var cnt = 0;
for ((x, _) in g) {
if (f(x)) {
cnt = cnt + 1
}
};
cnt
}
/* Iterates over the group, aggregating its contents using `f`.
*
* `f` - takes the previous value of the accumulator and the next element in the
* group and returns the new value of the accumulator.
*
* `initializer` - initial value of the accumulator. */
function fold(g: Group<'K,'V>, f: function('B, 'V): 'B, initializer: 'B): 'B {
var res = initializer;
for ((x, _) in g) {
res = f(res, x)
};
res
}
/* Apply `f` to each distinct value in the group and sum up the results.
*
* `'N` must be a numeric type.
*/
extern function sum_of(g: Group<'K, 'V>, f: function('V): 'N): 'N