native_btree 0.1.0.alpha1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/CMakeLists.txt +54 -0
- data/Gemfile +18 -0
- data/LICENSE +165 -0
- data/README.md +67 -0
- data/Rakefile +48 -0
- data/ext/native_btree/CMakeLists.txt +38 -0
- data/ext/native_btree/comparator.c +13 -0
- data/ext/native_btree/constructor.c +19 -0
- data/ext/native_btree/conversion.c +54 -0
- data/ext/native_btree/extconf_cmake.rb +15 -0
- data/ext/native_btree/include/common.h +21 -0
- data/ext/native_btree/include/comparator.h +4 -0
- data/ext/native_btree/include/constructor.h +11 -0
- data/ext/native_btree/include/conversion.h +8 -0
- data/ext/native_btree/include/instance.h +22 -0
- data/ext/native_btree/include/iterators.h +5 -0
- data/ext/native_btree/include/native_btree.h +5 -47
- data/ext/native_btree/include/rbtree_type.h +22 -0
- data/ext/native_btree/instance.c +126 -0
- data/ext/native_btree/iterators.c +26 -0
- data/ext/native_btree/native_btree.c +20 -22
- data/ext/native_btree/rbtree_type.c +102 -0
- data/lib/native_btree/native_btree.bundle +0 -0
- data/lib/native_btree/native_btree.so +0 -0
- data/lib/native_btree/version.rb +2 -2
- data/lib/native_btree.rb +2 -1
- data/native_btree.gemspec +33 -0
- data/spec/debug.rb +24 -0
- data/spec/native_btree_class_spec.rb +24 -0
- data/spec/native_btree_instance_spec.rb +283 -0
- data/spec/native_btree_module_spec.rb +15 -0
- data/spec/spec_helper.rb +15 -0
- metadata +50 -18
- data/ext/native_btree/btree.cc +0 -128
- data/ext/native_btree/extconf.h +0 -11
- data/ext/native_btree/extconf.rb +0 -35
- data/ext/native_btree/rb_methods.cc +0 -221
@@ -1,221 +0,0 @@
|
|
1
|
-
#include <ruby.h>
|
2
|
-
#include <glib.h>
|
3
|
-
|
4
|
-
#include "native_btree.h"
|
5
|
-
#include "btree.h"
|
6
|
-
|
7
|
-
|
8
|
-
extern "C" {
|
9
|
-
|
10
|
-
class BTreeEQCtxt {
|
11
|
-
public:
|
12
|
-
VALUE eq = Qtrue;
|
13
|
-
BTree *tree2 = nullptr;
|
14
|
-
};
|
15
|
-
|
16
|
-
static void
|
17
|
-
btree_free(BTree *btree)
|
18
|
-
{
|
19
|
-
delete btree;
|
20
|
-
}
|
21
|
-
|
22
|
-
static void
|
23
|
-
btree_mark(gpointer obj)
|
24
|
-
{
|
25
|
-
reinterpret_cast<BTree *>(obj)->mark();
|
26
|
-
}
|
27
|
-
|
28
|
-
static VALUE
|
29
|
-
btree_enum_size(VALUE tree, VALUE args, VALUE eobj)
|
30
|
-
{
|
31
|
-
return btree_size(tree);
|
32
|
-
}
|
33
|
-
|
34
|
-
|
35
|
-
VALUE
|
36
|
-
btree_new(VALUE klass)
|
37
|
-
{
|
38
|
-
rb_need_block();
|
39
|
-
|
40
|
-
VALUE comparator = rb_block_lambda();
|
41
|
-
|
42
|
-
BTree *tree = new BTree(comparator);
|
43
|
-
|
44
|
-
VALUE instance = Data_Wrap_Struct(klass, btree_mark, btree_free, tree);
|
45
|
-
|
46
|
-
return instance;
|
47
|
-
}
|
48
|
-
|
49
|
-
VALUE
|
50
|
-
btree_init(VALUE self)
|
51
|
-
{
|
52
|
-
return self;
|
53
|
-
}
|
54
|
-
|
55
|
-
VALUE
|
56
|
-
btree_size(VALUE self)
|
57
|
-
{
|
58
|
-
BTree *tree;
|
59
|
-
Data_Get_Struct(self, BTree, tree);
|
60
|
-
|
61
|
-
VALUE result = INT2NUM(tree->size());
|
62
|
-
|
63
|
-
return result;
|
64
|
-
}
|
65
|
-
|
66
|
-
VALUE
|
67
|
-
btree_height(VALUE self)
|
68
|
-
{
|
69
|
-
BTree *tree;
|
70
|
-
Data_Get_Struct(self, BTree, tree);
|
71
|
-
|
72
|
-
VALUE result = INT2NUM(tree->height());
|
73
|
-
|
74
|
-
return result;
|
75
|
-
}
|
76
|
-
|
77
|
-
|
78
|
-
VALUE
|
79
|
-
btree_set(VALUE self, VALUE key, VALUE value)
|
80
|
-
{
|
81
|
-
BTree *tree;
|
82
|
-
Data_Get_Struct(self, BTree, tree);
|
83
|
-
|
84
|
-
tree->set(key, value);
|
85
|
-
|
86
|
-
return Qnil;
|
87
|
-
}
|
88
|
-
|
89
|
-
VALUE
|
90
|
-
btree_get(VALUE self, VALUE key)
|
91
|
-
{
|
92
|
-
BTree *tree;
|
93
|
-
Data_Get_Struct(self, BTree, tree);
|
94
|
-
|
95
|
-
return tree->get(key);
|
96
|
-
}
|
97
|
-
|
98
|
-
VALUE
|
99
|
-
btree_delete(VALUE self, VALUE key)
|
100
|
-
{
|
101
|
-
BTree *tree;
|
102
|
-
Data_Get_Struct(self, BTree, tree);
|
103
|
-
|
104
|
-
bool result = tree->del(key);
|
105
|
-
|
106
|
-
return result ? Qtrue : Qfalse;
|
107
|
-
}
|
108
|
-
|
109
|
-
VALUE
|
110
|
-
btree_clear(VALUE self)
|
111
|
-
{
|
112
|
-
BTree *tree;
|
113
|
-
Data_Get_Struct(self, BTree, tree);
|
114
|
-
|
115
|
-
tree->clear();
|
116
|
-
|
117
|
-
return Qnil;
|
118
|
-
}
|
119
|
-
|
120
|
-
VALUE
|
121
|
-
btree_has(VALUE self, VALUE key)
|
122
|
-
{
|
123
|
-
BTree *tree;
|
124
|
-
Data_Get_Struct(self, BTree, tree);
|
125
|
-
|
126
|
-
bool result = tree->has(key);
|
127
|
-
|
128
|
-
return result ? Qtrue : Qfalse;
|
129
|
-
}
|
130
|
-
|
131
|
-
VALUE
|
132
|
-
btree_each(VALUE self)
|
133
|
-
{
|
134
|
-
RETURN_SIZED_ENUMERATOR(self, 0, 0, btree_enum_size);
|
135
|
-
|
136
|
-
VALUE block = rb_block_lambda();
|
137
|
-
|
138
|
-
BTree *tree;
|
139
|
-
Data_Get_Struct(self, BTree, tree);
|
140
|
-
|
141
|
-
tree->each(block);
|
142
|
-
|
143
|
-
return Qnil;
|
144
|
-
}
|
145
|
-
|
146
|
-
// TODO: Need implementation
|
147
|
-
VALUE
|
148
|
-
btree_cmp(VALUE self, VALUE tree2)
|
149
|
-
{
|
150
|
-
return Qnil;
|
151
|
-
}
|
152
|
-
|
153
|
-
static gboolean
|
154
|
-
eql_comparator(gpointer k, gpointer v, gpointer data)
|
155
|
-
{
|
156
|
-
VALUE key = reinterpret_cast<VALUE>(k);
|
157
|
-
VALUE val = reinterpret_cast<VALUE>(v);
|
158
|
-
BTreeEQCtxt *context = reinterpret_cast<BTreeEQCtxt *>(data);
|
159
|
-
|
160
|
-
VALUE val2 = context->tree2->get(key);
|
161
|
-
|
162
|
-
// key not found
|
163
|
-
if (NIL_P(val2)) {
|
164
|
-
context->eq = Qfalse;
|
165
|
-
|
166
|
-
return TRUE;
|
167
|
-
}
|
168
|
-
|
169
|
-
// values !eql
|
170
|
-
if (!rb_eql(val, val2)) {
|
171
|
-
context->eq = Qfalse;
|
172
|
-
|
173
|
-
return TRUE;
|
174
|
-
}
|
175
|
-
|
176
|
-
return FALSE;
|
177
|
-
}
|
178
|
-
|
179
|
-
static VALUE// obj arg recur
|
180
|
-
recursive_eql(VALUE tree, VALUE tree2, int recur)
|
181
|
-
{
|
182
|
-
if (recur)
|
183
|
-
return Qtrue; /* Subtle! */
|
184
|
-
|
185
|
-
BTree *t, *t2;
|
186
|
-
Data_Get_Struct(tree, BTree, t);
|
187
|
-
Data_Get_Struct(tree2, BTree, t2);
|
188
|
-
|
189
|
-
BTreeEQCtxt context;
|
190
|
-
// context.eq == Qtrue;
|
191
|
-
|
192
|
-
context.tree2 = t2;
|
193
|
-
|
194
|
-
t->each(eql_comparator, reinterpret_cast<gpointer>(&context));
|
195
|
-
|
196
|
-
return context.eq;
|
197
|
-
}
|
198
|
-
|
199
|
-
VALUE
|
200
|
-
btree_equal(VALUE self, VALUE tree2)
|
201
|
-
{
|
202
|
-
BTree *t, *t2;
|
203
|
-
Data_Get_Struct(self, BTree, t);
|
204
|
-
Data_Get_Struct(tree2, BTree, t2);
|
205
|
-
|
206
|
-
if (self == tree2)
|
207
|
-
return Qtrue;
|
208
|
-
|
209
|
-
if (CLASS_OF(tree2) != btree_class)
|
210
|
-
return Qfalse;
|
211
|
-
|
212
|
-
if (t->size() != t2->size() || t->height() != t2->height())
|
213
|
-
return Qfalse;
|
214
|
-
|
215
|
-
if (t->size() == 0)
|
216
|
-
return Qtrue;
|
217
|
-
// func obj paired arg
|
218
|
-
return rb_exec_recursive_paired(recursive_eql, self, tree2, tree2);
|
219
|
-
}
|
220
|
-
|
221
|
-
}
|