diff --git a/zabbrev/Program.cs b/zabbrev/Program.cs index 88de86f..701adbf 100644 --- a/zabbrev/Program.cs +++ b/zabbrev/Program.cs @@ -1028,9 +1028,9 @@ namespace zabbrev // Add to a Max Heap swPart = Stopwatch.StartNew(); Console.Error.Write("Build max heap with naive score...".PadRight(40)); - PriorityQueue maxHeap = new(new PatternComparer()); - PriorityQueue maxHeapRefactor = new(new PatternComparer()); - PriorityQueue maxHeapLength = new(new PatternComparer()); + PriorityQueue maxHeap = new(new PatternComparer()); + PriorityQueue maxHeapRefactor = new(new PatternComparer()); + PriorityQueue maxHeapLength = new(new PatternComparer()); foreach (KeyValuePair phraseRevisedIterator in patternDictionary) { KeyValuePair phrase = phraseRevisedIterator; @@ -1040,12 +1040,12 @@ namespace zabbrev if (phrase.Key.Length <= CUTOFF_LONG_PATTERN) { // Save all short patterns - maxHeap.Enqueue(phrase.Value, phrase.Value.Savings); + maxHeap.Enqueue(phrase.Value, (phrase.Value.Savings, phrase.Value.Key)); } else { // Store longer patterns for later - maxHeapLength.Enqueue(phrase.Value, phrase.Key.Length); + maxHeapLength.Enqueue(phrase.Value, (phrase.Key.Length, phrase.Value.Key)); } } } @@ -1058,8 +1058,8 @@ namespace zabbrev tempHashSet.Add(candidate.Key[..^1]); if (!tempHashSet.Contains(candidate.Key)) { - maxHeap.Enqueue(candidate, candidate.Savings); - maxHeapRefactor.Enqueue(candidate, candidate.Key.Length); + maxHeap.Enqueue(candidate, (candidate.Savings, candidate.Key)); + maxHeapRefactor.Enqueue(candidate, (candidate.Key.Length, candidate.Key)); } } tempHashSet = null; @@ -1106,8 +1106,7 @@ namespace zabbrev PatternData KPD = bestCandidateList[currentAbbrev]; KPD.Savings = deltaSavings; bestCandidateList.RemoveAt(currentAbbrev); - maxHeap.Enqueue(KPD, KPD.Savings); - } + maxHeap.Enqueue(KPD, (KPD.Savings, KPD.Key)); } else { if (printDebug) @@ -1126,7 +1125,7 @@ namespace zabbrev { if (printDebug) Console.Error.WriteLine("Removing abbrev: " + FormatAbbreviation(bestCandidateList[i].Key)); - maxHeap.Enqueue(bestCandidateList[i], bestCandidateList[i].Savings); + maxHeap.Enqueue(bestCandidateList[i], (bestCandidateList[i].Savings, bestCandidateList[i].Key)); bestCandidateList.RemoveAt(i); i -= 1; currentAbbrev -= 1; @@ -1178,7 +1177,7 @@ namespace zabbrev // Restore best candidate list to numberOfAbbrevs patterns for (int i = (NumberOfAbbrevs + oversample - 1); i >= NumberOfAbbrevs; i--) { - maxHeap.Enqueue(bestCandidateList[i], bestCandidateList[i].Savings); + maxHeap.Enqueue(bestCandidateList[i], (bestCandidateList[i].Savings, bestCandidateList[i].Key)); bestCandidateList.RemoveAt(i); } } @@ -1823,18 +1822,18 @@ namespace zabbrev } } - public class PatternComparer : IComparer + public class PatternComparer : IComparer<(int savings, string key)> { - public int Compare(int x, int y) + public int Compare((int savings, string key) x, (int savings, string key) y) { // Here we compare this instance with other. // If this is supposed to come before other once sorted then // we should return a negative number. // If they are the same, then return 0. // If this one should come after other then return a positive number. - if (x > y) return -1; - if (x < y) return 1; - return 0; + if (x.savings > y.savings) return -1; + if (x.savings < y.savings) return 1; + return string.CompareOrdinal(x.key, y.key); } } @@ -2173,7 +2172,12 @@ namespace zabbrev } else { - returnList.Sort((firstPair, nextPair) => ((int)firstPair.Key.Length).CompareTo((int)nextPair.Key.Length)); + returnList.Sort((firstPair, nextPair) => + { + int cmp = ((int)firstPair.Key.Length).CompareTo((int)nextPair.Key.Length); + if (cmp != 0) return cmp; + return string.CompareOrdinal(nextPair.Key, firstPair.Key); + }); returnList.Reverse(); for (int i = NumberOfAbbrevs; i < abbrevList.Count; i++)