Class: SC::Helpers::PackedOptimizer
- Inherits:
-
Object
- Object
- SC::Helpers::PackedOptimizer
- Defined in:
- lib/sproutcore/helpers/packed_optimizer.rb
Overview
Given a set of targets, this can determine the optimial mix of loading packed targets vs individual targets to yield the smaller number of assets. To use this optimizer, just call the optimize() method.
Instance Attribute Summary collapse
-
#packed ⇒ Object
readonly
Returns the value of attribute packed.
-
#unpacked ⇒ Object
readonly
Returns the value of attribute unpacked.
Class Method Summary collapse
-
.optimize(targets) ⇒ Object
Returns two arrays: the first array are targets that should be loaded packed.
Instance Method Summary collapse
-
#initialize ⇒ PackedOptimizer
constructor
A new instance of PackedOptimizer.
-
#process(packed_target, targets) ⇒ Object
Sorts the passed array of targets into packed and unpacked, starting with the passed target as the packed target.
-
#process_target(target) ⇒ Object
Sorts a single target into the correct bucket based on whether it is seen or not.
Constructor Details
#initialize ⇒ PackedOptimizer
Returns a new instance of PackedOptimizer.
45 46 47 48 49 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 45 def initialize @seen = [] @packed = [] @unpacked = [] end |
Instance Attribute Details
#packed ⇒ Object (readonly)
Returns the value of attribute packed.
42 43 44 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 42 def packed @packed end |
#unpacked ⇒ Object (readonly)
Returns the value of attribute unpacked.
43 44 45 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 43 def unpacked @unpacked end |
Class Method Details
.optimize(targets) ⇒ Object
Returns two arrays: the first array are targets that should be loaded packed. The array of targets that should be loaded, but not packed. And optional third array is also returned that includes targets which were passed in but are no longer needed because they are included in a packed target.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 20 def self.optimize(targets) packed = [] unpacked = targets cnt = packed.size + unpacked.size # for each target, try to use the packed version and see how many # total items we come back with. If the total number of targets is # less than the current best option, use that instead. targets.each do |target| cur_packed, cur_unpacked = self.new.process(target, targets) cur_cnt = cur_packed.size + cur_unpacked.size if cur_cnt < cnt packed = cur_packed unpacked = cur_unpacked cnt = cur_cnt end end # return best! return [packed, unpacked] end |
Instance Method Details
#process(packed_target, targets) ⇒ Object
Sorts the passed array of targets into packed and unpacked, starting with the passed target as the packed target.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 53 def process(packed_target, targets) # manually add in packed target... @seen << packed_target @packed << packed_target packed_target..each { |t| @seen << t } # then handle the rest of the targets targets.each { |t| process_target(t) } return [packed, unpacked] end |
#process_target(target) ⇒ Object
Sorts a single target into the correct bucket based on whether it is seen or not. If you also pass true to the second param, the required targets will also be sorted.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sproutcore/helpers/packed_optimizer.rb', line 68 def process_target(target) # if target was seen already, nothing to do return if @seen.include?(target) # add to seen @seen << target # have we already seen any of the required targets? req_targets = target. if req_targets.size > 0 already_seen = req_targets.find do |t| @seen.include?(t) end else already_seen = true end # if we have seen one, then we can't use the packed version so put it # in the unpacked set. if already_seen req_targets.each { |t| process_target(t) } @unpacked << target # add last to keep order intact # if we have not seen any required, then we can mark this as packed. # yeah! mark the required targets as seen also else req_targets.each { |t| @seen << t } @packed << target end end |