diff --git a/TallyCore/VoteCounter.cs b/TallyCore/VoteCounter.cs index 490c5bac..0bcd2125 100644 --- a/TallyCore/VoteCounter.cs +++ b/TallyCore/VoteCounter.cs @@ -181,34 +181,25 @@ public bool Merge(string fromVote, string toVote, VoteType voteType) if (voteType == VoteType.Rank) { - List>> removedVotes = new List>>(); + Dictionary>, string> mergedVotes = new Dictionary>, string>(); - bool merged = false; foreach (var vote in votesSet) { if (VoteString.CondenseRankVote(vote.Key) == fromVote) { string toContent = VoteString.GetVoteContent(toVote, voteType); - string revisedKey = VoteString.ModifyVoteLine(vote.Key, content: toContent); - if (Rename(vote, revisedKey, voteType)) - { - removedVotes.Add(vote); - merged = true; - } + mergedVotes.Add(vote, revisedKey); } } - if (merged) + foreach (var merge in mergedVotes) { - foreach (var removed in removedVotes) - { - votesSet.Remove(removed.Key); - } + Rename(merge.Key, merge.Value, VoteType.Rank); } - - return merged; + + return mergedVotes.Count > 0; } @@ -333,7 +324,6 @@ public bool Rename(string oldVote, string newVote, VoteType voteType) return false; } - /// /// Rename a vote. /// @@ -351,6 +341,7 @@ private bool Rename(KeyValuePair> vote, string revisedKe return false; var votesSet = GetVotesCollection(voteType); + string oldVoteKey = vote.Key; HashSet votes; if (votesSet.TryGetValue(revisedKey, out votes)) @@ -362,10 +353,11 @@ private bool Rename(KeyValuePair> vote, string revisedKe votesSet[revisedKey] = vote.Value; } + votesSet.Remove(oldVoteKey); + return true; } - /// /// Add a supporter to the supplied vote. /// Adds the vote to the vote list if it didn't already exist.