Class: LRUHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bio/system/lruhash.rb

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

Instance Method Summary collapse

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

#defaultObject

Returns the value of attribute default.



17
18
19
# File 'lib/bio/system/lruhash.rb', line 17

def default
  @default
end

#default_procObject

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_sizeObject

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_procObject

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

#clearObject



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_ifObject



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_keyObject



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_pairObject 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_valueObject



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

Returns:

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

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

#keysObject



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

#sizeObject



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_sObject 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

#valuesObject



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