algorithmable 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/algorithmable/data_structs/queue.rb +85 -0
- data/lib/algorithmable/data_structs.rb +1 -0
- data/lib/algorithmable/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40e70c3fb85f8914dade38055945669ecaf6f950
|
4
|
+
data.tar.gz: 57c983790d111dd5169bdaae1a60070766773808
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7312c04abd0f47c2ee93bcfd1cbeecb6d96b81f0f7de1ca7c39ed23ab6e7826271bade36144de05c71308db96347807576706283582aa8e97b085e5b19ebc6b8
|
7
|
+
data.tar.gz: 33efeca966f560372813d7397474184cf1be1839e6f4d2a76f7b79bc50fd6e3e3ae993121afc1111f678b28c552c1827ed58ccd104b5c8508900ccc7107f460b
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
|
3
|
+
module Algorithmable
|
4
|
+
module DataStructs
|
5
|
+
class Queue
|
6
|
+
include Enumerable
|
7
|
+
include MonitorMixin
|
8
|
+
attr_reader :size
|
9
|
+
|
10
|
+
SEPARATOR = ':'
|
11
|
+
|
12
|
+
NoSuchElementError = Class.new(RuntimeError)
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@first = nil
|
16
|
+
@last = nil
|
17
|
+
@size = 0
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty?
|
22
|
+
!@size.nonzero?
|
23
|
+
end
|
24
|
+
|
25
|
+
def peek
|
26
|
+
synchronize do
|
27
|
+
fail NoSuchElementError if empty?
|
28
|
+
@first.item
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def enqueue(item)
|
33
|
+
synchronize do
|
34
|
+
old_last = @last
|
35
|
+
@last = Node.new item
|
36
|
+
@last.succ = nil
|
37
|
+
@first = @last if empty?
|
38
|
+
old_last.succ = @last unless empty?
|
39
|
+
@size = @size.next
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def dequeue
|
44
|
+
synchronize do
|
45
|
+
fail NoSuchElementError if empty?
|
46
|
+
item = @first.item
|
47
|
+
@first = @first.succ
|
48
|
+
@size = @size.pred
|
49
|
+
@last = nil if empty?
|
50
|
+
item
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s
|
55
|
+
synchronize do
|
56
|
+
to_a.join SEPARATOR
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def each(&block)
|
61
|
+
synchronize do
|
62
|
+
(@first || []).each(&block)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Node
|
67
|
+
attr_accessor :succ, :item
|
68
|
+
|
69
|
+
def initialize(item)
|
70
|
+
@item = item
|
71
|
+
@succ = nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_s
|
75
|
+
@item.to_s
|
76
|
+
end
|
77
|
+
|
78
|
+
def each(&block)
|
79
|
+
yield self
|
80
|
+
@succ.each(&block) if @succ
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: algorithmable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Lishtaba
|
@@ -145,6 +145,7 @@ files:
|
|
145
145
|
- lib/algorithmable/data_structs/linked_list.rb
|
146
146
|
- lib/algorithmable/data_structs/linked_list/impl.rb
|
147
147
|
- lib/algorithmable/data_structs/linked_list/node.rb
|
148
|
+
- lib/algorithmable/data_structs/queue.rb
|
148
149
|
- lib/algorithmable/graphs.rb
|
149
150
|
- lib/algorithmable/graphs/traversals.rb
|
150
151
|
- lib/algorithmable/graphs/traversals/breadth_first.rb
|