Class: MinMax
- Inherits:
-
Object
- Object
- MinMax
- Defined in:
- lib/min_max.rb,
lib/min_max/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.6"
Instance Attribute Summary collapse
-
#priority_blk ⇒ Object
readonly
Returns the value of attribute priority_blk.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
Class Method Summary collapse
Instance Method Summary collapse
- #add(*args) ⇒ Object
- #contains?(val) ⇒ Boolean
- #count(val) ⇒ Object
- #each(*args, &blk) ⇒ Object
- #first ⇒ Object
- #inspect ⇒ Object
- #last ⇒ Object
- #peek_max ⇒ Object
- #peek_min ⇒ Object
- #pop_max(*args) ⇒ Object
- #pop_min(*args) ⇒ Object
- #push(*args) ⇒ Object
- #to_a ⇒ Object
- #to_a_asc ⇒ Object
- #to_a_desc ⇒ Object
Instance Attribute Details
#priority_blk ⇒ Object (readonly)
Returns the value of attribute priority_blk.
9 10 11 |
# File 'lib/min_max.rb', line 9 def priority_blk @priority_blk end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
9 10 11 |
# File 'lib/min_max.rb', line 9 def storage @storage end |
Class Method Details
.[](*args, &blk) ⇒ Object
11 12 13 |
# File 'lib/min_max.rb', line 11 def self.[](*args, &blk) new(*args, &blk) end |
.new(*args, &blk) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/min_max.rb', line 15 def self.new(*args, &blk) self._new.tap{|s| s.instance_eval{ @priority_blk = (blk || proc{|x| x.respond_to?(:priority) ? x.priority : x.to_i }) @storage = Hash.new{|h,k| h[k] = [0, nil] } } s.push(*args) } end |
Instance Method Details
#add(*args) ⇒ Object
39 40 41 |
# File 'lib/min_max.rb', line 39 def add(*args) push(*args) end |
#contains?(val) ⇒ Boolean
87 88 89 |
# File 'lib/min_max.rb', line 87 def contains?(val) counts.has_key?(val.hash) && counts[val.hash] > 0 end |
#count(val) ⇒ Object
83 84 85 |
# File 'lib/min_max.rb', line 83 def count(val) counts.has_key?(val.hash) ? counts[val.hash] : 0 end |
#each(*args, &blk) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/min_max.rb', line 69 def each(*args, &blk) if block_given? _each(*args) do |p| blk[retrieve(p, false)] end else to_enum(:each, *args) end end |
#first ⇒ Object
61 62 63 |
# File 'lib/min_max.rb', line 61 def first peek_min end |
#inspect ⇒ Object
99 100 101 |
# File 'lib/min_max.rb', line 99 def inspect "MinMax[#{each.first(10).map(&:to_s).join(", ")}#{size > 10 ? ", ..." : ""}]" end |
#last ⇒ Object
65 66 67 |
# File 'lib/min_max.rb', line 65 def last peek_max end |
#peek_max ⇒ Object
57 58 59 |
# File 'lib/min_max.rb', line 57 def peek_max retrieve(_peek_max, false) end |
#peek_min ⇒ Object
53 54 55 |
# File 'lib/min_max.rb', line 53 def peek_min retrieve(_peek_min, false) end |
#pop_max(*args) ⇒ Object
43 44 45 46 |
# File 'lib/min_max.rb', line 43 def pop_max(*args) popped = _pop_max(*args) popped.kind_of?(Array) ? popped.map{|p| retrieve(p) } : retrieve(popped) end |
#pop_min(*args) ⇒ Object
48 49 50 51 |
# File 'lib/min_max.rb', line 48 def pop_min(*args) popped = _pop_min(*args) popped.kind_of?(Array) ? popped.map{|p| retrieve(p) } : retrieve(popped) end |
#push(*args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/min_max.rb', line 25 def push(*args) mapped = args.map do |a| hash = a.hash entry = self.storage[hash] entry[0] += 1 entry[1] ||= a [ (self.priority_blk.call(a) rescue 0), hash ] end _push(mapped) end |
#to_a ⇒ Object
79 80 81 |
# File 'lib/min_max.rb', line 79 def to_a each.to_a end |
#to_a_asc ⇒ Object
91 92 93 |
# File 'lib/min_max.rb', line 91 def to_a_asc _to_a_asc.map{|p| retrieve(p, false) } end |
#to_a_desc ⇒ Object
95 96 97 |
# File 'lib/min_max.rb', line 95 def to_a_desc _to_a_desc.map{|p| retrieve(p, false) } end |