emongo / t / pqueue_test.erl

-module(pqueue_test).

-compile(export_all).

test(Data, F) ->
    {Time1, Sorted1} = timer:tc(lists, sort, [Data]),
    {Time21, Pushed1} = timer:tc(lists, foldl, [fun({P, I}, Q) ->
                                                        pqueue:push(P, I, Q)
                                                end, pqueue:new(), Data]),
    
    Sorted = [I || {_, V} = I <- Sorted1, F(V)],
    Pushed = pqueue:filter(F, Pushed1),
    
    {Time22, _} = timer:tc(?MODULE, pop_items, [Pushed]),
    Time2 = Time21 + Time22,
    
    io:format("Time sorted: ~p~nTime pushed: ~p~nDivide: ~p~n", [Time1, Time2, Time1/Time2]),
    
    LenS = length(Sorted),
    LenP = pqueue:size(Pushed),
    
    LenS = LenP,
    io:format("Length: ~p~n", [LenS]),

    lists:foldl(fun({_, I}, Q) ->
                        {I, NQ} = pqueue:pop(Q),
                        NQ
                end, Pushed, Sorted).

test() ->
    Len = 10000,
    Dev = 10,
    Fun = fun(I) -> I rem 3 =/= 0 end,
    
    Data = [{random:uniform(Dev), I} || I <- lists:seq(1, Len)],
    test(Data, Fun).

test2() ->
    Queue0 = push_items([{1, 1}, {1, 2}, {5, 3}, {2, 4}], pqueue:new()),
    4 = pqueue:size(Queue0),
    
    Queue1 = ensure_items([1, 2, 4], Queue0),
    1 = pqueue:size(Queue1),
    
    Queue2 = push_items([{2, 5}, {3, 6}], Queue1),
    3 = pqueue:size(Queue2),
    
    Queue3 = ensure_items([5, 3, 6], Queue2),
    
    Queue3 = pqueue:new().
    
push_items(Data, Queue) ->
    lists:foldl(fun({P, I}, Q) -> pqueue:push(P, I, Q) end,
                Queue,
                Data).

ensure_items(Items, Queue) ->
    lists:foldl(fun(I, Q) -> {I, NQ} = pqueue:pop(Q), NQ end,
                Queue,
                Items).

pop_items(Queue) ->
    case pqueue:size(Queue) of
        0 ->
            ok;
        _ ->
            {_Item, NewQueue} = pqueue:pop(Queue),
            pop_items(NewQueue)
    end.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.