ruby_structures 2.1.0 → 2.2.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: f84ec7afcc16ae06a9e385352868871144f62937
4
- data.tar.gz: 44ede61cd4466ccd6e54bc06b67231ecd7a3a293
3
+ metadata.gz: 3f90580fdf848a8b70513b2756ea932be5913ca0
4
+ data.tar.gz: 0bdf1a300665bfa031045f07ab5a01a9b4e17f34
5
5
  SHA512:
6
- metadata.gz: fccf6655d1f80ecf8d285e2b9598348d5d52ebee5d65170726d959daf08389ab93f19b3a951b279dd767424ead2cb55fd4e709654deb6af1c17f632496c89cb4
7
- data.tar.gz: 080193de352085e39b1054a18080cb34d6692747910f6392c3210bed15701d9645754d4ae056b2e09411c00310f854c38cc19d3c7502d1222c7884fcd6470ad0
6
+ metadata.gz: c9b4072b567a40780c949177e91bf4a26b92af651038e2eb1d39ac04ac3adb88bf0d37bb4bcf7e88f379fa8b72d6893912234c5615b6f5da1b74a1ac7cb330b3
7
+ data.tar.gz: fbac1d228a0a991f05506bbbca51177582c97612289e82015bd941dd67c9d258feab5fcb2c491c95756edf22ec608238d9d074094385af363e19000ebff4b6c1
@@ -0,0 +1,76 @@
1
+ class Heap
2
+ def initialize
3
+ @store = []
4
+ end
5
+
6
+ def peek
7
+ raise ArgumentError.new('Heap is empty') if @store.empty?
8
+ @store.first
9
+ end
10
+
11
+ def insert(el)
12
+ @store << el
13
+
14
+ el_idx = @store.length - 1
15
+ parent_idx = parent_idx(el_idx)
16
+
17
+ heapify_up(el_idx, parent_idx)
18
+
19
+ el
20
+ end
21
+
22
+ def extract
23
+ raise ArgumentError.new('Heap is empty') if @store.empty?
24
+ return @store.shift if @store.length <= 2
25
+
26
+ @store[0], @store[-1] = @store[-1], @store[0]
27
+ head = @store.pop
28
+
29
+ el_idx = 0
30
+ children_indices = children_indices(el_idx)
31
+
32
+ heapify_down(el_idx, children_indices)
33
+
34
+ head
35
+ end
36
+
37
+ private
38
+
39
+ def heapify_up(el_idx, parent_idx)
40
+ until el_idx == 0 || @store[el_idx] >= @store[parent_idx]
41
+ @store[el_idx], @store[parent_idx] = @store[parent_idx], @store[el_idx]
42
+ el_idx = parent_idx
43
+ parent_idx = parent_idx(el_idx)
44
+ end
45
+ end
46
+
47
+ def heapify_down(el_idx, children_indices)
48
+ return if children_indices.empty?
49
+ if children_indices.length == 1
50
+ child_idx = children_indices.first
51
+ if @store[el_idx] > @store[child_idx]
52
+ @store[el_idx], @store[child_idx] = @store[child_idx], @store[el_idx]
53
+ end
54
+ return
55
+ elsif @store[el_idx] > @store[children_indices.first] || @store[el_idx] > @store[children_indices.last]
56
+ child1, child2 = @store[children_indices.first], @store[children_indices.last]
57
+ lowest_child_idx = child1 <= child2 ? children_indices.first : children_indices.last
58
+ @store[el_idx], @store[lowest_child_idx] = @store[lowest_child_idx], @store[el_idx]
59
+
60
+ children_indices = children_indices(lowest_child_idx)
61
+ heapify_down(lowest_child_idx, children_indices)
62
+ end
63
+ end
64
+
65
+ def parent_idx(idx)
66
+ (idx - 1) / 2
67
+ end
68
+
69
+ def children_indices(idx)
70
+ idx1 = idx * 2 + 1
71
+ idx2 = idx * 2 + 2
72
+ [idx1, idx2].select { |idx| @store[idx] }
73
+ end
74
+
75
+ attr_reader :store
76
+ end
@@ -37,9 +37,7 @@ class LRUCache
37
37
  @hash[key] = node
38
38
 
39
39
  if @size == @max_size
40
- first_node_key = @linked_list.first.send(:key)
41
- @linked_list.remove(first_node_key)
42
- @hash.delete(first_node_key)
40
+ remove_node(:first)
43
41
  else
44
42
  @size += 1
45
43
  end
@@ -52,9 +50,7 @@ class LRUCache
52
50
  @hash[key] = node
53
51
 
54
52
  if @size == @max_size
55
- last_node_key = @linked_list.last.send(:key)
56
- @linked_list.remove(last_node_key)
57
- @hash.delete(last_node_key)
53
+ remove_node(:last)
58
54
  else
59
55
  @size += 1
60
56
  end
@@ -67,9 +63,7 @@ class LRUCache
67
63
  @hash[key] = node
68
64
 
69
65
  if @size == @max_size
70
- first_node_key = @linked_list.first.send(:key)
71
- @linked_list.remove(first_node_key)
72
- @hash.delete(first_node_key)
66
+ remove_node(:first)
73
67
  else
74
68
  @size += 1
75
69
  end
@@ -82,9 +76,7 @@ class LRUCache
82
76
  @hash[key] = node
83
77
 
84
78
  if @size == @max_size
85
- last_node_key = @linked_list.last.send(:key)
86
- @linked_list.remove(last_node_key)
87
- @hash.delete(last_node_key)
79
+ remove_node(:last)
88
80
  else
89
81
  @size += 1
90
82
  end
@@ -105,6 +97,12 @@ class LRUCache
105
97
 
106
98
  private
107
99
 
100
+ def remove_node(position)
101
+ node_key = @linked_list.send(position).send(:key)
102
+ @linked_list.remove(node_key)
103
+ @hash.delete(node_key)
104
+ end
105
+
108
106
  attr_accessor :max_size, :size
109
107
  attr_reader :hash, :linked_list
110
108
  end
@@ -3,3 +3,4 @@ require 'data_structures/queue'
3
3
  require 'data_structures/linked_list'
4
4
  require 'data_structures/binary_tree'
5
5
  require 'data_structures/lru_cache'
6
+ require 'data_structures/heap'
metadata CHANGED
@@ -1,23 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_structures
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Numeroff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-09 00:00:00.000000000 Z
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Ruby implementations of a Stack, Queue, Linked List, Binary Tree and
14
- LRU Cache. More to come!
13
+ description: Ruby implementations of a Stack, Queue, Linked List, Binary Tree, LRU
14
+ Cache and Heap. More to come!
15
15
  email: jnumeroff@hotmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/data_structures/binary_tree.rb
21
+ - lib/data_structures/heap.rb
21
22
  - lib/data_structures/linked_list.rb
22
23
  - lib/data_structures/lru_cache.rb
23
24
  - lib/data_structures/queue.rb