Class: TopN
- Inherits:
-
Object
- Object
- TopN
- Defined in:
- lib/top_n.rb
Overview
Note that while the number of keys is restricted, the values are not. This can cause memory issues if many values are added to the same key. If this is the type of data you are tracking, you may need a different solution.
This class tracks the top (or bottom) N values for a set of keys.
As keys and values are added, only the largest (or smallest) keys will be recorded. As larger (or smaller) keys are added, unneeded items are removed.
Keys may be any object that is comparable with < and >. Values may be any object, and are not processed beyond appending them to an internal list.
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
The currently tracked data as one blob.
-
#direction ⇒ Symbol
readonly
The configured direction.
-
#maxsize ⇒ Fixnum
readonly
The maxinum number of keys which will be tracked.
-
#size ⇒ FixNum
readonly
The current number of keys we are tracking.
-
#threshold_key ⇒ Object
readonly
The current value of the minimum (:top) or maximum (:bottom) key.
Instance Method Summary collapse
-
#add(key, value) ⇒ Array?
Add a key, value pair.
-
#find(key) ⇒ Array<Object>?
Find and return the list of values for a key.
-
#initialize(options = {}) ⇒ TopN
constructor
Create a new TopN object.
-
#keys ⇒ Array<Object>
Return the list of currently tracked keys.
Constructor Details
#initialize(options = {}) ⇒ TopN
Create a new TopN object. Options available:
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/top_n.rb', line 63 def initialize( = {}) = { maxsize: 100, direction: :top, }.merge() @maxsize = [:maxsize] @direction = [:direction] @data = {} @size = 0 @threshold_key = nil unless [:top, :bottom].include?(@direction) raise ArgumentError.new("direction must be :top or :bottom") end unless @maxsize.is_a?Fixnum raise ArgumentError.new("maxsize must be a Fixnum") end if @maxsize <= 0 raise ArgumentError.new("maxsize must be >= 1") end end |
Instance Attribute Details
#data ⇒ Hash (readonly)
The currently tracked data as one blob. This is tied to the current implementation, so its use is not recommended.
39 40 41 |
# File 'lib/top_n.rb', line 39 def data @data end |
#direction ⇒ Symbol (readonly)
The configured direction.
24 25 26 |
# File 'lib/top_n.rb', line 24 def direction @direction end |
#maxsize ⇒ Fixnum (readonly)
The maxinum number of keys which will be tracked.
20 21 22 |
# File 'lib/top_n.rb', line 20 def maxsize @maxsize end |
#size ⇒ FixNum (readonly)
The current number of keys we are tracking.
28 29 30 |
# File 'lib/top_n.rb', line 28 def size @size end |
#threshold_key ⇒ Object (readonly)
The current value of the minimum (:top) or maximum (:bottom) key.
32 33 34 |
# File 'lib/top_n.rb', line 32 def threshold_key @threshold_key end |
Instance Method Details
#add(key, value) ⇒ Array?
Add a key, value pair.
If the key already exists, the value will be appended to the existing list of values at that key.
If an existing (key, value) is permitted, and will result in the list of values at that key having the same value multiple times.
large to be tracked.
106 107 108 109 110 111 112 |
# File 'lib/top_n.rb', line 106 def add(key, value) if @direction == :top add_top(key, value) else add_bottom(key, value) end end |
#find(key) ⇒ Array<Object>?
Find and return the list of values for a key.
120 121 122 |
# File 'lib/top_n.rb', line 120 def find(key) @data[key] end |
#keys ⇒ Array<Object>
Return the list of currently tracked keys.
129 130 131 |
# File 'lib/top_n.rb', line 129 def keys @data.keys end |