Class: LRUHash
Overview
Hash with LRU expiry policy. There are at most max_size elements in a LruHash. When adding more elements old elements are removed according to LRU policy.
Defined Under Namespace
Classes: Node
Constant Summary collapse
- FETCH =
Proc.new {|k| raise KeyError, 'key not found'}
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#default_proc ⇒ Object
Returns the value of attribute default_proc.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#release_proc ⇒ Object
Returns the value of attribute release_proc.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #assoc(key) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #delete_if ⇒ Object
- #each_key ⇒ Object
- #each_pair ⇒ Object (also: #each)
- #each_value ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, &b) ⇒ Object
- #has_key?(key) ⇒ Boolean (also: #key?, #member?, #include?)
- #has_value?(value) ⇒ Boolean (also: #value?)
-
#initialize(max_size, default_value = nil, &block) ⇒ LRUHash
constructor
A new instance of LRUHash.
- #key(value) ⇒ Object
- #keys ⇒ Object
- #rassoc(value) ⇒ Object
- #size ⇒ Object
- #store(key, value) ⇒ Object (also: #[]=)
- #to_s ⇒ Object (also: #inspect)
- #values ⇒ Object
- #values_at(*key_list) ⇒ Object
Constructor Details
#initialize(max_size, default_value = nil, &block) ⇒ LRUHash
Returns a new instance of LRUHash.
19 20 21 22 23 24 25 26 27 |
# File 'lib/bio/system/lruhash.rb', line 19 def initialize(max_size, default_value = nil, &block) @max_size = normalize_max(max_size) @default = default_value @default_proc = block @h = {} @head = Node.new @tail = front(Node.new) end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
17 18 19 |
# File 'lib/bio/system/lruhash.rb', line 17 def default @default end |
#default_proc ⇒ Object
Returns the value of attribute default_proc.
17 18 19 |
# File 'lib/bio/system/lruhash.rb', line 17 def default_proc @default_proc end |
#max_size ⇒ Object
Returns the value of attribute max_size.
16 17 18 |
# File 'lib/bio/system/lruhash.rb', line 16 def max_size @max_size end |
#release_proc ⇒ Object
Returns the value of attribute release_proc.
17 18 19 |
# File 'lib/bio/system/lruhash.rb', line 17 def release_proc @release_proc end |
Instance Method Details
#[](key) ⇒ Object
79 80 81 82 83 |
# File 'lib/bio/system/lruhash.rb', line 79 def [](key) fetch(key) do |k| @default_proc ? @default_proc[self, k] : default end end |
#assoc(key) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/bio/system/lruhash.rb', line 115 def assoc(key) n = @h[key] if n front(n) [n.key, n.value] end end |
#clear ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/bio/system/lruhash.rb', line 182 def clear until empty? delete_oldest end self end |
#delete(key) ⇒ Object
162 163 164 |
# File 'lib/bio/system/lruhash.rb', line 162 def delete(key) n = @h[key] and remove_node(n).value end |
#delete_if ⇒ Object
166 167 168 169 170 |
# File 'lib/bio/system/lruhash.rb', line 166 def delete_if each_node do |n| remove_node n if yield n.key, n.value end end |
#each_key ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/bio/system/lruhash.rb', line 41 def each_key if block_given? each_node do |n| yield n.key end else enum_for :each_key end end |
#each_pair ⇒ Object Also known as: each
29 30 31 32 33 34 35 36 37 |
# File 'lib/bio/system/lruhash.rb', line 29 def each_pair if block_given? each_node do |n| yield [n.key, n.value] end else enum_for :each_pair end end |
#each_value ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/bio/system/lruhash.rb', line 51 def each_value if block_given? each_node do |n| yield n.value end else enum_for :each_value end end |
#empty? ⇒ Boolean
65 66 67 |
# File 'lib/bio/system/lruhash.rb', line 65 def empty? @head.succ.equal? @tail end |
#fetch(key, &b) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/bio/system/lruhash.rb', line 69 def fetch(key, &b) n = @h[key] if n front(n).value else (b || FETCH)[key] end end |
#has_key?(key) ⇒ Boolean Also known as: key?, member?, include?
93 94 95 |
# File 'lib/bio/system/lruhash.rb', line 93 def has_key?(key) @h.has_key? key end |
#has_value?(value) ⇒ Boolean Also known as: value?
101 102 103 104 105 106 107 |
# File 'lib/bio/system/lruhash.rb', line 101 def has_value?(value) each_pair do |k, v| return true if value.eql? v end false end |
#key(value) ⇒ Object
134 135 136 |
# File 'lib/bio/system/lruhash.rb', line 134 def key(value) pair = rassoc(value) and pair.first end |
#keys ⇒ Object
85 86 87 |
# File 'lib/bio/system/lruhash.rb', line 85 def keys @h.keys end |
#rassoc(value) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/bio/system/lruhash.rb', line 124 def rassoc(value) each_node do |n| if value.eql? n.value front(n) return [n.key, n.value] end end nil end |
#size ⇒ Object
61 62 63 |
# File 'lib/bio/system/lruhash.rb', line 61 def size @h.size end |
#store(key, value) ⇒ Object Also known as: []=
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bio/system/lruhash.rb', line 138 def store(key, value) # same optimization as in Hash key = key.dup.freeze if String === key && !key.frozen? n = @h[key] unless n if size == max_size # reuse node to optimize memory usage n = delete_oldest n.key = key n.value = value else n = Node.new key, value end @h[key] = n end front(n).value = value end |
#to_s ⇒ Object Also known as: inspect
190 191 192 193 194 |
# File 'lib/bio/system/lruhash.rb', line 190 def to_s s = nil each_pair {|k, v| (s ? (s << ', ') : s = '{') << k.to_s << '=>' << v.to_s} s ? (s << '}') : '{}' end |
#values ⇒ Object
89 90 91 |
# File 'lib/bio/system/lruhash.rb', line 89 def values @h.map {|k,n| n.value} end |
#values_at(*key_list) ⇒ Object
111 112 113 |
# File 'lib/bio/system/lruhash.rb', line 111 def values_at(*key_list) key_list.map {|k| self[k]} end |