-
Notifications
You must be signed in to change notification settings - Fork 118
/
set.dl
126 lines (111 loc) · 3.88 KB
/
set.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
/*
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 sets (see basic set operations defined in
* `ddlog_std.dl`).
*/
/* Applies closure `f` to each element of the set. */
function map(s: Set<'A>, f: function('A): 'B): Set<'B> {
var res = set_empty();
for (x in s) {
res.insert(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.
* If the set is empty, `None` is returned. */
function arg_min(s: Set<'A>, f: function('A): 'B): Option<'A> {
set_arg_min(s, 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 set is empty, `None` is returned. */
function arg_max(s: Set<'A>, f: function('A): 'B): Option<'A> {
set_arg_max(s, f)
}
/* Returns the first element of the set that satisfies predicate `f` or
* `None` if none of the elements satisfy the predicate. */
function find(s: Set<'A>, f: function('A): bool): Option<'A> {
for (x in s) {
if (f(x)) {
return Some{x}
}
};
None
}
/* Returns a vector containing only those elements in `s` that satisfy predicate
* `f`. */
function filter(s: Set<'A>, f: function('A): bool): Set<'A> {
var res = set_empty();
for (x in s) {
if (f(x)) {
res.insert(x)
}
};
res
}
/* Both filters and maps the set.
*
* Calls the closure on each element of the set. If the closure returns
* `Some{element}`, then that element is returned. */
function filter_map(s: Set<'A>, f: function('A): Option<'B>): Set<'B> {
var res = set_empty();
for (x in s) {
match (f(x)) {
None -> (),
Some{y} -> res.insert(y)
}
};
res
}
/* Returnds `true` iff all elements of the set satisfy predicate `f`. */
function all(s: Set<'A>, f: function('A): bool): bool {
for (x in s) {
if (not f(x)) {
return false
}
};
true
}
/* Returnds `true` iff at least one element of the set satisfies predicate `f`. */
function any(s: Set<'A>, f: function('A): bool): bool {
for (x in s) {
if (f(x)) {
return true
}
};
false
}
/* Iterates over the set is ascending order, aggregating its contents using `f`.
*
* `f` - takes the previous value of the accumulator and the next element in the
* set and returns the new value of the accumulator.
*
* `initializer` - initial value of the accumulator. */
function fold(s: Set<'A>, f: function('B, 'A): 'B, initializer: 'B): 'B {
var res = initializer;
for (x in s) {
res = f(res, x)
};
res
}
extern function set_arg_min(s: Set<'A>, f: function('A): 'B): Option<'A>
extern function set_arg_max(s: Set<'A>, f: function('A): 'B): Option<'A>