File: queue.erl

package info (click to toggle)
erlang 1%3A11.b.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 54,332 kB
  • ctags: 138,013
  • sloc: erlang: 566,932; ansic: 185,450; makefile: 14,148; java: 7,835; sh: 7,307; lisp: 5,249; pascal: 3,225; perl: 2,290; asm: 1,325; cpp: 306; tcl: 245; csh: 29; python: 21; sed: 9
file content (277 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download
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
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%% 
%%     $Id$
%%
-module(queue).

%% New module interface
-export([is_empty/1,len/1,
	 cons/2,head/1,tail/1,snoc/2,last/1,daeh/1,init/1,lait/1,
	 reverse/1,join/2,split/2]).

%% New "old style" module interface
-export([in_r/2,out_r/1,from_list/1]).

%%--------------------------------------------------------------------------
%% The original module interface

-export([new/0, in/2, out/1, to_list/1]).

%% efficient implementation of fifo queues

new() -> {[],[]}.

%% Append to tail
in(X, {R,F}=Q) when list(R), list(F) -> snoc(Q, X);
in(X, Q) ->
    erlang:fault(badarg, [X,Q]).

%% Take from head
out({In,[V]}) when list(In) ->
    {{value,V},r2f(In)};
out({In,[V|Out]}) when list(In) ->
    {{value,V},{In,Out}};
out({[],[]}=Q) ->
    {empty,Q};
out({[V],[]}) ->
    {{value,V},{[],[]}};
out({[_|_]=In,[]}) ->
    out(r2f(In));
out(Q) ->
    erlang:fault(badarg, [Q]).

to_list({In,Out}) when list(In), list(Out) ->
    Out++lists:reverse(In, []);
to_list(Q) ->
    erlang:fault(badarg, [Q]).

%%--------------------------------------------------------------------------
%% Some new "old style" functions for reversed queue handling.

%% Prepend to head
in_r(X, {R,F}=Q) when list(R), list(F) -> cons(X, Q);
in_r(X, Q) ->
    erlang:fault(badarg, [X,Q]).

%% Take from tail
out_r({[V],Out}) when list(Out) ->
    {{value,V},f2r(Out)};
out_r({[V|In],Out}) when list(Out) ->
    {{value,V},{In,Out}};
out_r({[],[]}=Q) ->
    {empty,Q};
out_r({[],[V]}) ->
    {{value,V},{[],[]}};
out_r({[],[_|_]=Out}) ->
    out_r(f2r(Out));
out_r(Q) ->
    erlang:fault(badarg, [Q]).

%% Create queue from list
from_list(L) when list(L) ->
    {[],L};
from_list(L) ->
    erlang:fault(badarg, [L]).

%%--------------------------------------------------------------------------
%% New API inspired by an Erlang user contribution "deque.erl" 
%% by Claes Wikstrom <klacke@kaja.klacke.net> 1999.
%%
%% This implementation does not use the internal data format from Klacke's
%% doubly ended queues that was "shamelessly stolen" from 
%% "Purely Functional Data structures" by Chris Okasaki, since the data
%% format of this module must remain the same in case some application
%% has saved a queue in external format or sends it to an old node.
%%
%% This implementation tries to do the best of the situation and should 
%% be almost as efficient as Okasaki's queues, except for len/1 that
%% is O(n) in this implementation instead of O(1).
%%
%% The implementation does this by trying to keep least at least one 
%% element in both the forward and the reversed lists so that 
%% i.e head/1 or last/1 will not have to reverse a list
%% to find the element.
%%
%% To be compatible with the old version of this module, as much data as 
%% possible is moved to the receiving side using lists:reverse/2 when data
%% is needed, except for two elements (when possible). These two elements
%% are kept to prevent alternating tail/1 and init/1 operations from 
%% moving data back and forth between the sides.
%%
%% An alternative would be to balance for equal list length when one side
%% is exhausted. Although this could be better for a general double
%% ended queue, it would more han double the amortized cost for 
%% the normal case (one way queue).

is_empty({[],[]}) ->
    true;
is_empty({In,Out}) when list(In), list(Out) ->
    false;
is_empty(Q) ->
    erlang:fault(badarg, [Q]).

len({In,Out}) when list(In), list(Out) ->
    erlang:length(In)+erlang:length(Out);
len(Q) ->
    erlang:fault(badarg, [Q]).



%% Cons to head
%%
%% Put at least one element in each list, if it is cheap
cons(X, {[],[]}) ->
    {[], [X]};
cons(X, {[], [_]=F}) ->
    {F,[X]};
cons(X, {R,F}) when list(R), list(F) ->
    {R,[X|F]};
cons(X, Q) ->
    erlang:fault(badarg, [X,Q]).

%% Return head element
head({R,[H|_]}) when list(R) ->
    H;
head({[],[]}=Q) ->
    erlang:fault(empty, [Q]);
head({[H],[]}) ->
    H;
head({[_|R],[]}) ->
    [H|_] = lists:reverse(R),
    H;
head(Q) ->
    erlang:fault(badarg, [Q]).

%% Remove head element and return resulting queue
tail({R, [_]}) when list(R) ->
    r2f(R);
tail({R, [_|F]}) when list(R) ->
    {R,F};
tail({[],[]}=Q) ->
    erlang:fault(empty, [Q]);
tail({[_],[]}) ->
    {[],[]};
tail({[_|_]=R,[]}) ->
    tail(r2f(R));
tail(Q) ->
    erlang:fault(badarg, [Q]).

%% Functions operating on the other end of the queue

%% Cons to tail
%%
%% Put at least one element in each list, if it is cheap
snoc({[],[]}, X) ->
    {[X], []};
snoc({[_]=R, []}, X) ->
    {[X], R};
snoc({R,F}, X) when list(R), list(F) ->
    {[X|R],F};
snoc(Q, X) ->
    erlang:fault(badarg, [Q,X]).

%% Return last element
daeh(Q) -> last(Q).

last({[H|_],F}) when list(F) ->
    H;
last({[],[]}=Q) ->
    erlang:fault(empty, [Q]);
last({[],[H]}) ->
    H;
last({[],[_|R]}) ->
    [H|_] = lists:reverse(R),
    H;
last(Q) ->
    erlang:fault(badarg, [Q]).

%% Remove tail element and return resulting queue
lait(Q) -> init(Q).

init({[_], F}) when list(F) ->
    f2r(F);
init({[_|R], F}) when list(F) ->
    {R,F};
init({[],[]}=Q) ->
    erlang:fault(empty, [Q]);
init({[],[_]}) ->
    {[],[]};
init({[],[_|_]=F}) ->
    init(f2r(F));
init(Q) ->
    erlang:fault(badarg, [Q]).



%% Return reversed queue
reverse({R,F}) when list(R), list(F) ->
    {F,R};
reverse(Q) ->
    erlang:fault(badarg, [Q]).

%% Join two queues (perhaps not very efficient)
join({R,F}=Q, {[],[]}) when list(R), list(F) ->
    Q;
join({[],[]}, {R,F}=Q) when list(R), list(F) ->
    Q;
join({R1,F1}, {R2,F2}) when list(R1), list(F1), list(R2), list(F2) ->
    {R2,F1++lists:reverse(R1,F2)};
join(Q1, Q2) ->
    erlang:fault(badarg, [Q1,Q2]).

%% Split a queue in two (perhaps not very efficient)
split(N, {R,F}=Q) when N =:= 0, list(R), list(F) ->
    {{[],[]},Q};
split(N, {R,F}=Q) when integer(N), N > 0, list(R), list(F) ->
    Lf = erlang:length(F),
    if  N < Lf ->
	    {F1,F2} = lists:split(N, F),
	    {f2r(F1),{R,F2}};
        N > Lf ->
	    case catch lists:split(erlang:length(R)-(N-Lf), R) of
		{'EXIT',_} ->
		    erlang:fault(badarg, [N,Q]);
		{R1,R2} ->
		    {{R2,F},r2f(R1)}
	    end;
	true ->
	    {f2r(F),r2f(R)}
    end;
split(N, Q) ->
    erlang:fault(badarg, [N,Q]).

%%--------------------------------------------------------------------------
%% Internal workers

%% Move all but two from R to F, if there are enough
r2f([]) ->
    {[],[]};
r2f([_]=R) ->
    {[],R};
r2f([H1,H2]) ->
    {[H1],[H2]};
r2f([H1,H2|T]) ->
    {[H1,H2],lists:reverse(T, [])}.

%% Move all but two from F to R, if there are enough
f2r([]) ->
    {[],[]};
f2r([_]=F) ->
    {F,[]};
f2r([H1,H2]) ->
    {[H2],[H1]};
f2r([H1,H2|T]) ->
    {lists:reverse(T, []),[H1,H2]}.