diff options
| author | Stephen Blott | 2016-01-28 16:21:44 +0000 |
|---|---|---|
| committer | Stephen Blott | 2016-01-28 16:21:44 +0000 |
| commit | b586d9dea17496bcedee37320841020592db35bd (patch) | |
| tree | d29815936baed815c5b753c01dc57d1589694fc5 | |
| parent | adafa5e8546627e993da6d03b59a27fd4a9162b7 (diff) | |
| download | vimium-b586d9dea17496bcedee37320841020592db35bd.tar.bz2 | |
Simplify hint-string generation
| -rw-r--r-- | content_scripts/link_hints.coffee | 49 |
1 files changed, 16 insertions, 33 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index 8fc1446b..68cbb915 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -440,39 +440,22 @@ class AlphabetHints # may be of different lengths. # hintStrings: (linkCount) -> - # Determine how many digits the link hints will require in the worst case. Usually we do not need - # all of these digits for every link single hint, so we can show shorter hints for a few of the links. - digitsNeeded = Math.ceil(@logXOfBase(linkCount, @linkHintCharacters.length)) - # Short hints are the number of hints we can possibly show which are (digitsNeeded - 1) digits in length. - shortHintCount = Math.floor( - (Math.pow(@linkHintCharacters.length, digitsNeeded) - linkCount) / - @linkHintCharacters.length) - longHintCount = linkCount - shortHintCount - - hintStrings = [] - - if (digitsNeeded > 1) - for i in [0...shortHintCount] - hintStrings.push(numberToHintString(i, @linkHintCharacters, digitsNeeded - 1)) - - start = shortHintCount * @linkHintCharacters.length - for i in [start...(start + longHintCount)] - hintStrings.push(numberToHintString(i, @linkHintCharacters, digitsNeeded)) - - @shuffleHints(hintStrings, @linkHintCharacters.length) - - # - # This shuffles the given set of hints so that they're scattered -- hints starting with the same character - # will be spread evenly throughout the array. - # - shuffleHints: (hints, characterSetLength) -> - buckets = ([] for i in [0...characterSetLength] by 1) - for hint, i in hints - buckets[i % buckets.length].push(hint) - result = [] - for bucket in buckets - result = result.concat(bucket) - result + return [] if linkCount == 0 + + # In the following: + # - at no point is any hint in hints a prefix of any other hint + # - the shorter hints are always at the start of the list + hints = [""] + offset = 0 + while hints.length - offset < linkCount or hints.length == 1 + hint = hints[offset++] + hints.push hint + ch for ch in @linkHintCharacters + hints = hints[offset...offset+linkCount] + + # This shuffles the hints so that they're scattered; hints starting with the same character are spread + # evenly throughout the array. We reverse each hint, then sort them then reverse again. + hints = (hint.split("").reverse().join "" for hint in hints).sort() + return (hint.split("").reverse().join "" for hint in hints[...linkCount]) getMatchingHints: (hintMarkers) -> matchString = @hintKeystrokeQueue.join "" |
