Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (56 sloc) 1.628 kb
84adefa The R13B03 release.
Erlang/OTP authored
1 %%
2 %% %CopyrightBegin%
3 %%
4 %% Copyright Ericsson AB 2006-2009. All Rights Reserved.
5 %%
6 %% The contents of this file are subject to the Erlang Public License,
7 %% Version 1.1, (the "License"); you may not use this file except in
8 %% compliance with the License. You should have received a copy of the
9 %% Erlang Public License along with this software. If not, it can be
10 %% retrieved online at http://www.erlang.org/.
11 %%
12 %% Software distributed under the License is distributed on an "AS IS"
13 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14 %% the License for the specific language governing rights and limitations
15 %% under the License.
16 %%
17 %% %CopyrightEnd%
18 %%
19 -module(next_perm).
20
21 -export([next_perm/1, prev_perm/1, load/0, all_perm/1]).
22
23 load() ->
24 case whereis(next_perm) of
25 undefined ->
26 case erl_ddll:load_driver(".", "next_perm") of
27 ok -> ok;
28 {error, already_loaded} -> ok;
29 E -> exit(E)
30 end,
31 Port = open_port({spawn, "next_perm"}, []),
32 register(next_perm, Port);
33 _ ->
34 ok
35 end.
36
37 list_to_integer_binaries(L) ->
38 [<<I:32/integer-native>> || I <- L].
39
40 next_perm(L) ->
41 next_perm(L, 1).
42
43 prev_perm(L) ->
44 next_perm(L, 2).
45
46 next_perm(L, Nxt) ->
47 load(),
48 B = list_to_integer_binaries(L),
49 Port = whereis(next_perm),
50 port_command(Port, [Nxt, B]),
51 receive
52 [Port | Result] ->
53 Result
54 end.
55
56 all_perm(L) ->
57 New = prev_perm(L),
58 all_perm(New, L, [New]).
59
60 all_perm(L, L, Acc) ->
61 Acc;
62 all_perm(L, Orig, Acc) ->
63 New = prev_perm(L),
64 all_perm(New, Orig, [New | Acc]).
65
66
Something went wrong with that request. Please try again.