kuma_lib 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 614ea5d2ef2b951d666ba80f3ee7477fa3ed8161689bd1bc80adc14534cac4e6
4
+ data.tar.gz: 48bf788636c879868d01f15d46905ed28157aa75b18810e45ff4c7f1f432b632
5
+ SHA512:
6
+ metadata.gz: 562558c90b62b540d4d84bb98024aea7acd320ece7a3b090a296b9be3d74d95515c8b526112887905cb51f32b61332e8c492468198c6fc5a2bfe1fd8e1605276
7
+ data.tar.gz: 880d2215ed0883a905d7cc4c5ed669a81af53369fbe95ba2bf18b9927bb0651906689de526f2c7cea83e194b1a3ec91f03ff974e5ce7d92b645e5851de423acf
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.2
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+
15
+ # rspec failure tracking
16
+ .rspec_status
17
+
18
+ Gemfile.lock
19
+ .vscode
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-03-29
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at koshiro.kumagai.r7@dc.tohoku.ac.jp. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kuma_lib.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rake-compiler"
11
+
12
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Kumagai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # KumaLib
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kuma_lib`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kuma_lib'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kuma_lib
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kuma_lib. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/kuma_lib/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the KumaLib project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kuma_lib/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rake/extensiontask"
9
+
10
+ task build: :compile
11
+
12
+ Rake::ExtensionTask.new("kuma_lib") do |ext|
13
+ ext.ext_dir = "ext/kuma_lib"
14
+ ext.lib_dir = "lib/kuma_lib"
15
+ end
16
+
17
+ Rake::ExtensionTask.new("priority_queue") do |ext|
18
+ ext.ext_dir = "ext/priority_queue"
19
+ ext.lib_dir = "lib/kuma_lib"
20
+ end
21
+
22
+ Rake::ExtensionTask.new("kuma_set") do |ext|
23
+ ext.ext_dir = "ext/kuma_set"
24
+ ext.lib_dir = "lib/kuma_lib"
25
+ end
26
+
27
+ task default: %i[clobber compile spec]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "kuma_lib"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,182 @@
1
+ # KumaSet
2
+
3
+ C++のSetのバインディングです.
4
+ 評価関数で整数値を返す任意のオブジェクトを格納できます.
5
+
6
+ ___
7
+ ## 特異メソッド
8
+ ___
9
+
10
+ **new {|val| ...} -> KumaSet**
11
+ ```ruby
12
+ x = Kumalib::KumaSet.new {|val| val}
13
+ y = Kumalib::KumaSet.new {|val| val.to_i}
14
+ ```
15
+ KumaSetのインスタンスを作成します.
16
+ ブロックによってオブジェクトを評価し,その評価値によってオブジェクトが等しいかどうかを確認します.
17
+ そのため,ブロックは必ず64ビット整数を返すようにしてください.
18
+ ___
19
+ ## インスタンスメソッド
20
+ ___
21
+
22
+ **first -> Object | nil**
23
+
24
+ **min -> Object | nil**
25
+ ```ruby
26
+ x = Kumalib::KumaSet.new {|val| val}
27
+ x << 100 << 0
28
+ x.first # => 0
29
+ ```
30
+ 評価値最小の要素を返します.
31
+ 集合が空の場合は`nil`を返します.
32
+
33
+ **計算量** `O(1)`
34
+ ___
35
+
36
+ **last -> Object | nil**
37
+
38
+ **max -> Object | nil**
39
+ ```ruby
40
+ x = Kumalib::KumaSet.new {|val| val}
41
+ x << 100 << 0
42
+ x.last # => 100
43
+ ```
44
+ 評価値最大の要素を返します.
45
+ 集合が空の場合は`nil`を返します.
46
+
47
+ **計算量** `O(1)`
48
+ ___
49
+
50
+ **each -> Enumerator**
51
+
52
+ **each {|val| ...} -> self**
53
+ ```ruby
54
+ x = Kumalib::KumaSet.new {|val| val}
55
+ x << 100 << 0
56
+ i = 0
57
+ x.each {|val| i += val}
58
+ p i # => 100
59
+ ```
60
+ 各要素に対してブロックを実行します.
61
+ `Enumerable`を`include`しているため,`map`や`to_a`など多くのメソッドがサポートされています.
62
+ ただし次に示すものは再定義しています.
63
+ * `find`
64
+ * `first`
65
+ * `max`
66
+ * `member?`
67
+ * `min`
68
+
69
+ 詳しくは[ルリマ](https://docs.ruby-lang.org/ja/latest/class/Enumerable.html)参照お願いします.
70
+ ___
71
+
72
+ **empty? -> bool**
73
+ ```ruby
74
+ x = Kumalib::KumaSet.new {|val| val}
75
+ x.empty? # => true
76
+ ```
77
+ 集合が空かどうかを返します.
78
+
79
+ **計算量** `O(1)`
80
+ ___
81
+
82
+ **size -> Integer**
83
+ ```ruby
84
+ x = Kumalib::KumaSet.new {|val| val}
85
+ x.size # => 0
86
+ ```
87
+ 集合の大きさを返します.
88
+
89
+ **計算量** `O(1)`
90
+ ___
91
+
92
+ **clear -> nil** (破壊的)
93
+ ```ruby
94
+ x = Kumalib::KumaSet.new {|val| val}
95
+ x.insert(0)
96
+ x.clear
97
+ x.size # => 0
98
+ ```
99
+ 集合を空にします.
100
+
101
+ **計算量** `O(n)` (`n`は集合のサイズ)
102
+ ___
103
+
104
+ **insert(Object) -> bool** (破壊的)
105
+
106
+ **push(Object, ...) -> nil** (破壊的)
107
+
108
+ **<< Object -> self** (破壊的)
109
+ ```ruby
110
+ x = Kumalib::KumaSet.new {|val| val}
111
+ x.insert(0) # => true
112
+ x.insert(0) # => false
113
+ x.push(1, 2, 1, 3) # => nil
114
+ x.size # => 4
115
+ x << 5 << 0 # => x
116
+ x.szie # => 5
117
+ ```
118
+ 集合に要素を追加します.
119
+ `insert`は既に同じ評価値のオブジェクトがあれば`false`を,ない場合は`true`を返します.
120
+
121
+ **計算量** 要素1つ追加するのに`O(log n)` (`n`は集合のサイズ)
122
+ ___
123
+
124
+ **erase(Object) -> bool** (破壊的)
125
+ ```ruby
126
+ x = KumaLib::KumaSet.new {|val| val}
127
+ x.push(*(0..3))
128
+ x.erase(0)
129
+ x.to_a # => [1, 2, 3]
130
+ ```
131
+ 引数と同じ評価値の要素を削除します.
132
+ 削除した場合に`true`を,削除しなかった場合に`false`を返します.
133
+
134
+ **計算量** `O(log n)` (`n`は集合のサイズ)
135
+ ___
136
+
137
+ **include?(Object) -> bool**
138
+
139
+ **member?(Object) -> bool**
140
+ ```ruby
141
+ x = KumaLib::KumaSet.new {|val| val}
142
+ x << 0
143
+ x.include?(0) # => true
144
+ ```
145
+ 引数と同じ評価値のものが集合に含まれているかどうかを返します.
146
+
147
+ **計算量** `O(log n)` (`n`は集合のサイズ)
148
+ ___
149
+
150
+ **find(Object) -> Object | nil**
151
+ ```ruby
152
+ x = KumaLib::KumaSet.new {|val| val % 2}
153
+ x.push(0, 1)
154
+ x.find(3) # => 1
155
+ ```
156
+ 引数と同じ評価値の要素を返します.ない場合は`nil`を返します.
157
+
158
+ **計算量** `O(log n)` (`n`は集合のサイズ)
159
+ ___
160
+
161
+ **lower_bound(Object) -> Object | nil**
162
+ ```ruby
163
+ x = KumaLib::KumaSet.new {|val| val}
164
+ x.push(*(0..100))
165
+ x.lower_find(44) # => 44
166
+ ```
167
+ 引数の評価値以上の評価値最小の要素を返します.
168
+ ない場合は`nil`を返します.
169
+
170
+ **計算量** `O(log n)` (`n`は集合のサイズ)
171
+ ___
172
+
173
+ **upper_bound(Object) -> Object | nil**
174
+ ```ruby
175
+ x = KumaLib::KumaSet.new {|val| val}
176
+ x.push(*(0..100))
177
+ x.upper_bound(44) # => 45
178
+ ```
179
+ 引数の評価値よりも大きい評価値最小の要素を返します.
180
+ ない場合は`nil`を返します.
181
+
182
+ **計算量** `O(log n)` (`n`は集合のサイズ)
@@ -0,0 +1,107 @@
1
+ # Priority_Queue
2
+
3
+ 優先度付きキューです.
4
+
5
+ ___
6
+ ## クラスメソッド
7
+ ___
8
+
9
+ **new {|val| ...} -> Priority_Queue**
10
+ ```ruby
11
+ x = Kumalib::Priority_Queue.new {|val| val}
12
+ y = Kumalib::Priority_Queue.new {|val| val.to_i}
13
+ ```
14
+ Priority_Queueのインスタンスを作成します.
15
+ ブロックによってオブジェクトを評価し,その評価値によってオブジェクト並べます.
16
+ そのため,ブロックは必ず64ビット整数を返すようにしてください.
17
+ ___
18
+ ## インスタンスメソッド
19
+ ___
20
+ **empty? -> bool**
21
+ ```ruby
22
+ x = Kumalib::Priority_Queue.new {|val| val}
23
+ x.empty? # => true
24
+ ```
25
+ キューが空かどうかを返します.
26
+
27
+ **計算量** `O(1)`
28
+ ___
29
+
30
+ **size -> Integer**
31
+ ```ruby
32
+ x = Kumalib::Priority_Queue.new {|val| val}
33
+ x.size # => 0
34
+ ```
35
+ キューのサイズを返します.
36
+
37
+ **計算量** `O(1)`
38
+ ___
39
+
40
+ **top -> Object | nil**
41
+ ```ruby
42
+ x = Kumalib::Priority_Queue.new {|val| val}
43
+ x << 1
44
+ x.top # => 1
45
+ ```
46
+
47
+ キューに含まれる評価値最大のオブジェクトを返します.
48
+ キューが空の場合は`nil`を返します.
49
+ この時キューからの削除は行われません.
50
+
51
+ **計算量** `O(1)`
52
+ ___
53
+
54
+ **<< Object -> self**
55
+
56
+ **push(Object, ...) -> nil**
57
+ ```ruby
58
+ x = Kumalib::Priority_Queue.new {|val| val}
59
+ x << 1 << 2
60
+ x.push(2, 3, 4)
61
+ x.size # => 5
62
+ ```
63
+
64
+ キューにオブジェクトを追加します.
65
+
66
+ **計算量** 1つ追加するのに`O(log n)` (`n`はキューのサイズ)
67
+ ___
68
+
69
+ **pop -> nil**
70
+ ```ruby
71
+ x = Kumalib::Priority_Queue.new {|val| val}
72
+ x.push(1, 2, 3)
73
+ x.pop
74
+ x.size # => 2
75
+ ```
76
+
77
+ キューに含まれる評価値最大のオブジェクトを削除します.
78
+
79
+ **計算量** `O(log n)` (`n`はキューのサイズ)
80
+ ___
81
+
82
+ **shift -> Object | nil**
83
+ ```ruby
84
+ x = Kumalib::Priority_Queue.new {|val| val}
85
+ x.push(1, 2, 3)
86
+ x.shift # => 3
87
+ x.size # => 2
88
+ ```
89
+
90
+ キューに含まれる評価値最大のオブジェクトを削除して返します.
91
+ キューが空の場合は`nil`を返します.
92
+ つまり,`top`と`pop`を同時に行います.
93
+
94
+ **計算量** `O(log n)` (`n`はキューのサイズ)
95
+ ___
96
+
97
+ **clear -> nil**
98
+ ```ruby
99
+ x = Kumalib::Priority_Queue.new {|val| val}
100
+ x.push(*(0...100))
101
+ x.clear
102
+ x.empty? # => true
103
+ ```
104
+
105
+ キューを空にします.
106
+
107
+ **計算量** `O(n)` (`n`はキューのサイズ)
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ $CXXFLAGS += " -std=c++17"
6
+ create_makefile("kuma_lib/kuma_lib")
@@ -0,0 +1,9 @@
1
+ #include "kuma_lib.h"
2
+
3
+ VALUE rb_mKumaLib;
4
+
5
+ void
6
+ Init_kuma_lib(void)
7
+ {
8
+ rb_mKumaLib = rb_define_module("KumaLib");
9
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef KUMA_LIB_H
2
+ #define KUMA_LIB_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* KUMA_LIB_H */
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ $CXXFLAGS += " -std=c++17"
6
+ create_makefile("kuma_set/kuma_set")
@@ -0,0 +1,190 @@
1
+ #include <ruby.h>
2
+
3
+ #include <set>
4
+ #include <functional>
5
+
6
+ using ll = long long int;
7
+ using data_t = std::pair<void *, ll>;
8
+ using fn_t = std::function<bool(const data_t&, const data_t&)>;
9
+ using set_t = std::set<data_t, fn_t>;
10
+
11
+ struct KumaSet : public set_t {
12
+ private:
13
+ VALUE _block;
14
+ inline ll eval(VALUE val) {return NUM2LL(rb_funcall(_block, rb_intern("call"), 1, val));}
15
+ inline data_t make_data(VALUE val) {return {(void *)val, eval(val)};}
16
+
17
+ public:
18
+ KumaSet(fn_t f, VALUE block) : set_t(f), _block(block) {}
19
+ VALUE b() {return _block;}
20
+
21
+ std::pair<KumaSet::iterator, bool> insert(VALUE val) {return set_t::insert(make_data(val));}
22
+ int erase(VALUE val) {return set_t::erase(make_data(val));}
23
+ bool count(VALUE val) {return set_t::count(make_data(val));}
24
+ KumaSet::iterator find(VALUE val) {return set_t::find(make_data(val));}
25
+ KumaSet::iterator lower_bound(VALUE val) {return set_t::lower_bound(make_data(val));}
26
+ KumaSet::iterator upper_bound(VALUE val) {return set_t::upper_bound(make_data(val));}
27
+ };
28
+
29
+ VALUE rb_mKumaLib, rb_cKumaSet;
30
+
31
+ fn_t cmp = [](const data_t& d1, const data_t& d2) {
32
+ return d1.second < d2.second;
33
+ };
34
+
35
+ struct WrapKumaSet {
36
+ KumaSet *ks;
37
+ };
38
+
39
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
40
+
41
+ static void mark_kuma_set(void *ptr) {
42
+ KumaSet *ks = ((WrapKumaSet *)ptr)->ks;
43
+ rb_gc_mark(ks->b());
44
+ for (const data_t& e : *ks) rb_gc_mark(VALUE(e.first));
45
+ }
46
+
47
+ static void free_kuma_set(void *ptr) {
48
+ WrapKumaSet *wks = (WrapKumaSet *)ptr;
49
+ if (wks->ks) delete wks->ks;
50
+ xfree(ptr);
51
+ }
52
+
53
+ static size_t memsize_kuma_set(const void *ptr) {
54
+ return ((WrapKumaSet *)ptr)->ks->size() * sizeof(data_t);
55
+ }
56
+
57
+ static const rb_data_type_t kuma_set_type {
58
+ "KumaSet",
59
+ {mark_kuma_set, free_kuma_set, memsize_kuma_set,},
60
+ 0, 0,
61
+ 0,
62
+ };
63
+
64
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
65
+
66
+ #define GetWrapKumaSet(obj, wks) \
67
+ WrapKumaSet *(wks); \
68
+ TypedData_Get_Struct((obj), WrapKumaSet, &kuma_set_type, (wks))
69
+
70
+ static VALUE alloc_kuma_set(VALUE klass) {
71
+ WrapKumaSet *wks;
72
+ return TypedData_Make_Struct(klass, WrapKumaSet, &kuma_set_type, wks);
73
+ }
74
+
75
+ static VALUE init_kuma_set(VALUE self) {
76
+ GetWrapKumaSet(self, wks);
77
+ VALUE block = rb_block_proc();
78
+ wks->ks = new KumaSet(cmp, block);
79
+ return Qnil;
80
+ }
81
+
82
+ static VALUE first_kuma_set(VALUE self) {
83
+ GetWrapKumaSet(self, wks);
84
+ KumaSet::iterator itr = wks->ks->begin();
85
+ return itr == wks->ks->end() ? Qnil : VALUE(itr->first);
86
+ }
87
+
88
+ static VALUE last_kuma_set(VALUE self) {
89
+ GetWrapKumaSet(self, wks);
90
+ return VALUE(wks->ks->rbegin()->first);
91
+ }
92
+
93
+ static VALUE each_kuma_set(VALUE self) {
94
+ RETURN_ENUMERATOR(self, 0, 0);
95
+ GetWrapKumaSet(self, wks);
96
+ KumaSet *ks = wks->ks;
97
+ for (KumaSet::iterator itr = ks->begin(); itr != ks->end(); itr++) rb_yield(VALUE(itr->first));
98
+ return self;
99
+ }
100
+
101
+ static VALUE empty_kuma_set(VALUE self) {
102
+ GetWrapKumaSet(self, wks);
103
+ return wks->ks->empty() ? Qtrue : Qfalse;
104
+ }
105
+
106
+ static VALUE size_kuma_set(VALUE self) {
107
+ GetWrapKumaSet(self, wks);
108
+ return LL2NUM(wks->ks->size());
109
+ }
110
+
111
+ static VALUE clear_kuma_set(VALUE self) {
112
+ GetWrapKumaSet(self, wks);
113
+ wks->ks->clear();
114
+ return Qnil;
115
+ }
116
+
117
+ static VALUE insert_kuma_set(VALUE self, VALUE val) {
118
+ GetWrapKumaSet(self, wks);
119
+ return wks->ks->insert(val).second ? Qtrue : Qfalse;
120
+ }
121
+
122
+ static VALUE push_kuma_set(VALUE self, VALUE val) {
123
+ GetWrapKumaSet(self, wks);
124
+ wks->ks->insert(val);
125
+ return self;
126
+ }
127
+
128
+ static VALUE push_some_kuma_set(int argc, VALUE *argv, VALUE self) {
129
+ GetWrapKumaSet(self, wks);
130
+ for (int i = 0; i < argc; i++) wks->ks->insert(argv[i]);
131
+ return Qnil;
132
+ }
133
+
134
+ static VALUE erase_kuma_set(VALUE self, VALUE val) {
135
+ GetWrapKumaSet(self, wks);
136
+ return wks->ks->erase(val) ? Qtrue : Qfalse;
137
+ }
138
+
139
+ static VALUE include_kuma_set(VALUE self, VALUE val) {
140
+ GetWrapKumaSet(self, wks);
141
+ return wks->ks->count(val) ? Qtrue : Qfalse;
142
+ }
143
+
144
+ static VALUE find_kuma_set(VALUE self, VALUE val) {
145
+ GetWrapKumaSet(self, wks);
146
+ KumaSet::iterator itr = wks->ks->find(val);
147
+ return itr == wks->ks->end() ? Qnil : VALUE(itr->first);
148
+ }
149
+
150
+ static VALUE lower_bound_kuma_set(VALUE self, VALUE val) {
151
+ GetWrapKumaSet(self, wks);
152
+ KumaSet::iterator itr = wks->ks->lower_bound(val);
153
+ return itr == wks->ks->end() ? Qnil : VALUE(itr->first);
154
+ }
155
+
156
+ static VALUE upper_bound_kuma_set(VALUE self, VALUE val) {
157
+ GetWrapKumaSet(self, wks);
158
+ KumaSet::iterator itr = wks->ks->upper_bound(val);
159
+ return itr == wks->ks->end() ? Qnil : VALUE(itr->first);
160
+ }
161
+
162
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
163
+
164
+ extern "C" void Init_kuma_set(void) {
165
+ rb_mKumaLib = rb_define_module("KumaLib");
166
+ rb_cKumaSet = rb_define_class_under(rb_mKumaLib, "KumaSet", rb_cObject);
167
+ rb_include_module(rb_cKumaSet, rb_mEnumerable);
168
+
169
+ rb_undef_alloc_func(rb_cKumaSet);
170
+ rb_define_alloc_func(rb_cKumaSet, alloc_kuma_set);
171
+
172
+ rb_define_method(rb_cKumaSet, "initialize", init_kuma_set, 0);
173
+ rb_define_method(rb_cKumaSet, "first", first_kuma_set, 0);
174
+ rb_define_alias(rb_cKumaSet, "min", "first");
175
+ rb_define_method(rb_cKumaSet, "last", last_kuma_set, 0);
176
+ rb_define_alias(rb_cKumaSet, "max", "last");
177
+ rb_define_method(rb_cKumaSet, "each", each_kuma_set, 0);
178
+ rb_define_method(rb_cKumaSet, "empty?", empty_kuma_set, 0);
179
+ rb_define_method(rb_cKumaSet, "size", size_kuma_set, 0);
180
+ rb_define_method(rb_cKumaSet, "clear", clear_kuma_set, 0);
181
+ rb_define_method(rb_cKumaSet, "insert", insert_kuma_set, 1);
182
+ rb_define_method(rb_cKumaSet, "<<", push_kuma_set, 1);
183
+ rb_define_method(rb_cKumaSet, "push", push_some_kuma_set, -1);
184
+ rb_define_method(rb_cKumaSet, "erase", erase_kuma_set, 1);
185
+ rb_define_method(rb_cKumaSet, "include?", include_kuma_set, 1);
186
+ rb_define_alias(rb_cKumaSet, "member?", "include?");
187
+ rb_define_method(rb_cKumaSet, "find", find_kuma_set, 1);
188
+ rb_define_method(rb_cKumaSet, "lower_bound", lower_bound_kuma_set, 1);
189
+ rb_define_method(rb_cKumaSet, "upper_bound", upper_bound_kuma_set, 1);
190
+ }
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ $CXXFLAGS += " -std=c++17"
6
+ create_makefile("priority_queue/priority_queue")
@@ -0,0 +1,149 @@
1
+ #include <ruby.h>
2
+
3
+ #include <queue>
4
+ #include <functional>
5
+
6
+ using ll = long long int;
7
+ using data_t = std::pair<void *, ll>;
8
+ using fn_t = std::function<bool(const data_t&, const data_t&)>;
9
+ using pq_t = std::priority_queue<data_t, std::vector<data_t>, fn_t>;
10
+
11
+ struct PrioQueue : public pq_t {
12
+ private:
13
+ VALUE _block;
14
+ inline ll eval(VALUE val) const {return NUM2LL(rb_funcall(_block, rb_intern("call"), 1, val));}
15
+
16
+ public:
17
+ PrioQueue(fn_t f, VALUE block) : pq_t(f), _block(block) {}
18
+
19
+ void mark(void) const {
20
+ rb_gc_mark(_block);
21
+ for (const data_t& e : c) rb_gc_mark(VALUE(e.first));
22
+ }
23
+
24
+ void push(VALUE val) {
25
+ pq_t::push({(void *)val, eval(val)});
26
+ }
27
+
28
+ void clear() {
29
+ c.clear();
30
+ }
31
+ };
32
+
33
+ VALUE rb_mKumaLib, rb_cPrioQueue;
34
+
35
+ fn_t cmp = [](const data_t& d1, const data_t& d2) {
36
+ return d1.second < d2.second;
37
+ };
38
+
39
+ struct WrapPrioQueue {
40
+ PrioQueue *pq;
41
+ };
42
+
43
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
44
+
45
+ static void mark_prio_queue(void *ptr) {
46
+ ((WrapPrioQueue *)ptr)->pq->mark();
47
+ }
48
+
49
+ static void free_prio_queue(void *ptr) {
50
+ WrapPrioQueue *wpq = (WrapPrioQueue *)ptr;
51
+ if (wpq->pq) delete wpq->pq;
52
+ xfree(ptr);
53
+ }
54
+
55
+ static size_t memsize_prio_que(const void *ptr) {
56
+ return ((WrapPrioQueue *)ptr)->pq->size() * sizeof(data_t);
57
+ }
58
+
59
+ static const rb_data_type_t prio_queue_type {
60
+ "PriorityQueue",
61
+ {mark_prio_queue, free_prio_queue, memsize_prio_que,},
62
+ 0, 0,
63
+ 0,
64
+ };
65
+
66
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
67
+
68
+ #define GetWrapPrioQueue(obj, wpq) \
69
+ WrapPrioQueue *(wpq); \
70
+ TypedData_Get_Struct((obj), WrapPrioQueue, &prio_queue_type, (wpq));
71
+
72
+ static VALUE alloc_prio_queue(VALUE klass) {
73
+ WrapPrioQueue *wpq = nullptr;
74
+ return TypedData_Make_Struct(klass, WrapPrioQueue, &prio_queue_type, wpq);
75
+ }
76
+
77
+ static VALUE init_prio_queue(VALUE self) {
78
+ VALUE block = rb_block_proc();
79
+ GetWrapPrioQueue(self, wpq)
80
+ wpq->pq = new PrioQueue(cmp, block);
81
+ return Qnil;
82
+ }
83
+
84
+ static VALUE empty_prio_queue(VALUE self) {
85
+ GetWrapPrioQueue(self, wpq)
86
+ return wpq->pq->empty() ? Qtrue : Qfalse;
87
+ }
88
+
89
+ static VALUE size_prio_queue(VALUE self) {
90
+ GetWrapPrioQueue(self, wpq)
91
+ return LL2NUM(wpq->pq->size());
92
+ }
93
+
94
+ static VALUE top_prio_queue(VALUE self) {
95
+ GetWrapPrioQueue(self, wpq)
96
+ return wpq->pq->empty() ? Qnil : VALUE(wpq->pq->top().first);
97
+ }
98
+
99
+ static VALUE push_prio_queue(VALUE self, VALUE val) {
100
+ GetWrapPrioQueue(self, wpq)
101
+ wpq->pq->push(val);
102
+ return self;
103
+ }
104
+
105
+ static VALUE push_some_prio_que(int argc, VALUE *argv, VALUE self) {
106
+ GetWrapPrioQueue(self, wpq);
107
+ for (int i = 0; i < argc; i++) wpq->pq->push(argv[i]);
108
+ return Qnil;
109
+ }
110
+
111
+ static VALUE pop_prio_queue(VALUE self) {
112
+ GetWrapPrioQueue(self, wpq)
113
+ wpq->pq->pop();
114
+ return Qnil;
115
+ }
116
+
117
+ static VALUE shift_prio_queue(VALUE self) {
118
+ GetWrapPrioQueue(self, wpq)
119
+ if (wpq->pq->empty()) return Qnil;
120
+ VALUE r = VALUE(wpq->pq->top().first);
121
+ wpq->pq->pop();
122
+ return r;
123
+ }
124
+
125
+ static VALUE clear_prio_queue(VALUE self) {
126
+ GetWrapPrioQueue(self, wpq)
127
+ wpq->pq->clear();
128
+ return Qnil;
129
+ }
130
+
131
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
132
+
133
+ extern "C" void Init_priority_queue(void) {
134
+ rb_mKumaLib = rb_define_module("KumaLib");
135
+ rb_cPrioQueue = rb_define_class_under(rb_mKumaLib, "PriorityQueue", rb_cObject);
136
+
137
+ rb_undef_alloc_func(rb_cPrioQueue);
138
+ rb_define_alloc_func(rb_cPrioQueue, alloc_prio_queue);
139
+
140
+ rb_define_method(rb_cPrioQueue, "initialize", init_prio_queue, 0);
141
+ rb_define_method(rb_cPrioQueue, "empty?", empty_prio_queue, 0);
142
+ rb_define_method(rb_cPrioQueue, "size", size_prio_queue, 0);
143
+ rb_define_method(rb_cPrioQueue, "top", top_prio_queue, 0);
144
+ rb_define_method(rb_cPrioQueue, "<<", push_prio_queue, 1);
145
+ rb_define_method(rb_cPrioQueue, "push", push_some_prio_que, -1);
146
+ rb_define_method(rb_cPrioQueue, "pop", pop_prio_queue, 0);
147
+ rb_define_method(rb_cPrioQueue, "shift", shift_prio_queue, 0);
148
+ rb_define_method(rb_cPrioQueue, "clear", clear_prio_queue, 0);
149
+ }
data/kuma_lib.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kuma_lib/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kuma_lib"
7
+ spec.version = KumaLib::VERSION
8
+ spec.authors = ["kumakumarb"]
9
+ spec.email = ["rinngo0119@gmail.com"]
10
+
11
+ spec.summary = "atcoder library."
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ spec.homepage = "https://github.com/kumakumarb/kuma_lib.git"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/kumakumarb/kuma_lib.git"
21
+ spec.metadata["changelog_uri"] = "https://github.com/kumakumarb/kuma_lib.git"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ spec.extensions = %w[
32
+ ext/kuma_lib/extconf.rb
33
+ ext/priority_queue/extconf.rb
34
+ ext/kuma_set/extconf.rb
35
+ ]
36
+
37
+ # Uncomment to register a new dependency of your gem
38
+ # spec.add_dependency "example-gem", "~> 1.0"
39
+
40
+ # For more information and examples about making a new gem, checkout our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
data/lib/kuma_lib.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "kuma_lib/version"
4
+ require_relative "kuma_lib/kuma_lib"
5
+ require_relative "kuma_lib/priority_queue"
6
+ require_relative "kuma_lib/kuma_set"
7
+
8
+ module KumaLib
9
+ class KumaError < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KumaLib
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kuma_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kumakumarb
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - rinngo0119@gmail.com
16
+ executables: []
17
+ extensions:
18
+ - ext/kuma_lib/extconf.rb
19
+ - ext/priority_queue/extconf.rb
20
+ - ext/kuma_set/extconf.rb
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".github/workflows/main.yml"
24
+ - ".gitignore"
25
+ - ".rspec"
26
+ - ".ruby-version"
27
+ - CHANGELOG.md
28
+ - CODE_OF_CONDUCT.md
29
+ - Gemfile
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - docs/ja/kuma_set.md
36
+ - docs/ja/priority_queue.md
37
+ - ext/kuma_lib/extconf.rb
38
+ - ext/kuma_lib/kuma_lib.c
39
+ - ext/kuma_lib/kuma_lib.h
40
+ - ext/kuma_set/extconf.rb
41
+ - ext/kuma_set/kuma_set.cpp
42
+ - ext/priority_queue/extconf.rb
43
+ - ext/priority_queue/priority_queue.cpp
44
+ - kuma_lib.gemspec
45
+ - lib/kuma_lib.rb
46
+ - lib/kuma_lib/version.rb
47
+ homepage: https://github.com/kumakumarb/kuma_lib.git
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/kumakumarb/kuma_lib.git
52
+ source_code_uri: https://github.com/kumakumarb/kuma_lib.git
53
+ changelog_uri: https://github.com/kumakumarb/kuma_lib.git
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.4.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.4
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: atcoder library.
73
+ test_files: []