native_btree 0.1.0.alpha1 → 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 +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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e2df9fa087346ceb88e3352b81707085432b0f2b55cb6315175236bcdfb5167
|
4
|
+
data.tar.gz: 5ce010f70cfafe99d7a89ffe1517330da355bde7a52bc3255711dc56e159b14e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a071792ca329c65f44f04ab4609dce810136fe96d2250d785bdce5d26008adc8c81c03b36a7bc1afd5a7000932ce069af74d368fa4f504afad5fdca5d828efb
|
7
|
+
data.tar.gz: 5790f7446d3390d004101d25c8b7f716c19cdc79cbec64e8f4741667aee281fb8d72ef3b9dca28aef4382d0c52b678a626220cd673ac32e8e8e1f4f3b41cb0b6
|
data/CHANGELOG.md
ADDED
@@ -0,0 +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
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
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
|
24
|
+
|
25
|
+
* to_a()
|
26
|
+
* to_h()
|
27
|
+
* clear()
|
28
|
+
* includes?()
|
data/CMakeLists.txt
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
|
2
|
+
|
3
|
+
project(ruby-native-btree
|
4
|
+
LANGUAGES C
|
5
|
+
VERSION 0.2.0
|
6
|
+
HOMEPAGE_URL https://github.com/unixs/ruby-native-btree)
|
7
|
+
|
8
|
+
include(CheckSymbolExists)
|
9
|
+
|
10
|
+
set(REQUIRED_RUBY_VERSION 2.6.0)
|
11
|
+
|
12
|
+
set(CMAKE_C_STANDARD 11)
|
13
|
+
set(CMAKE_C_EXTENSIONS ON)
|
14
|
+
set(CMAKE_C_STANDARD_REQUIRED ON)
|
15
|
+
|
16
|
+
set(CMAKE_C_FLAGS_DEBUG " -O0 -ggdb3 -Wall -Wextra -Wpedantic -Wno-unused-parameter")
|
17
|
+
set(CMAKE_C_FLAGS_RELEASE "-O3")
|
18
|
+
set(CMAKE_C_FLAGS "-pipe -march=native")
|
19
|
+
|
20
|
+
# Force -fPIC
|
21
|
+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
22
|
+
|
23
|
+
if(NOT CMAKE_BUILD_TYPE)
|
24
|
+
set(CMAKE_BUILD_TYPE "Release")
|
25
|
+
endif()
|
26
|
+
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
27
|
+
|
28
|
+
set(CMAKE_SHARED_LIBRARY_PREFIX "")
|
29
|
+
|
30
|
+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
|
31
|
+
set(CMAKE_SHARED_LIBRARY_SUFFIX ".bundle")
|
32
|
+
set(CMAKE_FIND_FRAMEWORK LAST)
|
33
|
+
message(STATUS "CMAKE_FIND_FRAMEWORK='${CMAKE_FIND_FRAMEWORK}'")
|
34
|
+
endif()
|
35
|
+
|
36
|
+
find_package(PkgConfig REQUIRED)
|
37
|
+
|
38
|
+
# Find Ruby
|
39
|
+
find_package(Ruby ${REQUIRED_RUBY_VERSION} REQUIRED)
|
40
|
+
include_directories(${Ruby_INCLUDE_DIRS})
|
41
|
+
link_libraries(${Ruby_LIBRARIES})
|
42
|
+
message("Ruby_INCLUDE_DIRS: ${Ruby_INCLUDE_DIRS}")
|
43
|
+
|
44
|
+
message(STATUS "Find Ruby deps.")
|
45
|
+
|
46
|
+
# Find jemalloc
|
47
|
+
pkg_check_modules(JEMALLOC jemalloc)
|
48
|
+
if(JEMALLOC_FOUND)
|
49
|
+
include_directories(${JEMALLOC_INCLUDE_DIRS})
|
50
|
+
# link_libraries(${JEMALLOC_LIBRARIES})
|
51
|
+
endif()
|
52
|
+
|
53
|
+
# Attach extension dir
|
54
|
+
add_subdirectory("ext/native_btree")
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in ruby_btree.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem 'awesome_print', '~> 1.9'
|
10
|
+
gem 'debase', '~> 0.2', platforms: [:mri_26, :mri_27]
|
11
|
+
gem 'pry', '~> 0.14'
|
12
|
+
gem 'rubocop', '~> 1.35.0'
|
13
|
+
gem 'rubocop-rake', '~> 0.6.0'
|
14
|
+
gem 'rubocop-rspec', '~> 2.12.1'
|
15
|
+
gem 'rspec', '~> 3.11.0'
|
16
|
+
gem 'ruby-debug-ide', '~> 0.7'
|
17
|
+
gem 'solargraph', '~> 0.46'
|
18
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# GLib Balanced binary tree (GTree) bindings for Ruby
|
2
|
+
|
3
|
+
[](https://github.com/unixs/ruby-native-btree/actions/workflows/main.yml)
|
4
|
+
[](https://badge.fury.io/rb/native_btree)
|
5
|
+
[](https://www.gnu.org/licenses/lgpl-3.0)
|
6
|
+
|
7
|
+
|
8
|
+
[GTree - balanced binary tree from GLib library](https://docs.gtk.org/glib/struct.Tree.html)
|
9
|
+
|
10
|
+
In most cases it will behave same as Hash, but keys will be ordered by passed comparator.
|
11
|
+
|
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
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'native_btree'
|
34
|
+
|
35
|
+
# Pass comparator for keys as block
|
36
|
+
tree = NativeBtree::Btree.new { |a, b| a - b }
|
37
|
+
|
38
|
+
tree[1] = '111'
|
39
|
+
tree[3] = '333'
|
40
|
+
tree[2] = '222'
|
41
|
+
|
42
|
+
tree.each { |k, v| puts "#{k} => #{v}" }
|
43
|
+
|
44
|
+
# 1 => 111
|
45
|
+
# 2 => 222
|
46
|
+
# 3 => 333
|
47
|
+
# nil
|
48
|
+
|
49
|
+
tree.size
|
50
|
+
# 3
|
51
|
+
|
52
|
+
tree.height
|
53
|
+
# 2
|
54
|
+
```
|
55
|
+
|
56
|
+
## API ref
|
57
|
+
|
58
|
+
You must provide your own comparator for keys in `new` class method block.
|
59
|
+
|
60
|
+
### Methods
|
61
|
+
|
62
|
+
* `[]= (alias: set)`
|
63
|
+
* `[] (alias: get)`
|
64
|
+
* `delete`
|
65
|
+
* `size`
|
66
|
+
* `height`
|
67
|
+
* `each`
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "rspec/core/rake_task"
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
rescue LoadError
|
9
|
+
warn "RSpec rake tasks was not loaded"
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require "rubocop/rake_task"
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
rescue LoadError
|
16
|
+
warn "Rubocop rake tasks was not loaded"
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: %i[cmake:build]
|
20
|
+
|
21
|
+
BUILD_DIR = "build"
|
22
|
+
|
23
|
+
namespace :cmake do
|
24
|
+
desc "Remove build directory"
|
25
|
+
task :rmbuild do
|
26
|
+
sh "rm -rf #{BUILD_DIR}"
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Configure ext CMake project"
|
30
|
+
task :configure do
|
31
|
+
sh "cmake -S . -B #{BUILD_DIR}"
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Build ext CMake project"
|
35
|
+
task build: %i[cmake:configure] do
|
36
|
+
sh "cmake --build #{BUILD_DIR}"
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "`Rebuild ext CMake project"
|
40
|
+
task rebuild: %i[cmake:configure] do
|
41
|
+
sh "cmake --build #{BUILD_DIR} --clean-first"
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Clean ext CMake project"
|
45
|
+
task :clean do
|
46
|
+
sh "cmake --build #{BUILD_DIR} --target clean"
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
set(EXT_NAME "native_btree")
|
2
|
+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib/${EXT_NAME})
|
3
|
+
|
4
|
+
message(STATUS "Find ext deps.")
|
5
|
+
|
6
|
+
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
|
7
|
+
|
8
|
+
include_directories(include ${GLIB2_INCLUDE_DIRS})
|
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
|
+
|
18
|
+
add_library(rbtree_type OBJECT rbtree_type.c)
|
19
|
+
add_library(constructor OBJECT constructor.c)
|
20
|
+
add_library(instance OBJECT instance.c)
|
21
|
+
add_library(comparator OBJECT comparator.c)
|
22
|
+
add_library(iterators OBJECT iterators.c)
|
23
|
+
add_library(conversion OBJECT conversion.c)
|
24
|
+
|
25
|
+
add_library(${EXT_NAME}
|
26
|
+
SHARED
|
27
|
+
native_btree.c)
|
28
|
+
|
29
|
+
target_link_libraries(${EXT_NAME}
|
30
|
+
PRIVATE
|
31
|
+
conversion
|
32
|
+
iterators
|
33
|
+
rbtree_type
|
34
|
+
constructor
|
35
|
+
instance
|
36
|
+
comparator
|
37
|
+
${GLIB2_LDFLAGS})
|
38
|
+
#set_target_properties(${EXT_NAME} PROPERTIES BUNDLE TRUE)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#include <comparator.h>
|
2
|
+
|
3
|
+
gint
|
4
|
+
rbtree_native_comparator(gconstpointer a, gconstpointer b, gpointer data)
|
5
|
+
{
|
6
|
+
RBTree *rbtree = (RBTree *) data;
|
7
|
+
|
8
|
+
VALUE result = rb_funcall(rbtree->comparator, rb_intern("call"), 2, (VALUE) a, (VALUE) b);
|
9
|
+
|
10
|
+
gint compare_result = NUM2INT(result);
|
11
|
+
|
12
|
+
return compare_result;
|
13
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#include <constructor.h>
|
2
|
+
#include <comparator.h>
|
3
|
+
#include <rbtree_type.h>
|
4
|
+
|
5
|
+
VALUE
|
6
|
+
rbtree_initialize(VALUE self)
|
7
|
+
{
|
8
|
+
EXTRACT_RBTREE_SELF(rbtree);
|
9
|
+
|
10
|
+
rbtree->gtree = g_tree_new_with_data(
|
11
|
+
rbtree_native_comparator,
|
12
|
+
rbtree
|
13
|
+
);
|
14
|
+
|
15
|
+
rb_need_block();
|
16
|
+
rbtree->comparator = rb_block_proc();
|
17
|
+
|
18
|
+
return self;
|
19
|
+
}
|
@@ -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,15 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
require "rake"
|
3
|
+
|
4
|
+
abort "CMake build tool not found" unless find_executable('cmake')
|
5
|
+
|
6
|
+
File.open("Makefile", "wb") do |f|
|
7
|
+
dummy_makefile(".").each do |line|
|
8
|
+
f.puts(line)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
app = Rake.application
|
13
|
+
app.init
|
14
|
+
app.load_rakefile
|
15
|
+
app['cmake:rebuild'].invoke
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <glib.h>
|
3
|
+
|
4
|
+
#ifndef _NATIVE_BTREE_COMMON_
|
5
|
+
#define _NATIVE_BTREE_COMMON_
|
6
|
+
|
7
|
+
#include <rbtree_type.h>
|
8
|
+
|
9
|
+
#define NATIVE_BTREE_MODULE "NativeBtree"
|
10
|
+
#define NATIVE_BTREE_CLASS "Btree"
|
11
|
+
|
12
|
+
#define EXTRACT_RBTREE(from, to) \
|
13
|
+
RBTree *to; \
|
14
|
+
TypedData_Get_Struct(from, RBTree, &rbtree_type, rbtree)
|
15
|
+
|
16
|
+
#define EXTRACT_RBTREE_SELF(to) EXTRACT_RBTREE(self, to)
|
17
|
+
|
18
|
+
extern VALUE rbtree_class;
|
19
|
+
extern VALUE rbtree_module;
|
20
|
+
|
21
|
+
#endif //_NATIVE_BTREE_COMMON_
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#include <common.h>
|
2
|
+
|
3
|
+
VALUE
|
4
|
+
rbtree_set(VALUE self, VALUE key, VALUE value);
|
5
|
+
|
6
|
+
VALUE
|
7
|
+
rbtree_get(VALUE self, VALUE key);
|
8
|
+
|
9
|
+
VALUE
|
10
|
+
rbtree_delete(VALUE self, VALUE key);
|
11
|
+
|
12
|
+
VALUE
|
13
|
+
rbtree_size(VALUE self);
|
14
|
+
|
15
|
+
VALUE
|
16
|
+
rbtree_height(VALUE self);
|
17
|
+
|
18
|
+
VALUE
|
19
|
+
rbtree_clear(VALUE self);
|
20
|
+
|
21
|
+
VALUE
|
22
|
+
rbtree_is_include(VALUE self, VALUE key);
|
@@ -1,51 +1,9 @@
|
|
1
1
|
#ifndef _NATIVE_BTREE_
|
2
|
-
#include <ruby.h>
|
3
2
|
|
4
|
-
#
|
5
|
-
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
extern VALUE btree_module;
|
10
|
-
|
11
|
-
VALUE
|
12
|
-
btree_new(VALUE klass);
|
13
|
-
|
14
|
-
VALUE
|
15
|
-
btree_init(VALUE self);
|
16
|
-
|
17
|
-
VALUE
|
18
|
-
btree_size(VALUE self);
|
19
|
-
|
20
|
-
VALUE
|
21
|
-
btree_height(VALUE self);
|
22
|
-
|
23
|
-
VALUE
|
24
|
-
btree_set(VALUE self, VALUE key, VALUE value);
|
25
|
-
|
26
|
-
VALUE
|
27
|
-
btree_get(VALUE self, VALUE key);
|
28
|
-
|
29
|
-
VALUE
|
30
|
-
btree_delete(VALUE self, VALUE key);
|
31
|
-
|
32
|
-
VALUE
|
33
|
-
btree_clear(VALUE self);
|
34
|
-
|
35
|
-
VALUE
|
36
|
-
btree_has(VALUE self, VALUE key);
|
37
|
-
|
38
|
-
VALUE
|
39
|
-
btree_each(VALUE self);
|
40
|
-
|
41
|
-
VALUE
|
42
|
-
btree_cmp(VALUE self, VALUE tree2);
|
43
|
-
|
44
|
-
VALUE
|
45
|
-
btree_equal(VALUE self, VALUE tree2);
|
46
|
-
|
47
|
-
#if defined(__cplusplus)
|
48
|
-
}
|
49
|
-
#endif
|
3
|
+
#include <common.h>
|
4
|
+
#include <constructor.h>
|
5
|
+
#include <instance.h>
|
6
|
+
#include <iterators.h>
|
7
|
+
#include <conversion.h>
|
50
8
|
|
51
9
|
#endif // _NATIVE_BTREE_
|