priority_queue_cxx17 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,245 @@
1
+ # Copyright (c) 2014 Roberto Esposito
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ require "minitest/autorun"
22
+ require "fc"
23
+
24
+ class TestFastContainers < MiniTest::Unit::TestCase
25
+ def test_new_object_creation
26
+ assert !FastContainers::PriorityQueue.new(:max).nil?
27
+ end
28
+
29
+ def test_new_handling_of_bad_parameter
30
+ assert_raises(TypeError) do
31
+ FastContainers::PriorityQueue.new(true)
32
+ end
33
+ end
34
+
35
+ def test_push_returns_self
36
+ pq = FastContainers::PriorityQueue.new(:max)
37
+ assert_equal pq, pq.push("test",10)
38
+ end
39
+
40
+ def test_top_on_a_single_element_queue_returns_that_element
41
+ pq = FastContainers::PriorityQueue.new(:max);
42
+ pq.push("test",10)
43
+ assert_equal "test", pq.top
44
+ end
45
+
46
+ def test_next_on_a_single_element_queue_returns_that_element
47
+ pq = FastContainers::PriorityQueue.new(:max);
48
+ pq.push("test",10)
49
+ assert_equal "test", pq.next
50
+ end
51
+
52
+
53
+ def test_top_returns_the_maximal_element_in_a_max_queue
54
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
55
+ pq.push("10", 10)
56
+ pq.push("30", 30)
57
+ pq.push("20", 20)
58
+ assert_equal "30", pq.top
59
+ end
60
+
61
+ def test_next_returns_the_maximal_element_in_a_max_queue
62
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
63
+ pq.push("10", 10)
64
+ pq.push("30", 30)
65
+ pq.push("20", 20)
66
+ assert_equal "30", pq.next
67
+ end
68
+
69
+
70
+ def test_top_returns_the_minimal_element_in_a_min_queue
71
+ pq = FastContainers::PriorityQueue.new(:min) # this is a max queue
72
+ pq.push("10", 10)
73
+ pq.push("30", 30)
74
+ pq.push("20", 20)
75
+ assert_equal "10", pq.top
76
+ end
77
+
78
+ def test_next_returns_the_minimal_element_in_a_min_queue
79
+ pq = FastContainers::PriorityQueue.new(:min) # this is a max queue
80
+ pq.push("10", 10)
81
+ pq.push("30", 30)
82
+ pq.push("20", 20)
83
+ assert_equal "10", pq.next
84
+ end
85
+
86
+ def test_top_key_returns_the_top_priority
87
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
88
+ pq.push("10", 10)
89
+ pq.push("30", 30)
90
+ pq.push("20", 20)
91
+ assert_equal 30, pq.top_key
92
+ end
93
+
94
+ def test_next_key_returns_the_top_priority
95
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
96
+ pq.push("10", 10)
97
+ pq.push("30", 30)
98
+ pq.push("20", 20)
99
+ assert_equal 30, pq.next_key
100
+ end
101
+
102
+
103
+ def test_pop_removes_an_element_from_the_top
104
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
105
+ pq.push("10", 10)
106
+ pq.push("30", 30)
107
+ pq.push("20", 20)
108
+ extracted = pq.pop
109
+
110
+ assert_equal 20, pq.top_key
111
+ assert_equal "30", extracted
112
+ end
113
+
114
+ def test_pop_raises_an_exception_if_the_queue_is_empty
115
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
116
+ pq.push("10", 10)
117
+ pq.pop
118
+ assert_raises(RuntimeError) {
119
+ pq.pop
120
+ }
121
+ end
122
+
123
+ def test_empty_returns_false_if_the_queue_is_not_empty
124
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
125
+ pq.push("10", 10)
126
+ assert !pq.empty?
127
+ end
128
+
129
+ def test_empty_returns_true_if_the_queue_is_empty
130
+ pq = FastContainers::PriorityQueue.new(:max) # this is a max queue
131
+ assert pq.empty?
132
+ end
133
+
134
+ def test_size_on_empty_queue
135
+ pq = FastContainers::PriorityQueue.new(:max)
136
+ assert_equal 0, pq.size
137
+ end
138
+
139
+ def test_size_on_non_empty_queue
140
+ pq = FastContainers::PriorityQueue.new(:max)
141
+ pq.push("x",10);
142
+ pq.push("y",20);
143
+ pq.push("z",30);
144
+ assert_equal 3, pq.size
145
+ end
146
+
147
+ def test_enumerable
148
+ pq = FastContainers::PriorityQueue.new(:max)
149
+ pq.push(1,10);
150
+ pq.push(2,20);
151
+ pq.push(3,30);
152
+ sum_o = 0
153
+ sum_p = 0
154
+ pq.map { |o,p| sum_o+=o; sum_p+=p }
155
+ assert_equal 6, sum_o
156
+ assert_equal 60, sum_p
157
+ end
158
+
159
+ def test_top_key_on_empty_queues
160
+ pq = FastContainers::PriorityQueue.new(:max)
161
+ assert_nil pq.top_key
162
+ end
163
+
164
+ def test_top_on_empty_queues
165
+ pq = FastContainers::PriorityQueue.new(:max)
166
+ assert_nil pq.top
167
+ end
168
+
169
+ def test_second_best_key
170
+ pq = FastContainers::PriorityQueue.new(:max)
171
+ pq.push("x", 100);
172
+ pq.push("y", 80);
173
+ pq.push("z", 40);
174
+ pq.push("w", 60);
175
+ pq.push("i", 90);
176
+ pq.push("j", 95);
177
+
178
+ assert_equal pq.second_best_key, 95
179
+ end
180
+
181
+ def test_second_best_key_on_empty_pq
182
+ pq = FastContainers::PriorityQueue.new(:max)
183
+ assert_nil pq.second_best_key
184
+ end
185
+
186
+ def test_second_best_key_on_size_1_pq
187
+ pq = FastContainers::PriorityQueue.new(:max)
188
+ pq.push("x", 100)
189
+ assert_nil pq.second_best_key
190
+ end
191
+
192
+ def test_second_best_key_on_size_2_pq
193
+ pq = FastContainers::PriorityQueue.new(:max)
194
+ pq.push("x", 100)
195
+ pq.push("x", 80)
196
+
197
+ assert_equal 80, pq.second_best_key
198
+ end
199
+
200
+ def test_each_will_iterate_over_all_elements
201
+ pq = FastContainers::PriorityQueue.new(:max);
202
+ pq.push("x", 100);
203
+ pq.push("y", 80);
204
+ pq.push("z", 40);
205
+ pq.push("w", 60);
206
+ pq.push("i", 90);
207
+ pq.push("j", 95);
208
+
209
+ objects = Set.new
210
+
211
+ result = pq.each do |obj, priority|
212
+ objects << obj
213
+ end
214
+
215
+ assert_equal objects, Set.new(["x","y","z","w","i","j"])
216
+ assert_equal pq, result
217
+ end
218
+
219
+ def test_each_will_abort_and_return_nil_if_queue_changes
220
+ pq = FastContainers::PriorityQueue.new(:max);
221
+ pq.push("x", 100);
222
+ pq.push("y", 80);
223
+ pq.push("z", 40);
224
+ pq.push("w", 60);
225
+ pq.push("i", 90);
226
+ pq.push("j", 95);
227
+
228
+ objects = Set.new
229
+
230
+ exception = assert_raises RuntimeError do
231
+ count = 0
232
+ pq.each do |obj, priority|
233
+ if count==3
234
+ pq.push("no way!", 90)
235
+ end
236
+
237
+ count+=1
238
+ objects << obj
239
+ end
240
+ end
241
+
242
+ assert_match /a change in the priority queue invalidated the current iterator/, exception.message
243
+ assert_equal 4, objects.size
244
+ end
245
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: priority_queue_cxx17
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.5
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Esposito, Hugo Jiménez Hernández
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Fast priority queue implementation (c++ wrapper, see README.md for a
28
+ comparison with other libraries)
29
+ email:
30
+ - boborbt@gmail.com, hjimenezhdez@gmail.com
31
+ executables: []
32
+ extensions:
33
+ - ext/fast_containers/extconf.rb
34
+ extra_rdoc_files:
35
+ - README.md
36
+ files:
37
+ - README.md
38
+ - ext/fast_containers/FastContainers.cpp
39
+ - ext/fast_containers/extconf.rb
40
+ - ext/fast_containers/fc_pq.cpp
41
+ - ext/fast_containers/fc_pq.h
42
+ - lib/fc.rb
43
+ - test/performance/test_fc_vs_algorithms.rb
44
+ - test/performance/test_fc_vs_cpriority_queue.rb
45
+ - test/performance/test_fc_vs_em_priority_queue.rb
46
+ - test/performance/test_fc_vs_pqueue.rb
47
+ - test/performance/test_fc_vs_priority_queue.rb
48
+ - test/test_fast_containers.rb
49
+ homepage: https://github.com/HugoJH/priority_queue_cxx
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options:
55
+ - "--main"
56
+ - README.md
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Fast (c++ wrapper) priority queue implementation for ruby.
74
+ test_files: []