ione 1.2.0.pre8 → 1.2.0.pre9

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: 7877b339794b199432492b1cdb1eee2661ea1cad
4
- data.tar.gz: fed2cbd02eda48abd7aa225740de42b039bc56cc
3
+ metadata.gz: 269fc51bb786b26f473b2eec82fb37495832842e
4
+ data.tar.gz: 7c709b42991c6879f9a0cd1df579e31b158a0d32
5
5
  SHA512:
6
- metadata.gz: 82a13a762739bb9302f600d1ea3061391432b8e8a98e294808dae0d12db7865dbeac989ead14c440cdc866e4dc29e6e2761a14eb7508d96d812147c03aa9db24
7
- data.tar.gz: 81adb0b983ace8e30f8cd27bd0061e6e230dded1ef68d46632b7a5ca2f08d87132bb32197bcfeb6a4ba8f2da06c9f394515ee7a6d972f233b235b6176f78ac58
6
+ metadata.gz: 5e984ed67e16f8dcbcd10671e3033f911b17af68eb5cb2e11231c01dd4dd83fdaff70681e928100226dafbd7fb027ebe64bc20d178a3e216945ce7bd7134a9a2
7
+ data.tar.gz: 37dc89c4553b85de563ea94caffc8d07f555e622c1ab3d00170be9dfb6c47ae0d7c754ce6eac09a586eaff3040ad065db6a05e61eca35201737f6ab397a0b96c
data/lib/ione/heap.rb CHANGED
@@ -5,6 +5,7 @@ module Ione
5
5
  class Heap
6
6
  def initialize
7
7
  @items = []
8
+ @indexes = {}
8
9
  end
9
10
 
10
11
  def size
@@ -16,8 +17,11 @@ module Ione
16
17
  end
17
18
 
18
19
  def push(item)
19
- @items << item
20
- bubble_up(@items.size - 1)
20
+ unless @indexes.include?(item)
21
+ @items << item
22
+ @indexes[item] = @items.size - 1
23
+ bubble_up(@items.size - 1)
24
+ end
21
25
  end
22
26
  alias_method :<<, :push
23
27
 
@@ -29,10 +33,14 @@ module Ione
29
33
  if @items.size == 0
30
34
  nil
31
35
  elsif @items.size == 1
32
- @items.pop
36
+ item = @items.pop
37
+ @indexes.delete(item)
38
+ item
33
39
  else
34
40
  item = @items.first
41
+ @indexes.delete(item)
35
42
  @items[0] = @items.pop
43
+ @indexes[@items[0]] = 0
36
44
  bubble_down(0)
37
45
  item
38
46
  end
@@ -42,10 +50,14 @@ module Ione
42
50
  if item == @items.first
43
51
  pop
44
52
  elsif item == @items.last
45
- @items.pop
46
- elsif (i = index(item))
53
+ item = @items.pop
54
+ @indexes.delete(item)
55
+ item
56
+ elsif (i = @indexes[item])
47
57
  item = @items[i]
58
+ @indexes.delete(item)
48
59
  @items[i] = @items.pop
60
+ @indexes[@items[i]] = i
49
61
  bubble_up(bubble_down(i))
50
62
  item
51
63
  end
@@ -53,25 +65,14 @@ module Ione
53
65
 
54
66
  private
55
67
 
56
- def index(item, root_index=0)
57
- left_index = (root_index * 2) + 1
58
- right_index = (root_index * 2) + 2
59
- root_item = @items[root_index]
60
- if root_item == item
61
- root_index
62
- elsif left_index < @items.length && item >= @items[left_index] && (i = index(item, left_index))
63
- i
64
- elsif right_index < @items.length && item >= @items[right_index] && (i = index(item, right_index))
65
- i
66
- end
67
- end
68
-
69
68
  def bubble_up(index)
70
69
  parent_index = (index - 1)/2
71
70
  if parent_index >= 0 && @items[parent_index] > @items[index]
72
71
  item = @items[index]
73
72
  @items[index] = @items[parent_index]
74
73
  @items[parent_index] = item
74
+ @indexes[@items[index]] = index
75
+ @indexes[@items[parent_index]] = parent_index
75
76
  bubble_up(parent_index)
76
77
  else
77
78
  index
@@ -80,16 +81,18 @@ module Ione
80
81
 
81
82
  def bubble_down(index)
82
83
  child_index = (index * 2) + 1
83
- if child_index >= @items.length
84
+ if child_index >= @items.size
84
85
  index
85
86
  else
86
- if child_index + 1 < @items.length && @items[child_index] > @items[child_index + 1]
87
+ if child_index + 1 < @items.size && @items[child_index] > @items[child_index + 1]
87
88
  child_index += 1
88
89
  end
89
90
  if @items[index] > @items[child_index]
90
91
  item = @items[index]
91
92
  @items[index] = @items[child_index]
92
93
  @items[child_index] = item
94
+ @indexes[@items[index]] = index
95
+ @indexes[@items[child_index]] = child_index
93
96
  bubble_down(child_index)
94
97
  else
95
98
  index
data/lib/ione/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Ione
4
- VERSION = '1.2.0.pre8'.freeze
4
+ VERSION = '1.2.0.pre9'.freeze
5
5
  end
@@ -43,13 +43,20 @@ module Ione
43
43
  heap.size.should == 4
44
44
  end
45
45
 
46
- it 'is aliase as #<<' do
46
+ it 'is aliased as #<<' do
47
47
  heap << 4
48
48
  heap << 3
49
49
  heap << 6
50
50
  heap << 5
51
51
  heap.size.should == 4
52
52
  end
53
+
54
+ it 'does not add duplicates' do
55
+ heap << 3
56
+ heap << 3
57
+ heap << 3
58
+ heap.size.should == 1
59
+ end
53
60
  end
54
61
 
55
62
  describe '#peek' do
@@ -102,17 +109,6 @@ module Ione
102
109
  heap.pop.should == 3
103
110
  heap.pop.should be_nil
104
111
  end
105
-
106
- it 'returns each duplicate' do
107
- heap.push(3)
108
- heap.push(4)
109
- heap.push(3)
110
- heap.push(3)
111
- heap.pop.should == 3
112
- heap.pop.should == 3
113
- heap.pop.should == 3
114
- heap.pop.should == 4
115
- end
116
112
  end
117
113
 
118
114
  describe '#delete' do
@@ -168,15 +164,6 @@ module Ione
168
164
  heap.push(5)
169
165
  heap.delete(6).should be_nil
170
166
  end
171
-
172
- it 'removes the first instance of the item from the heap' do
173
- heap.push(3)
174
- heap.push(3)
175
- heap.push(5)
176
- heap.delete(3).should == 3
177
- heap.delete(3).should == 3
178
- heap.size.should == 1
179
- end
180
167
  end
181
168
  end
182
169
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ione
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.pre8
4
+ version: 1.2.0.pre9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Hultberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-27 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Reactive programming framework for Ruby, painless evented IO, futures
14
14
  and an efficient byte buffer