forked from vmware/differential-datalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.dl
195 lines (175 loc) · 5.75 KB
/
map.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
186
187
188
189
190
191
192
193
194
195
/*
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 maps (see basic map operations defined in
* `ddlog_std.dl`).
*/
/* Applies closure `f` to each value in the map. */
function map(m: Map<'K,'V1>, f: function('V1): 'V2): Map<'K,'V2> {
var res = map_empty();
for ((k, v) in m) {
res.insert(k, f(v))
};
res
}
/* Applies closure `f` to each key/value pair in the map. */
function map(m: Map<'K,'V1>, f: function('K, 'V1): 'V2): Map<'K, 'V2> {
var res = map_empty();
for ((k, v) in m) {
res.insert(k, f(k, v))
};
res
}
/* Apply transformation `f` to all entries in the map in place, i.e., by
* modifying `m` rather than creating a new map. */
function map_in_place(m: mut Map<'K,'V>, f: function('K, mut 'V)) {
map_map_in_place(m, f)
}
/* Returns a map obtained by applying `f` to each key of `m`.
* The size of the result may be smaller if `f` maps two or more distinct keys
* to the same new key. In this case the value at the greatest of the original
* keys is retained. */
function map_keys(m: Map<'K1,'V>, f: function('K1): 'K2): Map<'K2, 'V> {
var res = map_empty();
for ((k,v) in m) {
res.insert(f(k), v)
};
res
}
/* Returns the first element of the map that satisfies predicate `f` or
* `None` if none of the elements satisfy the predicate. */
function find(m: Map<'K, 'V>, f: function('V): bool): Option<'V> {
for ((_,v) in m) {
if (f(v)) {
return Some{v}
}
};
None
}
/* Returnds `true` iff at least one value in the map satisfies predicate `f`. */
function any(m: Map<'K, 'V>, f: function('V): bool): bool {
for ((_, v) in m) {
if (f(v)) {
return true
}
};
false
}
/* Returns a map containing only values that satisfy the predicate. */
function filter(m: Map<'K, 'V>, f: function('V): bool): Map<'K, 'V> {
var res = map_empty();
for (kv in m) {
if (f(kv.1)) {
res.insert(kv.0, kv.1)
}
};
res
}
/* Returns a map containing only key/value pairs that satisfy the predicate. */
function filter(m: Map<'K, 'V>, f: function('K, 'V): bool): Map<'K, 'V> {
var res = map_empty();
for ((k, v) in m) {
if (f(k, v)) {
res.insert(k, v)
}
};
res
}
/* Map values and collect the `Some{}` results. */
function filter_map(m: Map<'K,'V1>, f: function('V1): Option<'V2>): Map<'K,'V2> {
var res = map_empty();
for ((k, v) in m) {
match (f(v)) {
None -> (),
Some{v} -> {
res.insert(k, v)
}
}
};
res
}
/* Map key/value pairs and collect the `Some{}` results. */
function filter_map(m: Map<'K,'V1>, f: function('K, 'V1): Option<'V2>): Map<'K,'V2> {
var res = map_empty();
for ((k, v) in m) {
match (f(k, v)) {
None -> (),
Some{v} -> {
res.insert(k, v)
}
}
};
res
}
/* Retains only the values specified by the predicate.
* In other words, remove all values `v` such that `f(v)` returns `false`. */
function retain(m: mut Map<'K,'V>, f: function('V): bool) {
var keys = m.keys();
for (k in keys) {
match (m.get(k)) {
Some{v} -> if (not f(v)) {
m.remove(k);
},
None -> ()
}
};
}
/* Retains only the key/value pairs specified by the predicate.
* In other words, remove all pairs `k->v` such that `f(k,v)` returns
* `false`. */
function retain(m: mut Map<'K,'V>, f: function('K, 'V): bool) {
var keys = m.keys();
for (k in keys) {
match (m.get(k)) {
Some{v} -> if (not f(k,v)) {
m.remove(k);
},
None -> ()
}
};
}
/* Iterates over map values is ascending order of keys, aggregating its contents using `f`.
*
* `f` - takes the previous value of the accumulator and the next value in the
* map and returns the new value of the accumulator.
*
* `initializer` - initial value of the accumulator. */
function fold(m: Map<'K,'V>, f: function('A, 'V): 'A, initializer: 'A): 'A {
var res = initializer;
for ((_, v) in m) {
res = f(res, v)
};
res
}
/* Iterates over key/value pairs in the map is ascending order of keys, aggregating
* its contents using `f`.
*
* `f` - takes the previous value of the accumulator and the next key/value
* pait in the map and returns the new value of the accumulator.
*
* `initializer` - initial value of the accumulator. */
function fold(m: Map<'K,'V>, f: function('A, 'K, 'V): 'A, initializer: 'A): 'A {
var res = initializer;
for ((k, v) in m) {
res = f(res, k, v)
};
res
}
extern function map_map_in_place(m: mut Map<'K,'V>, f: function('K, mut 'V))