Crystalの入力を爆速にする
執筆途中です
getchar_unlocked
hr.icon
↑これから若干修正しています(末尾に改行がない場合壊れた問題を修正)
moduleに変更
code: (cr)
module FastIn
extend self
lib LibC
fun getchar = getchar_unlocked : Char
end
module Scanner
extend self
{% for int_t in Int::Primitive.union_types %}
{% if Int::Signed.union_types.includes?(int_t) %}
{% else %}
{% end %}
c = next_char
res = {{ int_t }}.new(c.ord - '0'.ord)
sgn = 1
case c
when '-'
res = {{ int_t }}.new(LibC.getchar.ord - '0'.ord)
sgn = -1
when '+'
res = {{ int_t }}.new(LibC.getchar.ord - '0'.ord)
end
until ascii_voidchar?(c = LibC.getchar)
res = (res << 1) + (res << 3) + (c.ord ^ 48)
end
res * sgn + offset
end
{% end %}
def read_char : Char
next_char
end
def read_word : String
c = next_char
until ascii_voidchar?(c = LibC.getchar)
s << c
end
s.join
end
private def next_char : Char
c = '_'
while ascii_voidchar?(c = LibC.getchar)
end
c
end
private def ascii_voidchar?(c)
c.ascii_whitespace? || c.ord == -1
end
end
end