-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIter.mag
218 lines (191 loc) · 5.11 KB
/
Iter.mag
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
declare type PGGIter;
declare attributes PGGIter: cache;
declare type PGGIter_List: PGGIter;
declare attributes PGGIter_List: list, idx;
declare type PGGIter_Filter: PGGIter;
declare attributes PGGIter_Filter: iter, filter;
declare type PGGIter_Truncate: PGGIter;
declare attributes PGGIter_Truncate: iter, limit;
declare type PGGIter_Enumerate: PGGIter;
declare attributes PGGIter_Enumerate: iter, count;
declare type PGGIter_Apply: PGGIter;
declare attributes PGGIter_Apply: iter, map;
declare type PGGIter_User: PGGIter;
declare attributes PGGIter_User: state, has_next;
intrinsic PGG_Iter(has_next :: UserProgram : State:=false) -> PGGIter
{Makes an iterator X with whose state attribute is State and `HasNext(X)` is `has_next(X)`.}
X := New(PGGIter_User);
X`state := State;
X`has_next := has_next;
return X;
end intrinsic;
intrinsic HasNext(X :: PGGIter_User) -> BoolElt, .
{True if X has a next item.}
return X`has_next(X);
end intrinsic;
intrinsic PGG_ToIter(xs :: []) -> PGGIter
{Makes an iterator.}
X := New(PGGIter_List);
X`list := SequenceToList(xs);
X`idx := 0;
return X;
end intrinsic;
intrinsic PGG_ToIter(xs :: List) -> PGGIter
{Makes an iterator.}
X := New(PGGIter_List);
X`list := xs;
X`idx := 0;
return X;
end intrinsic;
intrinsic HasNext(X :: PGGIter_List) -> BoolElt, .
{True if X has a next item.}
if X`idx lt #X`list then
X`idx +:= 1;
return true, X`list[X`idx];
else
return false, _;
end if;
end intrinsic;
intrinsic ToList(X :: PGGIter) -> List
{Converts X to a list.}
ret := [];
while true do
ok, x := HasNext(X);
if ok then
Append(~ret, x);
else
return ret;
end if;
end while;
end intrinsic;
// we make a specialized version for the common case of just dealing with lists directly
intrinsic ToList(X :: PGGIter_List) -> List
{"}
if X`idx eq 0 then
return X`list;
else
return X`list[X`idx+1..#X`list];
end if;
end intrinsic;
intrinsic ToSequence(X :: PGGIter) -> []
{Converts X to a sequence.}
return [x : x in ToList(X)];
end intrinsic;
intrinsic Enumerate(X :: PGGIter) -> PGGIter
{The sequence <i,x_i> for x_i in X.}
Y := New(PGGIter_Enumerate);
Y`iter := X;
Y`count := 0;
return Y;
end intrinsic;
intrinsic HasNext(X :: PGGIter_Enumerate) -> BoolElt, .
{True if X has a next item.}
ok, x := HasNext(X`iter);
if ok then
X`count +:= 1;
return true, <X`count, x>;
else
return false, _;
end if;
end intrinsic;
intrinsic Apply(X :: PGGIter, f) -> PGGIter
{The sequence f(x) for x in X.}
Y := New(PGGIter_Apply);
Y`iter := X;
Y`map := f;
return Y;
end intrinsic;
intrinsic HasNext(X :: PGGIter) -> BoolElt, .
{True if X has a next item.}
ok, x := HasNext(X`iter);
if ok then
return true, X`map(x);
else
return false, _;
end if;
end intrinsic;
intrinsic Filter(X :: PGGIter, f) -> PGGIter
{The x in X such that f(x) is true.}
Y := New(PGGIter_Filter);
Y`iter := X;
Y`filter := f;
return Y;
end intrinsic;
intrinsic HasNext(X :: PGGIter_Filter) -> BoolElt, .
{True if X has a next item.}
idx := 0;
while true do
ok, x := HasNext(X`iter);
if ok then
if X`filter(x) then
return true, x;
end if;
else
return false, _;
end if;
end while;
end intrinsic;
intrinsic Reverse(X :: PGGIter) -> PGGIter
{The reverse.}
return PGG_ToIter(Reverse(ToList(X)));
end intrinsic;
intrinsic Shuffle(X :: PGGIter) -> PGGIter
{Randomize the order of X.}
xs := ToList(X);
if #xs eq 0 then
return X;
else
g := Random(SymmetricGroup(#xs));
return PGG_ToIter([* xs[i^g] : i in [1..#xs] *]);
end if;
end intrinsic;
intrinsic SortBy(X :: PGGIter, keyfunc :: UserProgram) -> PGGIter
{Sorts X so that keyfunc(x) are naturally sorted.}
xs := ToList(X);
keys := [keyfunc(x) : x in xs];
Sort(~keys, ~permut);
return PGG_ToIter([* xs[i^permut] : i in [1..#xs] *]);
end intrinsic;
intrinsic FilterHasNext(X :: PGGIter, f) -> BoolElt, ., RngIntElt
{True if there is `x` in X such that `f(x)` is true. If so, returns the first such `x` and its index.}
idx := 0;
while true do
idx +:= 1;
ok, x := HasNext(X);
if ok then
if f(x) then
return true, x, idx;
end if;
else
return false, _, _;
end if;
end while;
end intrinsic;
intrinsic Truncate(X :: PGGIter, n :: RngIntElt) -> PGGIter_Truncate
{Limits the length of X to n.}
require n ge 0: "n must be at least 0";
Y := New(PGGIter_Truncate);
Y`iter := X;
Y`limit := n;
return Y;
end intrinsic;
intrinsic Truncate(X :: PGGIter_List, n :: RngIntElt) -> PGGIter_List
{"}
xs := ToList(X);
if #xs gt n then
return PGG_ToIter(xs[1..n]);
else
return X;
end if;
end intrinsic;
intrinsic HasNext(X :: PGGIter) -> BoolElt, .
{True if X has a next item.}
if X`limit gt 0 then
ok, x := HasNext(X`iter);
if ok then
X`limit -:= 1;
return true, x;
end if;
end if;
return false, _;
end intrinsic;