Class: Optimizer

Inherits
Hash show all
Defined in
lib/kyanite/optimizer.rb

Optimizer

Kyanite definitions

Optimizer

Kyanite tests and examples

TestKyaniteOptimizer

Find objects with min or max score. Each object is written to the Optimizer by a push command, together with it's score. content_max and content_min result the object with the highest / lowest score. The access to the second and third objects is possible by deleting objects with delete_max or delete_min.

Example

require 'kyanite/optimizer' 
test = Optimizer.new
test.push  1,  'hello'
test.push  5,  'item'
test.push  6,  'maximum23'
test.push -1,  'minimum'
test.push -1,  'another minimum'  
test.content_max
>= "maximum23"
test.content_min
>= "minimum"

More tests and examples here.

Instance Method Summary (collapse)

Instance Method Details

- (Object) cleanup

Deletes all objects in the middle.



108
109
110
111
112
113
114
# File 'lib/kyanite/optimizer.rb', line 108

def cleanup
  return false if size <= 2
  keys.sort[1..-2].each do | key | 
    self.delete(key)
  end
  true
end

- (Object) content_max(range = 0..0)

Returns the content with maximum score.

content_max(0)      # returns the first content with maximum score
content_max(-1)     # returns the last content with maximum score  
content_max(0..-1)  # returns all contents with maximum score (as Array)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kyanite/optimizer.rb', line 62

def content_max(range=0..0)
  return nil  if size == 0
  range = (range..range) if range.kind_of?(Fixnum)    
  if (range.end - range.begin) == 0
    return Marshal.load(self[keys.max][range.begin])
  else
    result = []
    self[keys.max][range].each do | m |
      result << Marshal.load(m)
    end
    return result
  end
end

- (Object) content_min(range = 0..0)

Returns the content with minimum score.

content_min(0)      # returns the first content with minimum score
content_min(-1)     # returns the last content with minimum score  
content_min(0..-1)  # returns all contents with minimum score (as Array)


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kyanite/optimizer.rb', line 82

def content_min(range=0..0)
  return nil  if size == 0
  range = (range..range) if range.kind_of?(Fixnum)    
  if (range.end - range.begin) == 0
    return Marshal.load(self[keys.min][range.begin])
  else
    result = []
    self[keys.min][range].each do | m |
      result << Marshal.load(m)
    end
    return result
  end
end

- (Object) delete_max

Deletes the object with the highest score.



125
126
127
128
129
# File 'lib/kyanite/optimizer.rb', line 125

def delete_max
  return false if size <= 1
  self.delete(keys.max)
  true
end

- (Object) delete_min

Deletes the object with the lowest score.



118
119
120
121
122
# File 'lib/kyanite/optimizer.rb', line 118

def delete_min
  return false if size <= 1
  self.delete(keys.min)
  true
end

- (Object) push(score, content, options = {})

Load the optimizer with objects.



98
99
100
101
102
103
104
# File 'lib/kyanite/optimizer.rb', line 98

def push( score, content, options={} )
  if self.has_key?(score) 
    self[score] << Marshal.dump(content) 
  else
    self[score] = [ Marshal.dump(content) ]
  end
end

- (Numeric) score_max

Value of maximum score

Returns:

  • (Numeric)

    Value of maximum score



44
45
46
47
# File 'lib/kyanite/optimizer.rb', line 44

def score_max
  return nil  if size == 0  
  keys.max
end

- (Numeric) score_min

Value of minimum score

Returns:

  • (Numeric)

    Value of minimum score



51
52
53
54
# File 'lib/kyanite/optimizer.rb', line 51

def score_min
  return nil  if size == 0  
  keys.min
end