Class: MinMax

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#priority_blkObject (readonly)

Returns the value of attribute priority_blk.



9
10
11
# File 'lib/min_max.rb', line 9

def priority_blk
  @priority_blk
end

#storageObject (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

Returns:

  • (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

#firstObject



61
62
63
# File 'lib/min_max.rb', line 61

def first
  peek_min
end

#inspectObject



99
100
101
# File 'lib/min_max.rb', line 99

def inspect
  "MinMax[#{each.first(10).map(&:to_s).join(", ")}#{size > 10 ? ", ..." : ""}]"
end

#lastObject



65
66
67
# File 'lib/min_max.rb', line 65

def last
  peek_max
end

#peek_maxObject



57
58
59
# File 'lib/min_max.rb', line 57

def peek_max
  retrieve(_peek_max, false)
end

#peek_minObject



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_aObject



79
80
81
# File 'lib/min_max.rb', line 79

def to_a
  each.to_a
end

#to_a_ascObject



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_descObject



95
96
97
# File 'lib/min_max.rb', line 95

def to_a_desc
  _to_a_desc.map{|p| retrieve(p, false) }
end