bitset 1.0.0 → 1.0.1
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/README.markdown +12 -5
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/bitset.gemspec +20 -29
- data/ext/bitset/bitset.c +82 -43
- data/ext/bitset/builtin.h +1390 -0
- data/ext/bitset/exact-int.h +229 -0
- data/ext/bitset/extconf.rb +1 -1
- data/lib/{bitset/bitset.rb → bitset.rb} +2 -0
- data/spec/bitset_spec.rb +31 -2
- metadata +6 -4
@@ -0,0 +1,229 @@
|
|
1
|
+
/* Exact-width integer types
|
2
|
+
* Portable Snippets - https://gitub.com/nemequ/portable-snippets
|
3
|
+
* Created by Evan Nemerson <evan@nemerson.com>
|
4
|
+
*
|
5
|
+
* To the extent possible under law, the authors have waived all
|
6
|
+
* copyright and related or neighboring rights to this code. For
|
7
|
+
* details, see the Creative Commons Zero 1.0 Universal license at
|
8
|
+
* https://creativecommons.org/publicdomain/zero/1.0/
|
9
|
+
*
|
10
|
+
* This header tries to define psnip_(u)int(8|16|32|64)_t to
|
11
|
+
* appropriate types given your system. For most systems this means
|
12
|
+
* including <stdint.h> and adding a few preprocessor definitions.
|
13
|
+
*
|
14
|
+
* If you prefer, you can define any necessary types yourself.
|
15
|
+
* Snippets in this repository which rely on these types will not
|
16
|
+
* attempt to include this header if you have already defined the
|
17
|
+
* types it uses.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#if !defined(PSNIP_EXACT_INT_H)
|
21
|
+
# define PSNIP_EXACT_INT_H
|
22
|
+
# if !defined(PSNIP_EXACT_INT_HAVE_STDINT)
|
23
|
+
# if defined(_STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
24
|
+
# define PSNIP_EXACT_INT_HAVE_STDINT
|
25
|
+
# elif defined(__has_include)
|
26
|
+
# if __has_include(<stdint.h>)
|
27
|
+
# define PSNIP_EXACT_INT_HAVE_STDINT
|
28
|
+
# endif
|
29
|
+
# elif \
|
30
|
+
defined(HAVE_STDINT_H) || \
|
31
|
+
defined(_STDINT_H_INCLUDED) || \
|
32
|
+
defined(_STDINT_H) || \
|
33
|
+
defined(_STDINT_H_)
|
34
|
+
# define PSNIP_EXACT_INT_HAVE_STDINT
|
35
|
+
# elif \
|
36
|
+
(defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))) || \
|
37
|
+
(defined(_MSC_VER) && (_MSC_VER >= 1600)) || \
|
38
|
+
(defined(__SUNPRO_C) && (__SUNPRO_C >= 0x570)) || \
|
39
|
+
(defined(__WATCOMC__) && (__WATCOMC__ >= 1250))
|
40
|
+
# define PSNIP_EXACT_INT_HAVE_STDINT
|
41
|
+
# endif
|
42
|
+
# endif
|
43
|
+
|
44
|
+
# if \
|
45
|
+
defined(__INT8_TYPE__) && defined(__INT16_TYPE__) && defined(__INT32_TYPE__) && defined(__INT64_TYPE__) && \
|
46
|
+
defined(__UINT8_TYPE__) && defined(__UINT16_TYPE__) && defined(__UINT32_TYPE__) && defined(__UINT64_TYPE__)
|
47
|
+
# define psnip_int8_t __INT8_TYPE__
|
48
|
+
# define psnip_int16_t __INT16_TYPE__
|
49
|
+
# define psnip_int32_t __INT32_TYPE__
|
50
|
+
# define psnip_int64_t __INT64_TYPE__
|
51
|
+
# define psnip_uint8_t __UINT8_TYPE__
|
52
|
+
# define psnip_uint16_t __UINT16_TYPE__
|
53
|
+
# define psnip_uint32_t __UINT32_TYPE__
|
54
|
+
# define psnip_uint64_t __UINT64_TYPE__
|
55
|
+
# elif defined(PSNIP_EXACT_INT_HAVE_STDINT)
|
56
|
+
# include <stdint.h>
|
57
|
+
# if !defined(psnip_int8_t)
|
58
|
+
# define psnip_int8_t int8_t
|
59
|
+
# endif
|
60
|
+
# if !defined(psnip_uint8_t)
|
61
|
+
# define psnip_uint8_t uint8_t
|
62
|
+
# endif
|
63
|
+
# if !defined(psnip_int16_t)
|
64
|
+
# define psnip_int16_t int16_t
|
65
|
+
# endif
|
66
|
+
# if !defined(psnip_uint16_t)
|
67
|
+
# define psnip_uint16_t uint16_t
|
68
|
+
# endif
|
69
|
+
# if !defined(psnip_int32_t)
|
70
|
+
# define psnip_int32_t int32_t
|
71
|
+
# endif
|
72
|
+
# if !defined(psnip_uint32_t)
|
73
|
+
# define psnip_uint32_t uint32_t
|
74
|
+
# endif
|
75
|
+
# if !defined(psnip_int64_t)
|
76
|
+
# define psnip_int64_t int64_t
|
77
|
+
# endif
|
78
|
+
# if !defined(psnip_uint64_t)
|
79
|
+
# define psnip_uint64_t uint64_t
|
80
|
+
# endif
|
81
|
+
# elif defined(_MSC_VER)
|
82
|
+
# if !defined(psnip_int8_t)
|
83
|
+
# define psnip_int8_t __int8
|
84
|
+
# endif
|
85
|
+
# if !defined(psnip_uint8_t)
|
86
|
+
# define psnip_uint8_t unsigned __int8
|
87
|
+
# endif
|
88
|
+
# if !defined(psnip_int16_t)
|
89
|
+
# define psnip_int16_t __int16
|
90
|
+
# endif
|
91
|
+
# if !defined(psnip_uint16_t)
|
92
|
+
# define psnip_uint16_t unsigned __int16
|
93
|
+
# endif
|
94
|
+
# if !defined(psnip_int32_t)
|
95
|
+
# define psnip_int32_t __int32
|
96
|
+
# endif
|
97
|
+
# if !defined(psnip_uint32_t)
|
98
|
+
# define psnip_uint32_t unsigned __int32
|
99
|
+
# endif
|
100
|
+
# if !defined(psnip_int64_t)
|
101
|
+
# define psnip_int64_t __int64
|
102
|
+
# endif
|
103
|
+
# if !defined(psnip_uint64_t)
|
104
|
+
# define psnip_uint64_t unsigned __int64
|
105
|
+
# endif
|
106
|
+
# else
|
107
|
+
# include <limits.h>
|
108
|
+
# if !defined(psnip_int8_t)
|
109
|
+
# if defined(CHAR_MIN) && defined(CHAR_MAX) && (CHAR_MIN == (-127-1)) && (CHAR_MAX == 127)
|
110
|
+
# define psnip_int8_t char
|
111
|
+
# elif defined(SHRT_MIN) && defined(SHRT_MAX) && (SHRT_MIN == (-127-1)) && (SHRT_MAX == 127)
|
112
|
+
# define psnip_int8_t short
|
113
|
+
# elif defined(INT_MIN) && defined(INT_MAX) && (INT_MIN == (-127-1)) && (INT_MAX == 127)
|
114
|
+
# define psnip_int8_t int
|
115
|
+
# elif defined(LONG_MIN) && defined(LONG_MAX) && (LONG_MIN == (-127-1)) && (LONG_MAX == 127)
|
116
|
+
# define psnip_int8_t long
|
117
|
+
# elif defined(LLONG_MIN) && defined(LLONG_MAX) && (LLONG_MIN == (-127-1)) && (LLONG_MAX == 127)
|
118
|
+
# define psnip_int8_t long long
|
119
|
+
# else
|
120
|
+
# error Unable to locate 8-bit signed integer type.
|
121
|
+
# endif
|
122
|
+
# endif
|
123
|
+
# if !defined(psnip_uint8_t)
|
124
|
+
# if defined(UCHAR_MAX) && (UCHAR_MAX == 255)
|
125
|
+
# define psnip_uint8_t unsigned char
|
126
|
+
# elif defined(USHRT_MAX) && (USHRT_MAX == 255)
|
127
|
+
# define psnip_uint8_t unsigned short
|
128
|
+
# elif defined(UINT_MAX) && (UINT_MAX == 255)
|
129
|
+
# define psnip_uint8_t unsigned int
|
130
|
+
# elif defined(ULONG_MAX) && (ULONG_MAX == 255)
|
131
|
+
# define psnip_uint8_t unsigned long
|
132
|
+
# elif defined(ULLONG_MAX) && (ULLONG_MAX == 255)
|
133
|
+
# define psnip_uint8_t unsigned long long
|
134
|
+
# else
|
135
|
+
# error Unable to locate 8-bit unsigned integer type.
|
136
|
+
# endif
|
137
|
+
# endif
|
138
|
+
# if !defined(psnip_int16_t)
|
139
|
+
# if defined(CHAR_MIN) && defined(CHAR_MAX) && (CHAR_MIN == (-32767-1)) && (CHAR_MAX == 32767)
|
140
|
+
# define psnip_int16_t char
|
141
|
+
# elif defined(SHRT_MIN) && defined(SHRT_MAX) && (SHRT_MIN == (-32767-1)) && (SHRT_MAX == 32767)
|
142
|
+
# define psnip_int16_t short
|
143
|
+
# elif defined(INT_MIN) && defined(INT_MAX) && (INT_MIN == (-32767-1)) && (INT_MAX == 32767)
|
144
|
+
# define psnip_int16_t int
|
145
|
+
# elif defined(LONG_MIN) && defined(LONG_MAX) && (LONG_MIN == (-32767-1)) && (LONG_MAX == 32767)
|
146
|
+
# define psnip_int16_t long
|
147
|
+
# elif defined(LLONG_MIN) && defined(LLONG_MAX) && (LLONG_MIN == (-32767-1)) && (LLONG_MAX == 32767)
|
148
|
+
# define psnip_int16_t long long
|
149
|
+
# else
|
150
|
+
# error Unable to locate 16-bit signed integer type.
|
151
|
+
# endif
|
152
|
+
# endif
|
153
|
+
# if !defined(psnip_uint16_t)
|
154
|
+
# if defined(UCHAR_MAX) && (UCHAR_MAX == 65535)
|
155
|
+
# define psnip_uint16_t unsigned char
|
156
|
+
# elif defined(USHRT_MAX) && (USHRT_MAX == 65535)
|
157
|
+
# define psnip_uint16_t unsigned short
|
158
|
+
# elif defined(UINT_MAX) && (UINT_MAX == 65535)
|
159
|
+
# define psnip_uint16_t unsigned int
|
160
|
+
# elif defined(ULONG_MAX) && (ULONG_MAX == 65535)
|
161
|
+
# define psnip_uint16_t unsigned long
|
162
|
+
# elif defined(ULLONG_MAX) && (ULLONG_MAX == 65535)
|
163
|
+
# define psnip_uint16_t unsigned long long
|
164
|
+
# else
|
165
|
+
# error Unable to locate 16-bit unsigned integer type.
|
166
|
+
# endif
|
167
|
+
# endif
|
168
|
+
# if !defined(psnip_int32_t)
|
169
|
+
# if defined(CHAR_MIN) && defined(CHAR_MAX) && (CHAR_MIN == (-2147483647-1)) && (CHAR_MAX == 2147483647)
|
170
|
+
# define psnip_int32_t char
|
171
|
+
# elif defined(SHRT_MIN) && defined(SHRT_MAX) && (SHRT_MIN == (-2147483647-1)) && (SHRT_MAX == 2147483647)
|
172
|
+
# define psnip_int32_t short
|
173
|
+
# elif defined(INT_MIN) && defined(INT_MAX) && (INT_MIN == (-2147483647-1)) && (INT_MAX == 2147483647)
|
174
|
+
# define psnip_int32_t int
|
175
|
+
# elif defined(LONG_MIN) && defined(LONG_MAX) && (LONG_MIN == (-2147483647-1)) && (LONG_MAX == 2147483647)
|
176
|
+
# define psnip_int32_t long
|
177
|
+
# elif defined(LLONG_MIN) && defined(LLONG_MAX) && (LLONG_MIN == (-2147483647-1)) && (LLONG_MAX == 2147483647)
|
178
|
+
# define psnip_int32_t long long
|
179
|
+
# else
|
180
|
+
# error Unable to locate 32-bit signed integer type.
|
181
|
+
# endif
|
182
|
+
# endif
|
183
|
+
# if !defined(psnip_uint32_t)
|
184
|
+
# if defined(UCHAR_MAX) && (UCHAR_MAX == 4294967295)
|
185
|
+
# define psnip_uint32_t unsigned char
|
186
|
+
# elif defined(USHRT_MAX) && (USHRT_MAX == 4294967295)
|
187
|
+
# define psnip_uint32_t unsigned short
|
188
|
+
# elif defined(UINT_MAX) && (UINT_MAX == 4294967295)
|
189
|
+
# define psnip_uint32_t unsigned int
|
190
|
+
# elif defined(ULONG_MAX) && (ULONG_MAX == 4294967295)
|
191
|
+
# define psnip_uint32_t unsigned long
|
192
|
+
# elif defined(ULLONG_MAX) && (ULLONG_MAX == 4294967295)
|
193
|
+
# define psnip_uint32_t unsigned long long
|
194
|
+
# else
|
195
|
+
# error Unable to locate 32-bit unsigned integer type.
|
196
|
+
# endif
|
197
|
+
# endif
|
198
|
+
# if !defined(psnip_int64_t)
|
199
|
+
# if defined(CHAR_MIN) && defined(CHAR_MAX) && (CHAR_MIN == (-9223372036854775807LL-1)) && (CHAR_MAX == 9223372036854775807LL)
|
200
|
+
# define psnip_int64_t char
|
201
|
+
# elif defined(SHRT_MIN) && defined(SHRT_MAX) && (SHRT_MIN == (-9223372036854775807LL-1)) && (SHRT_MAX == 9223372036854775807LL)
|
202
|
+
# define psnip_int64_t short
|
203
|
+
# elif defined(INT_MIN) && defined(INT_MAX) && (INT_MIN == (-9223372036854775807LL-1)) && (INT_MAX == 9223372036854775807LL)
|
204
|
+
# define psnip_int64_t int
|
205
|
+
# elif defined(LONG_MIN) && defined(LONG_MAX) && (LONG_MIN == (-9223372036854775807LL-1)) && (LONG_MAX == 9223372036854775807LL)
|
206
|
+
# define psnip_int64_t long
|
207
|
+
# elif defined(LLONG_MIN) && defined(LLONG_MAX) && (LLONG_MIN == (-9223372036854775807LL-1)) && (LLONG_MAX == 9223372036854775807LL)
|
208
|
+
# define psnip_int64_t long long
|
209
|
+
# else
|
210
|
+
# error Unable to locate 64-bit signed integer type.
|
211
|
+
# endif
|
212
|
+
# endif
|
213
|
+
# if !defined(psnip_uint64_t)
|
214
|
+
# if defined(UCHAR_MAX) && (UCHAR_MAX == 18446744073709551615ULL)
|
215
|
+
# define psnip_uint64_t unsigned char
|
216
|
+
# elif defined(USHRT_MAX) && (USHRT_MAX == 18446744073709551615ULL)
|
217
|
+
# define psnip_uint64_t unsigned short
|
218
|
+
# elif defined(UINT_MAX) && (UINT_MAX == 18446744073709551615ULL)
|
219
|
+
# define psnip_uint64_t unsigned int
|
220
|
+
# elif defined(ULONG_MAX) && (ULONG_MAX == 18446744073709551615ULL)
|
221
|
+
# define psnip_uint64_t unsigned long
|
222
|
+
# elif defined(ULLONG_MAX) && (ULLONG_MAX == 18446744073709551615ULL)
|
223
|
+
# define psnip_uint64_t unsigned long long
|
224
|
+
# else
|
225
|
+
# error Unable to locate 64-bit unsigned integer type.
|
226
|
+
# endif
|
227
|
+
# endif
|
228
|
+
# endif
|
229
|
+
#endif
|
data/ext/bitset/extconf.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'mkmf'
|
2
|
-
create_makefile 'bitset'
|
2
|
+
create_makefile 'bitset/bitset'
|
data/spec/bitset_spec.rb
CHANGED
@@ -143,7 +143,7 @@ describe Bitset do
|
|
143
143
|
it '... even for large numbers of bits' do
|
144
144
|
bs = Bitset.new(10_000)
|
145
145
|
size = 5000
|
146
|
-
bs.set(
|
146
|
+
bs.set((0...size).to_a)
|
147
147
|
expect(bs.cardinality).to eq(size)
|
148
148
|
|
149
149
|
bs = Bitset.from_s "01001101000000000000000000000011000010100100000000000000010000101000000000000000100000000100000000000010100100010000000010000100000100000001001000110000000000100010000000010100000000000000110000000000000000000000000100000000100010010000000000000000000001000000000000000000000000000001000000000000000000000000000100000000010010000000000000000000100100000000000000001000000010000001000000000000001000001100010001000000000000001000001000001000000000000001100010000010010001000000010000100000000000110000"
|
@@ -259,7 +259,7 @@ describe Bitset do
|
|
259
259
|
it 'iterates over each set bit in the Bitset' do
|
260
260
|
bs = Bitset.new(4)
|
261
261
|
sets = [0,3]
|
262
|
-
bs.set
|
262
|
+
bs.set sets
|
263
263
|
sets2 = []
|
264
264
|
bs.each_set { |bit| sets2 << bit }
|
265
265
|
expect(sets2).to eq(sets)
|
@@ -381,4 +381,33 @@ describe Bitset do
|
|
381
381
|
end
|
382
382
|
end
|
383
383
|
|
384
|
+
describe :to_a do
|
385
|
+
it "can convert to an array of set positions" do
|
386
|
+
bs = Bitset.new(68)
|
387
|
+
bs.set 1, 64, 65
|
388
|
+
|
389
|
+
expect(bs.to_a).to eq([1, 64, 65])
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
describe :to_binary_array do
|
394
|
+
it "can convert to an array of 1s and 0s" do
|
395
|
+
bs = Bitset.new(68)
|
396
|
+
bs.set 1, 64, 65
|
397
|
+
|
398
|
+
expect(bs.to_binary_array.values_at(1, 64, 65, 66)).to eq([1, 1, 1, 0])
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
describe :pack do
|
403
|
+
it 'round trips with #unpack' do
|
404
|
+
%w{110 00010 101010011101011100000110011010110011010001}.each do |bits|
|
405
|
+
bitset = Bitset.from_s bits
|
406
|
+
packed = bitset.pack
|
407
|
+
unpacked = Bitset.unpack(packed)
|
408
|
+
expect(bitset).to eq(unpacked)
|
409
|
+
end
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
384
413
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler McMullen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A fast C-based Bitset. It supports the standard set operations as well
|
14
|
-
as operations you may expect on bit arrays
|
14
|
+
as operations you may expect on bit arrays,such as popcount.
|
15
15
|
email: eric.boesch@nist.gov
|
16
16
|
executables: []
|
17
17
|
extensions:
|
@@ -26,8 +26,10 @@ files:
|
|
26
26
|
- VERSION
|
27
27
|
- bitset.gemspec
|
28
28
|
- ext/bitset/bitset.c
|
29
|
+
- ext/bitset/builtin.h
|
30
|
+
- ext/bitset/exact-int.h
|
29
31
|
- ext/bitset/extconf.rb
|
30
|
-
- lib/bitset
|
32
|
+
- lib/bitset.rb
|
31
33
|
- spec/bitset_spec.rb
|
32
34
|
homepage: http://github.com/ericboesch/bitset
|
33
35
|
licenses:
|