Class: Eg::BinaryChop

Inherits:
Fit::ColumnFixture show all
Defined in:
lib/eg/binary_chop.rb

Constant Summary

Constants inherited from Fit::Fixture

Fit::Fixture::GRAY, Fit::Fixture::GREEN, Fit::Fixture::RED, Fit::Fixture::YELLOW

Instance Attribute Summary collapse

Attributes inherited from Fit::Fixture

#args, #counts, #listener, #summary

Instance Method Summary collapse

Methods inherited from Fit::ColumnFixture

#check, #do_cell, #do_row, #do_rows, #reset

Methods inherited from Fit::Fixture

camel, #check, #do_cell, #do_cells, #do_row, #do_rows, #do_table, #do_tables, #error, escape, #exception, #find_class, #fixture_name, #get_args_for_table, #get_linked_fixture_with_args, gray, #ignore, #info, #initialize, #interpret_following_tables, #interpret_tables, label, metadata, #parse, #right, #total_errors, #totals, #wrong

Constructor Details

This class inherits a constructor from Fit::Fixture

Instance Attribute Details

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/eg/binary_chop.rb', line 10

def key
  @key
end

Instance Method Details

#arrayObject



23
# File 'lib/eg/binary_chop.rb', line 23

def array; @array; end

#array=(value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/eg/binary_chop.rb', line 16

def array= value
  unless value.kind_of? Array
    @array = [value]
  else
    @array = value
  end
end

#chop_friday(key, array) ⇒ Object



93
94
95
96
# File 'lib/eg/binary_chop.rb', line 93

def chop_friday key, array
  array.each_with_index {|e, i| return i if key == e}
  -1
end

#chop_monday(key, array) ⇒ Object

Search methods



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eg/binary_chop.rb', line 37

def chop_monday key, array
  min = 0
  max = array.size - 1
  while min <= max
    probe = (min + max) / 2
    if key == array[probe]
      return probe
    elsif key > array[probe]
      min = probe + 1
    else
      max = probe - 1
    end
  end
  -1
end

#chop_thursday(key, array) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eg/binary_chop.rb', line 77

def chop_thursday key, array
  min = 0
  max = array.size - 1
  while min <= max
    probe = (rand * (max - min) + min).to_i
    if key == array[probe]
      return probe
    elsif key > array[probe]
      min = probe + 1
    else
      max = probe - 1
    end
  end
  -1
end

#chop_tuesday(key, array) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eg/binary_chop.rb', line 53

def chop_tuesday key, array
  min = 0
  max = array.size - 1
  while min <= max
    probe = (min + max) / 2
    case key <=> array[probe]
      when 0 then return probe
      when 1 then min = probe + 1
      when -1 then max = probe - 1
      else raise "Unexpected result from <=>"
    end
  end
  -1
end

#chop_wednesday(key, array) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/eg/binary_chop.rb', line 68

def chop_wednesday key, array
  return -1 if array.size.zero?
  probe = array.size / 2
  return probe if key == array[probe]
  return chop_wednesday(key, array[0, probe]) if key < array[probe]
  result = chop_wednesday(key, array[(probe + 1)..-1])
  return (result < 0) ? result : result + probe + 1
end

#executeObject



12
13
14
# File 'lib/eg/binary_chop.rb', line 12

def execute
  @array = [] if @array.nil?
end

#friObject



33
# File 'lib/eg/binary_chop.rb', line 33

def fri; chop_friday(key, array); end

#monObject



29
# File 'lib/eg/binary_chop.rb', line 29

def mon; chop_monday(key, array); end

#resultObject



25
26
27
# File 'lib/eg/binary_chop.rb', line 25

def result
  chop_friday key, array
end

#thrObject



32
# File 'lib/eg/binary_chop.rb', line 32

def thr; chop_thursday(key, array); end

#tueObject



30
# File 'lib/eg/binary_chop.rb', line 30

def tue; chop_tuesday(key, array); end

#wedObject



31
# File 'lib/eg/binary_chop.rb', line 31

def wed; chop_wednesday(key, array); end