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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1181d5287c079dc0321f3259c5ecd5d6dff80828
4
- data.tar.gz: d12ff3b96482603ff93812d535c0f1f755da613f
3
+ metadata.gz: 40e70c3fb85f8914dade38055945669ecaf6f950
4
+ data.tar.gz: 57c983790d111dd5169bdaae1a60070766773808
5
5
  SHA512:
6
- metadata.gz: 4d7d1d20304d7d9d8031c42ffb7b66fb21974c0443fd96b7b9296d2ef13542e1964c20d47cfe9817f35cbe29154f7daafbea504de7e387424704e990ce4c13c3
7
- data.tar.gz: 440087c7477c51cf434951b5b584e6a8647451d6b90b5f451fb4b7122b7b2b7cc13bd13e0509cdd3ce146331390067c634e327806919f5758ab8a395f4056c47
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
@@ -2,6 +2,7 @@ module Algorithmable
2
2
  module DataStructs
3
3
  autoload :Bag, 'algorithmable/data_structs/bag'
4
4
  autoload :LinkedList, 'algorithmable/data_structs/linked_list'
5
+ autoload :Queue, 'algorithmable/data_structs/queue'
5
6
 
6
7
  class << self
7
8
  def new_bag
@@ -1,3 +1,3 @@
1
1
  module Algorithmable
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  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.5.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