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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2011. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(diameter_enum).
%%
%% This module constructs finite enumerations.
%%
%% An enumeration is represented as a function on integers, 0 mapping
%% to the number of values enumerated and successive integers mapping
%% to enumerated values. The function will fail on anything but 0 and
%% positive integers less then or equal to the value of the function
%% at 0.
%%
%% The purpose of this is to provide a way of stepping through a large
%% number of values without explicitly constructing the list of all
%% possible values. For example, consider the following function that
%% given a list of lists constructs the list of all possible lists
%% constructed by choosing one element from each sublist.
%%
%% combine([H]) ->
%% [[X] || X <- H];
%% combine([H|T]) ->
%% Ys = combine(T),
%% [[X|Y] || X <- H, Y <- Ys].
%%
%% Eg. [[1,2],[3,4,5]] -> [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]]
%%
%% If L is a list of three 1000 element lists then combine(L) would
%% construct a list of length 10^9 which will likely exhaust available
%% memory. (Which is how this module came into being. A tail-recursive
%% implementation doesn't fare much better.) By contrast,
%%
%% F = enum:combine([enum:new(L) || L <- Lists])
%%
%% only maps existing lists. It may still be undesirable to step
%% through a very large number of values but it's possible, and easy
%% to step through a selection of values as an alternative.
%%
%% Functions that return enumerations.
-export([new/1,
combine/1,
reverse/1,
map/2,
append/1,
duplicate/2,
nthtail/2,
seq/2,
seq/3,
zip/1,
zip/2,
slice/3,
split/2]).
%% Functions that operate on existing enumerations.
-export([foreach/2,
foldl/3,
foldr/3,
all/2,
any/2,
member/2,
last/1,
nth/2,
to_list/1]).
%% ------------------------------------------------------------------------
%% new/1
%%
%% Turn a list/tuple of values into an enumeration that steps through
%% each element. Turn anything else into an enumeration of that single
%% value.
%% ------------------------------------------------------------------------
new(L)
when is_list(L) ->
new(list_to_tuple(L));
new(T)
when is_tuple(T) ->
enum(size(T), fun(N) -> element(N,T) end);
new(T) ->
fun(0) -> 1; (1) -> T end.
enum(Ord, F) ->
fun(0) -> Ord; (N) when 0 < N, N =< Ord -> F(N) end.
%% ------------------------------------------------------------------------
%% combine/1
%%
%% Map a list/tuple of enumerations to the enumeration of all
%% lists/tuples constructed by choosing one value from each
%% enumeration in the list/tuple.
%% ------------------------------------------------------------------------
combine(T)
when is_tuple(T) ->
F = combine(tuple_to_list(T)),
enum(F(0), fun(N) -> list_to_tuple(F(N)) end);
combine([]) ->
fun(0) -> 0 end;
%% Given positive integers n_1,...,n_k, construct a bijection from
%% {0,...,\prod_{i=1}^k} n_i - 1} to {0,...,n_1} x ... x {0,...,n_k}
%% that maps N to (N_1,...,N_k) where:
%%
%% N_1 = (N div 1) rem n_1
%% ...
%% N_k = (N div n_1*...*n_{k-1}) rem n_k
%%
%% That is:
%%
%% N_i = (N div \prod_{j=1}^{i-1} n_j) rem n_i
%%
%% This corresponds to looping through N_1, incrementing N_2 as N_1
%% loops, and so on up through N_k. The inverse map is as follows.
%%
%% (N_1,...,N_k) -> N = N_1 + N_2*n_1 + ... + N_k*n_{k-1}*...*n_1
%%
%% = \sum_{i=1}^k N_i*\prod_{j=i}^{i-1} n_j
%%
%% [Proof: Induction on k. For k=1 we have the identity map. If
%% g_k : (N_1,...,N_k) |-> N above is bijective then consider
%% the bijection
%%
%% G : (t,n) |--> t + n*K, K = n_k*...*n_1
%%
%% from {0,...,K-1} x {0,...,n_{k+1}-1} onto {0,...,n_{k+1}*K - 1}
%% with inverse F : n |--> (n rem K, n div K). Since
%%
%% g_{k+1}(N_1,...,N_{k+1}) = g_k(N_1,...,N_K) + N_{k+1}*K
%% = G(g_k(N_1,...,N_K), N_{k+1})
%%
%% and G, g_k and ((N-1,...,N_k),N_{k+1}) -> (N_1,...,N_{k+1})
%% are all bijections, so is g_{k+1}.]
combine([_|_] = L) ->
[Ord | Divs] = lists:foldl(fun(F,[D|_] = A) -> [F(0)*D | A] end, [1], L),
RL = lists:reverse(L),
enum(Ord, fun(N) -> combine(N, Ord, Divs, RL) end).
%% Since we use 0 to return the number of elements enumerated, use
%% bijections from {1,...,N} rather than {0,...,N-1}.
combine(N, Ord, Divs, L)
when 0 < N, N =< Ord ->
{Vs, []} = lists:foldl(fun(F, {A, [D|Ds]}) ->
{[F(1 + (((N-1) div D) rem F(0))) | A], Ds}
end,
{[], Divs},
L),
Vs.
%% ------------------------------------------------------------------------
%% reverse/1
%%
%% Construct the enumeration that reverses the order in which values
%% are traversed.
%% ------------------------------------------------------------------------
reverse(E) ->
Ord = E(0),
enum(Ord, fun(N) -> E(Ord + 1 - N) end).
%% ------------------------------------------------------------------------
%% map/2
%%
%% Construct an enumeration that maps enumerated values.
%% ------------------------------------------------------------------------
map(Fun, E) ->
enum(E(0), fun(N) -> Fun(E(N)) end).
%% ------------------------------------------------------------------------
%% append/2
%%
%% Construct an enumeration that successively steps through each of a
%% list of enumerations.
%% ------------------------------------------------------------------------
append(Es) ->
[Ord | Os] = lists:foldl(fun(E, [N|_] = A) -> [N+E(0)|A] end, [0], Es),
Rev = lists:reverse(Es),
enum(Ord, fun(N) -> append(N, Os, Rev) end).
append(N, [Ord | _], [E | _])
when N > Ord ->
E(N - Ord);
append(N, [_|Os], [_|Es]) ->
append(N, Os, Es).
%% ------------------------------------------------------------------------
%% duplicate/2
%%
%% Construct an enumeration that traverses an enumeration multiple
%% times. Equivalent to append(lists:duplicate(N, E)).
%% ------------------------------------------------------------------------
duplicate(N, E) ->
Ord = E(0),
enum(N*Ord, fun(M) -> E(1 + ((M-1) rem Ord)) end).
%% ------------------------------------------------------------------------
%% nthtail/2
%%
%% Construct an enumeration that omits values at the head of an
%% existing enumeration.
%% ------------------------------------------------------------------------
nthtail(N, E)
when 0 =< N ->
nthtail(E(0) - N, N, E).
nthtail(Ord, N, E)
when 0 =< Ord ->
enum(Ord, fun(M) -> E(M+N) end).
%% ------------------------------------------------------------------------
%% seq/[23]
%%
%% Construct an enumeration that steps through a sequence of integers.
%% ------------------------------------------------------------------------
seq(From, To) ->
seq(From, To, 1).
seq(From, To, Incr)
when From =< To ->
enum((To - From + Incr) div Incr, fun(N) -> From + (N-1)*Incr end).
%% ------------------------------------------------------------------------
%% zip/[12]
%%
%% Construct an enumeration whose nth value is the list of nth values
%% of a list of enumerations.
%% ------------------------------------------------------------------------
zip(Es) ->
zip(fun(T) -> T end, Es).
zip(_, []) ->
[];
zip(Fun, Es) ->
enum(lists:min([E(0) || E <- Es]), fun(N) -> Fun([E(N) || E <- Es]) end).
%% ------------------------------------------------------------------------
%% slice/3
%%
%% Construct an enumeration of a given length from a given starting point.
%% ------------------------------------------------------------------------
slice(N, Len, E)
when is_integer(N), N > 0, is_integer(Len), Len >= 0 ->
slice(N, Len, E(0) - (N - 1), E).
slice(_, _, Tail, _)
when Tail < 1 ->
fun(0) -> 0 end;
slice(N, Len, Tail, E) ->
enum(lists:min([Len, Tail]), fun(M) -> E(N-1+M) end).
%% ------------------------------------------------------------------------
%% split/2
%%
%% Split an enumeration into a list of enumerations of the specified
%% length. The last enumeration of the list may have order less than
%% this length.
%% ------------------------------------------------------------------------
split(Len, E)
when is_integer(Len), Len > 0 ->
split(1, E(0), Len, E, []).
split(N, Ord, _, _, Acc)
when N > Ord ->
lists:reverse(Acc);
split(N, Ord, Len, E, Acc) ->
split(N+Len, Ord, Len, E, [slice(N, Len, E) | Acc]).
%% ------------------------------------------------------------------------
%% foreach/2
%%
%% Apply a fun to each value of an enumeration.
%% ------------------------------------------------------------------------
foreach(Fun, E) ->
foldl(fun(N,ok) -> Fun(N), ok end, ok, E).
%% ------------------------------------------------------------------------
%% foldl/3
%% foldr/3
%%
%% Fold through values in an enumeration.
%% ------------------------------------------------------------------------
foldl(Fun, Acc, E) ->
foldl(E(0), 1, Fun, Acc, E).
foldl(M, N, _, Acc, _)
when N == M+1 ->
Acc;
foldl(M, N, Fun, Acc, E) ->
foldl(M, N+1, Fun, Fun(E(N), Acc), E).
foldr(Fun, Acc, E) ->
foldl(Fun, Acc, reverse(E)).
%% ------------------------------------------------------------------------
%% all/2
%%
%% Do all values of an enumeration satisfy a predicate?
%% ------------------------------------------------------------------------
all(Pred, E) ->
all(E(0), 1, Pred, E).
all(M, N, _, _)
when N == M+1 ->
true;
all(M, N, Pred, E) ->
Pred(E(N)) andalso all(M, N+1, Pred, E).
%% Note that andalso/orelse are tail-recusive as of R13A.
%% ------------------------------------------------------------------------
%% any/2
%%
%% Does any value of an enumeration satisfy a predicate?
%% ------------------------------------------------------------------------
any(Pred, E) ->
any(E(0), 1, Pred, E).
any(M, N, _, _)
when N == M+1 ->
false;
any(M, N, Pred, E) ->
Pred(E(N)) orelse any(M, N+1, Pred, E).
%% ------------------------------------------------------------------------
%% member/2
%%
%% Does a value match any in an enumeration?
%% ------------------------------------------------------------------------
member(X, E) ->
member(E(0), 1, X, E).
member(M, N, _, _)
when N == M+1 ->
false;
member(M, N, X, E) ->
match(E(N), X) orelse member(M, N+1, X, E).
match(X, X) ->
true;
match(_, _) ->
false.
%% ------------------------------------------------------------------------
%% last/1
%%
%% Return the last value of an enumeration.
%% ------------------------------------------------------------------------
last(E) ->
E(E(0)).
%% ------------------------------------------------------------------------
%% nth/2
%%
%% Return a selected value of an enumeration.
%% ------------------------------------------------------------------------
nth(N, E) ->
E(N).
%% ------------------------------------------------------------------------
%% to_list/1
%%
%% Turn an enumeration into a list. Not good if the very many values
%% are enumerated.
%% ------------------------------------------------------------------------
to_list(E) ->
foldr(fun(X,A) -> [X|A] end, [], E).
|