Skip to content

Instantly share code, notes, and snippets.

@astroza
Created March 6, 2018 17:35
Show Gist options
  • Save astroza/b02418ab993c54da739d9a1d2c40455a to your computer and use it in GitHub Desktop.
Save astroza/b02418ab993c54da739d9a1d2c40455a to your computer and use it in GitHub Desktop.
Deep hash
def index_path(deep_hash, path)
sp = path.split("/")
d = 0
while d < sp.length
hk = sp.slice(sp.length - d - 1, sp.length)
if deep_hash[d] == nil
deep_hash[d] = {}
end
l = deep_hash[d][hk]
if l == nil
l = []
deep_hash[d][hk] = l
end
l.push(path)
d = d + 1
end
end
def find_file(deep_hash, relative_path)
sp = relative_path.split("/")
d = sp.length - 1
while d >= 0
hk = sp.slice(sp.length - d - 1, sp.length)
if deep_hash[d] != nil and deep_hash[d][hk] != nil
files = deep_hash[d][hk]
files.each do |file|
if file.include? relative_path
return [file, d]
end
end
end
d = d - 1
end
return ["", -1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment