Skip to content

Instantly share code, notes, and snippets.

@p1scescom
Created November 24, 2020 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p1scescom/a277599cef499e482cdeae470f851760 to your computer and use it in GitHub Desktop.
Save p1scescom/a277599cef499e482cdeae470f851760 to your computer and use it in GitHub Desktop.
This code on MIT License.
function main()
{{_cursor_}}
end
import Base.StringVector
myparse(::Type{String}, str::AbstractString) = str
myparse(T, str::AbstractString) = parse(T, str)
isdelim(x::UInt8, xs::Set{UInt8}) = x in xs
const delimset = Set([0x0a, 0x20])
function myreaduntil(s::IO, delims::Set{UInt8})::Vector{UInt8}
out = StringVector(0)
c::UInt8 = 0x00
while !eof(s)
c = read(s, UInt8)
!isdelim(c, delims) && break
end
push!(out, c)
eof(s) && return out
while !eof(s)
c = read(s, UInt8)
isdelim(c, delims) && break
push!(out, c)
end
return out
end
function readword(io::IO = stdin, delims = delimset)::String
word = myreaduntil(io, delims)
i = length(word)
if i == 0 || word[i] != 0x0a; return String(word)
elseif i < 2 || word[i-1] != 0x0d; return String(resize!(word,i-1))
else; return String(resize!(word,i-2))
end
end
reads(ty::Type) = myparse(ty, readword())
reads(tys...)::Tuple{tys...} = Tuple{tys...}(reads(ty) for ty in tys)
function readvec(tys::Tuple , len::Signed)::Tuple{map(x -> Vector{x},tys)...}
rv = Tuple{map(x -> Vector{x},tys)...}(Vector{ty}(undef,len) for ty in tys)
for i in 1:len, j in 1:length(tys); @inbounds rv[j][i] = reads(tys[j]) end
rv
end
readvec(ty::Type, len::Signed)::Vector{ty} = @inbounds ty[reads(ty) for i in 1:len]
function readmat(ty::Type, s...)::Matrix{ty}
v = Matrix{ty}(undef, s...)
@inbounds for i in 1:s[1]; v[i,:] = readvec(ty, s[2]) end
v
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment