Class: Lru

Inherits:
Object
  • Object
show all
Defined in:
lib/lrjew/lru.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexed_bounded_stack) ⇒ Lru

Returns a new instance of Lru.



4
5
6
7
# File 'lib/lrjew/lru.rb', line 4

def initialize(indexed_bounded_stack)
  @stack = indexed_bounded_stack
  @gets = @hits = @misses = @evictions = 0
end

Instance Attribute Details

#evictionsObject (readonly)

Returns the value of attribute evictions.



2
3
4
# File 'lib/lrjew/lru.rb', line 2

def evictions
  @evictions
end

#getsObject (readonly)

Returns the value of attribute gets.



2
3
4
# File 'lib/lrjew/lru.rb', line 2

def gets
  @gets
end

#hitsObject (readonly)

Returns the value of attribute hits.



2
3
4
# File 'lib/lrjew/lru.rb', line 2

def hits
  @hits
end

#missesObject (readonly)

Returns the value of attribute misses.



2
3
4
# File 'lib/lrjew/lru.rb', line 2

def misses
  @misses
end

Instance Method Details

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lrjew/lru.rb', line 9

def include?(item)
  @gets += 1
  if included = @stack.include?(item)
    @hits += 1
    @stack.delete(item)
    @stack.push(item)
  else
    @misses += 1
    node, shifted = @stack.push(item)
    @evictions += 1 if shifted
  end
  included
end

#inspectObject



23
24
25
26
27
28
29
30
31
# File 'lib/lrjew/lru.rb', line 23

def inspect
  [
    "gets", @gets,
    "hits", @hits,
    "misses", @misses,
    "evictions", @evictions,
    "rate", @hits.to_f / @gets
  ].join("\t")
end