s_matrix 1.0.0 → 1.0.1
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.
- checksums.yaml +4 -4
- data/.gitattributes +1 -0
- data/README.md +11 -2
- data/ext/s_matrix/gmatx.cpp +8 -3
- data/ext/s_matrix/gmatx.h +5 -2
- data/ext/s_matrix/matx_title.h +0 -4
- data/ext/s_matrix/s_matrix.cpp +52 -2
- data/lib/s_matrix/version.rb +1 -1
- data/s_matrix.gemspec +2 -2
- data/spec/smatrix_spec.rb +27 -0
- data/todolist.txt +28 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 873b7c7f5cf89a315b84cd9b373923819962522c
|
4
|
+
data.tar.gz: 9706ed9b6207ddeba5970c09ba027c6bc0739b62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5015507f5e37081199965b0098dc2ee9d0f6e4df0bab055c55d159bbb1127b6e9295e36350586634980d140ab80adcfdc3906a7a8b33aff6f4156bec27518793
|
7
|
+
data.tar.gz: c16ea95270874368b5c53f531f455d1c2abef4e3412c335860a8b3b2e8c754be8187694b3eb20d5778335b55dc59b7e9b10f0533a0ee00a660f420486fa2712e
|
data/.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.rb linguist-language=Java
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Result from test: rspec ./spec/performance\_spec.rb<br>
|
|
18
18
|
|
19
19
|
Add this line to your application's Gemfile:
|
20
20
|
|
21
|
-
gem 's_matrix'
|
21
|
+
gem 's_matrix', '~>1.0'
|
22
22
|
|
23
23
|
And then execute:
|
24
24
|
|
@@ -48,5 +48,14 @@ Or install it yourself as:
|
|
48
48
|
EQUIPMENTS.get\_row('20002')<br>
|
49
49
|
# each
|
50
50
|
EQUIPMENTS.each {|k, v| k == '10001', v == {'id' => '10001', 'name' => 'sword', ...} }
|
51
|
-
#
|
51
|
+
# all
|
52
|
+
EQUIPMENTS.all.should == { '20002' => {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}, '20003' => {} }
|
53
|
+
# ids
|
54
|
+
EQUIPMENTS.ids.should == ['20002', '20003']
|
55
|
+
# keys
|
56
|
+
EQUIPMENTS.keys.should == ['id', 'name', 'hp']
|
57
|
+
# to\_s for debug
|
52
58
|
puts EQUIPMENTS.to_s
|
59
|
+
|
60
|
+
### todolist
|
61
|
+
https://github.com/libinzhangyuan/s\_matrix/blob/master/todolist.txt
|
data/ext/s_matrix/gmatx.cpp
CHANGED
@@ -38,8 +38,13 @@ t_key_value_hash GMatx::get_row(const std::string& id) const
|
|
38
38
|
return row.to_key_value_hash(titles);
|
39
39
|
}
|
40
40
|
|
41
|
-
|
42
|
-
|
41
|
+
const std::vector<std::string>& GMatx::get_titles(void) const
|
42
|
+
{
|
43
|
+
return m_titles.get_titles();
|
44
|
+
}
|
45
|
+
|
46
|
+
// typedef void (*each_call_func)(const std::string& /*key*/, const t_key_value_hash& /*row_content*/, void* args);
|
47
|
+
void GMatx::each_call(each_call_func func, void* args) const
|
43
48
|
{
|
44
49
|
const std::vector<std::string>& titles = m_titles.get_titles();
|
45
50
|
for (std::map<std::string /* id */, MatxRow>::const_iterator iter = m_contents.begin();
|
@@ -47,7 +52,7 @@ void GMatx::each_call(each_call_func func) const
|
|
47
52
|
{
|
48
53
|
const std::string& id = iter->first;
|
49
54
|
const MatxRow& row = iter->second;
|
50
|
-
func(id, row.to_key_value_hash(titles));
|
55
|
+
func(id, row.to_key_value_hash(titles), args);
|
51
56
|
}
|
52
57
|
}
|
53
58
|
|
data/ext/s_matrix/gmatx.h
CHANGED
@@ -15,11 +15,14 @@ public:
|
|
15
15
|
// 当找不到id所对应的行时,返回的t_key_value_hash数据的size=0
|
16
16
|
// size of t_key_value_hash will be zero when couldn't find the row.
|
17
17
|
t_key_value_hash get_row(const std::string& id) const;
|
18
|
+
const std::vector<std::string>& get_titles(void) const;
|
18
19
|
std::string to_s(void) const;
|
19
20
|
size_t size(void) const;
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
// iterator function
|
23
|
+
// the args parameter of each_call function will pass to callback function each_call_func.
|
24
|
+
typedef void (*each_call_func)(const std::string& /*key*/, const t_key_value_hash& /*row_content*/, void* args);
|
25
|
+
void each_call(each_call_func func, void* args) const;
|
23
26
|
|
24
27
|
private:
|
25
28
|
MatxTitle m_titles;
|
data/ext/s_matrix/matx_title.h
CHANGED
@@ -9,10 +9,6 @@ class MatxTitle
|
|
9
9
|
public:
|
10
10
|
MatxTitle(void);
|
11
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
12
|
void add_title(const std::string& title);
|
17
13
|
bool is_title_exist(const std::string& title) const;
|
18
14
|
const std::vector<std::string>& get_titles(void) const;
|
data/ext/s_matrix/s_matrix.cpp
CHANGED
@@ -126,7 +126,7 @@ static VALUE t_get_row(VALUE self, VALUE id)
|
|
126
126
|
return row_hash_to_ruby_hash(row_hash);
|
127
127
|
}
|
128
128
|
|
129
|
-
static void
|
129
|
+
static void callback_func__each(const std::string& key, const t_key_value_hash& row_content, void* args)
|
130
130
|
{
|
131
131
|
rb_yield_values(2, rb_str_new_cstr(key.c_str()), row_hash_to_ruby_hash(row_content));
|
132
132
|
}
|
@@ -140,10 +140,57 @@ static VALUE t_each(VALUE self)
|
|
140
140
|
|
141
141
|
class GMatx* pMatx = NULL;
|
142
142
|
Data_Get_Struct(self, class GMatx, pMatx);
|
143
|
-
pMatx->each_call(
|
143
|
+
pMatx->each_call(callback_func__each, NULL);
|
144
144
|
return self;
|
145
145
|
}
|
146
146
|
|
147
|
+
static void callback_func__all(const std::string& key, const t_key_value_hash& row_content, void* args)
|
148
|
+
{
|
149
|
+
VALUE* p_ret_hash = (VALUE*)(args);
|
150
|
+
rb_hash_aset(*p_ret_hash, rb_str_new_cstr(key.c_str()), row_hash_to_ruby_hash(row_content));
|
151
|
+
}
|
152
|
+
|
153
|
+
static VALUE t_all(VALUE self)
|
154
|
+
{
|
155
|
+
VALUE ret_hash = rb_hash_new();
|
156
|
+
|
157
|
+
class GMatx* pMatx = NULL;
|
158
|
+
Data_Get_Struct(self, class GMatx, pMatx);
|
159
|
+
pMatx->each_call(callback_func__all, &ret_hash);
|
160
|
+
return ret_hash;
|
161
|
+
}
|
162
|
+
|
163
|
+
static void callback_func__ids(const std::string& key, const t_key_value_hash& row_content, void* args)
|
164
|
+
{
|
165
|
+
VALUE* p_ret_array = (VALUE*)(args);
|
166
|
+
rb_ary_push(*p_ret_array, rb_str_new_cstr(key.c_str()));
|
167
|
+
}
|
168
|
+
|
169
|
+
static VALUE t_ids(VALUE self)
|
170
|
+
{
|
171
|
+
VALUE ret_array = rb_ary_new();
|
172
|
+
|
173
|
+
class GMatx* pMatx = NULL;
|
174
|
+
Data_Get_Struct(self, class GMatx, pMatx);
|
175
|
+
pMatx->each_call(callback_func__ids, &ret_array);
|
176
|
+
return ret_array;
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE t_keys(VALUE self)
|
180
|
+
{
|
181
|
+
VALUE ret_array = rb_ary_new();
|
182
|
+
|
183
|
+
class GMatx* pMatx = NULL;
|
184
|
+
Data_Get_Struct(self, class GMatx, pMatx);
|
185
|
+
const std::vector<std::string>& titles = pMatx->get_titles();
|
186
|
+
for (size_t i = 0; i < titles.size(); ++i)
|
187
|
+
{
|
188
|
+
const std::string& title = titles[i];
|
189
|
+
rb_ary_push(ret_array, rb_str_new_cstr(title.c_str()));
|
190
|
+
}
|
191
|
+
return ret_array;
|
192
|
+
}
|
193
|
+
|
147
194
|
static VALUE t_to_s(VALUE self)
|
148
195
|
{
|
149
196
|
class GMatx* pMatx = NULL;
|
@@ -174,4 +221,7 @@ extern "C" void Init_s_matrix()
|
|
174
221
|
rb_define_method(cSMatrix, "add_row", (VALUE(*)(ANYARGS))t_add_row, 2);
|
175
222
|
rb_define_method(cSMatrix, "get_row", (VALUE(*)(ANYARGS))t_get_row, 1);
|
176
223
|
rb_define_method(cSMatrix, "each", (VALUE(*)(ANYARGS))t_each, 0);
|
224
|
+
rb_define_method(cSMatrix, "all", (VALUE(*)(ANYARGS))t_all, 0);
|
225
|
+
rb_define_method(cSMatrix, "ids", (VALUE(*)(ANYARGS))t_ids, 0);
|
226
|
+
rb_define_method(cSMatrix, "keys", (VALUE(*)(ANYARGS))t_keys, 0);
|
177
227
|
}
|
data/lib/s_matrix/version.rb
CHANGED
data/s_matrix.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = SMatrix::VERSION
|
9
9
|
spec.authors = ["zhangyuan"]
|
10
10
|
spec.email = ["libinzhangyuan@gmail.com"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
11
|
+
spec.summary = %q{Reduce memory usage for a large Hash.}
|
12
|
+
spec.description = %q{Matrix store the hash like this: {'234234' => {'id' => '23232', 'name' => 'haha'}, ...}}
|
13
13
|
spec.homepage = "https://github.com/libinzhangyuan/s_matrix"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/spec/smatrix_spec.rb
CHANGED
@@ -96,5 +96,32 @@ describe SMatrix do
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
+
describe 'all && ids && keys' do
|
100
|
+
it '' do
|
101
|
+
a = SMatrix.new
|
102
|
+
expect(a.all).to eq({})
|
103
|
+
expect(a.ids).to eq([])
|
104
|
+
expect(a.keys).to eq([])
|
105
|
+
|
106
|
+
a.add_row('2', {a: 2})
|
107
|
+
expect(a.all).to eq({'2' => {'a' => '2'}})
|
108
|
+
expect(a.ids).to eq(['2'])
|
109
|
+
expect(a.keys).to eq(['a'])
|
110
|
+
|
111
|
+
a.add_row('2', {b: 3})
|
112
|
+
expect(a.all).to eq({'2' => {'a' => nil, 'b' => '3'}})
|
113
|
+
expect(a.ids).to eq(['2'])
|
114
|
+
expect(a.keys).to eq(['a', 'b'])
|
115
|
+
|
116
|
+
a.add_row('3', {b: 3})
|
117
|
+
expect(a.all).to eq({
|
118
|
+
'2' => {'a' => nil, 'b' => '3'},
|
119
|
+
'3' => {'a' => nil, 'b' => '3'}
|
120
|
+
})
|
121
|
+
expect(a.ids).to eq(['2', '3'])
|
122
|
+
expect(a.keys).to eq(['a', 'b'])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
99
126
|
end
|
100
127
|
|
data/todolist.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
todolist:
|
2
|
+
|
3
|
+
1. function with first priority:
|
4
|
+
-done all
|
5
|
+
-return {'23423' => {'name' => 'haha', 'hp' => '23425'}, '234232' => {} }
|
6
|
+
-done ids
|
7
|
+
-return ['233423', '234134']
|
8
|
+
keys
|
9
|
+
-return ['name', 'hp']
|
10
|
+
first
|
11
|
+
empty?
|
12
|
+
include? {|k, v| k == '2323232'}
|
13
|
+
find_by name: 'haha', hp: '2342'
|
14
|
+
select {|k, v| k == '2232'}
|
15
|
+
merge
|
16
|
+
|
17
|
+
|
18
|
+
2. function with normal priority:
|
19
|
+
each_key; each_value; each_pair
|
20
|
+
all? any? one? none? key?
|
21
|
+
collect {|k, v| k} map inject reduce
|
22
|
+
|
23
|
+
3. function with low priority
|
24
|
+
sort sort_by
|
25
|
+
|
26
|
+
4. add delete feature. or not? also merge!
|
27
|
+
|
28
|
+
5. changing equipment_strengthen.plist test to another. this one take 30M disk storage.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s_matrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhangyuan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,7 +80,8 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '3.1'
|
83
|
-
description:
|
83
|
+
description: 'Matrix store the hash like this: {''234234'' => {''id'' => ''23232'',
|
84
|
+
''name'' => ''haha''}, ...}'
|
84
85
|
email:
|
85
86
|
- libinzhangyuan@gmail.com
|
86
87
|
executables: []
|
@@ -88,6 +89,7 @@ extensions:
|
|
88
89
|
- ext/s_matrix/extconf.rb
|
89
90
|
extra_rdoc_files: []
|
90
91
|
files:
|
92
|
+
- ".gitattributes"
|
91
93
|
- ".gitignore"
|
92
94
|
- Gemfile
|
93
95
|
- LICENSE
|
@@ -116,6 +118,7 @@ files:
|
|
116
118
|
- spec/smatrix_spec.rb
|
117
119
|
- spec/spec_helper.rb
|
118
120
|
- spec/xml_load.rb
|
121
|
+
- todolist.txt
|
119
122
|
homepage: https://github.com/libinzhangyuan/s_matrix
|
120
123
|
licenses:
|
121
124
|
- MIT
|
@@ -139,8 +142,7 @@ rubyforge_project:
|
|
139
142
|
rubygems_version: 2.2.2
|
140
143
|
signing_key:
|
141
144
|
specification_version: 4
|
142
|
-
summary:
|
143
|
-
in excel.
|
145
|
+
summary: Reduce memory usage for a large Hash.
|
144
146
|
test_files:
|
145
147
|
- spec/equipment_strengthen.plist
|
146
148
|
- spec/performance_spec.rb
|