Skip to content

Commit

Permalink
wip get hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Dec 17, 2024
1 parent caa50a0 commit faedc75
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
7 changes: 3 additions & 4 deletions flx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
flx: Fuzzy matching... a la Sublime Text.
"""

__shortname__ = "flx"
__longname__ = "flx: Fuzzy matching... a la Sublime Text"
__version__ = "0.1.0"
__shortname__ = "flx"
__longname__ = "flx: Fuzzy matching... a la Sublime Text"
__version__ = "0.1.0"
38 changes: 37 additions & 1 deletion flx/flx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from . import util

word_separators = [ ' ', '-', '_', ':', '.', '/', '\\', ]

default_score = -35
Expand Down Expand Up @@ -30,4 +32,38 @@ def boundary(last_ch, ch):

def inc_vec(vec, inc, beg, end):
"""Increment each element in `vec` between `beg` and `end` by `inc`."""
pass
inc = inc or 1
beg = beg or 0
end = end or len(vec)

while beg < end:
vec[beg] += inc
beg += 1

return vec

def get_hash_for_string(str):
"""Return hash-table for string where keys are characters.
Value is a sorted list of indexes for character occurrences.
"""
result = {}

str_len = len(str)
index = str_len - 1

while 0 <= index:
ch = str[index]

if capital(ch):
result = util.dict_insert(result, ch, index)

down_ch = ch.lower()
else:
down_ch = ch

result = util.dict_insert(result, down_ch, index)

index -= 1

return result
24 changes: 24 additions & 0 deletions flx/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def dict_set(dict, key, val):
if key is None:
return

dict[key] = val

return dict

def dict_get(dict, key):
if key is None:
return None

if not key in dict.keys():
return None

return dict[key]

def dict_insert(dict, key, val):
if not key in dict.keys():
dict[key] = []

dict[key].insert(0, val)

return dict
23 changes: 23 additions & 0 deletions test/test_flx.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@ def test_capital():
assert flx.capital('c') == False
assert flx.capital(' ') == False
pass

def test_inc_vec():
assert flx.inc_vec([1, 2, 3], 1, 0, None) == [2, 3, 4]
assert flx.inc_vec([1, 2, 3], 1, 1, None) == [1, 3, 4]
pass

def test_get_hash_for_string():
assert flx.get_hash_for_string("switch-to-buffer") == {
'r': [15],
'e': [14],
'f': [12, 13],
'u': [11],
'b': [10],
'-': [6, 9],
'o': [8],
't': [3, 7],
'h': [5],
'c': [4],
'i': [2],
'w': [1],
's': [0]
}
pass

0 comments on commit faedc75

Please sign in to comment.