Last active
February 9, 2016 15:37
-
-
Save sashaafm/9311d5ac9494f4688e22 to your computer and use it in GitHub Desktop.
Helper Func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gfe(head], _, []), do: {head, []} | |
def gfe([], {:empty, :empty}, new_queue), do: {nil, new_queue} | |
def gfe([], front, new_queue), do: {front, new_queue} | |
def gfe([{head_item, head_prio} = head | []], front, new_queue) do | |
{front_item, front_prio} = front | |
if head_prio > front_prio do | |
{head, new_queue ++ [front]} | |
else | |
{front, new_queue ++ [head]} | |
end | |
end | |
def gfe([{head_item, head_prio} = head | tail], front, new_queue) do | |
{front_item, front_prio} = front | |
{t_head_item, t_head_prio} = List.first tail | |
if front_prio >= head_prio && front_prio != :empty do | |
gfe tail, front, new_queue ++ [head] | |
else | |
if head_prio >= t_head_prio do | |
gfe tail -- [{t_head_item, t_head_prio}], head, | |
new_queue ++ [{t_head_item, t_head_prio}] | |
else | |
gfe tail -- [{t_head_item, t_head_prio}], {t_head_item, t_head_prio}, | |
new_queue ++ [head] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment