fastest version so far

This commit is contained in:
Jorge Morante 2023-04-14 09:38:59 +02:00
parent 6517ba2209
commit 93d5957f00
4 changed files with 43 additions and 13 deletions

View File

@ -1,5 +1,7 @@
require "spec"
require "../../../src/fingers/hinter"
require "../../../src/fingers/state"
require "../../../src/fingers/commands/load_config"
record StateDouble, selected_hints : Array(String)
@ -25,14 +27,11 @@ class TestFormatter < ::Fingers::Formatter
end
describe Fingers::Hinter do
input = "
ola ke ase
ke ase ola
ke olaola ke
ke ola ase
beep beep
"
input = 50.times.map do
10.times.map do
rand.to_s.split(".").last
end.join(" ")
end.join("\n")
width = 40
@ -40,13 +39,14 @@ beep beep
formatter = TestFormatter.new
patterns = ["ola"]
patterns = Fingers::Commands::LoadConfig::DEFAULT_PATTERNS.values.to_a
alphabet = "asdf".split("")
hinter = Fingers::Hinter.new(
input: input,
width: width,
patterns: patterns,
state: ::Fingers::State.new,
alphabet: alphabet,
output: output,
formatter: formatter,

View File

@ -1,4 +1,5 @@
require "file_utils"
require "./base"
require "../dirs"
require "../config"
require "../../tmux"

View File

@ -22,9 +22,15 @@ class Huffman
end
def generate_hints(alphabet : Array(String), n : Int32)
setup!(alphabet: alphabet, n: n)
cached_result = read_from_cache(alphabet, n)
Log.info { "from_cache: #{cached_result}" }
return cached_result unless cached_result.nil?
return alphabet if n <= alphabet.size
if n <= alphabet.size
return alphabet
end
setup!(alphabet: alphabet, n: n)
first_node = true
@ -50,7 +56,11 @@ class Huffman
result.push(translate_path(path)) if node.children.empty?
end
result.sort_by { |hint| hint.size }
final_result = result.sort_by { |hint| hint.size }
save_to_cache(alphabet, n, final_result)
final_result
end
private def setup!(alphabet, n)
@ -73,6 +83,21 @@ class Huffman
result
end
private def read_from_cache(alphabet, n) : Array(String) | Nil
File.read(cache_key(alphabet, n)).chomp.split(":")
rescue File::NotFoundError
Log.info { "hint cache miss" }
nil
end
private def save_to_cache(alphabet, n, result)
File.write(cache_key(alphabet, n), result.join(":"))
end
private def cache_key(alphabet, n)
Fingers::Dirs::CACHE / "#{alphabet.join("")}-#{n}"
end
private def arity
alphabet.size
end

View File

@ -90,7 +90,11 @@ class Tmux
end
def find_pane_by_id(id) : Pane | Nil
panes.find { |pane| pane.pane_id == id }
output = `#{tmux} display-message -t '#{id}' -F '#{PANE_FORMAT}' -p`.chomp
return nil if output.empty?
Pane.from_json(output)
end
def windows