Skip to content

Commit

Permalink
improving i/o speed of java templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Harniver committed Feb 3, 2024
1 parent 718afa8 commit 1da6e4a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# make-templates 0.2.004
# make-templates 0.2.005

This is a simple python-based tool generating solution templates (containing code for reading input and writing output), for tasks in either the yaml format for CMS or in the Terry format.

Expand Down
4 changes: 3 additions & 1 deletion make_templates/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import yaml
from os import path, symlink
from copy import deepcopy
from re import fullmatch
from re import fullmatch, sub
from typing import List
from importlib import util
from importlib.resources import files
Expand Down Expand Up @@ -168,6 +168,8 @@ def main(args):
exit(1)
name = task_yaml['name']
body = getattr(targets, t).generate(name, deepcopy(res), args.lang, limits.__dict__)
body = sub('\n +\n', '\n\n', body)
body = sub('\n +\n', '\n\n', body)
if t in ['md', 'tex']:
file = txt_file + '.' + t
if not path.isfile(file):
Expand Down
38 changes: 27 additions & 11 deletions make_templates/targets/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@
// fin = new FileInputStream("input.txt");
// fout = new FileOutputStream("output.txt");
Scanner scn = new Scanner(fin);
PrintStream prnt = new PrintStream(fout);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fout));
reader = new BufferedReader(new InputStreamReader(fin));
scn = new StringTokenizer(reader.readLine());
%s
fout.flush();
writer.flush();
}
static String next() throws IOException {
while (!scn.hasMoreTokens()) scn = new StringTokenizer(reader.readLine());
return scn.nextToken();
}
static BufferedReader reader;
static StringTokenizer scn;
}
"""

Expand Down Expand Up @@ -52,9 +61,12 @@
'string' : '%s'
}

read_dict = {
'string' : '()',
'char' : '().charAt(0)'
type_read = {
'int' : 'Integer.parseInt(%s)',
'long' : 'Long.parseLong(%s)',
'double' : 'Double.parseDouble(%s)',
'char' : '%s.charAt(0)',
'string' : '%s'
}

pending_declarations = {}
Expand Down Expand Up @@ -88,13 +100,15 @@ def build_inout(out:bool, types:List[str], refs:List[VarReference], end:bool):
if len(refs) == 0 and not (out and end):
return ""
if out:
fmt = '"%s%s"' % (" ".join(type_formats[t] if t in type_formats else t for t in types), "\\n" if end else " " if types[0] in type_formats else "")
return "prnt.format(%s);\n" % ", ".join([fmt] + [build_reference(r) for r in refs])
s = "writer.write(' ');\n".join("writer.write(%s);\n" % (("%s" if t == "string" else "String.valueOf(%s)") % build_reference(r)) for t,r in zip(types,refs))
if end is not None:
s += "writer.write('%s');\n" % ('\\n' if end else ' ')
return s
s = ""
for i in range(len(types)):
t = types[i]
r = refs[i]
s += pending_declarations[r.name] + build_reference(r) + " = scn.next" + (read_dict[t] if t in read_dict else t.capitalize()+'()') + ";\n"
s += pending_declarations[r.name] + build_reference(r) + " = " + type_read[t] % "next()" + ";\n"
return s

def build_block(prog:Block, lang:str):
Expand All @@ -121,8 +135,10 @@ def build_block(prog:Block, lang:str):
elif isinstance(c, InOutLine):
s += build_inout(c.out, c.types, c.items, True)
elif isinstance(c, FormatLine):
format = c.format[1:-1].replace('{}', '%d')
s += build_inout(True, [format], [c.var], False)
pre, post = c.format[1:-1].split('{}')
s += "writer.write(\"%s\");\n" % pre
s += build_inout(True, ['int'], [c.var], None)
s += "writer.write(\"%s\");\n" % post
elif isinstance(c, UserCode):
s += "// %s\n" % locale[lang][2]
elif isinstance(c, Instruction):
Expand Down

0 comments on commit 1da6e4a

Please sign in to comment.