sized_list 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rdoc", "~> 3.12"
5
+ gem "jeweler", "~> 1.8.4"
6
+ gem "rspec", ">= 0"
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.2.4)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.7)
12
+ rake (10.0.4)
13
+ rdoc (3.12.2)
14
+ json (~> 1.4)
15
+ rspec (2.13.0)
16
+ rspec-core (~> 2.13.0)
17
+ rspec-expectations (~> 2.13.0)
18
+ rspec-mocks (~> 2.13.0)
19
+ rspec-core (2.13.1)
20
+ rspec-expectations (2.13.0)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.13.1)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ jeweler (~> 1.8.4)
29
+ rdoc (~> 3.12)
30
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Doug Youch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = sized_list
2
+
3
+ Keeps only a limited number of items in a list. It removes the least recently used item.
4
+
5
+ == Contributing to sized_list
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Doug Youch. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "sized_list"
18
+ gem.homepage = "http://github.com/dyouch5@yahoo.com/sized_list"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Limited size list}
21
+ gem.description = %Q{Uses LRU functionality to keep a limited size list of items}
22
+ gem.email = "doug@sessionm.com"
23
+ gem.authors = ["Doug Youch"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "sized_list #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/sized_list.rb ADDED
@@ -0,0 +1,63 @@
1
+ class SizedList
2
+ include Enumerable
3
+
4
+ attr_reader :max_size
5
+
6
+ def initialize(max_size)
7
+ @max_size = max_size
8
+ @used = []
9
+ @items = {}
10
+ end
11
+
12
+ def get(key)
13
+ if value = @items[key]
14
+ used! key
15
+ end
16
+ value
17
+ end
18
+ alias [] get
19
+
20
+ def set(key, value)
21
+ @items[key] = value
22
+ remove_least_recently_used! if @items.size > @max_size
23
+ used! key
24
+ nil
25
+ end
26
+ alias []= set
27
+
28
+ def each
29
+ @items.each do |k, v|
30
+ yield v
31
+ end
32
+ end
33
+
34
+ def size
35
+ @items.size
36
+ end
37
+
38
+ def keys
39
+ @used
40
+ end
41
+
42
+ def values
43
+ @items.values
44
+ end
45
+
46
+ private
47
+
48
+ def used!(key)
49
+ if @used.first == key
50
+ # no-op
51
+ elsif @used.last == key
52
+ @used.unshift @used.pop
53
+ else
54
+ @used.reject! { |k| k == key }
55
+ @used.unshift key
56
+ end
57
+ end
58
+
59
+ def remove_least_recently_used!
60
+ key = @used.pop
61
+ @items.delete key
62
+ end
63
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe SizedList do
4
+ it "should store a limited number of items" do
5
+ list = SizedList.new 5
6
+ 5.times { |i| list.set "item-#{i}", 1 }
7
+ list.size.should == 5
8
+ 100.times { |i| list.set "new-item-#{i}", 1 }
9
+ list.size.should == 5
10
+ list['new-item-99'].should == 1
11
+ end
12
+
13
+ it "should keep the most recent used item at the top of the list" do
14
+ list = SizedList.new 5
15
+ list['a'] = 1
16
+ list['b'] = 1
17
+ list['c'] = 1
18
+ list['d'] = 1
19
+ list['e'] = 1
20
+ list.keys.should == ['e', 'd', 'c', 'b', 'a']
21
+
22
+ list['b'].should == 1
23
+ list.keys.should == ['b', 'e', 'd', 'c', 'a']
24
+
25
+ list['b'].should == 1
26
+ list.keys.should == ['b', 'e', 'd', 'c', 'a']
27
+ end
28
+
29
+ it "should remove the least accessed" do
30
+ list = SizedList.new 5
31
+ list['a'] = 1
32
+ list['b'] = 1
33
+ list['c'] = 1
34
+ list['d'] = 1
35
+ list['e'] = 1
36
+ list.keys.should == ['e', 'd', 'c', 'b', 'a']
37
+
38
+ list['a'].should == 1
39
+ list.keys.should == ['a', 'e', 'd', 'c', 'b']
40
+
41
+ list['new'] = 1
42
+ list.keys.should == ['new', 'a', 'e', 'd', 'c']
43
+
44
+ list['d'].should == 1
45
+ list.keys.should == ['d', 'new', 'a', 'e', 'c']
46
+
47
+ list['new2'] = 1
48
+ list.keys.should == ['new2', 'd', 'new', 'a', 'e']
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'sized_list'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sized_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Doug Youch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Uses LRU functionality to keep a limited size list of items
63
+ email: doug@sessionm.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.rdoc
75
+ - Rakefile
76
+ - VERSION
77
+ - lib/sized_list.rb
78
+ - spec/sized_list_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: http://github.com/dyouch5@yahoo.com/sized_list
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: 1772257885057037827
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.23
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Limited size list
108
+ test_files: []