native_btree 0.1.0 → 0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a71b1e34d117e61eb4ef28533f3d4371447b942e81bbfaa23b85af4fc81210ad
4
- data.tar.gz: 45b6b186bb06e9a58d5a26871f8ebb97dce6f3bffeeccb02169ea43dca972f86
3
+ metadata.gz: 6e2df9fa087346ceb88e3352b81707085432b0f2b55cb6315175236bcdfb5167
4
+ data.tar.gz: 5ce010f70cfafe99d7a89ffe1517330da355bde7a52bc3255711dc56e159b14e
5
5
  SHA512:
6
- metadata.gz: 3bea00617a86a99ad538d31825538ff2547748b30e377e55dcfa98134a25777a0cf33466405395c99836e9bba337f2177ec2d033efc7a4cca17fbe615c087d82
7
- data.tar.gz: 1d9ab5a418e9a9ad1e865ff31c1b126b738f2f546b35278fe483df1e357f8fccf427f9234fdc9ce031075d61956b28600af669aa7187d8b2d7f06a6146ff252d
6
+ metadata.gz: 5a071792ca329c65f44f04ab4609dce810136fe96d2250d785bdce5d26008adc8c81c03b36a7bc1afd5a7000932ce069af74d368fa4f504afad5fdca5d828efb
7
+ data.tar.gz: 5790f7446d3390d004101d25c8b7f716c19cdc79cbec64e8f4741667aee281fb8d72ef3b9dca28aef4382d0c52b678a626220cd673ac32e8e8e1f4f3b41cb0b6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
1
8
  ## [Unreleased]
2
9
 
3
- ## [0.1.0.alpha1] - 2022-08-20
10
+ * filter()
11
+ * filter!()
12
+ * each_key()
13
+ * each_value()
14
+ * select()
15
+ * select!
16
+
17
+ ## [0.2.0] - 2022-09-07
18
+
19
+ ### Changed
20
+
21
+ * Small refactoring
22
+
23
+ ### Added
4
24
 
5
- - Initial release
25
+ * to_a()
26
+ * to_h()
27
+ * clear()
28
+ * includes?()
data/CMakeLists.txt CHANGED
@@ -2,9 +2,11 @@ cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
2
2
 
3
3
  project(ruby-native-btree
4
4
  LANGUAGES C
5
- VERSION 0.1.0
5
+ VERSION 0.2.0
6
6
  HOMEPAGE_URL https://github.com/unixs/ruby-native-btree)
7
7
 
8
+ include(CheckSymbolExists)
9
+
8
10
  set(REQUIRED_RUBY_VERSION 2.6.0)
9
11
 
10
12
  set(CMAKE_C_STANDARD 11)
data/README.md CHANGED
@@ -1,12 +1,33 @@
1
1
  # GLib Balanced binary tree (GTree) bindings for Ruby
2
2
 
3
3
  [![Ruby](https://github.com/unixs/ruby-native-btree/actions/workflows/main.yml/badge.svg)](https://github.com/unixs/ruby-native-btree/actions/workflows/main.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/native_btree.svg)](https://badge.fury.io/rb/native_btree)
5
+ [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
6
+
4
7
 
5
8
  [GTree - balanced binary tree from GLib library](https://docs.gtk.org/glib/struct.Tree.html)
6
9
 
7
10
  In most cases it will behave same as Hash, but keys will be ordered by passed comparator.
8
11
 
9
- ## Basic usage:
12
+ ## Requirements
13
+
14
+ * **CMake** build tool version **>= 3.14.0**
15
+ * `pkg-config` tool
16
+ * **GLib** library
17
+ * On Ubuntu run:
18
+ ```bash
19
+ sudo apt-get install pkg-config cmake libglib2.0-dev
20
+ ```
21
+ * On MacOS run:
22
+ ```
23
+ brew install pkg-config cmake glib
24
+ ```
25
+ * On FreeBSD use pkg or ports.
26
+ * On windows use Google. I don't really know how it can work at this moment... But i think it's possible
27
+
28
+ **Check CMake tool version!**
29
+
30
+ ## Basic usage
10
31
 
11
32
  ```ruby
12
33
  require 'native_btree'
@@ -7,11 +7,20 @@ pkg_check_modules(GLIB2 REQUIRED glib-2.0)
7
7
 
8
8
  include_directories(include ${GLIB2_INCLUDE_DIRS})
9
9
 
10
+ list(APPEND CMAKE_REQUIRED_LIBRARIES ${GLIB2_LDFLAGS})
11
+ list(APPEND CMAKE_REQUIRED_INCLUDES ${GLIB2_INCLUDE_DIRS})
12
+ check_symbol_exists(g_tree_remove_all "glib.h" HAVE_GTREE_REMOVE_ALL)
13
+
14
+ if(HAVE_GTREE_REMOVE_ALL)
15
+ add_compile_definitions(HAVE_GTREE_REMOVE_ALL)
16
+ endif()
17
+
10
18
  add_library(rbtree_type OBJECT rbtree_type.c)
11
19
  add_library(constructor OBJECT constructor.c)
12
20
  add_library(instance OBJECT instance.c)
13
21
  add_library(comparator OBJECT comparator.c)
14
22
  add_library(iterators OBJECT iterators.c)
23
+ add_library(conversion OBJECT conversion.c)
15
24
 
16
25
  add_library(${EXT_NAME}
17
26
  SHARED
@@ -19,6 +28,7 @@ add_library(${EXT_NAME}
19
28
 
20
29
  target_link_libraries(${EXT_NAME}
21
30
  PRIVATE
31
+ conversion
22
32
  iterators
23
33
  rbtree_type
24
34
  constructor
@@ -0,0 +1,54 @@
1
+ #include <conversion.h>
2
+
3
+ static gboolean
4
+ rbtree_to_h_callback(gpointer key, gpointer value, gpointer data)
5
+ {
6
+ VALUE hash = (VALUE) data;
7
+
8
+ rb_hash_aset(hash, (VALUE) key, (VALUE) value);
9
+
10
+ return FALSE;
11
+ }
12
+
13
+
14
+ static gboolean
15
+ rbtree_to_a_callback(gpointer key, gpointer value, gpointer data)
16
+ {
17
+ VALUE array = (VALUE) data;
18
+ VALUE array2 = rb_ary_new2(2);
19
+
20
+ rb_ary_push(array2, (VALUE) key);
21
+ rb_ary_push(array2, (VALUE) value);
22
+
23
+ rb_ary_push(array, array2);
24
+
25
+ return FALSE;
26
+ }
27
+
28
+
29
+ VALUE
30
+ rbtree_to_h(VALUE self)
31
+ {
32
+ EXTRACT_RBTREE_SELF(rbtree);
33
+
34
+ VALUE hash = rb_hash_new();
35
+
36
+ g_tree_foreach(rbtree->gtree, rbtree_to_h_callback, (gpointer) hash);
37
+
38
+ return hash;
39
+ }
40
+
41
+
42
+ VALUE
43
+ rbtree_to_a(VALUE self)
44
+ {
45
+ EXTRACT_RBTREE_SELF(rbtree);
46
+
47
+ gint tree_size = g_tree_nnodes(rbtree->gtree);
48
+
49
+ VALUE array = rb_ary_new2(tree_size);
50
+
51
+ g_tree_foreach(rbtree->gtree, rbtree_to_a_callback, (gpointer) array);
52
+
53
+ return array;
54
+ }
@@ -0,0 +1,8 @@
1
+ #include <common.h>
2
+
3
+
4
+ VALUE
5
+ rbtree_to_h(VALUE self);
6
+
7
+ VALUE
8
+ rbtree_to_a(VALUE self);
@@ -14,3 +14,9 @@ rbtree_size(VALUE self);
14
14
 
15
15
  VALUE
16
16
  rbtree_height(VALUE self);
17
+
18
+ VALUE
19
+ rbtree_clear(VALUE self);
20
+
21
+ VALUE
22
+ rbtree_is_include(VALUE self, VALUE key);
@@ -4,43 +4,6 @@
4
4
  #include <constructor.h>
5
5
  #include <instance.h>
6
6
  #include <iterators.h>
7
-
8
- /*
9
- VALUE
10
- btree_new(VALUE klass);
11
-
12
- VALUE
13
- btree_init(VALUE self);
14
-
15
- VALUE
16
- btree_size(VALUE self);
17
-
18
- VALUE
19
- btree_height(VALUE self);
20
-
21
- VALUE
22
- btree_set(VALUE self, VALUE key, VALUE value);
23
-
24
- VALUE
25
- btree_get(VALUE self, VALUE key);
26
-
27
- VALUE
28
- btree_delete(VALUE self, VALUE key);
29
-
30
- VALUE
31
- btree_clear(VALUE self);
32
-
33
- VALUE
34
- btree_has(VALUE self, VALUE key);
35
-
36
- VALUE
37
- btree_each(VALUE self);
38
-
39
- VALUE
40
- btree_cmp(VALUE self, VALUE tree2);
41
-
42
- VALUE
43
- btree_equal(VALUE self, VALUE tree2);
44
- */
7
+ #include <conversion.h>
45
8
 
46
9
  #endif // _NATIVE_BTREE_
@@ -1,5 +1,18 @@
1
1
  #include <instance.h>
2
2
 
3
+ #ifndef HAVE_GTREE_REMOVE_ALL
4
+
5
+ static gboolean
6
+ rbtree_remove_node(gpointer key, gpointer val, gpointer data) {
7
+ RBTree *rbtree = (RBTree *) data;
8
+
9
+ g_tree_remove(rbtree->gtree, key);
10
+
11
+ return FALSE;
12
+ }
13
+
14
+ #endif
15
+
3
16
 
4
17
  VALUE
5
18
  rbtree_set(VALUE self, VALUE key, VALUE value)
@@ -81,3 +94,33 @@ rbtree_size(VALUE self)
81
94
 
82
95
  return result;
83
96
  }
97
+
98
+
99
+ VALUE
100
+ rbtree_clear(VALUE self)
101
+ {
102
+ EXTRACT_RBTREE_SELF(rbtree);
103
+
104
+ #ifdef HAVE_GTREE_REMOVE_ALL
105
+ g_tree_remove_all(rbtree->gtree);
106
+ #else
107
+ g_tree_foreach(rbtree->gtree, rbtree_remove_node, (gpointer) rbtree);
108
+ #endif
109
+
110
+ return self;
111
+ }
112
+
113
+
114
+ VALUE
115
+ rbtree_is_include(VALUE self, VALUE key)
116
+ {
117
+ EXTRACT_RBTREE_SELF(rbtree);
118
+
119
+ gpointer exists = g_tree_lookup(rbtree->gtree, (gconstpointer) key);
120
+
121
+ if (exists == NULL) {
122
+ return Qfalse;
123
+ }
124
+
125
+ return Qtrue;
126
+ }
@@ -15,15 +15,14 @@ Init_native_btree()
15
15
 
16
16
  rb_define_method(rbtree_class, "[]=", rbtree_set, 2);
17
17
  rb_define_alias(rbtree_class, "set", "[]=");
18
-
19
18
  rb_define_method(rbtree_class, "[]", rbtree_get, 1);
20
19
  rb_define_alias(rbtree_class, "get", "[]");
21
-
22
20
  rb_define_method(rbtree_class, "delete", rbtree_delete, 1);
23
-
24
21
  rb_define_method(rbtree_class, "each", rbtree_each, 0);
25
-
26
22
  rb_define_method(rbtree_class, "size", rbtree_size, 0);
27
23
  rb_define_method(rbtree_class, "height", rbtree_height, 0);
24
+ rb_define_method(rbtree_class, "clear", rbtree_clear, 0);
25
+ rb_define_method(rbtree_class, "include?", rbtree_is_include, 1);
26
+ rb_define_method(rbtree_class, "to_h", rbtree_to_h, 0);
27
+ rb_define_method(rbtree_class, "to_a", rbtree_to_a, 0);
28
28
  }
29
-
Binary file
@@ -1,3 +1,3 @@
1
1
  module NativeBtree
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -137,8 +137,22 @@ RSpec.describe NativeBtree do
137
137
  end
138
138
 
139
139
  describe "#clear method" do
140
- xit "respond to" do
141
- expect(described_cless.respond_to?(:clear)).to be true
140
+ it "respond to" do
141
+ expect(tree).to respond_to(:clear)
142
+ end
143
+
144
+ it 'clear tree' do
145
+ tree[1] = 11
146
+ tree[2] = 22
147
+ tree[3] = 33
148
+ expect(tree.size).to be 3
149
+ tree.clear()
150
+ expect(tree.size).to be 0
151
+ end
152
+
153
+ it 'return self' do
154
+ tree[1] = 11
155
+ expect(tree.clear()).to be tree
142
156
  end
143
157
  end
144
158
 
@@ -155,21 +169,51 @@ RSpec.describe NativeBtree do
155
169
  end
156
170
 
157
171
  describe "#include? method" do
158
- xit "respond to" do
159
- expect(described_cless.respond_to?(:include?)).to be true
172
+ it "respond to" do
173
+ expect(tree).to respond_to(:include?)
174
+ end
175
+
176
+ it 'return true is key exists' do
177
+ tree[3] = 33
178
+ expect(tree.include?(3)).to be true
179
+ end
180
+
181
+ it 'return false if key not exists' do
182
+ tree[3] = 33
183
+ expect(tree.include?(4)).to be false
160
184
  end
161
185
  end
162
186
 
163
187
  describe "to_ methods" do
164
188
  describe "#to_a" do
165
- xit "respond to" do
166
- expect(described_cless.respond_to?(:to_a)).to be true
189
+ it "respond to" do
190
+ expect(tree).to respond_to(:to_a)
191
+ end
192
+
193
+ it 'return Array' do
194
+ expect(tree.to_a).to be_kind_of(Array)
195
+ end
196
+
197
+ it 'has similar items' do
198
+ tree[2] = 22
199
+ tree[1] = 11
200
+ expect(tree.to_a()[0][1]).to be 11
167
201
  end
168
202
  end
169
203
 
170
204
  describe "#to_h" do
171
- xit "respond to" do
172
- expect(described_cless.respond_to?(:to_h)).to be true
205
+ it "respond to" do
206
+ expect(tree).to respond_to(:to_h)
207
+ end
208
+
209
+ it "return Hash" do
210
+ expect(tree.to_h).to be_kind_of(Hash)
211
+ end
212
+
213
+ it 'has similar keys' do
214
+ tree[2] = 22
215
+ tree[1] = 11
216
+ expect(tree.to_h()[1]).to be 11
173
217
  end
174
218
  end
175
219
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native_btree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Feodorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby bindings to GTree balanced binary tree from GLib library.
14
14
  email:
@@ -27,11 +27,13 @@ files:
27
27
  - ext/native_btree/CMakeLists.txt
28
28
  - ext/native_btree/comparator.c
29
29
  - ext/native_btree/constructor.c
30
+ - ext/native_btree/conversion.c
30
31
  - ext/native_btree/extconf_cmake.rb
31
32
  - ext/native_btree/include/btree.h
32
33
  - ext/native_btree/include/common.h
33
34
  - ext/native_btree/include/comparator.h
34
35
  - ext/native_btree/include/constructor.h
36
+ - ext/native_btree/include/conversion.h
35
37
  - ext/native_btree/include/instance.h
36
38
  - ext/native_btree/include/iterators.h
37
39
  - ext/native_btree/include/native_btree.h