memoize 1.2.3 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +15 -0
- data/MANIFEST +3 -3
- data/README +7 -2
- data/Rakefile +16 -6
- data/examples/example_fibonacci.rb +45 -0
- data/examples/example_memoize.rb +62 -0
- data/lib/memoize.rb +4 -4
- data/memoize.gemspec +29 -0
- data/test/{tc_memoize.rb → test_memoize.rb} +17 -13
- metadata +60 -39
data/CHANGES
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
= 1.3.0 - 13-Jul-2009
|
2
|
+
* Updated to work with 1.9.x - now passes explicit arguments to super. Still
|
3
|
+
works with 1.8.x as well.
|
4
|
+
* Changed license to Artistic 2.0.
|
5
|
+
* Removed an unnecessary read attribute when writing to a cache file.
|
6
|
+
* Added a warranty section to the README file.
|
7
|
+
* Changed the test file name to 'test_memoize.rb' in order to more closely
|
8
|
+
follow Ruby convention.
|
9
|
+
* Changed the example file names so that they are each preceded with the
|
10
|
+
text 'example_' to avoid any confusion with test files.
|
11
|
+
* The test-unit gem is now a pre-requisite for this library.
|
12
|
+
* Added a couple of rake tasks for running example programs.
|
13
|
+
* Added a few more tests.
|
14
|
+
* Some gemspec updates, including explicit license.
|
15
|
+
|
1
16
|
= 1.2.3 - 24-May-2007
|
2
17
|
* Added a Rakefile with tasks for testing and installation.
|
3
18
|
* Removed the install.rb file. Installation is now handled by the Rakefile tasks.
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -63,10 +63,15 @@ Memoize#memoize(method, file=nil)
|
|
63
63
|
or the forums at http://www.rubyforge.org/projects/shards.
|
64
64
|
|
65
65
|
== License
|
66
|
-
|
66
|
+
Artistic 2.0
|
67
|
+
|
68
|
+
== Warranty
|
69
|
+
This package is provided "as is" and without any express or
|
70
|
+
implied warranties, including, without limitation, the implied
|
71
|
+
warranties of merchantability and fitness for a particular purpose.
|
67
72
|
|
68
73
|
== Copyright
|
69
|
-
(C) 2005-
|
74
|
+
(C) 2005-2009 Daniel J. Berger
|
70
75
|
All Rights Reserved
|
71
76
|
|
72
77
|
= Author
|
data/Rakefile
CHANGED
@@ -3,22 +3,32 @@ require 'rake/testtask'
|
|
3
3
|
require 'rbconfig'
|
4
4
|
include Config
|
5
5
|
|
6
|
-
desc 'Install the memoize
|
6
|
+
desc 'Install the memoize library (non-gem)'
|
7
7
|
task :install do
|
8
|
-
sitelibdir = CONFIG[
|
9
|
-
file =
|
8
|
+
sitelibdir = CONFIG['sitelibdir']
|
9
|
+
file = 'lib/memoize.rb'
|
10
10
|
FileUtils.cp(file, sitelibdir, :verbose => true)
|
11
11
|
end
|
12
12
|
|
13
|
+
desc 'Install the memoize library as a gem'
|
13
14
|
task :install_gem do
|
14
15
|
ruby 'memoize.gemspec'
|
15
|
-
file = Dir[
|
16
|
-
sh
|
16
|
+
file = Dir['*.gem'].first
|
17
|
+
sh 'gem install #{file}'
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Run the fibonacci example & benchmarks'
|
21
|
+
task :example_fib do
|
22
|
+
ruby '-Ilib examples/example_fibonacci.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
desc 'Run the memoize example & benchmarks'
|
26
|
+
task :example_memoize do
|
27
|
+
ruby '-Ilib examples/example_memoize.rb'
|
17
28
|
end
|
18
29
|
|
19
30
|
Rake::TestTask.new do |t|
|
20
31
|
t.libs << 'test'
|
21
32
|
t.verbose = true
|
22
33
|
t.warning = true
|
23
|
-
t.test_files = FileList['test/tc_memoize.rb']
|
24
34
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
###################################################################
|
2
|
+
# fibonacci.rb
|
3
|
+
#
|
4
|
+
# Demonstrates, via Benchmark, the difference between a memoized
|
5
|
+
# version of the fibonnaci method versus a non-memoized version.
|
6
|
+
#
|
7
|
+
# You can run this via the 'example_fib' rake task.
|
8
|
+
###################################################################
|
9
|
+
require 'benchmark'
|
10
|
+
require 'memoize'
|
11
|
+
include Memoize
|
12
|
+
|
13
|
+
# Our fibonacci function
|
14
|
+
def fib(n)
|
15
|
+
return n if n < 2
|
16
|
+
fib(n-1) + fib(n-2)
|
17
|
+
end
|
18
|
+
|
19
|
+
file = File.join((ENV['HOME'] || ENV['USERPROFILE']), 'fib.cache')
|
20
|
+
|
21
|
+
max_iter = ARGV[0].to_i
|
22
|
+
max_fib = ARGV[1].to_i
|
23
|
+
|
24
|
+
max_iter = 100 if max_iter == 0
|
25
|
+
max_fib = 25 if max_fib == 0
|
26
|
+
|
27
|
+
print "\nBenchmarking against version: " + MEMOIZE_VERSION + "\n\n"
|
28
|
+
|
29
|
+
Benchmark.bm(35) do |x|
|
30
|
+
x.report("Not memoized:"){
|
31
|
+
max_iter.times{ fib(max_fib) }
|
32
|
+
}
|
33
|
+
|
34
|
+
x.report("Memoized:"){
|
35
|
+
memoize(:fib)
|
36
|
+
max_iter.times{ fib(max_fib) }
|
37
|
+
}
|
38
|
+
|
39
|
+
x.report("Memoized to file:"){
|
40
|
+
memoize(:fib, file)
|
41
|
+
max_iter.times{ fib(max_fib) }
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
File.delete(file) if File.exists?(file)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#################################################################
|
2
|
+
# test_memoize.rb
|
3
|
+
#
|
4
|
+
# Simple example to demonstrate the use of memoize and cache.
|
5
|
+
# You can run this via the 'rake example_memoize' task.
|
6
|
+
#################################################################
|
7
|
+
require "memoize"
|
8
|
+
|
9
|
+
puts "MEMOIZE VERSION: " + Memoize::MEMOIZE_VERSION
|
10
|
+
|
11
|
+
class Foo
|
12
|
+
include Memoize
|
13
|
+
def fib(n)
|
14
|
+
return n if n < 2
|
15
|
+
fib(n-1) + fib(n-2)
|
16
|
+
end
|
17
|
+
|
18
|
+
def factorial(n)
|
19
|
+
f = 1
|
20
|
+
n.downto(2) { |x| f *= x }
|
21
|
+
return f
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if $0 == __FILE__
|
26
|
+
a = Foo.new
|
27
|
+
b = Foo.new
|
28
|
+
c = Foo.new
|
29
|
+
|
30
|
+
file = ENV["HOME"] || ENV["USERPROFILE"] + "/fib.cache"
|
31
|
+
|
32
|
+
cache1 = b.memoize(:fib)
|
33
|
+
cache2 = b.memoize(:factorial)
|
34
|
+
cache3 = c.memoize(:fib, file)
|
35
|
+
|
36
|
+
p cache1
|
37
|
+
b.fib(3)
|
38
|
+
p cache1
|
39
|
+
|
40
|
+
p cache2
|
41
|
+
b.factorial(3)
|
42
|
+
p cache2
|
43
|
+
|
44
|
+
p cache1
|
45
|
+
|
46
|
+
print "\nWithout memoization\n\n"
|
47
|
+
puts "Start: " + Time.now.to_s
|
48
|
+
a.fib(32)
|
49
|
+
puts "Stop: " + Time.now.to_s
|
50
|
+
|
51
|
+
print "\n\nWith memoization\n\n"
|
52
|
+
puts "Start: " + Time.now.to_s
|
53
|
+
b.fib(32)
|
54
|
+
puts "Stop: " + Time.now.to_s
|
55
|
+
|
56
|
+
print "\n\nWith memoization to file\n\n"
|
57
|
+
puts "Start: " + Time.now.to_s
|
58
|
+
c.fib(32)
|
59
|
+
puts "Stop: " + Time.now.to_s
|
60
|
+
|
61
|
+
File.delete(file) if File.exists?(file)
|
62
|
+
end
|
data/lib/memoize.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
module Memoize
|
2
|
-
MEMOIZE_VERSION =
|
2
|
+
MEMOIZE_VERSION = '1.3.0'
|
3
3
|
|
4
4
|
# Memoize the method +name+. If +file+ is provided, then the method results
|
5
5
|
# are stored on disk as well as in memory.
|
6
6
|
def memoize(name, file=nil)
|
7
|
-
cache = File.open(file,
|
7
|
+
cache = File.open(file, 'rb'){ |io| Marshal.load(io) } rescue {}
|
8
8
|
|
9
9
|
(class<<self; self; end).send(:define_method, name) do |*args|
|
10
10
|
unless cache.has_key?(args)
|
11
|
-
cache[args] = super
|
12
|
-
File.open(file,
|
11
|
+
cache[args] = super(*args)
|
12
|
+
File.open(file, 'wb'){ |f| Marshal.dump(cache, f) } if file
|
13
13
|
end
|
14
14
|
cache[args]
|
15
15
|
end
|
data/memoize.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
spec = Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'memoize'
|
5
|
+
gem.version = '1.3.0'
|
6
|
+
gem.author = 'Daniel J. Berger'
|
7
|
+
gem.email = 'djberg96@gmail.com'
|
8
|
+
gem.homepage = 'http://www.rubyforge.org/projects/shards'
|
9
|
+
gem.platform = Gem::Platform::RUBY
|
10
|
+
gem.summary = 'Speeds up methods at the cost of memory (or disk space)'
|
11
|
+
gem.test_file = 'test/test_memoize.rb'
|
12
|
+
gem.has_rdoc = true
|
13
|
+
gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
|
14
|
+
gem.license = 'Artistic 2.0'
|
15
|
+
|
16
|
+
gem.rubyforge_project = 'shards'
|
17
|
+
gem.extra_rdoc_files = ['MANIFEST', 'README', 'CHANGES']
|
18
|
+
|
19
|
+
gem.add_dependency('test-unit', '>= 2.0.2')
|
20
|
+
|
21
|
+
gem.description = <<-EOF
|
22
|
+
The memoize library allows you to cache methods for faster lookup.
|
23
|
+
Cached results can either be stored in memory (the default) or to
|
24
|
+
a file.
|
25
|
+
EOF
|
26
|
+
end
|
27
|
+
|
28
|
+
Gem.manage_gems if Gem::RubyGemsVersion.to_f < 1.0
|
29
|
+
Gem::Builder.new(spec).build
|
@@ -1,18 +1,13 @@
|
|
1
1
|
###############################################
|
2
|
-
#
|
2
|
+
# test_memoize.rb
|
3
3
|
#
|
4
|
-
# Test suite for the memoize
|
4
|
+
# Test suite for the memoize library.
|
5
5
|
###############################################
|
6
|
-
|
6
|
+
require 'rubygems'
|
7
|
+
gem 'test-unit'
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
11
|
-
Dir.chdir("test") rescue nil
|
12
|
-
end
|
13
|
-
|
14
|
-
require "test/unit"
|
15
|
-
require "memoize"
|
9
|
+
require 'test/unit'
|
10
|
+
require 'memoize'
|
16
11
|
|
17
12
|
class TC_Memoize < Test::Unit::TestCase
|
18
13
|
include Memoize
|
@@ -20,7 +15,7 @@ class TC_Memoize < Test::Unit::TestCase
|
|
20
15
|
def setup
|
21
16
|
@cache1 = nil
|
22
17
|
@cache2 = nil
|
23
|
-
@file = (ENV[
|
18
|
+
@file = File.join((ENV['HOME'] || ENV['USERPROFILE']), 'test.cache')
|
24
19
|
end
|
25
20
|
|
26
21
|
def fib(n)
|
@@ -35,7 +30,7 @@ class TC_Memoize < Test::Unit::TestCase
|
|
35
30
|
end
|
36
31
|
|
37
32
|
def test_version
|
38
|
-
assert_equal(
|
33
|
+
assert_equal('1.3.0', Memoize::MEMOIZE_VERSION)
|
39
34
|
end
|
40
35
|
|
41
36
|
def test_memoize
|
@@ -53,6 +48,15 @@ class TC_Memoize < Test::Unit::TestCase
|
|
53
48
|
assert_equal(55, fib(10))
|
54
49
|
end
|
55
50
|
|
51
|
+
def test_memoize_file_properties
|
52
|
+
assert_false(File.exists?(@file))
|
53
|
+
assert_nothing_raised{ memoize(:fib, @file) }
|
54
|
+
assert_false(File.exists?(@file))
|
55
|
+
assert_nothing_raised{ fib(10) }
|
56
|
+
assert_true(File.exists?(@file))
|
57
|
+
assert_true(File.size(@file) > 0)
|
58
|
+
end
|
59
|
+
|
56
60
|
# Ensure that a cache is returned, that it's a hash, and that each
|
57
61
|
# memoized method retains its own cache properly.
|
58
62
|
def test_memoize_cache
|
metadata
CHANGED
@@ -1,53 +1,74 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.2
|
3
|
-
specification_version: 1
|
4
2
|
name: memoize
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date: 2007-05-24 00:00:00 -06:00
|
8
|
-
summary: Speeds up methods at the cost of memory (or disk space)
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: djberg96@gmail.com
|
12
|
-
homepage: http://www.rubyforge.org/projects/shards
|
13
|
-
rubyforge_project:
|
14
|
-
description: Speeds up methods at the cost of memory (or disk space)
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 1.3.0
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Daniel J. Berger
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-20 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: test-unit
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.2
|
24
|
+
version:
|
25
|
+
description: " The memoize library allows you to cache methods for faster lookup.\n Cached results can either be stored in memory (the default) or to\n a file.\n"
|
26
|
+
email: djberg96@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
41
30
|
|
42
31
|
extra_rdoc_files:
|
43
32
|
- MANIFEST
|
44
33
|
- README
|
45
34
|
- CHANGES
|
46
|
-
|
47
|
-
|
48
|
-
|
35
|
+
files:
|
36
|
+
- CHANGES
|
37
|
+
- examples/example_fibonacci.rb
|
38
|
+
- examples/example_memoize.rb
|
39
|
+
- lib/memoize.rb
|
40
|
+
- MANIFEST
|
41
|
+
- memoize.gemspec
|
42
|
+
- Rakefile
|
43
|
+
- README
|
44
|
+
- test/test_memoize.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://www.rubyforge.org/projects/shards
|
47
|
+
licenses:
|
48
|
+
- Artistic 2.0
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
49
51
|
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
50
66
|
requirements: []
|
51
67
|
|
52
|
-
|
53
|
-
|
68
|
+
rubyforge_project: shards
|
69
|
+
rubygems_version: 1.3.4
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Speeds up methods at the cost of memory (or disk space)
|
73
|
+
test_files:
|
74
|
+
- test/test_memoize.rb
|