lrjew 1.0 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/lrjew +8 -11
- data/lib/lrjew.rb +4 -4
- data/lib/lrjew/bounded_stack.rb +5 -4
- data/lib/lrjew/lru.rb +6 -1
- data/spec/bounded_stack_spec.rb +10 -0
- metadata +3 -3
data/bin/lrjew
CHANGED
@@ -1,20 +1,17 @@
|
|
1
1
|
#!/bin/env ruby
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'lrjew'
|
4
4
|
|
5
|
-
lru_size
|
5
|
+
lru_size = (ARGV.shift || 100_000).to_i
|
6
|
+
print_freq = (ARGV.shift || 50_000).to_i
|
6
7
|
|
7
|
-
|
8
|
+
indexed_stack = IndexedBoundedStack.new(BoundedStack.new(LinkedList.new, lru_size))
|
8
9
|
|
9
|
-
|
10
|
+
lru = Lru.new(indexed_stack)
|
10
11
|
|
11
|
-
|
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) %
|
16
|
-
|
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 '
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
require '
|
3
|
+
require 'lrjew/linked_list'
|
4
|
+
require 'lrjew/bounded_stack'
|
5
|
+
require 'lrjew/indexed_bounded_stack'
|
6
|
+
require 'lrjew/lru'
|
data/lib/lrjew/bounded_stack.rb
CHANGED
@@ -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
|
-
|
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,
|
data/spec/bounded_stack_spec.rb
CHANGED
@@ -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