depq 0.4 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -0
- data/lib/depq.rb +71 -39
- data/test/test-depq.rb +12 -6
- metadata +29 -33
data/README
CHANGED
@@ -21,6 +21,7 @@ depq is double-ended stable priority queue with priority update operation implem
|
|
21
21
|
* ((<reference manual|URL:http://depq.rubyforge.org/rdoc/classes/Depq.html>))
|
22
22
|
* ((<home page on rubyforge|URL:http://depq.rubyforge.org/>))
|
23
23
|
* ((<project info on rubyforge|URL:http://rubyforge.org/projects/depq/>))
|
24
|
+
* ((<depq on gemcutter|URL:http://gemcutter.org/gems/depq>))
|
24
25
|
* ((<source repository on github|URL:http://github.com/akr/depq>))
|
25
26
|
* ((<raa entry|URL:http://raa.ruby-lang.org/project/depq/>))
|
26
27
|
|
data/lib/depq.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# depq.rb - Double-Ended Priority Queue.
|
2
2
|
#
|
3
|
-
# Copyright (C) 2009 Tanaka Akira <akr@fsij.org>
|
4
|
-
#
|
3
|
+
# Copyright (C) 2009-2011 Tanaka Akira <akr@fsij.org>
|
4
|
+
#
|
5
5
|
# Redistribution and use in source and binary forms, with or without
|
6
6
|
# modification, are permitted provided that the following conditions are met:
|
7
|
-
#
|
7
|
+
#
|
8
8
|
# 1. Redistributions of source code must retain the above copyright notice, this
|
9
9
|
# list of conditions and the following disclaimer.
|
10
10
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
@@ -12,7 +12,7 @@
|
|
12
12
|
# and/or other materials provided with the distribution.
|
13
13
|
# 3. The name of the author may not be used to endorse or promote products
|
14
14
|
# derived from this software without specific prior written permission.
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17
17
|
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18
18
|
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
@@ -34,7 +34,7 @@
|
|
34
34
|
# * priority - you can get a value with minimum priority
|
35
35
|
# * double-ended - you can get a value with maximum priority too
|
36
36
|
# * stable - you will get the value inserted first with minimum/maximum priority
|
37
|
-
# * priority
|
37
|
+
# * priority change - you can change the priority of a inserted value. (usable for Dijkstra's shortest path algorithm and various graph algorithms)
|
38
38
|
# * implicit heap - compact heap representation using array. most operations are O(log n) at worst
|
39
39
|
# * iterator operations: nlargest, nsmallest and merge
|
40
40
|
#
|
@@ -420,9 +420,9 @@ class Depq
|
|
420
420
|
# class Cmp
|
421
421
|
# def call(a,b) a.casecmp(b) end
|
422
422
|
# end
|
423
|
-
# q = Depq.new(Cmp.new)
|
424
|
-
# q.insert "Foo"
|
425
|
-
# q.insert "bar"
|
423
|
+
# q = Depq.new(Cmp.new)
|
424
|
+
# q.insert "Foo"
|
425
|
+
# q.insert "bar"
|
426
426
|
# p q.delete_min #=> "bar"
|
427
427
|
# p q.delete_min #=> "Foo"
|
428
428
|
#
|
@@ -785,12 +785,12 @@ class Depq
|
|
785
785
|
#
|
786
786
|
# This method doesn't delete the element from the queue.
|
787
787
|
#
|
788
|
-
# q = Depq.new
|
789
|
-
# p q.find_min_locator #=> nil
|
790
|
-
# q.insert 3
|
791
|
-
# q.insert 1
|
792
|
-
# q.insert 2
|
793
|
-
# p q.find_min_locator #=> #<Depq::Locator: 1>
|
788
|
+
# q = Depq.new
|
789
|
+
# p q.find_min_locator #=> nil
|
790
|
+
# q.insert 3
|
791
|
+
# q.insert 1
|
792
|
+
# q.insert 2
|
793
|
+
# p q.find_min_locator #=> #<Depq::Locator: 1>
|
794
794
|
# p q.find_min_locator #=> #<Depq::Locator: 1>
|
795
795
|
# p q.delete_min #=> 1
|
796
796
|
# p q.find_min_locator #=> #<Depq::Locator: 2>
|
@@ -827,12 +827,12 @@ class Depq
|
|
827
827
|
#
|
828
828
|
# This method doesn't delete the element from the queue.
|
829
829
|
#
|
830
|
-
# q = Depq.new
|
831
|
-
# p q.find_min #=> nil
|
832
|
-
# q.insert 3
|
833
|
-
# q.insert 1
|
834
|
-
# q.insert 2
|
835
|
-
# p q.find_min #=> 1
|
830
|
+
# q = Depq.new
|
831
|
+
# p q.find_min #=> nil
|
832
|
+
# q.insert 3
|
833
|
+
# q.insert 1
|
834
|
+
# q.insert 2
|
835
|
+
# p q.find_min #=> 1
|
836
836
|
# p q.find_min #=> 1
|
837
837
|
# p q.delete_min #=> 1
|
838
838
|
# p q.find_min #=> 2
|
@@ -952,12 +952,16 @@ class Depq
|
|
952
952
|
#
|
953
953
|
def delete_locator(loc)
|
954
954
|
check_locator(loc)
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
955
|
+
index = loc.send(:index)
|
956
|
+
if @heapsize <= index
|
957
|
+
_, priority, subpriority = get_entry(index)
|
958
|
+
last = self.size - 1
|
959
|
+
if index != last
|
960
|
+
loc2, priority2, subpriority2 = get_entry(last)
|
961
|
+
set_entry(index, loc2, priority2, subpriority2)
|
962
|
+
loc2.send(:index=, index)
|
963
|
+
end
|
964
|
+
delete_entry(last)
|
961
965
|
loc.send(:internal_deleted, priority, subpriority)
|
962
966
|
loc
|
963
967
|
else
|
@@ -1173,7 +1177,7 @@ class Depq
|
|
1173
1177
|
# q.insert 4
|
1174
1178
|
# q.insert 3
|
1175
1179
|
# p q.min #=> 2
|
1176
|
-
# q.replace_min(5)
|
1180
|
+
# q.replace_min(5)
|
1177
1181
|
# p q.delete_min #=> 3
|
1178
1182
|
# p q.delete_min #=> 4
|
1179
1183
|
# p q.delete_min #=> 5
|
@@ -1275,34 +1279,48 @@ class Depq
|
|
1275
1279
|
nil
|
1276
1280
|
end
|
1277
1281
|
|
1282
|
+
# :call-seq:
|
1283
|
+
# Depq.nlargest(n, iter)
|
1284
|
+
# Depq.nlargest(n, iter) {|e| order }
|
1285
|
+
#
|
1278
1286
|
# returns the largest n elements in iter as an array.
|
1279
1287
|
#
|
1280
1288
|
# The result array is ordered from the minimum element.
|
1281
1289
|
#
|
1282
1290
|
# p Depq.nlargest(3, [5, 2, 3, 1, 4, 6, 7]) #=> [5, 6, 7]
|
1283
1291
|
#
|
1292
|
+
# If the block is given, the elements are compared by
|
1293
|
+
# the corresponding block values.
|
1294
|
+
#
|
1295
|
+
# p Depq.nlargest(3, [5, 2, 3, 1, 4, 6, 7]) {|e| -e } #=> [3, 2, 1]
|
1296
|
+
#
|
1284
1297
|
def Depq.nlargest(n, iter)
|
1285
1298
|
limit = (n * Math.log(1+n)).ceil
|
1286
1299
|
limit = 1024 if limit < 1024
|
1287
1300
|
q = Depq.new
|
1288
1301
|
threshold = nil
|
1289
|
-
iter.each {|
|
1302
|
+
iter.each {|e|
|
1303
|
+
if block_given?
|
1304
|
+
v = yield e
|
1305
|
+
else
|
1306
|
+
v = e
|
1307
|
+
end
|
1290
1308
|
if q.size < n
|
1291
1309
|
if q.size == 0
|
1292
1310
|
threshold = v
|
1293
1311
|
else
|
1294
1312
|
threshold = v if (v <=> threshold) < 0
|
1295
1313
|
end
|
1296
|
-
q.insert v
|
1314
|
+
q.insert e, v
|
1297
1315
|
else
|
1298
1316
|
if (v <=> threshold) > 0
|
1299
|
-
q.insert v
|
1317
|
+
q.insert e, v
|
1300
1318
|
if limit < q.size
|
1301
1319
|
tmp = []
|
1302
|
-
n.times { tmp << q.
|
1320
|
+
n.times { tmp << q.delete_max_locator }
|
1303
1321
|
q.clear
|
1304
|
-
q.
|
1305
|
-
threshold = tmp.last
|
1322
|
+
tmp.each {|loc| q.insert_locator loc }
|
1323
|
+
threshold = tmp.last.priority
|
1306
1324
|
end
|
1307
1325
|
end
|
1308
1326
|
end
|
@@ -1314,34 +1332,48 @@ class Depq
|
|
1314
1332
|
a
|
1315
1333
|
end
|
1316
1334
|
|
1335
|
+
# :call-seq:
|
1336
|
+
# Depq.nsmallest(n, iter)
|
1337
|
+
# Depq.nsmallest(n, iter) {|e| order }
|
1338
|
+
#
|
1317
1339
|
# returns the smallest n elements in iter as an array.
|
1318
1340
|
#
|
1319
1341
|
# The result array is ordered from the minimum element.
|
1320
1342
|
#
|
1321
1343
|
# p Depq.nsmallest(5, [5, 2, 3, 1, 4, 6, 7]) #=> [1, 2, 3, 4, 5]
|
1322
1344
|
#
|
1345
|
+
# If the block is given, the elements are compared by
|
1346
|
+
# the corresnponding block values.
|
1347
|
+
#
|
1348
|
+
# p Depq.nsmallest(5, [5, 2, 3, 1, 4, 6, 7]) {|e| -e } #=> [7, 6, 5, 4, 3]
|
1349
|
+
#
|
1323
1350
|
def Depq.nsmallest(n, iter)
|
1324
1351
|
limit = (n * Math.log(1+n)).ceil
|
1325
1352
|
limit = 1024 if limit < 1024
|
1326
1353
|
q = Depq.new
|
1327
1354
|
threshold = nil
|
1328
|
-
iter.each {|
|
1355
|
+
iter.each {|e|
|
1356
|
+
if block_given?
|
1357
|
+
v = yield e
|
1358
|
+
else
|
1359
|
+
v = e
|
1360
|
+
end
|
1329
1361
|
if q.size < n
|
1330
1362
|
if q.size == 0
|
1331
1363
|
threshold = v
|
1332
1364
|
else
|
1333
1365
|
threshold = v if (v <=> threshold) > 0
|
1334
1366
|
end
|
1335
|
-
q.insert v
|
1367
|
+
q.insert e, v
|
1336
1368
|
else
|
1337
1369
|
if (v <=> threshold) < 0
|
1338
|
-
q.insert v
|
1370
|
+
q.insert e, v
|
1339
1371
|
if limit < q.size
|
1340
1372
|
tmp = []
|
1341
|
-
n.times { tmp << q.
|
1373
|
+
n.times { tmp << q.delete_min_locator }
|
1342
1374
|
q.clear
|
1343
|
-
q.
|
1344
|
-
threshold = tmp.last
|
1375
|
+
tmp.each {|loc| q.insert_locator loc }
|
1376
|
+
threshold = tmp.last.priority
|
1345
1377
|
end
|
1346
1378
|
end
|
1347
1379
|
end
|
data/test/test-depq.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# test-depq.rb - test for depq.rb
|
2
2
|
#
|
3
3
|
# Copyright (C) 2009 Tanaka Akira <akr@fsij.org>
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Redistribution and use in source and binary forms, with or without
|
6
6
|
# modification, are permitted provided that the following conditions are met:
|
7
|
-
#
|
7
|
+
#
|
8
8
|
# 1. Redistributions of source code must retain the above copyright notice, this
|
9
9
|
# list of conditions and the following disclaimer.
|
10
10
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
@@ -12,7 +12,7 @@
|
|
12
12
|
# and/or other materials provided with the distribution.
|
13
13
|
# 3. The name of the author may not be used to endorse or promote products
|
14
14
|
# derived from this software without specific prior written permission.
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
17
17
|
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
18
18
|
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
@@ -92,7 +92,7 @@ class Depq
|
|
92
92
|
if loc.send(:index) != i
|
93
93
|
raise "index mismatch"
|
94
94
|
end
|
95
|
-
unless self.equal? loc.depq
|
95
|
+
unless self.equal? loc.depq
|
96
96
|
raise "depq mismatch"
|
97
97
|
end
|
98
98
|
i += 1
|
@@ -401,13 +401,13 @@ class TestDepq < Test::Unit::TestCase
|
|
401
401
|
q.insert "bar"
|
402
402
|
assert_equal("Foo", q.delete_min)
|
403
403
|
assert_equal("bar", q.delete_min)
|
404
|
-
|
404
|
+
|
405
405
|
q = Depq.new(:casecmp)
|
406
406
|
q.insert "Foo"
|
407
407
|
q.insert "bar"
|
408
408
|
assert_equal("bar", q.delete_min)
|
409
409
|
assert_equal("Foo", q.delete_min)
|
410
|
-
|
410
|
+
|
411
411
|
q = Depq.new(lambda {|a,b| a.casecmp(b) })
|
412
412
|
q.insert "Foo"
|
413
413
|
q.insert "bar"
|
@@ -1030,6 +1030,9 @@ class TestDepq < Test::Unit::TestCase
|
|
1030
1030
|
a = Depq.nlargest(3, [5, 1, 3, 2, 4, 6, 7])
|
1031
1031
|
assert_equal([5, 6, 7], a)
|
1032
1032
|
|
1033
|
+
a = Depq.nlargest(3, [5, 1, 3, 2, 4, 6, 7]) {|e| -e }
|
1034
|
+
assert_equal([3, 2, 1], a)
|
1035
|
+
|
1033
1036
|
assert_equal([1,2], Depq.nlargest(3, [1,2]))
|
1034
1037
|
|
1035
1038
|
a = []
|
@@ -1042,6 +1045,9 @@ class TestDepq < Test::Unit::TestCase
|
|
1042
1045
|
a = Depq.nsmallest(5, [5, 2, 3, 1, 4, 6, 7])
|
1043
1046
|
assert_equal([1, 2, 3, 4, 5], a)
|
1044
1047
|
|
1048
|
+
a = Depq.nsmallest(5, [5, 2, 3, 1, 4, 6, 7]) {|e| -e }
|
1049
|
+
assert_equal([7, 6, 5, 4, 3], a)
|
1050
|
+
|
1045
1051
|
assert_equal([1,2], Depq.nsmallest(3, [1,2]))
|
1046
1052
|
|
1047
1053
|
a = []
|
metadata
CHANGED
@@ -1,61 +1,57 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: depq
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.5'
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Tanaka Akira
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2009-11-29 00:00:00 +09:00
|
13
|
-
default_executable:
|
12
|
+
date: 2011-10-27 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
14
|
+
description: ! 'depq is a Double-Ended Priority Queue library.
|
15
|
+
|
16
|
+
It is a data structure which can insert elements and delete elements with minimum
|
17
|
+
and maximum priority.
|
15
18
|
|
16
|
-
description: |
|
17
|
-
depq is a Double-Ended Priority Queue library.
|
18
|
-
It is a data structure which can insert elements and delete elements with minimum and maximum priority.
|
19
19
|
If there are elements which has same priority, the element inserted first is chosen.
|
20
|
+
|
20
21
|
The priority can be changed after the element is inserted.
|
21
22
|
|
23
|
+
'
|
22
24
|
email: akr@fsij.org
|
23
25
|
executables: []
|
24
|
-
|
25
26
|
extensions: []
|
26
|
-
|
27
27
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
files:
|
28
|
+
files:
|
30
29
|
- README
|
31
30
|
- lib/depq.rb
|
32
|
-
|
31
|
+
- test/test-depq.rb
|
33
32
|
homepage: http://depq.rubyforge.org/
|
34
33
|
licenses: []
|
35
|
-
|
36
34
|
post_install_message:
|
37
35
|
rdoc_options: []
|
38
|
-
|
39
|
-
require_paths:
|
36
|
+
require_paths:
|
40
37
|
- lib
|
41
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
53
50
|
requirements: []
|
54
|
-
|
55
51
|
rubyforge_project: depq
|
56
|
-
rubygems_version: 1.
|
52
|
+
rubygems_version: 1.8.11
|
57
53
|
signing_key:
|
58
54
|
specification_version: 3
|
59
55
|
summary: Stable Double-Ended Priority Queue.
|
60
|
-
test_files:
|
56
|
+
test_files:
|
61
57
|
- test/test-depq.rb
|