diff options
author | Matthew Sackman <matthew@rabbitmq.com> | 2011-07-28 15:02:11 +0100 |
---|---|---|
committer | Matthew Sackman <matthew@rabbitmq.com> | 2011-07-28 15:02:11 +0100 |
commit | 0db8533141a584e8ed63ee8c6dc8ba6a41029b1b (patch) | |
tree | d832c14805b48e417571530e2aee2c4b3793c886 | |
parent | 4a60226bc8b715c2d64cac16e7b59cc0cacafa71 (diff) | |
download | rabbitmq-server-0db8533141a584e8ed63ee8c6dc8ba6a41029b1b.tar.gz |
Minor refactor and introduce horrific inefficiency
-rw-r--r-- | src/priority_queue.erl | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/src/priority_queue.erl b/src/priority_queue.erl index ccc87cd5..34787903 100644 --- a/src/priority_queue.erl +++ b/src/priority_queue.erl @@ -90,12 +90,8 @@ len({pqueue, Queues}) -> to_list({queue, In, Out}) when is_list(In), is_list(Out) -> [{0, V} || V <- Out ++ lists:reverse(In, [])]; to_list({pqueue, Queues}) -> - [{P1, V} || {P, Q} <- Queues, - case P of - infinity -> P1 = P, true; - _ -> P1 = -P, true - end, - {0, V} <- to_list(Q)]. + [{maybe_negate_priority(P), V} || {P, Q} <- Queues, + {0, V} <- to_list(Q)]. in(Item, Q) -> in(Item, 0, Q). @@ -109,10 +105,7 @@ in(X, Priority, _Q = {queue, [], []}) -> in(X, Priority, Q = {queue, _, _}) -> in(X, Priority, {pqueue, [{0, Q}]}); in(X, Priority, {pqueue, Queues}) -> - P = case Priority of - infinity -> Priority; - _ -> -Priority - end, + P = maybe_negate_priority(Priority), {pqueue, case lists:keysearch(P, 1, Queues) of {value, {_, Q}} -> lists:keyreplace(P, 1, Queues, {P, in(X, Q)}); @@ -193,3 +186,6 @@ r2f([]) -> {queue, [], []}; r2f([_] = R) -> {queue, [], R}; r2f([X,Y]) -> {queue, [X], [Y]}; r2f([X,Y|R]) -> {queue, [X,Y], lists:reverse(R, [])}. + +maybe_negate_priority(infinity) -> infinity; +maybe_negate_priority(P) -> -P. |