algorithmable 0.4.0 → 0.5.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: b0dedcc11bcfa5814e48d25ba1090a2495e187b1
4
- data.tar.gz: 79d772b02883f4550b8abdc3b48afe7c60e96d89
3
+ metadata.gz: 1181d5287c079dc0321f3259c5ecd5d6dff80828
4
+ data.tar.gz: d12ff3b96482603ff93812d535c0f1f755da613f
5
5
  SHA512:
6
- metadata.gz: 5e8f1d7d60d1bcba3a293c9860516a9bf42d1ac847e2bd7f532f9b33a649b45e89125d1e645c8735d16b6b77d6a0f659da36adbd8d133a22b64b2c9b1529deb9
7
- data.tar.gz: b47c85b9d228aef4ebea3d03fe5eefa4a2c41951eb94438b902835dadf1c437270a58dd125c0b1c5ed6e1d78e4e42098ea47dc8fe1c27844b6bd6c9ae7571f7a
6
+ metadata.gz: 4d7d1d20304d7d9d8031c42ffb7b66fb21974c0443fd96b7b9296d2ef13542e1964c20d47cfe9817f35cbe29154f7daafbea504de7e387424704e990ce4c13c3
7
+ data.tar.gz: 440087c7477c51cf434951b5b584e6a8647451d6b90b5f451fb4b7122b7b2b7cc13bd13e0509cdd3ce146331390067c634e327806919f5758ab8a395f4056c47
@@ -0,0 +1,44 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ class Bag
4
+ include Enumerable
5
+ attr_reader :size, :head
6
+
7
+ def initialize
8
+ @head = nil
9
+ @size = 0
10
+ end
11
+
12
+ def empty?
13
+ @head.nil?
14
+ end
15
+
16
+ def add(item)
17
+ old_head = @head
18
+ @head = Node.new(item)
19
+ @head.succ = old_head
20
+ @size = @size.next
21
+ end
22
+
23
+ def each(&block)
24
+ @head.each(&block) if @head
25
+ end
26
+
27
+ private
28
+
29
+ class Node
30
+ attr_accessor :succ, :item
31
+
32
+ def initialize(item)
33
+ @item = item
34
+ @succ = nil
35
+ end
36
+
37
+ def each(&block)
38
+ yield self
39
+ @succ.each(&block) if @succ
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,102 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module LinkedList
4
+ class Impl
5
+ include Enumerable
6
+ attr_reader :length
7
+
8
+ def initialize
9
+ @head = nil
10
+ @tail = nil
11
+ @length = 0
12
+ @op_counter = 0
13
+ end
14
+
15
+ def unshift(data)
16
+ link_head_node data
17
+ end
18
+
19
+ def push(data)
20
+ link_tail_node data
21
+ end
22
+
23
+ def first
24
+ @head ? @head.dup : fail_index_error
25
+ end
26
+
27
+ def last
28
+ @tail ? @tail.dup : fail_index_error
29
+ end
30
+
31
+ def each(&block)
32
+ next_node = @head
33
+ nodes = []
34
+ while next_node
35
+ nodes << next_node
36
+ next_node = next_node.succ
37
+ end
38
+ nodes.each(&block)
39
+ end
40
+
41
+ private
42
+
43
+ def link_head_node(data)
44
+ prev_head = @head
45
+ node = make_node data, nil, prev_head
46
+ @head = node
47
+
48
+ if prev_head
49
+ prev_head.pred = node
50
+ else
51
+ @tail = node
52
+ end
53
+
54
+ bump_length!
55
+ bump_op_counter!
56
+ end
57
+
58
+ def link_tail_node(data)
59
+ prev_tail = @tail
60
+ node = make_node data, prev_tail, nil
61
+ @tail = node
62
+
63
+ if prev_tail
64
+ prev_tail.succ = node
65
+ else
66
+ @head = node
67
+ end
68
+
69
+ bump_length!
70
+ bump_op_counter!
71
+ end
72
+
73
+ def make_node(data, pred = nil, succ = nil)
74
+ Node.new data, pred, succ
75
+ end
76
+
77
+ def bump_length!
78
+ @length = @length.next
79
+ end
80
+
81
+ def bump_op_counter!
82
+ @op_counter = @op_counter.next
83
+ end
84
+
85
+ def fail_index_error(message = nil)
86
+ fail IndexError, message
87
+ end
88
+
89
+ class Node
90
+ attr_accessor :pred, :succ
91
+ attr_reader :data
92
+
93
+ def initialize(data, pred = nil, succ = nil)
94
+ @data = data
95
+ @pred = pred
96
+ @succ = succ
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,7 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module LinkedList
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module LinkedList
4
+ autoload :Node, 'algorithmable/data_structs/linked_list/node'
5
+ autoload :Impl, 'algorithmable/data_structs/linked_list/impl'
6
+
7
+ def self.new
8
+ Impl.new
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ autoload :Bag, 'algorithmable/data_structs/bag'
4
+ autoload :LinkedList, 'algorithmable/data_structs/linked_list'
5
+
6
+ class << self
7
+ def new_bag
8
+ Bag.new
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Algorithmable
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/lib/algorithmable.rb CHANGED
@@ -6,6 +6,7 @@ require 'English'
6
6
 
7
7
  module Algorithmable
8
8
  autoload :Sort, 'algorithmable/sort'
9
+ autoload :DataStructs, 'algorithmable/data_structs'
9
10
  autoload :Graphs, 'algorithmable/graphs'
10
11
 
11
12
  class << self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algorithmable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Lishtaba
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,6 +140,11 @@ files:
140
140
  - algorithmable.gemspec
141
141
  - cucumber.yml
142
142
  - lib/algorithmable.rb
143
+ - lib/algorithmable/data_structs.rb
144
+ - lib/algorithmable/data_structs/bag.rb
145
+ - lib/algorithmable/data_structs/linked_list.rb
146
+ - lib/algorithmable/data_structs/linked_list/impl.rb
147
+ - lib/algorithmable/data_structs/linked_list/node.rb
143
148
  - lib/algorithmable/graphs.rb
144
149
  - lib/algorithmable/graphs/traversals.rb
145
150
  - lib/algorithmable/graphs/traversals/breadth_first.rb