Skip to content

Commit

Permalink
dedupe Node.nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Apr 20, 2018
1 parent 14bb836 commit 714954b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,25 @@ func NewNode(ID uint64, store StableStore) *Node {
return n
}

func removeDuplicatesNodes(a []*Node) []*Node {
result := []*Node{}
seen := map[uint64]*Node{}
for _, val := range a {
if _, ok := seen[val.ID]; !ok {
result = append(result, val)
seen[val.ID] = val
}
}
return result
}

// MingleNodes lets each node know about the other, including itself.
func MingleNodes(nodes ...*Node) {
for _, n := range nodes {
// guard against adding same node twice
// TODO: fix this since it breaks down if len(nodes) > n.nodes even when some of those nodes already exist in n.nodes
if len(n.nodes) < len(nodes) {
n.nodes = append(n.nodes, nodes...)
}
incomingNodes := nodes
unDedupedNodes := append(incomingNodes, n.nodes...)
dedupedNodes := removeDuplicatesNodes(unDedupedNodes)
n.nodes = dedupedNodes
}
}

Expand Down

0 comments on commit 714954b

Please sign in to comment.