depq 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,54 +24,63 @@
24
24
  # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
25
  # OF SUCH DAMAGE.
26
26
 
27
- require './depq'
27
+ require 'depq'
28
28
  require 'test/unit'
29
29
 
30
30
  class Depq
31
- module SimpleHeap
32
- def validation(q, ary)
33
- 0.upto(size(ary)-1) {|i|
34
- _, x = get_entry(ary, i)
35
- j = i*2+1
36
- k = i*2+2
37
- if j < size(ary) && !upper?(q, ary, i, j)
38
- _, y = get_entry(ary, j)
39
- raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{y.inspect}=pri[#{j}]"
40
- end
41
- if k < size(ary) && !upper?(q, ary, i, k)
42
- _, z = get_entry(ary, k)
43
- raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{z.inspect}=pri[#{k}]"
44
- end
45
- }
46
- end
31
+ def mm_validation(&upper)
32
+ 0.upto(self.size-1) {|i|
33
+ _, x = get_entry(i)
34
+ j = i*2+1
35
+ k = i*2+2
36
+ if j < self.size && !upper.call(i, j)
37
+ _, y = get_entry(j)
38
+ raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{y.inspect}=pri[#{j}]"
39
+ end
40
+ if k < self.size && !upper.call(i, k)
41
+ _, z = get_entry(k)
42
+ raise "wrong binary heap: pri[#{i}]=#{x.inspect} > #{z.inspect}=pri[#{k}]"
43
+ end
44
+ }
47
45
  end
48
46
 
49
- def IntervalHeap.validation(q, ary)
50
- range=0...size(ary)
47
+ def min_validation
48
+ mm_validation &method(:min_upper?)
49
+ end
50
+ MinMode[:validation] = :min_validation
51
+
52
+ def max_validation
53
+ mm_validation &method(:max_upper?)
54
+ end
55
+ MaxMode[:validation] = :max_validation
56
+
57
+ def itv_validation
58
+ range=0...self.size
51
59
  range.each {|j|
52
60
  imin = parent_minside(j)
53
61
  imax = parent_maxside(j)
54
62
  jmin = minside(j)
55
- if minside?(j) && range.include?(imin) && pcmp(q, ary, imin, j) > 0
63
+ if minside?(j) && range.include?(imin) && pcmp(imin, j) > 0
56
64
  raise "ary[#{imin}].priority > ary[#{j}].priority "
57
65
  end
58
- if maxside?(j) && range.include?(imax) && pcmp(q, ary, imax, j) < 0
66
+ if maxside?(j) && range.include?(imax) && pcmp(imax, j) < 0
59
67
  raise "ary[#{imax}].priority < ary[#{j}].priority "
60
68
  end
61
- if range.include?(imin) && pcmp(q, ary, imin, j) == 0 && scmp(q, ary, imin, j) > 0
69
+ if range.include?(imin) && pcmp(imin, j) == 0 && scmp(imin, j) > 0
62
70
  raise "ary[#{imin}].subpriority < ary[#{j}].subpriority "
63
71
  end
64
- if range.include?(imax) && pcmp(q, ary, imax, j) == 0 && scmp(q, ary, imax, j) > 0
72
+ if range.include?(imax) && pcmp(imax, j) == 0 && scmp(imax, j) > 0
65
73
  raise "ary[#{imax}].subpriority < ary[#{j}].subpriority "
66
74
  end
67
- if maxside?(j) && range.include?(jmin) && pcmp(q, ary, jmin, j) == 0 && scmp(q, ary, jmin, j) > 0
75
+ if maxside?(j) && range.include?(jmin) && pcmp(jmin, j) == 0 && scmp(jmin, j) > 0
68
76
  raise "ary[#{jmin}].subpriority < ary[#{j}].subpriority "
69
77
  end
70
78
  }
71
79
  end
80
+ IntervalMode[:validation] = :itv_validation
72
81
 
73
82
  def validation
74
- @mode.validation(self, @ary) if @mode
83
+ send(Mode[@mode][:validation]) if @mode
75
84
  if @ary.length % ARY_SLICE_SIZE != 0
76
85
  raise "wrong length"
77
86
  end
@@ -945,6 +954,42 @@ class TestDepq < Test::Unit::TestCase
945
954
  assert_equal(nil, q.delete_unspecified_locator)
946
955
  end
947
956
 
957
+ def test_replace_min
958
+ q = Depq.new
959
+ q.insert 1
960
+ q.insert 2
961
+ q.insert 0
962
+ assert_equal(0, q.min)
963
+ loc = q.find_min_locator
964
+ assert_equal(2, loc.subpriority)
965
+ assert_equal(3, q.totalcount)
966
+ assert_equal(loc, q.replace_min(10))
967
+ assert_equal(4, q.totalcount)
968
+ assert_equal(1, q.delete_min)
969
+ assert_equal(2, q.delete_min)
970
+ assert_equal(3, q.find_min_locator.subpriority)
971
+ assert_equal(10, q.delete_min)
972
+ assert_equal(nil, q.delete_min)
973
+ end
974
+
975
+ def test_replace_max
976
+ q = Depq.new
977
+ q.insert 3
978
+ q.insert 4
979
+ q.insert 2
980
+ assert_equal(4, q.max)
981
+ loc = q.find_max_locator
982
+ assert_equal(1, loc.subpriority)
983
+ assert_equal(3, q.totalcount)
984
+ assert_equal(loc, q.replace_max(1))
985
+ assert_equal(4, q.totalcount)
986
+ assert_equal(3, q.delete_max)
987
+ assert_equal(2, q.delete_max)
988
+ assert_equal(3, q.find_min_locator.subpriority)
989
+ assert_equal(1, q.delete_max)
990
+ assert_equal(nil, q.delete_max)
991
+ end
992
+
948
993
  def test_each
949
994
  q = Depq.new
950
995
  a = [1,2,0]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: depq
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.3"
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tanaka Akira
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-23 00:00:00 +09:00
12
+ date: 2009-11-29 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,7 +28,7 @@ extra_rdoc_files: []
28
28
 
29
29
  files:
30
30
  - README
31
- - depq.rb
31
+ - lib/depq.rb
32
32
  has_rdoc: true
33
33
  homepage: http://depq.rubyforge.org/
34
34
  licenses: []
@@ -37,7 +37,7 @@ post_install_message:
37
37
  rdoc_options: []
38
38
 
39
39
  require_paths:
40
- - .
40
+ - lib
41
41
  required_ruby_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
43
  - - ">="
@@ -58,4 +58,4 @@ signing_key:
58
58
  specification_version: 3
59
59
  summary: Stable Double-Ended Priority Queue.
60
60
  test_files:
61
- - test-depq.rb
61
+ - test/test-depq.rb