memoize-1.3.1/0000755000175000017500000000000012135751313011236 5ustar daidaimemoize-1.3.1/metadata.yml0000644000175000017500000000313312135751313013541 0ustar daidai--- !ruby/object:Gem::Specification name: memoize version: !ruby/object:Gem::Version version: 1.3.1 platform: ruby authors: - Daniel J. Berger autorequire: bindir: bin cert_chain: [] date: 2009-08-08 00:00:00 -06:00 default_executable: dependencies: - !ruby/object:Gem::Dependency name: test-unit type: :development version_requirement: version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 2.0.2 version: 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" email: djberg96@gmail.com executables: [] extensions: [] extra_rdoc_files: - MANIFEST - README - CHANGES files: - CHANGES - examples/example_fibonacci.rb - examples/example_memoize.rb - lib/memoize.rb - MANIFEST - memoize.gemspec - Rakefile - README - test/test_memoize.rb has_rdoc: true homepage: http://www.rubyforge.org/projects/shards licenses: - Artistic 2.0 post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: "0" version: requirements: [] rubyforge_project: shards rubygems_version: 1.3.5 signing_key: specification_version: 3 summary: Speeds up methods at the cost of memory (or disk space) test_files: - test/test_memoize.rb memoize-1.3.1/test/0000755000175000017500000000000012135751313012215 5ustar daidaimemoize-1.3.1/test/test_memoize.rb0000644000175000017500000000370212135751313015250 0ustar daidai############################################### # test_memoize.rb # # Test suite for the memoize library. ############################################### require 'rubygems' gem 'test-unit' require 'test/unit' require 'memoize' class TC_Memoize < Test::Unit::TestCase include Memoize def setup @cache1 = nil @cache2 = nil @file = File.join((ENV['HOME'] || ENV['USERPROFILE']), 'test.cache') end def fib(n) return n if n < 2 fib(n-1) + fib(n-2) end def factorial(n) f = 1 n.downto(2) { |x| f *= x } f end def test_version assert_equal('1.3.1', Memoize::MEMOIZE_VERSION) end def test_memoize assert_respond_to(self, :memoize) assert_nothing_raised{ fib(5) } assert_nothing_raised{ memoize(:fib) } assert_nothing_raised{ fib(50) } assert_equal(55, fib(10)) end def test_memoize_with_file assert_nothing_raised{ fib(5) } assert_nothing_raised{ memoize(:fib, @file) } assert_nothing_raised{ fib(50) } assert_equal(55, fib(10)) end def test_memoize_file_properties assert_false(File.exists?(@file)) assert_nothing_raised{ memoize(:fib, @file) } assert_false(File.exists?(@file)) assert_nothing_raised{ fib(10) } assert_true(File.exists?(@file)) assert_true(File.size(@file) > 0) end # Ensure that a cache is returned, that it's a hash, and that each # memoized method retains its own cache properly. def test_memoize_cache assert_nothing_raised{ @cache1 = self.memoize(:fib) } assert_nothing_raised{ @cache2 = self.memoize(:factorial) } assert_nothing_raised{ self.fib(3) } assert_nothing_raised{ self.factorial(3) } assert_kind_of(Hash, @cache1) assert_kind_of(Hash, @cache2) assert_not_equal(@cache1, @cache2) end def teardown @cache1 = nil @cache2 = nil File.delete(@file) if File.exists?(@file) end end memoize-1.3.1/README0000644000175000017500000000423012135751313012115 0ustar daidai= Description A method that speeds methods up at the cost of memory (or disk space). = Prerequisites Ruby 1.8.0 or later = Installation == Standard Installation rake test (optional) rake install == Gems Installation rake test (optional) rake gem install = Synopsis require "memoize" include Memoize # Inefficient fibonacci method def fib(n) return n if n < 2 fib(n-1) + fib(n-2) end fib(100) # Slow memoize(:fib) fib(100) # Fast # Or store the cache to a file for later use memoize(:fib, "fib.cache") fib(100) # Fast = Constants Memoize::MEMOIZE_VERSION Returns the version of this package as a String. = Methods Memoize#memoize(method, file=nil) Takes a +method+ (symbol) and caches the results of +method+ in a hash table. If you call +method+ again with the same arguments, memoize gives you the value from the table instead of letting the method compute the value again. If +file+ is provided, the results are cached to that file. Note that this uses Marshal internally. Beware of changes in the Marshal format should you happen to upgrade. Returns the cache, which you can inspect or manipulate directly if you are so inclined. = Acknowledgements Code borrowed from Nobu Nakada (ruby-talk:155159). Code borrowed from Ara Howard (ruby-talk:173428). Code borrowed from Andrew Johnson (http://tinyurl.com/8ymx8) Ideas taken from Brian Buckley and Sean O'Halpin. Tiny URL provided for Andrew Johnson because I could not find the ruby-talk reference. The gateway may have been broken at the time. == Known Issues None that I'm aware of. Please report any problems on the Shards tracker or the forums at http://www.rubyforge.org/projects/shards. == License Artistic 2.0 == Warranty This package is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose. == Copyright (C) 2005-2009 Daniel J. Berger All Rights Reserved = Author Daniel J. Berger djberg96 at gmail dot com IRC nick: imperator (freenode) memoize-1.3.1/Rakefile0000644000175000017500000000134012135751313012701 0ustar daidairequire 'rake' require 'rake/testtask' require 'rbconfig' include Config desc 'Install the memoize library (non-gem)' task :install do sitelibdir = CONFIG['sitelibdir'] file = 'lib/memoize.rb' FileUtils.cp(file, sitelibdir, :verbose => true) end desc 'Install the memoize library as a gem' task :install_gem do ruby 'memoize.gemspec' file = Dir['*.gem'].first sh 'gem install #{file}' end desc 'Run the fibonacci example & benchmarks' task :example_fib do ruby '-Ilib examples/example_fibonacci.rb' end desc 'Run the memoize example & benchmarks' task :example_memoize do ruby '-Ilib examples/example_memoize.rb' end Rake::TestTask.new do |t| t.libs << 'test' t.verbose = true t.warning = true end memoize-1.3.1/memoize.gemspec0000644000175000017500000000163412135751313014254 0ustar daidairequire 'rubygems' spec = Gem::Specification.new do |gem| gem.name = 'memoize' gem.version = '1.3.1' gem.author = 'Daniel J. Berger' gem.license = 'Artistic 2.0' gem.email = 'djberg96@gmail.com' gem.homepage = 'http://www.rubyforge.org/projects/shards' gem.platform = Gem::Platform::RUBY gem.summary = 'Speeds up methods at the cost of memory (or disk space)' gem.test_file = 'test/test_memoize.rb' gem.has_rdoc = true gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') } gem.rubyforge_project = 'shards' gem.extra_rdoc_files = ['MANIFEST', 'README', 'CHANGES'] gem.add_development_dependency('test-unit', '>= 2.0.2') gem.description = <<-EOF The memoize library allows you to cache methods for faster lookup. Cached results can either be stored in memory (the default) or to a file. EOF end Gem::Builder.new(spec).build memoize-1.3.1/MANIFEST0000644000175000017500000000024112135751313012364 0ustar daidai* CHANGES * MANIFEST * README * Rakefile * memoize.gemspec * examples/example_fibonacci.rb * examples/example_memoize.rb * lib/memoize.rb * test/test_memoize.rb memoize-1.3.1/lib/0000755000175000017500000000000012135751313012004 5ustar daidaimemoize-1.3.1/lib/memoize.rb0000644000175000017500000000113112135751313013772 0ustar daidaimodule Memoize # The version of the memoize library MEMOIZE_VERSION = '1.3.1' # Memoize the method +name+. If +file+ is provided, then the method results # are stored on disk as well as in memory. def memoize(name, file=nil) cache = File.open(file, 'rb'){ |io| Marshal.load(io) } rescue {} (class<