Class: Eymiha::Histogram
- Inherits:
-
Hash
- Object
- Hash
- Eymiha::Histogram
- Defined in:
- lib/eymiha/util/histogram.rb
Overview
A Histogram is a hash whose values are counts of the occurences of the keys.
Instance Method Summary collapse
-
#<(count) ⇒ Object
Returns a new Histogram containing the elements with occurences less than the given count.
-
#<=(count) ⇒ Object
Returns a new Histogram containing the elements with occurences less than or equal to the given count.
-
#==(count) ⇒ Object
If count is a number, a new Histogram containing the elements with occurences equal to the given countis returned.
-
#>(count) ⇒ Object
Returns a new Histogram containing the elements with occurences greater than the given count.
-
#>=(count) ⇒ Object
Returns a new Histogram containing the elements with occurences greater than or equal to the given count.
-
#add(key, count = 1) ⇒ Object
Adds the key to the instance if it doesn’t already exist, and adds the specified count to it value.
-
#count(check_keys = keys) ⇒ Object
Returns the number of occurences of the given key.
-
#initialize(key = nil, number = 1) ⇒ Histogram
constructor
Creates and returns an instance.
-
#key_count ⇒ Object
Returns the number of keys in the instance.
Constructor Details
#initialize(key = nil, number = 1) ⇒ Histogram
Creates and returns an instance. If arguments are given, they are passed to the add method to initialize the new instance.
18 19 20 21 |
# File 'lib/eymiha/util/histogram.rb', line 18 def initialize(key=nil,number=1) super() add(key,number) unless key == nil end |
Instance Method Details
#<(count) ⇒ Object
Returns a new Histogram containing the elements with occurences less than the given count.
90 91 92 93 94 |
# File 'lib/eymiha/util/histogram.rb', line 90 def <(count) result = Histogram.new each_pair { |k,v| result.add(k,v) if v < count } result end |
#<=(count) ⇒ Object
Returns a new Histogram containing the elements with occurences less than or equal to the given count.
82 83 84 85 86 |
# File 'lib/eymiha/util/histogram.rb', line 82 def <=(count) result = Histogram.new each_pair { |k,v| result.add(k,v) if v <= count } result end |
#==(count) ⇒ Object
If count is a number, a new Histogram containing the elements with occurences equal to the given countis returned. If the count is a histogram, true is returned if the instance contains the same keys and values; false otherwise.
100 101 102 103 104 105 106 107 108 |
# File 'lib/eymiha/util/histogram.rb', line 100 def ==(count) if count.kind_of? Numeric result = Histogram.new each_pair { |k,v| result.add(k,v) if v >= count } result else super end end |
#>(count) ⇒ Object
Returns a new Histogram containing the elements with occurences greater than the given count.
74 75 76 77 78 |
# File 'lib/eymiha/util/histogram.rb', line 74 def >(count) result = Histogram.new each_pair { |k,v| result.add(k,v) if v > count } result end |
#>=(count) ⇒ Object
Returns a new Histogram containing the elements with occurences greater than or equal to the given count.
66 67 68 69 70 |
# File 'lib/eymiha/util/histogram.rb', line 66 def >=(count) result = Histogram.new each_pair { |k,v| result.add(k,v) if v >= count } result end |
#add(key, count = 1) ⇒ Object
Adds the key to the instance if it doesn’t already exist, and adds the specified count to it value. If the key is
-
an Array, each member of the array is added. if the member itself is an Array, then if that array has one member, the member is added as a key; if two members, the member is added as a key and a count; otherwise, a HistogramException is thrown.
-
another histogram, the keys and counts in the key are added to the instance.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/eymiha/util/histogram.rb', line 36 def add(key,count=1) if key.kind_of? Array key.each { |member| if member.kind_of? Array if member.size == 1 add(member[0]) elsif (member.size == 2) && (member[1].kind_of? Fixnum) add(member[0],member[1]) else raise HistogramException, "add cannot interpret Array to add" end else add member end } elsif key.kind_of? Histogram key.each_pair { |k,v| add(k,v) } else self[key] = ((value = self[key]) == nil) ? count : value+count end self end |
#count(check_keys = keys) ⇒ Object
Returns the number of occurences of the given key. If an array of keys is given, the sum of the number of occurences of those keys is returned.
25 26 27 28 29 30 |
# File 'lib/eymiha/util/histogram.rb', line 25 def count(check_keys = keys) check_keys = [check_keys] if ! check_keys.kind_of? Array count = 0 check_keys.each { |key| count += self[key] if has_key? key } count end |
#key_count ⇒ Object
Returns the number of keys in the instance.
60 61 62 |
# File 'lib/eymiha/util/histogram.rb', line 60 def key_count keys.size end |