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.7"
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
- #each_asc ⇒ Object
- #each_desc ⇒ 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] }.compare_by_identity } 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) storage.has_key?(val.object_id) end |
#count(val) ⇒ Object
83 84 85 |
# File 'lib/min_max.rb', line 83 def count(val) storage.has_key?(val.object_id) ? storage[val.object_id].first : 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 |
#each_asc ⇒ Object
101 102 103 104 |
# File 'lib/min_max.rb', line 101 def each_asc return to_enum(:each_asc) unless block_given? _to_a_asc.each{|p| yield retrieve(p, false) } end |
#each_desc ⇒ Object
106 107 108 109 |
# File 'lib/min_max.rb', line 106 def each_desc return to_enum(:each_desc) unless block_given? _to_a_desc.map{|p| retrieve(p, false) } end |
#first ⇒ Object
61 62 63 |
# File 'lib/min_max.rb', line 61 def first peek_min end |
#inspect ⇒ Object
111 112 113 |
# File 'lib/min_max.rb', line 111 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| object_id = a.object_id entry = self.storage[object_id] entry[0] += 1 entry[1] ||= a [ (self.priority_blk.call(a) rescue 0), object_id ] 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 94 |
# File 'lib/min_max.rb', line 91 def to_a_asc return to_enum(:to_a_asc) unless block_given? _to_a_asc.map{|p| retrieve(p, false) } end |
#to_a_desc ⇒ Object
96 97 98 99 |
# File 'lib/min_max.rb', line 96 def to_a_desc return to_enum(:to_a_desc) unless block_given? _to_a_desc.map{|p| retrieve(p, false) } end |