Created
February 28, 2015 19:44
-
-
Save ksherlock/857ae33d5b4e9a3699b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
kw = %w{ | |
_Bool _Complex _Noreturn _Imaginary | |
auto asm break case char | |
continue const comp default do | |
double else enum extern extended | |
float for goto if int | |
inline long pascal | |
register restrict return | |
short sizeof static struct switch | |
segment signed typedef union unsigned | |
void volatile while | |
} | |
@T = [].fill(0,256) {|x| x }.sample(256) | |
@Used = [].fill(0,256) {|x| false } | |
def hash(str, target=nil) | |
#raise "Target already used: #{target}" if @Used[target] | |
h = 0 | |
used = {} | |
used.default = 0 | |
stack = [] | |
str.each_byte{|c| | |
h = h ^ c; | |
used[h] = used[h] + 1 | |
stack.push(h) | |
h = @T[h] | |
} | |
puts "#{str}: #{h} : #{target}" | |
puts "Used: ",JSON.generate(stack) | |
# str = "a" = 97, target = 1 | |
# h = T[97] | |
# hh = [97] | |
# update so T[97] = target, T[xxx] = T[97] | |
# best case - h IS the target | |
if h == target || target == nil | |
stack.each{|x| @Used[x] = true} | |
return h | |
end | |
# can't swap elements if it was previously used. | |
last = stack.last | |
raise "oops! #{last}" if used[last] > 1 | |
raise "oops! #{last}" if @Used[last] | |
index = @T.find_index(target) | |
raise "oops! #{index}" if used[index] > 0 | |
raise "oops! #{index}" if @Used[index] | |
# also try updating @T[stack[-2]] to point to index. | |
@T[index], @T[last] = @T[last], @T[index] | |
# update the used list | |
stack.each{|x| @Used[x] = true} | |
return target | |
end | |
target = 0 | |
hhh = {} | |
hhh.default = 0 | |
kw.sort! | |
kw.each{|str| | |
target = target + 1 | |
h = hash(str) | |
hhh[h] = hhh[h] + 1 | |
} | |
puts JSON.generate(@T) | |
puts JSON.generate(hhh) | |
#puts JSON.generate(@Used) | |
exit | |
kw.sort! | |
hash_list = [] | |
kw.each {|str| | |
h = hash(str) | |
hash_list[h] ||= [] | |
hash_list[h].push(str) | |
} | |
puts JSON.generate(hash_list) | |
puts hash_list.select {|a| a }.inject{|a, b| a.length > b.length ? a : b }.length | |
exit | |
# kw.each {|str| | |
# hash = 0 | |
# str.each_byte{|x| hash = hash + x} | |
# printf("%04x %s\n", hash , str) | |
# } | |
prev = "" | |
kw.each {|str| | |
first = str[0] | |
if first != prev | |
puts " end;" unless prev == "" | |
puts " case '#{first}': begin" | |
prev = first | |
end | |
h = hash(str) & 0x3f | |
value = str.downcase.gsub(/^_/, '') | |
value += 'sy' | |
puts " if hash = #{h} then if str = '#{str}' then kw := #{value};" | |
} | |
puts " end;" unless prev == "" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment