gettc 1.6.2 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bfdcf3cad6f2ab23e59e2d081b56f6d1b2e2c6e5
4
- data.tar.gz: 8d45ab50e760b31e3e0de2ec2ae05b76f1c75c81
3
+ metadata.gz: bbe6b178b2e8a91c7cafef4182730cf4873d177f
4
+ data.tar.gz: 163a10b0052c1d94dc9d5d4ea463984633a744e2
5
5
  SHA512:
6
- metadata.gz: 4dcd3251badaeaba339c3b62593c644083985ec126d27ff464de1e8188cc27dc558b1c4ba18c394c905113d53fd80bfe61d963c3bd8dec870fbf10d86b0edf28
7
- data.tar.gz: c929bbfe92fbf5e1cc03c38132d6e295e9a236e0675ac638f13da89bec994a1c84cf92442a0b046277788b8033ab248264bf22998b45091de53b45ca9a7adce3
6
+ metadata.gz: 684edc1982ebfe09fe0dd271f7d3c916163be15eccf295002e68f78551275d74d1f8d3302748d17b62c269a04e3d964577d3cc5fdd4574b4bf40b288067988d6
7
+ data.tar.gz: 79cfa937b2c0f9170d13f977209fb2de63d9385cde36efca45da0d12048eaae6420ced5798f2752588974053f33b82d3a01407de06493ff3dbb2ab8102ddb319
@@ -1,5 +1,5 @@
1
1
  module Gettc
2
- VERSION = "1.6.2"
2
+ VERSION = "1.7"
3
3
  end
4
4
 
5
5
  require "gettc/download"
@@ -7,139 +7,139 @@ require "gettc/signature"
7
7
  require "gettc/print"
8
8
 
9
9
  module Gettc
10
- GenerateError = Class.new StandardError
11
- class ProblemDirExists < GenerateError
12
- attr_accessor :dir
13
- def initialize dir, msg = nil
14
- if msg.nil? then
15
- msg = "Cannot create problem directory because it already exists"
16
- end
17
- @dir = dir
18
- super "#{msg} (#{dir})"
19
- end
20
- end
21
- class SourceDirNotExist < GenerateError
22
- attr_accessor :dir
23
- def initialize dir, msg = "Source directory does not exist"
24
- @dir = dir
25
- super "#{msg} (#{dir})"
26
- end
27
- end
28
- class TargetDirNotExist < GenerateError
29
- attr_accessor :dir
30
- def initialize dir, msg = "Target directory does not exist"
31
- @dir = dir
32
- super "#{msg} (#{dir})"
33
- end
34
- end
35
- class TemplateError < GenerateError
36
- attr_accessor :dir
37
- def initialize err, source, msg = "Template error"
38
- @err = err
39
- @source = source
40
- super "#{msg} (#{source})\n#{err}"
41
- end
42
- end
43
- class Generator
44
- def initialize config_d, target_d
45
- @source_d = File.join config_d, "template"
46
- @target_d = target_d
47
- raise SourceDirNotExist.new @source_d unless File.directory? @source_d
48
- raise TargetDirNotExist.new @target_d unless File.directory? @target_d
10
+ GenerateError = Class.new StandardError
11
+ class ProblemDirExists < GenerateError
12
+ attr_accessor :dir
13
+ def initialize dir, msg = nil
14
+ if msg.nil? then
15
+ msg = "Cannot create problem directory because it already exists"
16
+ end
17
+ @dir = dir
18
+ super "#{msg} (#{dir})"
19
+ end
20
+ end
21
+ class SourceDirNotExist < GenerateError
22
+ attr_accessor :dir
23
+ def initialize dir, msg = "Source directory does not exist"
24
+ @dir = dir
25
+ super "#{msg} (#{dir})"
26
+ end
27
+ end
28
+ class TargetDirNotExist < GenerateError
29
+ attr_accessor :dir
30
+ def initialize dir, msg = "Target directory does not exist"
31
+ @dir = dir
32
+ super "#{msg} (#{dir})"
33
+ end
34
+ end
35
+ class TemplateError < GenerateError
36
+ attr_accessor :dir
37
+ def initialize err, source, msg = "Template error"
38
+ @err = err
39
+ @source = source
40
+ super "#{msg} (#{source})\n#{err}"
41
+ end
42
+ end
43
+ class Generator
44
+ def initialize config_d, target_d
45
+ @source_d = File.join config_d, "template"
46
+ @target_d = target_d
47
+ raise SourceDirNotExist.new @source_d unless File.directory? @source_d
48
+ raise TargetDirNotExist.new @target_d unless File.directory? @target_d
49
49
 
50
- include_d = File.join config_d, "include"
51
- load_engines include_d
52
- end
53
- def generate prob
54
- @prob = prob
55
- @prob_d = File.join @target_d, prob.name
56
- raise ProblemDirExists.new @prob_d if File.exists? @prob_d
57
- FileUtils.mkdir @prob_d
50
+ include_d = File.join config_d, "include"
51
+ load_engines include_d
52
+ end
53
+ def generate prob
54
+ @prob = prob
55
+ @prob_d = File.join @target_d, prob.name
56
+ raise ProblemDirExists.new @prob_d if File.exists? @prob_d
57
+ FileUtils.mkdir @prob_d
58
58
 
59
- method_sig = @prob.definitions["Method signature"]
60
- if method_sig.nil? then
61
- $stderr.puts "[Warning] No definition for method signature found"
62
- else
63
- vars = parse_method_signature method_sig
64
- func = vars.shift
65
- end
66
- @context = binding
59
+ method_sig = @prob.definitions["Method signature"]
60
+ if method_sig.nil? then
61
+ $stderr.puts "[Warning] No definition for method signature found"
62
+ else
63
+ vars = parse_method_signature method_sig
64
+ func = vars.shift
65
+ end
66
+ @context = binding
67
67
 
68
- walk @source_d, @prob_d
69
- end
70
- private
71
- def gen_images images, images_d
72
- images.each do |image|
73
- filename = File.join images_d, image.name
74
- File.open filename, "wb" do |f| f.write image.content end
75
- end
76
- end
77
- def gen_cases cases, data_d
78
- cases.each_index do |i|
79
- c = cases[i]
80
- File.open File.join(data_d, "#{i.to_s}.in"), "w" do |f|
81
- f.write c.input
82
- end
83
- File.open File.join(data_d, "#{i.to_s}.out"), "w" do |f|
84
- f.write c.output
85
- end
86
- end
87
- end
88
- def gen_template source, target
89
- before = File.open source, "r" do |f| f.read end
90
- begin
91
- after = ERB.new(before).result @context
92
- rescue SyntaxError, NameError => err
93
- raise TemplateError.new err, source
94
- end
95
- File.open target, "w" do |f| f.write after end
96
- end
97
- def filter target_d, name
98
- if name == "{images_d}" then
99
- gen_images @prob.images, target_d
100
- elsif name == "{examples_d}" then
101
- gen_cases @prob.examples, target_d
102
- elsif name == "{systests_d}" then
103
- gen_cases @prob.systests, target_d
104
- else
105
- target_n = name.gsub /\{(\w*)\}/ do |match|
106
- @prob.name if $1 == "name"
107
- end
108
- return target_n
109
- end
110
- return nil
111
- end
112
- def load_engines include_d
113
- return unless File.exists? include_d
114
- Dir.foreach include_d do |name|
115
- child = File.join include_d, name
116
- if File.directory? child then
117
- engine = File.join child, "engine.rb"
118
- if File.exists? engine then
119
- unless (Pathname.new engine).absolute? then
120
- engine = "./" + engine
121
- end
122
- require engine
123
- end
124
- end
125
- end
126
- end
127
- def walk source_d, target_d
128
- Dir.foreach source_d do |name|
129
- if name != "." and name != ".." then
130
- source_p = File.join source_d, name
131
- target_n = filter target_d, name
132
- unless target_n.nil? then
133
- target_p = File.join target_d, target_n
134
- if File.directory? source_p then
135
- FileUtils.mkdir target_p unless File.exists? target_p
136
- walk source_p, target_p
137
- elsif File.file? source_p then
138
- gen_template source_p, target_p
139
- end
140
- end
141
- end
142
- end
143
- end
144
- end
145
- end
68
+ walk @source_d, @prob_d
69
+ end
70
+ private
71
+ def gen_images images, images_d
72
+ images.each do |image|
73
+ filename = File.join images_d, image.name
74
+ File.open filename, "wb" do |f| f.write image.content end
75
+ end
76
+ end
77
+ def gen_cases cases, data_d
78
+ cases.each_index do |i|
79
+ c = cases[i]
80
+ File.open File.join(data_d, "#{i.to_s}.in"), "w" do |f|
81
+ f.write c.input
82
+ end
83
+ File.open File.join(data_d, "#{i.to_s}.out"), "w" do |f|
84
+ f.write c.output
85
+ end
86
+ end
87
+ end
88
+ def gen_template source, target
89
+ before = File.open source, "r" do |f| f.read end
90
+ begin
91
+ after = ERB.new(before).result @context
92
+ rescue SyntaxError, NameError => err
93
+ raise TemplateError.new err, source
94
+ end
95
+ File.open target, "w" do |f| f.write after end
96
+ end
97
+ def filter target_d, name
98
+ if name == "{images_d}" then
99
+ gen_images @prob.images, target_d
100
+ elsif name == "{examples_d}" then
101
+ gen_cases @prob.examples, target_d
102
+ elsif name == "{systests_d}" then
103
+ gen_cases @prob.systests, target_d
104
+ else
105
+ target_n = name.gsub /\{(\w*)\}/ do |match|
106
+ @prob.name if $1 == "name"
107
+ end
108
+ return target_n
109
+ end
110
+ return nil
111
+ end
112
+ def load_engines include_d
113
+ return unless File.exists? include_d
114
+ Dir.foreach include_d do |name|
115
+ child = File.join include_d, name
116
+ if File.directory? child then
117
+ engine = File.join child, "engine.rb"
118
+ if File.exists? engine then
119
+ unless (Pathname.new engine).absolute? then
120
+ engine = "./" + engine
121
+ end
122
+ require engine
123
+ end
124
+ end
125
+ end
126
+ end
127
+ def walk source_d, target_d
128
+ Dir.foreach source_d do |name|
129
+ if name != "." and name != ".." then
130
+ source_p = File.join source_d, name
131
+ target_n = filter target_d, name
132
+ unless target_n.nil? then
133
+ target_p = File.join target_d, target_n
134
+ if File.directory? source_p then
135
+ FileUtils.mkdir target_p unless File.exists? target_p
136
+ walk source_p, target_p
137
+ elsif File.file? source_p then
138
+ gen_template source_p, target_p
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -1,3 +1,3 @@
1
1
  username: gettc
2
2
  password: algorithm
3
- version: 1.6.2
3
+ version: 1.7
@@ -58,33 +58,40 @@ module Gettc
58
58
  end
59
59
  end
60
60
  class CppEngine
61
- attr_accessor :func, :vars
61
+ attr_reader :declare, :input, :output
62
62
  def initialize func, vars
63
63
  @func = func
64
64
  @vars = vars
65
+ compute_declare
66
+ compute_input
67
+ compute_output
65
68
  end
66
- def declare
67
- ret = " " + @func.to_cpp
68
- ret << "("
69
- indent = " " * ret.size
70
- temp = @vars.map do |var| var.to_cpp true end
71
- ret << temp.join(",\n#{indent}")
72
- ret << ")"
73
- return ret
69
+ private
70
+ def compute_declare
71
+ @declare = @func.to_cpp
72
+ @declare << "("
73
+ indent = " " * @declare.size
74
+ temp = @vars.map do |var|
75
+ var.to_cpp true
76
+ end
77
+ @declare << temp.join(",\n#{indent}")
78
+ @declare << ")"
74
79
  end
75
- def input
80
+ def compute_input
76
81
  temp = @vars.map do |var|
77
82
  x = var.to_cpp
78
83
  x << "; tc::read(ifs, "
79
84
  x << var.name
80
85
  x << ");"
81
86
  end
82
- return temp.join " tc::next(ifs);\n"
87
+ @input = temp.join " tc::next(ifs);\n"
83
88
  end
84
- def output
85
- ret = "tc::show(ofs, solver." << @func.name << "("
86
- temp = @vars.map do |var| var.name end
87
- ret << temp.join(", ") << "));"
89
+ def compute_output
90
+ @output = "tc::show(ofs, solver." << @func.name << "("
91
+ temp = @vars.map do |var|
92
+ var.name
93
+ end
94
+ @output << temp.join(", ") << "));"
88
95
  end
89
96
  end
90
97
  end
@@ -0,0 +1,70 @@
1
+ require "gettc/types"
2
+
3
+ module Gettc
4
+ class Type
5
+ def to_go
6
+ if self == TInt then
7
+ return "int"
8
+ elsif self == TLong then
9
+ return "int64"
10
+ elsif self == TFloat then
11
+ return "float32"
12
+ elsif self == TDouble then
13
+ return "float64"
14
+ elsif self == TChar then
15
+ return "byte"
16
+ elsif self == TString then
17
+ return "string"
18
+ elsif self == TBoolean then
19
+ return "bool"
20
+ elsif is_a? TArray then
21
+ return "[]#{subtype.to_go}"
22
+ else
23
+ return "unknown"
24
+ end
25
+ end
26
+ def dumb_go
27
+ if self == TInt then
28
+ return "0"
29
+ elsif self == TLong then
30
+ return "0"
31
+ elsif self == TFloat then
32
+ return "0"
33
+ elsif self == TDouble then
34
+ return "0"
35
+ elsif self == TChar then
36
+ return "'$'"
37
+ elsif self == TString then
38
+ return '"$"'
39
+ elsif self == TBoolean then
40
+ return "true"
41
+ elsif is_a? TArray then
42
+ return self.to_go + " {}"
43
+ else
44
+ return "nil"
45
+ end
46
+ end
47
+ end
48
+ class Signature
49
+ def to_go
50
+ return @name + " " + @type.to_go
51
+ end
52
+ end
53
+ class GoEngine
54
+ attr_reader :declare, :input, :output, :func_name
55
+ def initialize func, vars
56
+ @declare = vars.map do |var|
57
+ var.to_go
58
+ end
59
+ temp = vars.map do |var|
60
+ var.name
61
+ end
62
+ @output = temp.join ", "
63
+ temp = temp.map do |name|
64
+ "&" + name
65
+ end
66
+ @input = temp.join ", "
67
+ @func_name = func.name[0, 1].upcase + func.name[1..-1]
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,41 @@
1
+ package topcoder
2
+
3
+ import (
4
+ "io"
5
+ "io/ioutil"
6
+ "fmt"
7
+ "reflect"
8
+ )
9
+
10
+
11
+ type ErrType struct {
12
+ typ reflect.Type
13
+ }
14
+
15
+ func (me ErrType) Error() string {
16
+ return fmt.Sprintf("Cannot handle type %s", me.typ.String())
17
+ }
18
+
19
+ func NewErrType(typ reflect.Type) *ErrType {
20
+ return &ErrType { typ }
21
+ }
22
+
23
+
24
+ func rest(r io.Reader) string {
25
+ bs, err := ioutil.ReadAll(r)
26
+ if err != nil {
27
+ return err.Error()
28
+ }
29
+ return string(bs)
30
+ }
31
+
32
+
33
+ type ErrExpect string
34
+
35
+ func (me ErrExpect) Error() string {
36
+ return string(me)
37
+ }
38
+
39
+ func NewErrExpect(r io.Reader, what string) ErrExpect {
40
+ return ErrExpect(fmt.Sprintf("Expect %s at <%s>", what, rest(r)))
41
+ }