lrjew 1.0 → 1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/lrjew CHANGED
@@ -1,20 +1,17 @@
1
1
  #!/bin/env ruby
2
2
 
3
- require 'lru'
3
+ require 'lrjew'
4
4
 
5
- lru_size = ARGV[0] || 100_000
5
+ lru_size = (ARGV.shift || 100_000).to_i
6
+ print_freq = (ARGV.shift || 50_000).to_i
6
7
 
7
- lru = Lru.new(lru_size)
8
+ indexed_stack = IndexedBoundedStack.new(BoundedStack.new(LinkedList.new, lru_size))
8
9
 
9
- hits = 0
10
+ lru = Lru.new(indexed_stack)
10
11
 
11
- GC.disable
12
-
13
- ARGF.each_with_index do |line, idx|
12
+ $stdin.each_with_index do |line, idx|
14
13
  lru.include?(line)
15
- if (idx+1) % 50_000 == 0
16
- puts "#{Time.now}: #{lru.inspect}"
17
- GC.start
18
- GC.disable
14
+ if (idx+1) % print_freq == 0
15
+ p lru
19
16
  end
20
17
  end
data/lib/lrjew.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
 
3
- require 'lru/linked_list'
4
- require 'lru/bounded_stack'
5
- require 'lru/indexed_bounded_stack'
6
- require 'lru/lru'
3
+ require 'lrjew/linked_list'
4
+ require 'lrjew/bounded_stack'
5
+ require 'lrjew/indexed_bounded_stack'
6
+ require 'lrjew/lru'
@@ -20,15 +20,16 @@ class BoundedStack
20
20
  [node, shifted]
21
21
  end
22
22
 
23
+ def delete(data)
24
+ @list.delete(data)
25
+ @current_size -= 1
26
+ end
27
+
23
28
  def length
24
29
  @current_size
25
30
  end
26
-
27
31
  alias_method :size, :length
28
32
 
29
- def method_missing(method, *args, &block)
30
- @list.send(method, *args, &block)
31
- end
32
33
 
33
34
  def each(&block)
34
35
  @list.each(&block)
data/lib/lrjew/lru.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class Lru
2
+ include Enumerable
2
3
  attr_reader :evictions, :hits, :misses, :gets
3
4
 
4
5
  def initialize(indexed_bounded_stack)
@@ -14,12 +15,16 @@ class Lru
14
15
  @stack.push(item)
15
16
  else
16
17
  @misses += 1
17
- node, shifted = @stack.push(item)
18
+ _, shifted = @stack.push(item)
18
19
  @evictions += 1 if shifted
19
20
  end
20
21
  included
21
22
  end
22
23
 
24
+ def each(&block)
25
+ @stack.each(&block)
26
+ end
27
+
23
28
  def inspect
24
29
  [
25
30
  "gets", @gets,
@@ -26,4 +26,14 @@ describe BoundedStack do
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ describe '#delete' do
31
+ it "decrements the size" do
32
+ @bounded_stack.push(1)
33
+ @bounded_stack.push(2)
34
+ @bounded_stack.size.should == 2
35
+ @bounded_stack.delete(2)
36
+ @bounded_stack.size.should == 1
37
+ end
38
+ end
29
39
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lrjew
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 0
9
- version: "1.0"
8
+ - 3
9
+ version: "1.3"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Steve Jenson