s_matrix 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ #ifndef _MATX_TITLE_H_
2
+ #define _MATX_TITLE_H_
3
+
4
+ #include <string>
5
+ #include <vector>
6
+
7
+ class MatxTitle
8
+ {
9
+ public:
10
+ MatxTitle(void);
11
+
12
+ int get_i(void) {return m_titles.size();};
13
+ void set_i(const int i) {m_i = i; m_titles.push_back(std::string("fsfd"));};
14
+
15
+
16
+ void add_title(const std::string& title);
17
+ bool is_title_exist(const std::string& title) const;
18
+ const std::vector<std::string>& get_titles(void) const;
19
+ const std::string& operator[](size_t index) const;
20
+
21
+ private:
22
+ int m_i;
23
+ std::vector<std::string> m_titles;
24
+ };
25
+
26
+
27
+
28
+ #endif
@@ -0,0 +1,8 @@
1
+ #ifndef _GMATX_TYPE_DEF_H_
2
+ #define _GMATX_TYPE_DEF_H_
3
+ #include <map>
4
+ #include <string>
5
+
6
+ typedef std::map<std::string /* key */, std::string /* value */> t_key_value_hash;
7
+
8
+ #endif
@@ -0,0 +1,33 @@
1
+ have_library: checking for main() in -lstdc++... -------------------- yes
2
+
3
+ "gcc -o conftest -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0/x86_64-darwin13.0 -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0/ruby/backward -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0 -I. -I/usr/local/opt/libyaml/include -I/usr/local/opt/readline/include -I/usr/local/opt/libksba/include -I/usr/local/opt/openssl/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -Wdivision-by-zero -Wextra-tokens -fno-common -pipe conftest.c -L. -L/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/openssl/lib -L. -fstack-protector -L/usr/local/lib -lruby.2.1.0 -lpthread -ldl -lobjc "
4
+ checked program was:
5
+ /* begin */
6
+ 1: #include "ruby.h"
7
+ 2:
8
+ 3: int main(int argc, char **argv)
9
+ 4: {
10
+ 5: return 0;
11
+ 6: }
12
+ /* end */
13
+
14
+ "gcc -o conftest -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0/x86_64-darwin13.0 -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0/ruby/backward -I/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/include/ruby-2.1.0 -I. -I/usr/local/opt/libyaml/include -I/usr/local/opt/readline/include -I/usr/local/opt/libksba/include -I/usr/local/opt/openssl/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -Wdivision-by-zero -Wextra-tokens -fno-common -pipe conftest.c -L. -L/Users/zhangyuan/.rvm/rubies/ruby-2.1.0/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/openssl/lib -L. -fstack-protector -L/usr/local/lib -lruby.2.1.0 -lstdc++ -lpthread -ldl -lobjc "
15
+ checked program was:
16
+ /* begin */
17
+ 1: #include "ruby.h"
18
+ 2:
19
+ 3: /*top*/
20
+ 4: extern int t(void);
21
+ 5: int main(int argc, char **argv)
22
+ 6: {
23
+ 7: if (argc > 1000000) {
24
+ 8: printf("%p", &t);
25
+ 9: }
26
+ 10:
27
+ 11: return 0;
28
+ 12: }
29
+ 13: int t(void) { void ((*volatile p)()); p = (void ((*)()))main; return 0; }
30
+ /* end */
31
+
32
+ --------------------
33
+
@@ -0,0 +1,177 @@
1
+ #include "ruby.h"
2
+ #include "ruby/intern.h"
3
+ #include "ruby/st.h"
4
+ #include "ruby/util.h"
5
+
6
+ #include "gmatx.h"
7
+
8
+ static VALUE t_init(VALUE self);
9
+ static VALUE t_init_copy(VALUE self, VALUE orig);
10
+ static void free_matx(class GMatx* pMatx);
11
+ static VALUE t_alloc(VALUE klass);
12
+
13
+
14
+ VALUE sm_init(VALUE self)
15
+ {
16
+ VALUE arr;
17
+
18
+ arr = rb_ary_new();
19
+ rb_iv_set(self, "@arr", arr);
20
+ return self;
21
+ }
22
+
23
+ static VALUE sm_init_copy(VALUE self, VALUE orig)
24
+ {
25
+ if (self == orig)
26
+ return self;
27
+
28
+ if ( (TYPE(orig) != T_DATA) || (RDATA(orig)->dfree != (RUBY_DATA_FUNC)free_matx) )
29
+ rb_raise(rb_eTypeError, "Wrong origin obj when coping!");
30
+
31
+ class GMatx* p_self = NULL;
32
+ Data_Get_Struct(self, class GMatx, p_self);
33
+ if (p_self == NULL) rb_raise(rb_eTypeError, "Encapsulate struct not alloc -- self");
34
+
35
+ class GMatx* p_orig = NULL;
36
+ Data_Get_Struct(orig, class GMatx, p_orig);
37
+ if (p_orig == NULL) rb_raise(rb_eTypeError, "Encapsulate struct not alloc -- origin");
38
+
39
+ *p_self = *p_orig;
40
+ return self;
41
+ }
42
+
43
+ static void free_matx(class GMatx* pMatx)
44
+ {
45
+ delete pMatx;
46
+ }
47
+
48
+ static VALUE sm_alloc(VALUE klass)
49
+ {
50
+ class GMatx* pMatx = new GMatx;
51
+ return Data_Wrap_Struct(klass, 0, free_matx, pMatx);
52
+ }
53
+
54
+ static int t_add_row_hash_iter_func(VALUE key, VALUE value, VALUE p_row_hash)
55
+ {
56
+ t_key_value_hash* p_hash = (t_key_value_hash*)p_row_hash;
57
+
58
+ VALUE key_str = rb_funcall(key, rb_intern("to_s"), 0);
59
+ VALUE value_str = rb_funcall(value, rb_intern("to_s"), 0);
60
+ //printf("key: %s, value: %s, p_hash size %lu\n", StringValueCStr(key_str), StringValueCStr(value_str), p_hash->size());
61
+
62
+ (*p_hash)[StringValueCStr(key_str)] = StringValueCStr(value_str);
63
+ return ST_CONTINUE;
64
+ }
65
+
66
+
67
+ // row: {hp: 23, name: '2332342', 'text' => 'fsdfsd'}
68
+ static VALUE t_add_row(VALUE self, VALUE id, VALUE row)
69
+ {
70
+ Check_Type(row, T_HASH);
71
+
72
+
73
+ // 处理id
74
+ //
75
+ VALUE id_str = rb_funcall(id, rb_intern("to_s"), 0);
76
+ char* p_id_c_str = StringValuePtr(id_str);
77
+ // id 为空字符串
78
+ if (p_id_c_str[0] == 0)
79
+ rb_raise(rb_eTypeError, "id can't be blank!");
80
+
81
+ // 处理row
82
+ //
83
+ t_key_value_hash row_hash;
84
+ rb_hash_foreach(row, (int (*)(ANYARGS))t_add_row_hash_iter_func, (VALUE)&row_hash);
85
+
86
+ // 存数据
87
+ //
88
+ class GMatx* pMatx = NULL;
89
+ Data_Get_Struct(self, class GMatx, pMatx);
90
+ pMatx->add_row(std::string(p_id_c_str), row_hash);
91
+ //printf("\n\n after save a row\n%s\n", pMatx->to_s().c_str());
92
+
93
+ return self;
94
+ }
95
+
96
+ static VALUE row_hash_to_ruby_hash(const t_key_value_hash& row_hash)
97
+ {
98
+ VALUE ret_hash = rb_hash_new();
99
+ for (t_key_value_hash::const_iterator iter = row_hash.begin(); iter != row_hash.end(); ++iter)
100
+ {
101
+ const std::string& key = iter->first;
102
+ const std::string& value = iter->second;
103
+ if (value == MatxRow::null_string)
104
+ rb_hash_aset(ret_hash, rb_str_new_cstr(key.c_str()), Qnil);
105
+ else
106
+ rb_hash_aset(ret_hash, rb_str_new_cstr(key.c_str()), rb_str_new_cstr(value.c_str()));
107
+ }
108
+ return ret_hash;
109
+ }
110
+
111
+ static VALUE t_get_row(VALUE self, VALUE id)
112
+ {
113
+ // 处理id
114
+ //
115
+ VALUE id_str = rb_funcall(id, rb_intern("to_s"), 0);
116
+ char* p_id_c_str = StringValuePtr(id_str);
117
+ // id 为空字符串
118
+ if (p_id_c_str[0] == 0)
119
+ rb_raise(rb_eTypeError, "id can't be blank!");
120
+
121
+
122
+ class GMatx* pMatx = NULL;
123
+ Data_Get_Struct(self, class GMatx, pMatx);
124
+
125
+ t_key_value_hash row_hash = pMatx->get_row(std::string(p_id_c_str));
126
+ return row_hash_to_ruby_hash(row_hash);
127
+ }
128
+
129
+ static void do_each_call_func(const std::string& key, const t_key_value_hash& row_content)
130
+ {
131
+ rb_yield_values(2, rb_str_new_cstr(key.c_str()), row_hash_to_ruby_hash(row_content));
132
+ }
133
+
134
+ static VALUE t_each(VALUE self)
135
+ {
136
+ //raise '' if block do not giving
137
+ //raise '' if count of block params is not 2
138
+ // if (rb_block_arity() != 2)
139
+ // rb_raise(rb_eTypeError, "count of block params must 2");
140
+
141
+ class GMatx* pMatx = NULL;
142
+ Data_Get_Struct(self, class GMatx, pMatx);
143
+ pMatx->each_call(do_each_call_func);
144
+ return self;
145
+ }
146
+
147
+ static VALUE t_to_s(VALUE self)
148
+ {
149
+ class GMatx* pMatx = NULL;
150
+ Data_Get_Struct(self, class GMatx, pMatx);
151
+
152
+ return rb_str_new_cstr(pMatx->to_s().c_str());
153
+ }
154
+
155
+ static VALUE t_size(VALUE self)
156
+ {
157
+ class GMatx* pMatx = NULL;
158
+ Data_Get_Struct(self, class GMatx, pMatx);
159
+
160
+ return INT2NUM(pMatx->size());
161
+ }
162
+
163
+
164
+ VALUE cSMatrix;
165
+
166
+ extern "C" void Init_s_matrix()
167
+ {
168
+ cSMatrix = rb_define_class("SMatrix", rb_cObject);
169
+ rb_define_alloc_func(cSMatrix, sm_alloc);
170
+ rb_define_method(cSMatrix, "initialize", (VALUE(*)(ANYARGS))sm_init, 0);
171
+ rb_define_method(cSMatrix, "initialize_copy", (VALUE(*)(ANYARGS))sm_init_copy, 1);
172
+ rb_define_method(cSMatrix, "size", (VALUE(*)(ANYARGS))t_size, 0);
173
+ rb_define_method(cSMatrix, "to_s", (VALUE(*)(ANYARGS))t_to_s, 0);
174
+ rb_define_method(cSMatrix, "add_row", (VALUE(*)(ANYARGS))t_add_row, 2);
175
+ rb_define_method(cSMatrix, "get_row", (VALUE(*)(ANYARGS))t_get_row, 1);
176
+ rb_define_method(cSMatrix, "each", (VALUE(*)(ANYARGS))t_each, 0);
177
+ }
@@ -0,0 +1,5 @@
1
+ class SMatrix
2
+ VERSION = "0.0.3"
3
+ end
4
+
5
+ require "s_matrix/s_matrix"
data/lib/s_matrix.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "s_matrix/version"
2
+
3
+ require 's_matrix'
4
+
5
+
6
+ class SMatrix
7
+ def self.test
8
+ 3
9
+ end
10
+ end
data/s_matrix.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's_matrix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s_matrix"
8
+ spec.version = SMatrix::VERSION
9
+ spec.authors = ["zhangyuan"]
10
+ spec.email = ["libinzhangyuan@gmail.com"]
11
+ spec.summary = %q{educe memory usage for a large number of config. for example game config in excel.}
12
+ spec.description = %q{not finish}
13
+ spec.homepage = "https://github.com/libinzhangyuan/s_matrix"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.extensions = %w[ext/s_matrix/extconf.rb]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake", "~> 10.1"
24
+
25
+
26
+ spec.add_development_dependency "rspec", "~> 3.1"
27
+ spec.add_development_dependency "rake-compiler", "~> 0.9"
28
+
29
+
30
+ # 临时添加测试用
31
+ spec.add_development_dependency "plist", "~> 3.1.0"
32
+ end