gettc 1.10 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/bin/gettc +60 -69
  3. data/dist/config.yml +1 -1
  4. data/dist/include/cpp/engine.rb +78 -86
  5. data/dist/include/cpp/topcoder +236 -236
  6. data/dist/include/go/engine.rb +53 -61
  7. data/dist/include/haskell/engine.rb +112 -122
  8. data/dist/include/java/engine.rb +187 -184
  9. data/dist/include/javascript/engine.rb +26 -30
  10. data/dist/include/javascript/topcoder.js +3 -3
  11. data/dist/include/javascript/topcoder/errors.js +5 -5
  12. data/dist/include/javascript/topcoder/reader.js +188 -165
  13. data/dist/include/javascript/topcoder/writer.js +37 -33
  14. data/dist/include/python/engine.rb +43 -52
  15. data/dist/include/python/topcoder/__init__.pyc +0 -0
  16. data/dist/include/python/topcoder/__pycache__/__init__.cpython-34.pyc +0 -0
  17. data/dist/include/python/topcoder/__pycache__/errors.cpython-34.pyc +0 -0
  18. data/dist/include/python/topcoder/__pycache__/reader.cpython-34.pyc +0 -0
  19. data/dist/include/python/topcoder/__pycache__/writer.cpython-34.pyc +0 -0
  20. data/dist/include/python/topcoder/errors.pyc +0 -0
  21. data/dist/include/python/topcoder/reader.pyc +0 -0
  22. data/dist/include/python/topcoder/writer.pyc +0 -0
  23. data/dist/include/ruby/engine.rb +47 -52
  24. data/dist/include/ruby/topcoder/reader.rb +205 -193
  25. data/dist/include/ruby/topcoder/writer.rb +39 -37
  26. data/dist/template/bin/runner.rb +146 -151
  27. data/dist/template/bin/runner.sh +96 -96
  28. data/dist/template/prob/{name}.html +1 -1
  29. data/dist/template/solve/cpp/{name}.cpp +3 -4
  30. data/dist/template/solve/cpp/{name}Solver.cpp +14 -14
  31. data/dist/template/solve/go/{name}/{name}.go +3 -3
  32. data/dist/template/solve/go/{name}Solver.go +2 -2
  33. data/dist/template/solve/haskell/{name}.hs +6 -6
  34. data/dist/template/solve/haskell/{name}Solver.hs +10 -10
  35. data/dist/template/solve/java/{name}.java +4 -4
  36. data/dist/template/solve/java/{name}Solver.java +4 -4
  37. data/dist/template/solve/javascript/{name}.js +4 -6
  38. data/dist/template/solve/javascript/{name}Solver.js +11 -9
  39. data/dist/template/solve/python/{name}.py +1 -1
  40. data/dist/template/solve/python/{name}Solver.py +2 -2
  41. data/dist/template/solve/ruby/{name}.rb +4 -4
  42. data/dist/template/solve/ruby/{name}Solver.rb +14 -17
  43. data/dist/template/util/check/check.cpp +19 -19
  44. data/{core/lib → lib}/gettc.rb +0 -0
  45. data/lib/gettc/account.rb +14 -0
  46. data/lib/gettc/download.rb +211 -0
  47. data/lib/gettc/generate.rb +156 -0
  48. data/lib/gettc/parse.rb +237 -0
  49. data/lib/gettc/print.rb +54 -0
  50. data/lib/gettc/problem.rb +39 -0
  51. data/lib/gettc/signature.rb +63 -0
  52. data/lib/gettc/types.rb +93 -0
  53. data/lib/version.rb +3 -0
  54. data/test/gettc/download_test.rb +61 -0
  55. data/test/gettc/generate_test.rb +70 -0
  56. data/test/gettc/parse_test.rb +78 -0
  57. data/test/gettc/signature_test.rb +71 -0
  58. data/test/gettc/types_test.rb +31 -0
  59. metadata +28 -23
  60. data/core/lib/gettc/download.rb +0 -130
  61. data/core/lib/gettc/generate.rb +0 -145
  62. data/core/lib/gettc/parse.rb +0 -233
  63. data/core/lib/gettc/print.rb +0 -56
  64. data/core/lib/gettc/problem.rb +0 -33
  65. data/core/lib/gettc/signature.rb +0 -55
  66. data/core/lib/gettc/types.rb +0 -83
  67. data/core/lib/version.rb +0 -3
  68. data/core/test/gettc/download_test.rb +0 -29
  69. data/core/test/gettc/generate_test.rb +0 -31
  70. data/core/test/gettc/parse_test.rb +0 -104
  71. data/core/test/gettc/signature_test.rb +0 -54
  72. data/core/test/gettc/types_test.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be356b29f63fbb870ee7ffca99f1bcc3d0bcd52e
4
- data.tar.gz: 47c2abcafbe216880d20d01a6d11c0731bda6c66
3
+ metadata.gz: 1b7ea56d04d293fac9836322b3bd90bb3c0e19b3
4
+ data.tar.gz: 2e2b323f24c59f0976c1f73b9a822ccbd7e82a9e
5
5
  SHA512:
6
- metadata.gz: 2e057827784adaa9d9f2e8b55c299f0a3e67aa72945409d5540fd610a38168819c2e960af5f6a3d9c6065c9947e377b2e58fcd98419498cd686ae8bf2b5b2f4f
7
- data.tar.gz: 7c2bcb3dd3f338056b204f2d4f3d0505b949b7f58616a1bb5adbec67c1c21a2ef9e290b090658f6494feb1a04356c555fcce82b6fcea7d38db5e496d1b2d433b
6
+ metadata.gz: ce0bf1a1b452710291a014d2b52fa10af2170c2a6cff2e9d2602ce8affa75df61b08b198377be00102cc5f2c032ca50537e0aa330e92e8263594ef491f22061f
7
+ data.tar.gz: 18c9c44a6b39463d2d797e055614740dc72715ad4556436fee2062aa3588cd56aa10476375c2ed79b3da01c722b7f9c9c63d6c2609115712ed338656310fb56a
data/bin/gettc CHANGED
@@ -1,90 +1,81 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
- require "gettc"
3
+ require "gettc"
4
4
  include Gettc
5
5
 
6
6
  require "rubygems"
7
- require "fileutils"
8
- require "yaml"
9
-
10
- def init
11
- $config_d = ENV.fetch "GETTC_HOME", File.expand_path("~/.gettc")
12
- gem_d = Gem.loaded_specs["gettc"]
13
- if gem_d.nil? then
14
- $root_d = File.join File.dirname(__FILE__), "../"
15
- else
16
- $root_d = gem_d.full_gem_path
17
- end
18
- end
7
+ require "fileutils"
8
+ require "yaml"
19
9
 
20
10
  def replace_config
21
- FileUtils.mkdir $config_d
22
- default_d = File.join $root_d, "dist/."
23
- FileUtils.cp_r default_d, $config_d
11
+ FileUtils.mkdir($config_dir)
12
+ FileUtils.cp_r(File.join($root_dir, "dist/."), $config_dir)
24
13
  end
25
14
 
26
15
  def load_config
27
- replace_config unless Dir.exists? $config_d
28
- config = YAML.load_file File.join($config_d, 'config.yml')
16
+ replace_config unless Dir.exists?($config_dir)
17
+ config = YAML.load_file(File.join($config_dir, "config.yml"))
29
18
 
30
- version = config["version"]
31
- unless version.nil? then
32
- version = Gem::Version.new version
33
- end
19
+ version = config["version"]
20
+ version = Gem::Version.new(version) unless version.nil?
34
21
 
35
- if version.nil? or version < Gem::Version.new(VERSION) then
36
- FileUtils.rm_rf $config_d
37
- replace_config
38
- end
22
+ if version.nil? || version < Gem::Version.new(VERSION)
23
+ FileUtils.rm_rf($config_dir)
24
+ replace_config
25
+ end
39
26
 
40
- account = Account.new config["username"], config["password"]
41
- return account
42
- end
27
+ $account = Account.new(config["username"], config["password"])
28
+ end
29
+
30
+ def init
31
+ $config_dir = ENV.fetch("GETTC_HOME", File.expand_path("~/.gettc"))
32
+ gem_d = Gem.loaded_specs["gettc"]
33
+ $root_dir = gem_d ? gem_d.full_gem_path : File.join(File.dirname(__FILE__), "../")
34
+
35
+ load_config
36
+ $downloader = Downloader.new($account)
37
+ $parser = Parser.new($downloader)
38
+ $generator = Generator.new($config_dir, Dir.getwd)
39
+ end
40
+
41
+ def gettc(id)
42
+ problem = $parser.parse(id)
43
+ puts "#{problem.name} was used for #{problem.source}."
44
+ $generator.generate(problem)
45
+
46
+ puts "All done. Start solving with:"
47
+ puts " $ cd #{problem.name}/solve/<your_language>"
48
+ puts " $ make demo"
49
+ rescue TemplateError => err
50
+ puts err
51
+ end
43
52
 
44
53
  def main
45
- prog = File.basename __FILE__
46
- usage = <<-eos.strip
54
+ prog = File.basename(__FILE__)
55
+ usage = <<-eos.strip
47
56
  gettc <id>: Generate a solution for the TopCoder problem with that ID, eg 11138.
48
57
  gettc version: Print version string.
49
58
  gettc reset: Bring all settings to default state.
50
- eos
51
- if ARGV.empty?
52
- puts usage
53
- return
54
- end
55
- command = ARGV[0]
56
- if command == "version"
57
- puts VERSION
58
- elsif command == "reset"
59
- FileUtils.rm_rf $config_d
60
- replace_config
61
- else
62
- id = ARGV[0].to_i
63
- puts "You have given ID = #{id}"
64
- begin
65
- account = load_config
66
- robot = Downloader.new account
67
- parser = Parser.new robot
68
- generator = Generator.new $config_d, Dir.getwd
69
-
70
- print "Downloading problem to raw HTML ... "
71
- html = robot.download_problem id
72
- puts "Done"
73
-
74
- print "Parsing problem from raw HTML ... "
75
- prob = parser.parse html
76
- puts "Done"
77
-
78
- print "Generating problem directory for #{prob.name} ... "
79
- generator.generate prob
80
- puts "Done"
81
- rescue TemplateError => terr
82
- puts terr
83
- rescue StandardError => err
84
- puts err
85
- exit -1
86
- end
87
- end
59
+ eos
60
+
61
+ if ARGV.empty?
62
+ puts usage
63
+ return
64
+ end
65
+
66
+ command = ARGV[0]
67
+ case command
68
+ when "version"
69
+ puts VERSION
70
+ when "reset"
71
+ FileUtils.rm_rf($config_dir)
72
+ replace_config
73
+ else
74
+ gettc(command.to_i)
75
+ end
76
+ rescue StandardError => err
77
+ puts err
78
+ exit -1
88
79
  end
89
80
 
90
81
  init
@@ -1,3 +1,3 @@
1
1
  username: gettc
2
2
  password: algorithm
3
- version: 1.10
3
+ version: 2.0
@@ -1,97 +1,89 @@
1
1
  require "gettc/types"
2
2
 
3
3
  module Gettc
4
- class Type
5
- def to_cpp
6
- if is_a? TArray then
7
- ret = "vector<" << subtype.to_cpp
8
- ret << " " if subtype.is_a? TArray
9
- ret << ">"
10
- return ret
11
- end
4
+ class Type
5
+ def to_cpp
6
+ if self.is_a?(TArray)
7
+ ret = "vector<" << subtype.to_cpp
8
+ ret << " " if subtype.is_a?(TArray)
9
+ ret << ">"
10
+ return ret
11
+ end
12
12
 
13
- case self
14
- when TInt
15
- return "int"
16
- when TLong
17
- return "int64"
18
- when TFloat
19
- return "float"
20
- when TDouble
21
- return "double"
22
- when TChar
23
- return "char"
24
- when TString
25
- return "string"
26
- when TBoolean
27
- return "bool"
28
- end
13
+ case self
14
+ when TInt
15
+ "int"
16
+ when TLong
17
+ "int64"
18
+ when TFloat
19
+ "float"
20
+ when TDouble
21
+ "double"
22
+ when TChar
23
+ "char"
24
+ when TString
25
+ "string"
26
+ when TBoolean
27
+ "bool"
28
+ else
29
+ "unknown"
30
+ end
31
+ end
32
+
33
+ def dumb_cpp
34
+ return "#{to_cpp}()" if self.is_a?(TArray)
35
+
36
+ case self
37
+ when TInt, TLong, TFloat, TDouble
38
+ "0"
39
+ when TChar
40
+ "'$'"
41
+ when TString
42
+ '"$"'
43
+ when TBoolean
44
+ "true"
45
+ else
46
+ "Nil"
47
+ end
48
+ end
49
+ end
29
50
 
30
- return "unknown"
31
- end
32
- def dumb_cpp
33
- if is_a? TArray then
34
- return "#{to_cpp}()"
35
- end
51
+ class Signature
52
+ def to_cpp(declaring = false)
53
+ ret = type.to_cpp
54
+ ret << " "
55
+ ret << "const &" if declaring && type.obj?
56
+ ret << name
57
+ ret
58
+ end
59
+ end
36
60
 
37
- case self
38
- when TInt, TLong, TFloat, TDouble
39
- return "0"
40
- when TChar
41
- return "'$'"
42
- when TString
43
- return '"$"'
44
- when TBoolean then
45
- return "true"
46
- end
61
+ class CppEngine
62
+ def initialize(func, vars)
63
+ @func = func
64
+ @vars = vars
65
+ end
47
66
 
48
- return "Nil"
49
- end
67
+ def declare
68
+ ret = @func.to_cpp
69
+ ret << "("
70
+ indent = " " * ret.size
71
+ ret << @vars.map { |var| var.to_cpp(true) }.join(",\n#{indent}")
72
+ ret << ")"
50
73
  end
51
- class Signature
52
- def to_cpp declaring = false
53
- ret = type.to_cpp
54
- ret << " "
55
- ret << "const &" if declaring and type.obj?
56
- ret << name
57
- return ret
58
- end
74
+
75
+ def input
76
+ @vars.map do |var|
77
+ x = var.to_cpp
78
+ x << "; tc::read(ifs, "
79
+ x << var.name
80
+ x << ");"
81
+ end.join(" tc::next(ifs);\n")
59
82
  end
60
- class CppEngine
61
- attr_reader :declare, :input, :output
62
- def initialize func, vars
63
- @func = func
64
- @vars = vars
65
- compute_declare
66
- compute_input
67
- compute_output
68
- end
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 << ")"
79
- end
80
- def compute_input
81
- temp = @vars.map do |var|
82
- x = var.to_cpp
83
- x << "; tc::read(ifs, "
84
- x << var.name
85
- x << ");"
86
- end
87
- @input = temp.join " tc::next(ifs);\n"
88
- end
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(", ") << "));"
95
- end
83
+
84
+ def output
85
+ ret = "tc::show(ofs, solver." << @func.name << "("
86
+ ret << @vars.map(&:name).join(", ") << "));"
96
87
  end
88
+ end
97
89
  end
@@ -13,264 +13,264 @@
13
13
  typedef unsigned int uint;
14
14
  typedef long long int64;
15
15
  typedef unsigned long long uint64;
16
-
16
+
17
17
  namespace TopCoder {
18
- const int _CHUNK_SIZE = 65536;
19
-
20
- class ParseException: public std::exception {
21
- private:
22
- std::string message;
23
- void buildMessage(std::string const &required, char actual,
24
- std::string const &rest) {
25
- std::ostringstream oss;
26
- oss << required << " at: <" << rest << ">";
27
- oss << " (got '" << actual << "')";
28
- message = oss.str();
29
- }
30
-
31
- public:
32
- ParseException() : message("ParseException occurred") {}
33
- ParseException(std::string const &message) : message(message) {}
34
- ~ParseException() throw () {}
35
-
36
- virtual const char* what() const throw() {
37
- return message.c_str();
38
- }
39
-
40
- ParseException(std::string const &required, char actual,
41
- std::string const &rest) {
42
- buildMessage(required, actual, rest);
43
- }
44
-
45
- ParseException(char expected, char actual,
46
- std::string const &rest) {
47
- std::ostringstream oss;
48
- oss << "Expect '" << expected << "'";
49
- buildMessage(oss.str(), actual, rest);
50
- }
51
-
52
- ParseException(std::string const &required, std::string const &rest) {
53
- std::ostringstream oss;
54
- oss << required << " at: <" << rest << ">";
55
- message = oss.str();
56
- }
57
-
58
- ParseException(char expected) {
59
- std::ostringstream oss;
60
- oss << "Expect '" << expected << " before end of stream";
61
- message = oss.str();
62
- }
63
- };
64
-
65
- std::string _rest(std::istream &is) {
66
- std::ostringstream oss;
67
- char chunk[_CHUNK_SIZE];
68
- while (is.good()) {
69
- is.get(chunk, _CHUNK_SIZE);
70
- oss << chunk;
71
- }
72
- return oss.str();
73
- }
74
-
75
- void _spaces(std::istream &is) {
76
- while (is.good()) {
77
- int i = is.peek();
78
- if (isspace(i)) is.ignore(1);
79
- else break;
80
- }
81
- }
82
-
83
- void _expect(std::istream &is, char expected) {
84
- _spaces(is); char actual = is.peek();
85
- if (expected != actual)
86
- throw ParseException(expected, actual, _rest(is));
87
- is.ignore(1);
18
+ const int _CHUNK_SIZE = 65536;
19
+
20
+ class ParseException: public std::exception {
21
+ private:
22
+ std::string message;
23
+ void buildMessage(std::string const &required, char actual,
24
+ std::string const &rest) {
25
+ std::ostringstream oss;
26
+ oss << required << " at: <" << rest << ">";
27
+ oss << " (got '" << actual << "')";
28
+ message = oss.str();
88
29
  }
89
30
 
90
- void next(std::istream &is) {
91
- _expect(is, ',');
92
- }
31
+ public:
32
+ ParseException() : message("ParseException occurred") {}
33
+ ParseException(std::string const &message) : message(message) {}
34
+ ~ParseException() throw () {}
93
35
 
94
- void read(std::istream &is, int &i) {
95
- is >> i;
36
+ virtual const char* what() const throw() {
37
+ return message.c_str();
96
38
  }
97
39
 
98
- void read(std::istream &is, int64 &l) {
99
- is >> l;
40
+ ParseException(std::string const &required, char actual,
41
+ std::string const &rest) {
42
+ buildMessage(required, actual, rest);
100
43
  }
101
44
 
102
- void read(std::istream &is, float &f) {
103
- is >> f;
45
+ ParseException(char expected, char actual,
46
+ std::string const &rest) {
47
+ std::ostringstream oss;
48
+ oss << "Expect '" << expected << "'";
49
+ buildMessage(oss.str(), actual, rest);
104
50
  }
105
51
 
106
- void read(std::istream &is, double &d) {
107
- is >> d;
52
+ ParseException(std::string const &required, std::string const &rest) {
53
+ std::ostringstream oss;
54
+ oss << required << " at: <" << rest << ">";
55
+ message = oss.str();
108
56
  }
109
57
 
110
- void read(std::istream &is, char &c) {
111
- _spaces(is); char ch; is.get(ch);
112
- if (ch == '\'') {
113
- is.get(c);
114
- _expect(is, '\'');
115
- } else {
116
- c = ch;
117
- }
118
- }
119
-
120
- void read(std::istream &is, std::string &s) {
121
- _expect(is, '"');
122
- std::ostringstream oss;
123
- while (is.good()) {
124
- char c; is.get(c);
125
- if (c == '"') {
126
- std::streampos pos = is.tellg();
127
- _spaces(is);
128
- if (is.eof()) {
129
- s = oss.str();
130
- return;
131
- }
132
- char cc; is.get(cc);
133
- if (cc == ',' || cc == ']') {
134
- s = oss.str();
135
- is.unget();
136
- return;
137
- }
138
- is.seekg(pos);
139
- }
140
- oss << c;
141
- }
142
- throw ParseException('"');
58
+ ParseException(char expected) {
59
+ std::ostringstream oss;
60
+ oss << "Expect '" << expected << " before end of stream";
61
+ message = oss.str();
143
62
  }
144
-
145
- void read(std::istream &is, bool &b) {
146
- _spaces(is);
147
- std::ostringstream oss;
148
- while (is.good()) {
149
- int i = is.peek();
150
- if (!isalpha(i)) {
151
- break;
152
- }
153
- oss.put(i);
154
- is.ignore(1);
155
- }
156
- std::string s = oss.str();
157
- std::transform(s.begin(), s.end(), s.begin(), ::tolower);
158
- if (s == "true") {
159
- b = true;
160
- } else if (s == "false") {
161
- b = false;
162
- } else {
163
- throw ParseException("Expect a boolean value (true or false)", _rest(is));
164
- }
63
+ };
64
+
65
+ std::string _rest(std::istream &is) {
66
+ std::ostringstream oss;
67
+ char chunk[_CHUNK_SIZE];
68
+ while (is.good()) {
69
+ is.get(chunk, _CHUNK_SIZE);
70
+ oss << chunk;
71
+ }
72
+ return oss.str();
73
+ }
74
+
75
+ void _spaces(std::istream &is) {
76
+ while (is.good()) {
77
+ int i = is.peek();
78
+ if (isspace(i)) is.ignore(1);
79
+ else break;
80
+ }
81
+ }
82
+
83
+ void _expect(std::istream &is, char expected) {
84
+ _spaces(is); char actual = is.peek();
85
+ if (expected != actual)
86
+ throw ParseException(expected, actual, _rest(is));
87
+ is.ignore(1);
88
+ }
89
+
90
+ void next(std::istream &is) {
91
+ _expect(is, ',');
92
+ }
93
+
94
+ void read(std::istream &is, int &i) {
95
+ is >> i;
96
+ }
97
+
98
+ void read(std::istream &is, int64 &l) {
99
+ is >> l;
100
+ }
101
+
102
+ void read(std::istream &is, float &f) {
103
+ is >> f;
104
+ }
105
+
106
+ void read(std::istream &is, double &d) {
107
+ is >> d;
108
+ }
109
+
110
+ void read(std::istream &is, char &c) {
111
+ _spaces(is); char ch; is.get(ch);
112
+ if (ch == '\'') {
113
+ is.get(c);
114
+ _expect(is, '\'');
115
+ } else {
116
+ c = ch;
165
117
  }
166
-
167
- template <typename T>
168
- void read(std::istream &is, std::vector<T> &v) {
169
- v.clear();
170
- _expect(is, '[');
118
+ }
119
+
120
+ void read(std::istream &is, std::string &s) {
121
+ _expect(is, '"');
122
+ std::ostringstream oss;
123
+ while (is.good()) {
124
+ char c; is.get(c);
125
+ if (c == '"') {
126
+ std::streampos pos = is.tellg();
171
127
  _spaces(is);
172
- int i = is.peek();
173
- if (i == ']') {
174
- is.ignore(1);
175
- return;
128
+ if (is.eof()) {
129
+ s = oss.str();
130
+ return;
176
131
  }
177
- while (is.good()) {
178
- T e; read(is, e); v.push_back(e);
179
- _spaces(is); char c; is.get(c);
180
- if (c == ']') {
181
- return;
182
- }
183
- if (c != ',') {
184
- throw ParseException("Expect ',' or ']'", c, _rest(is));
185
- }
186
- }
187
- throw ParseException(']');
188
- }
189
-
190
- void show(std::ostream &os, int i) {
191
- os << i;
192
- }
193
-
194
- void show(std::ostream &os, int64 l) {
195
- os << l;
196
- }
197
-
198
- void show(std::ostream &os, float f) {
199
- os.precision(8);
200
- os << f;
201
- }
202
-
203
- void show(std::ostream &os, double d) {
204
- os.precision(16);
205
- os << d;
206
- }
207
-
208
- void show(std::ostream &os, char c) {
209
- os << '\'' << c << '\'';
210
- }
211
-
212
- void show(std::ostream &os, std::string const &s) {
213
- os << '"' << s << '"';
214
- }
215
-
216
- void show(std::ostream &os, bool b) {
217
- os << (b ? "true" : "false");
218
- }
219
-
220
- template <typename T>
221
- void show(std::ostream &os, std::vector<T> const &v) {
222
- typedef typename std::vector<T>::const_iterator Iterator;
223
- bool first = true;
224
- os << '[';
225
- for (Iterator it = v.begin(); it != v.end(); ++it) {
226
- if (!first) {
227
- os << ',';
228
- }
229
- show(os, *it);
230
- first = false;
132
+ char cc; is.get(cc);
133
+ if (cc == ',' || cc == ']') {
134
+ s = oss.str();
135
+ is.unget();
136
+ return;
231
137
  }
232
- os << ']';
233
- }
234
-
235
- bool same(int i, int j) {
236
- return i == j;
237
- }
238
-
239
- bool same(int64 i, int64 j) {
240
- return i == j;
241
- }
242
-
243
- bool same(char i, char j) {
244
- return i == j;
138
+ is.seekg(pos);
139
+ }
140
+ oss << c;
245
141
  }
246
-
247
- bool same(std::string const &x, std::string const &y) {
248
- return x == y;
142
+ throw ParseException('"');
143
+ }
144
+
145
+ void read(std::istream &is, bool &b) {
146
+ _spaces(is);
147
+ std::ostringstream oss;
148
+ while (is.good()) {
149
+ int i = is.peek();
150
+ if (!isalpha(i)) {
151
+ break;
152
+ }
153
+ oss.put(i);
154
+ is.ignore(1);
155
+ }
156
+ std::string s = oss.str();
157
+ std::transform(s.begin(), s.end(), s.begin(), ::tolower);
158
+ if (s == "true") {
159
+ b = true;
160
+ } else if (s == "false") {
161
+ b = false;
162
+ } else {
163
+ throw ParseException("Expect a boolean value (true or false)", _rest(is));
249
164
  }
250
-
251
- bool same(float x, float y) {
252
- float p = x * (1.0 - 1e-5);
253
- float q = x * (1.0 + 1e-5);
254
- return y >= std::min(p, q) && y <= std::max(p, q);
165
+ }
166
+
167
+ template <typename T>
168
+ void read(std::istream &is, std::vector<T> &v) {
169
+ v.clear();
170
+ _expect(is, '[');
171
+ _spaces(is);
172
+ int i = is.peek();
173
+ if (i == ']') {
174
+ is.ignore(1);
175
+ return;
255
176
  }
256
-
257
- bool same(double x, double y) {
258
- double p = x * (1.0 - 1e-9);
259
- double q = x * (1.0 + 1e-9);
260
- return y >= std::min(p, q) && y <= std::max(p, q);
177
+ while (is.good()) {
178
+ T e; read(is, e); v.push_back(e);
179
+ _spaces(is); char c; is.get(c);
180
+ if (c == ']') {
181
+ return;
182
+ }
183
+ if (c != ',') {
184
+ throw ParseException("Expect ',' or ']'", c, _rest(is));
185
+ }
261
186
  }
262
-
263
- template <typename T>
264
- bool same(std::vector<T> const &v1, std::vector<T> const &v2) {
265
- typedef typename std::vector<T>::const_iterator Iterator;
266
- Iterator i1 = v1.begin(),
267
- i2 = v2.begin();
268
- while (true) {
269
- if (i1 == v1.end()) return i2 == v2.end();
270
- if (i2 == v2.end()) return false;
271
- if (!same(*i1++, *i2++)) return false;
272
- }
187
+ throw ParseException(']');
188
+ }
189
+
190
+ void show(std::ostream &os, int i) {
191
+ os << i;
192
+ }
193
+
194
+ void show(std::ostream &os, int64 l) {
195
+ os << l;
196
+ }
197
+
198
+ void show(std::ostream &os, float f) {
199
+ os.precision(8);
200
+ os << f;
201
+ }
202
+
203
+ void show(std::ostream &os, double d) {
204
+ os.precision(16);
205
+ os << d;
206
+ }
207
+
208
+ void show(std::ostream &os, char c) {
209
+ os << '\'' << c << '\'';
210
+ }
211
+
212
+ void show(std::ostream &os, std::string const &s) {
213
+ os << '"' << s << '"';
214
+ }
215
+
216
+ void show(std::ostream &os, bool b) {
217
+ os << (b ? "true" : "false");
218
+ }
219
+
220
+ template <typename T>
221
+ void show(std::ostream &os, std::vector<T> const &v) {
222
+ typedef typename std::vector<T>::const_iterator Iterator;
223
+ bool first = true;
224
+ os << '[';
225
+ for (Iterator it = v.begin(); it != v.end(); ++it) {
226
+ if (!first) {
227
+ os << ',';
228
+ }
229
+ show(os, *it);
230
+ first = false;
231
+ }
232
+ os << ']';
233
+ }
234
+
235
+ bool same(int i, int j) {
236
+ return i == j;
237
+ }
238
+
239
+ bool same(int64 i, int64 j) {
240
+ return i == j;
241
+ }
242
+
243
+ bool same(char i, char j) {
244
+ return i == j;
245
+ }
246
+
247
+ bool same(std::string const &x, std::string const &y) {
248
+ return x == y;
249
+ }
250
+
251
+ bool same(float x, float y) {
252
+ float p = x * (1.0 - 1e-5);
253
+ float q = x * (1.0 + 1e-5);
254
+ return y >= std::min(p, q) && y <= std::max(p, q);
255
+ }
256
+
257
+ bool same(double x, double y) {
258
+ double p = x * (1.0 - 1e-9);
259
+ double q = x * (1.0 + 1e-9);
260
+ return y >= std::min(p, q) && y <= std::max(p, q);
261
+ }
262
+
263
+ template <typename T>
264
+ bool same(std::vector<T> const &v1, std::vector<T> const &v2) {
265
+ typedef typename std::vector<T>::const_iterator Iterator;
266
+ Iterator i1 = v1.begin(),
267
+ i2 = v2.begin();
268
+ while (true) {
269
+ if (i1 == v1.end()) return i2 == v2.end();
270
+ if (i2 == v2.end()) return false;
271
+ if (!same(*i1++, *i2++)) return false;
273
272
  }
273
+ }
274
274
  }
275
275
 
276
276
  #endif /* GETTC_TOPCODER_H */