Skip to content

Commit

Permalink
add missing merged clusters heap
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed May 21, 2017
1 parent 8d5b729 commit ecbd138
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions lib/rock/heaps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Rock.Heaps do
%Cluster{uuid: u_uuid} = u_cluster,
theta) do

merged_cluster = v_cluster |> Cluster.merge(u_cluster)
w_cluster = v_cluster |> Cluster.merge(u_cluster)

new_heaps =
heaps
Expand All @@ -44,20 +44,42 @@ defmodule Rock.Heaps do
if cross_link_count == 0 do
heap
else
heap |> Heap.add_item(merged_cluster, cross_link_count, theta)
heap |> Heap.add_item(w_cluster, cross_link_count, theta)
end
end)

new_heaps =
new_heaps
|> remove_heap(v_uuid)
|> remove_heap(u_uuid)

#need optimization, move to to heaps update ^
w_heap = new_heaps |> construct_w_heap(w_cluster)

{[w_heap | new_heaps], w_heap}
end

def remove_heap(heaps, uuid) do
defp remove_heap(heaps, uuid) do
heaps
|> Enum.filter(fn(%Heap{cluster: %Cluster{uuid: cluster_uuid}}) ->
uuid != cluster_uuid
end)
end

defp construct_w_heap(heaps, %Cluster{uuid: w_uuid} = w_cluster) do
items =
heaps
|> Enum.map(fn(%Heap{cluster: %Cluster{uuid: uuid}} = heap) ->
item = heap |> Heap.find_item(w_uuid)
{uuid, item}
end)
|> Enum.filter(fn({_, item}) ->
item != nil
end)
|> Enum.map(fn({uuid, {measure, cross_link_count, _}}) ->
{measure, cross_link_count, uuid}
end)

%Heap{cluster: w_cluster, items: items}
end
end
4 changes: 2 additions & 2 deletions test/rock/heaps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ defmodule Rock.HeapsTest do
cluster1 = %Cluster{uuid: uuid1} = clusters |> Enum.at(0)
cluster2 = %Cluster{uuid: uuid2} = clusters |> Enum.at(1)

new_heaps = heaps |> Heaps.update(cluster1, cluster2, theta)
{new_heaps, _cluster3} = heaps |> Heaps.update(cluster1, cluster2, theta)

assert Enum.count(heaps) == (Enum.count(new_heaps) + 2)
assert Enum.count(heaps) == (Enum.count(new_heaps) + 1)
refute new_heaps |> Enum.any?(fn(%Heap{cluster: %Cluster{uuid: uuid}}) ->
(uuid == uuid1) || (uuid == uuid2)
end)
Expand Down

0 comments on commit ecbd138

Please sign in to comment.