Class: Optimizer
Overview
Optimizer
- Kyanite definitions
- 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
-
#cleanup ⇒ Object
Deletes all objects in the middle.
-
#content_max(range = 0..0) ⇒ Object
Returns the content with maximum score.
-
#content_min(range = 0..0) ⇒ Object
Returns the content with minimum score.
-
#delete_max ⇒ Object
Deletes the object with the highest score.
-
#delete_min ⇒ Object
Deletes the object with the lowest score.
-
#push(score, content, options = {}) ⇒ Object
Load the optimizer with objects.
-
#score_max ⇒ Numeric
Value of maximum score.
-
#score_min ⇒ Numeric
Value of minimum score.
Methods inherited from Hash
#arrayize!, #compact_keys!, #compact_values!, #delete_key, #delete_value, #distribution, #first, #fuzzyget, #last, #to_dictionary
Instance Method Details
#cleanup ⇒ Object
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 |
#content_max(range = 0..0) ⇒ Object
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 |
#content_min(range = 0..0) ⇒ Object
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 |
#delete_max ⇒ Object
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 |
#delete_min ⇒ Object
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 |
#push(score, content, options = {}) ⇒ Object
Load the optimizer with objects.
98 99 100 101 102 103 104 |
# File 'lib/kyanite/optimizer.rb', line 98 def push( score, content, ={} ) if self.has_key?(score) self[score] << Marshal.dump(content) else self[score] = [ Marshal.dump(content) ] end end |
#score_max ⇒ Numeric
Returns 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 |
#score_min ⇒ Numeric
Returns 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 |