Created
July 31, 2017 18:10
-
-
Save N64N64/eb0484d27ff22cc993a34c1ee88623dc to your computer and use it in GitHub Desktop.
This file contains 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
local ffi = require 'ffi' | |
local C = ffi.C | |
ffi.cdef[[ | |
typedef void FILE; | |
FILE * fopen ( const char * filename, const char * mode ); | |
char * fgets ( char * str, int num, FILE * stream ); | |
int fclose ( FILE * stream ); | |
]] | |
local f = C.fopen('ngrams.tsv', 'r') | |
local bufsiz = 512 | |
local buf = ffi.new('char[?]', bufsiz) | |
local sum_by_key = ffi.new('int[?]', 2009) | |
ffi.fill(sum_by_key, ffi.sizeof(sum_by_key)) | |
local tab = string.byte('\t') | |
local zero = string.byte('0') | |
local function process(b) | |
local i = 0 | |
local k, v = 0, 0 | |
while b[i] ~= tab do | |
i = i + 1 | |
end | |
i = i + 1 | |
while b[i] ~= tab do | |
k = k*10 + b[i] - zero | |
i = i + 1 | |
end | |
i = i + 1 | |
while b[i] ~= tab do | |
v = v*10 + b[i] - zero | |
i = i + 1 | |
end | |
return k, v | |
end | |
while C.fgets(buf, bufsiz, f) == buf do | |
local k, v = process(buf) | |
sum_by_key[k] = sum_by_key[k] + v | |
end | |
local k, v = -1, -1 | |
for i=0,2009-1 do | |
local val = sum_by_key[i] | |
if val > v then | |
k = i | |
v = val | |
end | |
end | |
C.fclose(f) | |
if k ~= 2006 or v ~= 22569013 then | |
error('bad result '..k..' '..v..' sum_by_key[2006] = '..sum_by_key[2006]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment