summaryrefslogtreecommitdiff
path: root/src/lqueue.erl
blob: 8bbd52d9fc143625809db71a5e1ad285def5ba8c (plain)
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
-module(lqueue).

-compile([export_all]).

-define(QUEUE, queue).

new() -> {0, ?QUEUE:new()}.

is_empty({0, _Q}) ->
    true;
is_empty(_) ->
    false.

in(V, {L, Q}) -> {L+1, ?QUEUE:in(V, Q)}.
in_r(V, {L, Q}) -> {L+1, ?QUEUE:in_r(V, Q)}.

out({0, _Q} = Q) ->
    {empty, Q};
out({L, Q}) ->
    {Result, Q1} = ?QUEUE:out(Q),
    {Result, {L-1, Q1}}.

out_r({0, _Q} = Q) ->
    {empty, Q};
out_r({L, Q}) ->
    {Result, Q1} = ?QUEUE:out_r(Q),
    {Result, {L-1, Q1}}.

join({L1, Q1}, {L2, Q2}) ->
    {L1 + L2, ?QUEUE:join(Q1, Q2)}.

to_list({_L, Q}) ->
    ?QUEUE:to_list(Q).

from_list(L) ->
    {length(L), ?QUEUE:from_list(L)}.

queue_fold(Fun, Init, Q) ->
    case out(Q) of
        {empty, _Q}      -> Init;
        {{value, V}, Q1} -> queue_fold(Fun, Fun(V, Init), Q1)
    end.

len({L, _Q}) ->
    L.