victory 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +4 -6
- data/USAGE.md +261 -0
- data/ext/containers/bitset/LICENSE.txt +20 -0
- data/ext/containers/bitset/bitset.c +652 -0
- data/ext/containers/bitset/builtin.h +1390 -0
- data/ext/containers/bitset/exact-int.h +229 -0
- data/ext/containers/bitset/extconf.rb +4 -0
- data/ext/containers/xor_list/extconf.rb +1 -1
- data/ext/containers/xor_list/xor_list.c +1 -1
- data/extensions.rb +13 -0
- data/lib/algorithms/greedy.rb +26 -0
- data/lib/algorithms/sort.rb +1 -1
- data/lib/containers/bitset.rb +48 -0
- data/lib/containers/xor_list.rb +3 -0
- data/lib/victory/version.rb +1 -1
- data/lib/victory.rb +6 -8
- data/victory.gemspec +4 -10
- metadata +36 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e93ae56ddafc7ac1417ad10315adf6f567d22ee164b8edc5f248eed2c090cf27
|
4
|
+
data.tar.gz: 28f25fa29c6dd9a68e512a917c113c8e078ae4c7838bc3f21274567551e7a550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84ae4f944daad2fc7a053425a9c73cb230d41f7ff5846692fff6869e47e7d7461a6459041de6934ed5284aa2b324b9f5d4db1481ace28dfa5994903dc7a21124
|
7
|
+
data.tar.gz: 0e6b5adec80cc7268b6b35a72cba740d977e2fa2dd90b33d159cdfad394ab2ed8645438e6b3668f1aa2310ae7907516c14896026358b6314b74be4c01a175dc1
|
data/Rakefile
CHANGED
@@ -11,12 +11,10 @@ require "rake/extensiontask"
|
|
11
11
|
|
12
12
|
task :build => :compile
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
Rake::ExtensionTask.new(
|
17
|
-
|
18
|
-
Rake::ExtensionTask.new('containers/splaytree_map') { |ext| ext.name = "CSplayTreeMap" }
|
19
|
-
Rake::ExtensionTask.new('containers/xor_list') { |ext| ext.name = 'XORList' }
|
14
|
+
require_relative 'extensions'
|
15
|
+
extensions.each do |extension|
|
16
|
+
Rake::ExtensionTask.new(extension.path) { |ext| ext.name = extension.name }
|
17
|
+
end
|
20
18
|
|
21
19
|
require 'rspec/core/rake_task'
|
22
20
|
|
data/USAGE.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
In this markdown you will see the usage of all the datastructures and algorithms available through the standard library and this gem.
|
4
4
|
|
5
|
+
For them to work you firstly need to require the library with `require 'victory'`.
|
6
|
+
|
5
7
|
# Data Structures
|
6
8
|
|
7
9
|
* [Ranges and Loops](#ranges)
|
@@ -15,6 +17,7 @@ In this markdown you will see the usage of all the datastructures and algorithms
|
|
15
17
|
* [Tuple](#tuple)
|
16
18
|
* [Graph](#graph)
|
17
19
|
* [IOHelpers](#io-helpers)
|
20
|
+
* [Concurrent Structures](#concurrent)
|
18
21
|
|
19
22
|
<a name="ranges" />
|
20
23
|
|
@@ -205,6 +208,156 @@ t[2]
|
|
205
208
|
# => 2
|
206
209
|
```
|
207
210
|
|
211
|
+
<a name="bitset" />
|
212
|
+
|
213
|
+
## Bitset
|
214
|
+
|
215
|
+
From [Bitset](https://github.com/ericboesch/bitset):
|
216
|
+
|
217
|
+
You create a bitset like this:
|
218
|
+
|
219
|
+
>> Bitset.new(8)
|
220
|
+
=> 00000000
|
221
|
+
|
222
|
+
Here we created an 8-bit bitset. All bits are initialized to 0.
|
223
|
+
|
224
|
+
We can also create a bitset based on a string of ones and zeros.
|
225
|
+
|
226
|
+
>> Bitset.from_s('00010001')
|
227
|
+
=> 00010001
|
228
|
+
|
229
|
+
or from an array. Falsey values (false and nil) are converted to
|
230
|
+
zeroes; all other values, including 0 and "", are converted to ones.
|
231
|
+
|
232
|
+
>> Bitset.new [false, nil, 3, 0]
|
233
|
+
=> 0011
|
234
|
+
|
235
|
+
To input an array of ones and zeroes:
|
236
|
+
|
237
|
+
>> Bitset.new([0,1,1,0].map(&:positive?))
|
238
|
+
=> 0110
|
239
|
+
|
240
|
+
Obviously you can also set and clear bits...
|
241
|
+
|
242
|
+
>> bitset = Bitset.new(8)
|
243
|
+
=> 00000000
|
244
|
+
|
245
|
+
>> bitset[3] = true
|
246
|
+
=> 00010000
|
247
|
+
|
248
|
+
>> bitset[3] = false
|
249
|
+
=> 00000000
|
250
|
+
|
251
|
+
>> bitset.set(1, 3, 5, 7)
|
252
|
+
=> 01010101
|
253
|
+
|
254
|
+
>> bitset.clear(1, 5)
|
255
|
+
=> 00010001
|
256
|
+
|
257
|
+
Arrays of Integers can also be passed to #clear and #set (c/o brendon9x).
|
258
|
+
|
259
|
+
The point of a bitset is to be, effectively, an array of single bits. It should
|
260
|
+
support basic set and bitwise operations. So, let's look at a few of those.
|
261
|
+
|
262
|
+
>> a = Bitset.from_s('00001111')
|
263
|
+
=> 00001111
|
264
|
+
|
265
|
+
>> b = Bitset.from_s('01010101')
|
266
|
+
=> 01010101
|
267
|
+
|
268
|
+
>> a & b
|
269
|
+
=> 00000101
|
270
|
+
|
271
|
+
>> a | b
|
272
|
+
=> 01011111
|
273
|
+
|
274
|
+
>> b - a
|
275
|
+
=> 01010000
|
276
|
+
|
277
|
+
>> a ^ b
|
278
|
+
=> 01011010
|
279
|
+
|
280
|
+
>> ~a
|
281
|
+
=> 11110000
|
282
|
+
|
283
|
+
>> a.hamming(b)
|
284
|
+
=> 4
|
285
|
+
|
286
|
+
>> a.cardinality
|
287
|
+
=> 4
|
288
|
+
|
289
|
+
>> a.reverse
|
290
|
+
=> 11110000
|
291
|
+
|
292
|
+
# Tell whether all of the given bit numbers are set
|
293
|
+
>> a.set? 6
|
294
|
+
=> true
|
295
|
+
|
296
|
+
# Return a new Bitset composed of bits #1, #3, #5, #4, and #1
|
297
|
+
# again. Unlike Array#values_at, this function currently only
|
298
|
+
# accepts an array of Fixnums as its argument.
|
299
|
+
>> a.values_at [1,3,5,4,1]
|
300
|
+
=> 00110
|
301
|
+
|
302
|
+
# Tell whether all of the given bit numbers are clear
|
303
|
+
>> a.clear? 1,3,5
|
304
|
+
=> false
|
305
|
+
|
306
|
+
# Tell whether all bits are clear
|
307
|
+
>> a.empty?
|
308
|
+
=> false
|
309
|
+
|
310
|
+
# Pass all bits to the block
|
311
|
+
>> b.each { |v| puts v }
|
312
|
+
=> false
|
313
|
+
true
|
314
|
+
false
|
315
|
+
...
|
316
|
+
|
317
|
+
# Pass the positions of all set bits to the block
|
318
|
+
>> b.each_set { |bit| puts bit }
|
319
|
+
=> 1
|
320
|
+
3
|
321
|
+
5
|
322
|
+
7
|
323
|
+
|
324
|
+
# Return an array of the positions of all set bits
|
325
|
+
>> b.each_set # AKA b.to_a
|
326
|
+
=> [1, 3, 5, 7]
|
327
|
+
|
328
|
+
# b.each_set(index) == b.each_set[index], but faster.
|
329
|
+
>> b.each_set(-3) # Negative index wraps around.
|
330
|
+
=> 3
|
331
|
+
|
332
|
+
# b.each_set(index, len) == b.each_set[index, len], but faster.
|
333
|
+
>> b.each_set(2,2) # Block is also allowed
|
334
|
+
=> [5,7]
|
335
|
+
|
336
|
+
|
337
|
+
# The following methods modify a Bitset in place very quickly:
|
338
|
+
>> a.intersect!(b) # like a &= b
|
339
|
+
>> a.union!(b) # like a |= b
|
340
|
+
>> a.difference!(b) # like a -= b
|
341
|
+
>> a.xor!(b) # like a ^= b
|
342
|
+
>> a.reset! # Zeroes all bits
|
343
|
+
|
344
|
+
# Above, "like" does not mean "identical to." a |= b creates a new
|
345
|
+
# Bitset object. a.union!(b) changes an existing object which
|
346
|
+
# affects all variables that point to the same object.
|
347
|
+
|
348
|
+
# Attempting to apply bitwise binary operators or their in-place
|
349
|
+
# equivalents between bitsets of different sizes will raise an
|
350
|
+
# ArgumentError.
|
351
|
+
|
352
|
+
>> b.to_binary_array
|
353
|
+
=> [0, 1, 0, 1, 0, 1, 0, 1]
|
354
|
+
|
355
|
+
# b.dup and b.clone are also available.
|
356
|
+
|
357
|
+
# Marshal.dump and Marshal.load are also supported. If you want to
|
358
|
+
# save a few bytes and don't need Marshal.load to work, you can
|
359
|
+
# use #pack and Bitset.unpack instead.
|
360
|
+
|
208
361
|
<a name="graph" />
|
209
362
|
|
210
363
|
## Graph
|
@@ -285,6 +438,114 @@ Writer.string(a, line_sep: "\n", col_sep: ' ', mapper: ->(x) {"0,#{x}"})
|
|
285
438
|
Writer.file("b", a, line_sep: "\n", col_sep: ' ', mapper: ->x {"0,#{x}"})
|
286
439
|
```
|
287
440
|
|
441
|
+
<a name="concurrent" />
|
442
|
+
|
443
|
+
## Concurrent Structures
|
444
|
+
|
445
|
+
They are included from [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby). To use them:
|
446
|
+
```ruby
|
447
|
+
Containers::Concurrent::Array
|
448
|
+
include Containers
|
449
|
+
Concurrent::Array
|
450
|
+
```
|
451
|
+
From [Concurrent Ruby Features](https://github.com/ruby-concurrency/concurrent-ruby#features--documentation):
|
452
|
+
|
453
|
+
### General-purpose Concurrency Abstractions
|
454
|
+
|
455
|
+
* [Async](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Async.html):
|
456
|
+
A mixin module that provides simple asynchronous behavior to a class. Loosely based on Erlang's
|
457
|
+
[gen_server](http://www.erlang.org/doc/man/gen_server.html).
|
458
|
+
* [ScheduledTask](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ScheduledTask.html):
|
459
|
+
Like a Future scheduled for a specific future time.
|
460
|
+
* [TimerTask](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/TimerTask.html):
|
461
|
+
A Thread that periodically wakes up to perform work at regular intervals.
|
462
|
+
* [Promises](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Promises.html):
|
463
|
+
Unified implementation of futures and promises which combines features of previous `Future`,
|
464
|
+
`Promise`, `IVar`, `Event`, `dataflow`, `Delay`, and (partially) `TimerTask` into a single
|
465
|
+
framework. It extensively uses the new synchronization layer to make all the features
|
466
|
+
**non-blocking** and **lock-free**, with the exception of obviously blocking operations like
|
467
|
+
`#wait`, `#value`. It also offers better performance.
|
468
|
+
|
469
|
+
### Thread-safe Value Objects, Structures, and Collections
|
470
|
+
|
471
|
+
Collection classes that were originally part of the (deprecated) `thread_safe` gem:
|
472
|
+
|
473
|
+
* [Array](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Array.html) A thread-safe
|
474
|
+
subclass of Ruby's standard [Array](http://ruby-doc.org/core-2.2.0/Array.html).
|
475
|
+
* [Hash](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Hash.html) A thread-safe
|
476
|
+
subclass of Ruby's standard [Hash](http://ruby-doc.org/core-2.2.0/Hash.html).
|
477
|
+
* [Set](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Set.html) A thread-safe
|
478
|
+
subclass of Ruby's standard [Set](http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html).
|
479
|
+
* [Map](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Map.html) A hash-like object
|
480
|
+
that should have much better performance characteristics, especially under high concurrency,
|
481
|
+
than `Concurrent::Hash`.
|
482
|
+
* [Tuple](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Tuple.html) A fixed size
|
483
|
+
array with volatile (synchronized, thread safe) getters/setters.
|
484
|
+
|
485
|
+
Value objects inspired by other languages:
|
486
|
+
|
487
|
+
* [Maybe](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Maybe.html) A thread-safe,
|
488
|
+
immutable object representing an optional value, based on
|
489
|
+
[Haskell Data.Maybe](https://hackage.haskell.org/package/base-4.2.0.1/docs/Data-Maybe.html).
|
490
|
+
|
491
|
+
Structure classes derived from Ruby's [Struct](http://ruby-doc.org/core-2.2.0/Struct.html):
|
492
|
+
|
493
|
+
* [ImmutableStruct](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ImmutableStruct.html)
|
494
|
+
Immutable struct where values are set at construction and cannot be changed later.
|
495
|
+
* [MutableStruct](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/MutableStruct.html)
|
496
|
+
Synchronized, mutable struct where values can be safely changed at any time.
|
497
|
+
* [SettableStruct](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/SettableStruct.html)
|
498
|
+
Synchronized, write-once struct where values can be set at most once, either at construction
|
499
|
+
or any time thereafter.
|
500
|
+
|
501
|
+
Thread-safe variables:
|
502
|
+
|
503
|
+
* [Agent](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Agent.html): A way to
|
504
|
+
manage shared, mutable, *asynchronous*, independent state. Based on Clojure's
|
505
|
+
[Agent](http://clojure.org/agents).
|
506
|
+
* [Atom](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Atom.html): A way to manage
|
507
|
+
shared, mutable, *synchronous*, independent state. Based on Clojure's
|
508
|
+
[Atom](http://clojure.org/atoms).
|
509
|
+
* [AtomicBoolean](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/AtomicBoolean.html)
|
510
|
+
A boolean value that can be updated atomically.
|
511
|
+
* [AtomicFixnum](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/AtomicFixnum.html)
|
512
|
+
A numeric value that can be updated atomically.
|
513
|
+
* [AtomicReference](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/AtomicReference.html)
|
514
|
+
An object reference that may be updated atomically.
|
515
|
+
* [Exchanger](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Exchanger.html)
|
516
|
+
A synchronization point at which threads can pair and swap elements within pairs. Based on
|
517
|
+
Java's [Exchanger](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Exchanger.html).
|
518
|
+
* [MVar](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/MVar.html) A synchronized
|
519
|
+
single element container. Based on Haskell's
|
520
|
+
[MVar](https://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Concurrent-MVar.html) and
|
521
|
+
Scala's [MVar](http://docs.typelevel.org/api/scalaz/nightly/index.html#scalaz.concurrent.MVar$).
|
522
|
+
* [ThreadLocalVar](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ThreadLocalVar.html)
|
523
|
+
A variable where the value is different for each thread.
|
524
|
+
* [TVar](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/TVar.html) A transactional
|
525
|
+
variable implementing software transactional memory (STM). Based on Clojure's
|
526
|
+
[Ref](http://clojure.org/refs).
|
527
|
+
|
528
|
+
### Java-inspired ThreadPools and Other Executors
|
529
|
+
|
530
|
+
* See the [thread pool](http://ruby-concurrency.github.io/concurrent-ruby/master/file.thread_pools.html)
|
531
|
+
overview, which also contains a list of other Executors available.
|
532
|
+
|
533
|
+
### Thread Synchronization Classes and Algorithms
|
534
|
+
|
535
|
+
* [CountDownLatch](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/CountDownLatch.html)
|
536
|
+
A synchronization object that allows one thread to wait on multiple other threads.
|
537
|
+
* [CyclicBarrier](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/CyclicBarrier.html)
|
538
|
+
A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point.
|
539
|
+
* [Event](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Event.html) Old school
|
540
|
+
kernel-style event.
|
541
|
+
* [ReadWriteLock](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ReadWriteLock.html)
|
542
|
+
A lock that supports multiple readers but only one writer.
|
543
|
+
* [ReentrantReadWriteLock](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ReentrantReadWriteLock.html)
|
544
|
+
A read/write lock with reentrant and upgrade features.
|
545
|
+
* [Semaphore](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Semaphore.html)
|
546
|
+
A counting-based locking mechanism that uses permits.
|
547
|
+
* [AtomicMarkableReference](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/AtomicMarkableReference.html)
|
548
|
+
|
288
549
|
# Other useful links
|
289
550
|
|
290
551
|
* https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Tyler McMullen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|