roaring 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8760a84dea46c898206d34cfad17efe16e53d1c0c3b5398db7ce8aa641e9b07
4
- data.tar.gz: df1a1d1b60b5e3bdff65bb6995279844c6cc23e0f5aedca1a924704e937774ec
3
+ metadata.gz: 54a748fcae71c5cf513774177c51f101a9eebf4e256cb7c255830e3ce9e0120d
4
+ data.tar.gz: f34710a57c8a91757f0560cc673b256c4b103ef73640cb8fc9ee916b2afbaea1
5
5
  SHA512:
6
- metadata.gz: 384f8731d1143eddc4a1db541f51271d10916fbe4d39ec649598fee1619d3fb73deade6a77e6b7850af3f1dd94a06bd757763c180ee89d3fa36ae9ab93ecb749
7
- data.tar.gz: 7ebe63d64b779ef73ba4586ac91ecfc302e7b1aa1488ae320e366e72d2a7b778f53c85ab71121e17098be03a7d1b309294d24cfca12bc1bf75826e6a425f5bd5
6
+ metadata.gz: 28477225f8b0415f7309a0e1a0490236313c89273cd57b459389b6e7d71481c54c4701d0b877685c948eb8e9e900b4b3156559b70f5aa99894e86dee9546dc44
7
+ data.tar.gz: 9aa1826a4bf7ee418c2a0c84a14c3031df75e6ea26cbca9e779d3fc64af944f735453c65dd8aed322d95b2032b018eb1007f97e8a85c3aafef08b565bab811aa
data/Gemfile CHANGED
@@ -6,3 +6,7 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
+
10
+ gem "rake-compiler"
11
+
12
+ gem "minitest", "~> 5.0"
data/Gemfile.lock CHANGED
@@ -1,18 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roaring (0.0.0)
4
+ roaring (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ forwardable (1.3.2)
10
+ minitest (5.15.0)
11
+ prime (0.1.2)
12
+ forwardable
13
+ singleton
9
14
  rake (13.0.6)
15
+ rake-compiler (1.2.0)
16
+ rake
17
+ singleton (0.1.1)
10
18
 
11
19
  PLATFORMS
12
20
  x86_64-linux
13
21
 
14
22
  DEPENDENCIES
23
+ minitest (~> 5.0)
24
+ prime
15
25
  rake (~> 13.0)
26
+ rake-compiler
16
27
  roaring!
17
28
 
18
29
  BUNDLED WITH
data/LICENSE.txt CHANGED
@@ -1,21 +1,201 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2022 John Hawthorn
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.
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2016 The CRoaring authors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,32 +1,53 @@
1
1
  # Roaring
2
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/roaring`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A Ruby wrapper for CRoaring (a C/C++ implementation at https://github.com/RoaringBitmap/CRoaring)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Roaring Bitmaps are a compressed bitmap/bitset format/library.
6
+ More information and papers can be found at https://roaringbitmap.org/
6
7
 
7
8
  ## Installation
8
9
 
9
- Add this line to your application's Gemfile:
10
+ Install the gem and add to the application's Gemfile by executing:
10
11
 
11
- ```ruby
12
- gem 'roaring'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install roaring
12
+ $ bundle add roaring
22
13
 
23
14
  ## Usage
24
15
 
25
- TODO: Write usage instructions here
16
+ ``` ruby
17
+ require "roaring"
18
+
19
+ # Bitmaps can efficiently store a large range of 32-bit integers
20
+ bitmap = Roaring::Bitmap[1, 2, 3, 999]
21
+ bitmap << (2**32 - 1)
22
+
23
+ # Element access
24
+ bitmap.each { }
25
+ bitmap.first # => 1
26
+ bitmap.min # => 1
27
+ bitmap.max # => 4294967295
28
+ bitmap.last # => 4294967295
29
+ bitmap[3] # => 999
30
+
31
+ b1 = Roaring::Bitmap.new(200...500)
32
+ b2 = Roaring::Bitmap.new(100...1000)
33
+
34
+ # Support common set operations
35
+ (b1 & b2).size # => 300
36
+ (b1 ^ b2).size # => 600
37
+ (b2 - b1).size # => 600
38
+ (b1 - b2).empty? # => true
39
+ b1 < b2 # => true
40
+ (b2 - b1) == (b1 ^ b2) # => true
41
+
42
+ # (De)Serialization (also available via Marshal#{dump,load})
43
+ dump = bitmap.serialize
44
+ loaded = Roaring::Bitmap.deserialize(dump)
45
+ loaded == bitmap # => true
46
+ ```
26
47
 
27
48
  ## Development
28
49
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
51
 
31
52
  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
53
 
@@ -36,7 +57,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jhawth
36
57
 
37
58
  ## License
38
59
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
60
+ The gem is available as open source under the terms of the [Apache License, Version 2.0](https://opensource.org/licenses/Apache-2.0).
40
61
 
41
62
  ## Code of Conduct
42
63
 
data/Rakefile CHANGED
@@ -1,4 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rake/extensiontask"
13
+
14
+ task build: :compile
15
+
16
+ Rake::ExtensionTask.new("roaring") do |ext|
17
+ ext.lib_dir = "lib/roaring"
18
+ end
19
+
20
+ task default: %i[clobber compile test]
@@ -0,0 +1,274 @@
1
+ #include "ruby.h"
2
+ #include "roaring.h"
3
+
4
+ #include <stdio.h>
5
+
6
+ static VALUE cRoaringBitmap;
7
+ VALUE rb_mRoaring;
8
+
9
+ static inline uint32_t
10
+ NUM2UINT32(VALUE num) {
11
+ if (!FIXNUM_P(num) && !RB_TYPE_P(num, T_BIGNUM)) {
12
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(num));
13
+ } else if ((SIGNED_VALUE)num < (SIGNED_VALUE)INT2FIX(0)) {
14
+ rb_raise(rb_eRangeError, "Integer %"PRIdVALUE " must be >= 0 to use with Roaring::Bitmap", num);
15
+ } else {
16
+ return FIX2UINT(num);
17
+ }
18
+ }
19
+
20
+ static void rb_roaring_free(void *data)
21
+ {
22
+ roaring_bitmap_free(data);
23
+ }
24
+
25
+ static size_t rb_roaring_memsize(const void *data)
26
+ {
27
+ // This is probably an estimate, "frozen" refers to the "frozen"
28
+ // serialization format, which mimics the in-memory representation.
29
+ return sizeof(roaring_bitmap_t) + roaring_bitmap_frozen_size_in_bytes(data);
30
+ }
31
+
32
+ static const rb_data_type_t roaring_type = {
33
+ .wrap_struct_name = "roaring/bitmap",
34
+ .function = {
35
+ .dfree = rb_roaring_free,
36
+ .dsize = rb_roaring_memsize
37
+ },
38
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
39
+ };
40
+
41
+ static VALUE rb_roaring_alloc(VALUE self)
42
+ {
43
+ roaring_bitmap_t *data = roaring_bitmap_create();
44
+ return TypedData_Wrap_Struct(self, &roaring_type, data);
45
+ }
46
+
47
+ static VALUE rb_roaring_initialize_copy(VALUE self, VALUE other) {
48
+ roaring_bitmap_t *self_data;
49
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, self_data);
50
+
51
+ roaring_bitmap_t *other_data;
52
+ TypedData_Get_Struct(other, roaring_bitmap_t, &roaring_type, other_data);
53
+
54
+ roaring_bitmap_overwrite(self_data, other_data);
55
+
56
+ return self;
57
+ }
58
+
59
+ static VALUE rb_roaring_cardinality(VALUE self)
60
+ {
61
+
62
+ roaring_bitmap_t *data;
63
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
64
+ uint64_t cardinality = roaring_bitmap_get_cardinality(data);
65
+ return ULONG2NUM(cardinality);
66
+ }
67
+
68
+ static VALUE rb_roaring_add(VALUE self, VALUE val)
69
+ {
70
+ roaring_bitmap_t *data;
71
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
72
+
73
+ uint32_t num = NUM2UINT32(val);
74
+ roaring_bitmap_add(data, num);
75
+ return self;
76
+ }
77
+
78
+ static VALUE rb_roaring_include_p(VALUE self, VALUE val)
79
+ {
80
+ roaring_bitmap_t *data;
81
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
82
+
83
+ uint32_t num = NUM2UINT32(val);
84
+ return roaring_bitmap_contains(data, num) ? Qtrue : Qfalse;
85
+ }
86
+
87
+ static VALUE rb_roaring_empty_p(VALUE self)
88
+ {
89
+ roaring_bitmap_t *data;
90
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
91
+
92
+ return roaring_bitmap_is_empty(data) ? Qtrue : Qfalse;
93
+ }
94
+
95
+ static VALUE rb_roaring_clear(VALUE self)
96
+ {
97
+ roaring_bitmap_t *data;
98
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
99
+
100
+ roaring_bitmap_clear(data);
101
+ return self;
102
+ }
103
+
104
+ bool rb_roaring_each_i(uint32_t value, void *param) {
105
+ rb_yield(UINT2NUM(value));
106
+ return true; // iterate till the end
107
+ }
108
+
109
+ static VALUE rb_roaring_each(VALUE self)
110
+ {
111
+ roaring_bitmap_t *data;
112
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
113
+
114
+ roaring_iterate(data, rb_roaring_each_i, NULL);
115
+ return self;
116
+ }
117
+
118
+ static VALUE rb_roaring_aref(VALUE self, VALUE rankv)
119
+ {
120
+ roaring_bitmap_t *data;
121
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
122
+
123
+ uint32_t rank = NUM2UINT32(rankv);
124
+ uint32_t val;
125
+
126
+ if (roaring_bitmap_select(data, rank, &val)) {
127
+ return UINT2NUM(val);
128
+ } else {
129
+ return Qnil;
130
+ }
131
+ return self;
132
+ }
133
+
134
+ static VALUE rb_roaring_min(VALUE self)
135
+ {
136
+ roaring_bitmap_t *data;
137
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
138
+
139
+ uint32_t val = roaring_bitmap_minimum(data);
140
+ return UINT2NUM(val);
141
+ }
142
+
143
+ static VALUE rb_roaring_max(VALUE self)
144
+ {
145
+ roaring_bitmap_t *data;
146
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
147
+
148
+ uint32_t val = roaring_bitmap_maximum(data);
149
+ return UINT2NUM(val);
150
+ }
151
+
152
+ static VALUE rb_roaring_run_optimize(VALUE self)
153
+ {
154
+ roaring_bitmap_t *data;
155
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
156
+
157
+ return roaring_bitmap_run_optimize(data) ? Qtrue : Qfalse;
158
+ }
159
+
160
+ static VALUE rb_roaring_serialize(VALUE self)
161
+ {
162
+ roaring_bitmap_t *data;
163
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, data);
164
+
165
+ size_t size = roaring_bitmap_portable_size_in_bytes(data);
166
+ VALUE str = rb_str_buf_new(size);
167
+
168
+ size_t written = roaring_bitmap_portable_serialize(data, RSTRING_PTR(str));
169
+ rb_str_set_len(str, written);
170
+
171
+ return str;
172
+ }
173
+
174
+ static VALUE rb_roaring_deserialize(VALUE self, VALUE str)
175
+ {
176
+ roaring_bitmap_t *bitmap = roaring_bitmap_portable_deserialize_safe(RSTRING_PTR(str), RSTRING_LEN(str));
177
+
178
+ return TypedData_Wrap_Struct(cRoaringBitmap, &roaring_type, bitmap);
179
+ }
180
+
181
+ typedef roaring_bitmap_t *binary_func(const roaring_bitmap_t *, const roaring_bitmap_t *);
182
+ static VALUE rb_roaring_binary_op(VALUE self, VALUE other, binary_func func) {
183
+ roaring_bitmap_t *self_data;
184
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, self_data);
185
+
186
+ roaring_bitmap_t *other_data;
187
+ TypedData_Get_Struct(other, roaring_bitmap_t, &roaring_type, other_data);
188
+
189
+ roaring_bitmap_t *result = func(self_data, other_data);
190
+
191
+ return TypedData_Wrap_Struct(cRoaringBitmap, &roaring_type, result);
192
+ }
193
+
194
+ typedef bool binary_func_bool(const roaring_bitmap_t *, const roaring_bitmap_t *);
195
+ static VALUE rb_roaring_binary_op_bool(VALUE self, VALUE other, binary_func_bool func) {
196
+ roaring_bitmap_t *self_data;
197
+ TypedData_Get_Struct(self, roaring_bitmap_t, &roaring_type, self_data);
198
+
199
+ roaring_bitmap_t *other_data;
200
+ TypedData_Get_Struct(other, roaring_bitmap_t, &roaring_type, other_data);
201
+
202
+ bool result = func(self_data, other_data);
203
+ return result ? Qtrue : Qfalse;
204
+ }
205
+
206
+
207
+ static VALUE rb_roaring_and(VALUE self, VALUE other)
208
+ {
209
+ return rb_roaring_binary_op(self, other, roaring_bitmap_and);
210
+ }
211
+
212
+ static VALUE rb_roaring_or(VALUE self, VALUE other)
213
+ {
214
+ return rb_roaring_binary_op(self, other, roaring_bitmap_or);
215
+ }
216
+
217
+ static VALUE rb_roaring_xor(VALUE self, VALUE other)
218
+ {
219
+ return rb_roaring_binary_op(self, other, roaring_bitmap_xor);
220
+ }
221
+
222
+ static VALUE rb_roaring_andnot(VALUE self, VALUE other)
223
+ {
224
+ return rb_roaring_binary_op(self, other, roaring_bitmap_andnot);
225
+ }
226
+
227
+ static VALUE rb_roaring_eq(VALUE self, VALUE other)
228
+ {
229
+ return rb_roaring_binary_op_bool(self, other, roaring_bitmap_equals);
230
+ }
231
+
232
+ static VALUE rb_roaring_lt(VALUE self, VALUE other)
233
+ {
234
+ return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_strict_subset);
235
+ }
236
+
237
+ static VALUE rb_roaring_lte(VALUE self, VALUE other)
238
+ {
239
+ return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_subset);
240
+ }
241
+ void
242
+ Init_roaring(void)
243
+ {
244
+ rb_mRoaring = rb_define_module("Roaring");
245
+
246
+ cRoaringBitmap = rb_define_class_under(rb_mRoaring, "Bitmap", rb_cObject);
247
+ rb_define_alloc_func(cRoaringBitmap, rb_roaring_alloc);
248
+ rb_define_method(cRoaringBitmap, "initialize_copy", rb_roaring_initialize_copy, 1);
249
+ rb_define_method(cRoaringBitmap, "empty?", rb_roaring_empty_p, 0);
250
+ rb_define_method(cRoaringBitmap, "clear", rb_roaring_clear, 0);
251
+ rb_define_method(cRoaringBitmap, "cardinality", rb_roaring_cardinality, 0);
252
+ rb_define_method(cRoaringBitmap, "add", rb_roaring_add, 1);
253
+ rb_define_method(cRoaringBitmap, "<<", rb_roaring_add, 1);
254
+ rb_define_method(cRoaringBitmap, "include?", rb_roaring_include_p, 1);
255
+ rb_define_method(cRoaringBitmap, "each", rb_roaring_each, 0);
256
+ rb_define_method(cRoaringBitmap, "[]", rb_roaring_aref, 1);
257
+
258
+ rb_define_method(cRoaringBitmap, "&", rb_roaring_and, 1);
259
+ rb_define_method(cRoaringBitmap, "|", rb_roaring_or, 1);
260
+ rb_define_method(cRoaringBitmap, "^", rb_roaring_xor, 1);
261
+ rb_define_method(cRoaringBitmap, "-", rb_roaring_andnot, 1);
262
+
263
+ rb_define_method(cRoaringBitmap, "==", rb_roaring_eq, 1);
264
+ rb_define_method(cRoaringBitmap, "<", rb_roaring_lt, 1);
265
+ rb_define_method(cRoaringBitmap, "<=", rb_roaring_lte, 1);
266
+
267
+ rb_define_method(cRoaringBitmap, "min", rb_roaring_min, 0);
268
+ rb_define_method(cRoaringBitmap, "max", rb_roaring_max, 0);
269
+
270
+ rb_define_method(cRoaringBitmap, "run_optimize", rb_roaring_run_optimize, 0);
271
+
272
+ rb_define_method(cRoaringBitmap, "serialize", rb_roaring_serialize, 0);
273
+ rb_define_singleton_method(cRoaringBitmap, "deserialize", rb_roaring_deserialize, 1);
274
+ }
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ submodule = "#{__dir__}/roaring/"
6
+
7
+ $objs = ["cext.o", "#{submodule}/roaring.o"]
8
+ $CPPFLAGS += " -I#{submodule} "
9
+
10
+ create_makefile("roaring/roaring")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Roaring
4
- VERSION = "0.0.0"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/roaring.rb CHANGED
@@ -1,8 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "roaring/version"
4
+ require_relative "roaring/roaring"
5
+ require "set"
4
6
 
5
7
  module Roaring
6
8
  class Error < StandardError; end
7
- # Your code goes here...
9
+
10
+ class Bitmap
11
+ include Enumerable
12
+
13
+ alias size cardinality
14
+ alias length cardinality
15
+ alias count cardinality
16
+
17
+ alias + |
18
+
19
+ alias first min
20
+ alias last max
21
+
22
+ def initialize(enum = nil)
23
+ return unless enum
24
+
25
+ if enum.instance_of?(Roaring::Bitmap)
26
+ initialize_copy(enum)
27
+ else
28
+ enum.each { |x| self << x }
29
+ end
30
+ end
31
+
32
+ def self.[](*args)
33
+ if args.size == 0
34
+ new
35
+ elsif args.size == 1 && !(Integer === args[0])
36
+ new(args[0])
37
+ else
38
+ new(args)
39
+ end
40
+ end
41
+
42
+ def >(other)
43
+ other < self
44
+ end
45
+
46
+ def >=(other)
47
+ other <= self
48
+ end
49
+
50
+ def _dump level
51
+ serialize
52
+ end
53
+
54
+ def self._load args
55
+ deserialize(args)
56
+ end
57
+
58
+ def to_a
59
+ map(&:itself)
60
+ end
61
+
62
+ def to_set
63
+ ::Set.new(to_a)
64
+ end
65
+
66
+ def inspect
67
+ "#<#{self.class} cardinality=#{cardinality}>"
68
+ end
69
+ end
8
70
  end
data/roaring.gemspec CHANGED
@@ -8,30 +8,25 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["John Hawthorn"]
9
9
  spec.email = ["john@hawthorn.email"]
10
10
 
11
- spec.summary = "placeholder"
12
- spec.description = "placeholder"
11
+ spec.summary = "Roaring bitmaps for Ruby"
12
+ spec.description = spec.summary
13
13
  spec.homepage = "https://github.com/jhawthorn/roaring-ruby"
14
- spec.license = "MIT"
14
+ spec.license = "Apache-2.0"
15
15
  spec.required_ruby_version = ">= 2.6.0"
16
16
 
17
- spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = spec.homepage
19
- spec.metadata["changelog_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.metadata["changelog_uri"] = spec.metadata["homepage_uri"] = spec.homepage
20
18
 
21
19
  # Specify which files should be added to the gem when it is released.
22
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ spec.files = Dir.chdir(__dir__) do
24
22
  `git ls-files -z`.split("\x0").reject do |f|
25
- (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
24
  end
27
25
  end
28
26
  spec.bindir = "exe"
29
27
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
28
  spec.require_paths = ["lib"]
29
+ spec.extensions = ["ext/roaring/extconf.rb"]
31
30
 
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
34
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
31
+ spec.add_development_dependency "prime"
37
32
  end
data/sig/roaring.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Roaring
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,20 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roaring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-14 00:00:00.000000000 Z
12
- dependencies: []
13
- description: placeholder
11
+ date: 2022-06-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prime
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Roaring bitmaps for Ruby
14
28
  email:
15
29
  - john@hawthorn.email
16
30
  executables: []
17
- extensions: []
31
+ extensions:
32
+ - ext/roaring/extconf.rb
18
33
  extra_rdoc_files: []
19
34
  files:
20
35
  - CODE_OF_CONDUCT.md
@@ -23,18 +38,19 @@ files:
23
38
  - LICENSE.txt
24
39
  - README.md
25
40
  - Rakefile
26
- - bin/console
27
- - bin/setup
41
+ - ext/roaring/cext.c
42
+ - ext/roaring/extconf.rb
28
43
  - lib/roaring.rb
29
44
  - lib/roaring/version.rb
30
45
  - roaring.gemspec
46
+ - sig/roaring.rbs
31
47
  homepage: https://github.com/jhawthorn/roaring-ruby
32
48
  licenses:
33
- - MIT
49
+ - Apache-2.0
34
50
  metadata:
35
51
  homepage_uri: https://github.com/jhawthorn/roaring-ruby
36
- source_code_uri: https://github.com/jhawthorn/roaring-ruby
37
52
  changelog_uri: https://github.com/jhawthorn/roaring-ruby
53
+ source_code_uri: https://github.com/jhawthorn/roaring-ruby
38
54
  post_install_message:
39
55
  rdoc_options: []
40
56
  require_paths:
@@ -53,5 +69,5 @@ requirements: []
53
69
  rubygems_version: 3.3.3
54
70
  signing_key:
55
71
  specification_version: 4
56
- summary: placeholder
72
+ summary: Roaring bitmaps for Ruby
57
73
  test_files: []
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require "bundler/setup"
5
- require "roaring"
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 DELETED
@@ -1,8 +0,0 @@
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