memoize 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +4 -0
- data/README +16 -5
- data/lib/memoize.rb +33 -7
- data/test/tc_memoize.rb +16 -1
- metadata +4 -4
data/CHANGES
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
= 1.2.0 - 31-Dec-2005
|
2
|
+
* Added support for memoizing to file rather than memory
|
3
|
+
* Corresponding changes in the test suite and example programs
|
4
|
+
|
1
5
|
= 1.1.0 - 20-Oct-2005
|
2
6
|
* Memoize#memoize now returns the cache (ruby-talk: 160870 and following)
|
3
7
|
* Memoize#memoize now caches nil and false results
|
data/README
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
= Description
|
2
|
-
A method that speeds methods up at the cost of memory.
|
2
|
+
A method that speeds methods up at the cost of memory (or disk space).
|
3
3
|
|
4
4
|
= Prerequisites
|
5
5
|
Ruby 1.8.0 or later
|
@@ -28,22 +28,33 @@
|
|
28
28
|
|
29
29
|
memoize(:fib)
|
30
30
|
fib(100) # Fast
|
31
|
+
|
32
|
+
# Or store the cache to a file for later use
|
33
|
+
memoize(:fib, "fib.cache")
|
34
|
+
fib(100) # Fast
|
31
35
|
|
32
36
|
= Constants
|
33
37
|
Memoize::MEMOIZE_VERSION
|
34
38
|
Returns the version of this package as a String.
|
35
39
|
|
36
40
|
= Methods
|
37
|
-
Memoize#memoize(method)
|
38
|
-
Takes a method (symbol) and caches the results of +method+ in a hash
|
39
|
-
If you call +method+ again with the same arguments, memoize gives
|
40
|
-
value from the table instead of letting the method compute the
|
41
|
+
Memoize#memoize(method, file=nil)
|
42
|
+
Takes a +method+ (symbol) and caches the results of +method+ in a hash
|
43
|
+
table. If you call +method+ again with the same arguments, memoize gives
|
44
|
+
you the value from the table instead of letting the method compute the
|
45
|
+
value again.
|
46
|
+
|
47
|
+
If +file+ is provided, the results are cached to that file instead of
|
48
|
+
in memory. Note that this uses Marshal internally, so don't expect your
|
49
|
+
cache to work between different versions of Ruby, should you happen to
|
50
|
+
upgrade.
|
41
51
|
|
42
52
|
Returns the cache, which you can inspect or manipulate directly if you are
|
43
53
|
so inclined.
|
44
54
|
|
45
55
|
= Acknowledgements
|
46
56
|
Code borrowed from Nobu Nakada (ruby-talk:155159).
|
57
|
+
Code borrowed from Ara Howard (ruby-talk:173428).
|
47
58
|
Ideas taken from Brian Buckley and Sean O'Halpin.
|
48
59
|
|
49
60
|
== License
|
data/lib/memoize.rb
CHANGED
@@ -1,13 +1,39 @@
|
|
1
1
|
module Memoize
|
2
|
-
MEMOIZE_VERSION = "1.
|
3
|
-
|
2
|
+
MEMOIZE_VERSION = "1.2.0"
|
3
|
+
|
4
|
+
# Memoize the method +name+. If +file+ is provided, then the method results
|
5
|
+
# are stored on disk rather than in memory. This consumes virtually no
|
6
|
+
# memory and is persistant.
|
7
|
+
def memoize(name, file=nil)
|
4
8
|
meth = method(name)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
|
10
|
+
if file
|
11
|
+
cache = Hash.new.update(Marshal.load(File.read(file))) rescue {}
|
12
|
+
else
|
13
|
+
cache = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
if file
|
17
|
+
(class << self; self; end).class_eval do
|
18
|
+
define_method(name) do |*args|
|
19
|
+
unless cache.has_key?(args)
|
20
|
+
cache[args] = meth.call(*args)
|
21
|
+
File.open(file, "wb+"){|f| Marshal.dump(cache, f) }
|
22
|
+
end
|
23
|
+
cache[args]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
else
|
27
|
+
(class << self; self; end).class_eval do
|
28
|
+
define_method(name) do |*args|
|
29
|
+
if cache.has_key?(args)
|
30
|
+
cache[args]
|
31
|
+
else
|
32
|
+
cache[args] ||= meth.call(*args)
|
33
|
+
end
|
34
|
+
end
|
9
35
|
end
|
10
36
|
end
|
11
37
|
cache
|
12
38
|
end
|
13
|
-
end
|
39
|
+
end
|
data/test/tc_memoize.rb
CHANGED
@@ -20,6 +20,7 @@ class TC_Memoize < Test::Unit::TestCase
|
|
20
20
|
def setup
|
21
21
|
@cache1 = nil
|
22
22
|
@cache2 = nil
|
23
|
+
@file = ENV["HOME"] || ENV["USERPROFILE"] + "/test.cache"
|
23
24
|
end
|
24
25
|
|
25
26
|
def fib(n)
|
@@ -34,7 +35,7 @@ class TC_Memoize < Test::Unit::TestCase
|
|
34
35
|
end
|
35
36
|
|
36
37
|
def test_version
|
37
|
-
assert_equal("1.
|
38
|
+
assert_equal("1.2.0", Memoize::MEMOIZE_VERSION)
|
38
39
|
end
|
39
40
|
|
40
41
|
def test_memoize
|
@@ -42,6 +43,14 @@ class TC_Memoize < Test::Unit::TestCase
|
|
42
43
|
assert_nothing_raised{ fib(5) }
|
43
44
|
assert_nothing_raised{ memoize(:fib) }
|
44
45
|
assert_nothing_raised{ fib(50) }
|
46
|
+
assert_equal(55, fib(10))
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_memoize_with_file
|
50
|
+
assert_nothing_raised{ fib(5) }
|
51
|
+
assert_nothing_raised{ memoize(:fib, @file) }
|
52
|
+
assert_nothing_raised{ fib(50) }
|
53
|
+
assert_equal(55, fib(10))
|
45
54
|
end
|
46
55
|
|
47
56
|
# Ensure that a cache is returned, that it's a hash, and that each
|
@@ -58,4 +67,10 @@ class TC_Memoize < Test::Unit::TestCase
|
|
58
67
|
|
59
68
|
assert_not_equal(@cache1, @cache2)
|
60
69
|
end
|
70
|
+
|
71
|
+
def teardown
|
72
|
+
@cache1 = nil
|
73
|
+
@cachd2 = nil
|
74
|
+
File.delete(@file) if File.exists?(@file)
|
75
|
+
end
|
61
76
|
end
|
metadata
CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.8.10
|
|
3
3
|
specification_version: 1
|
4
4
|
name: memoize
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date: 2005-
|
8
|
-
summary: Speeds up methods at the cost of memory
|
6
|
+
version: 1.2.0
|
7
|
+
date: 2005-12-31
|
8
|
+
summary: Speeds up methods at the cost of memory (or disk space)
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: djberg96@gmail.com
|
12
12
|
homepage: http://www.rubyforge.org/projects/shards
|
13
13
|
rubyforge_project:
|
14
|
-
description: Speeds up methods at the cost of memory
|
14
|
+
description: Speeds up methods at the cost of memory (or disk space)
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|