fsfifo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1873cb49b135d37167ccf99dee689680af2db32e
4
+ data.tar.gz: 7f444b3acba163fcaf2a559c715531829bc36cc6
5
+ SHA512:
6
+ metadata.gz: c392f2a447179efda3885bef1599ba26826890fc5a326f000bfec3b46f96f3c0126312d23a78610e5562b8c3a3c4d08cccdee3b7f38ad10148609d67a0770cdb
7
+ data.tar.gz: f05d3151607942b4fceb4638e91200813140182ac67d8976fa672d3defe7ed0ec84d6b45e45fc1a88f8a1edfddb3e6c2f3dff6ae2e04d29735f6ed62a5e48109
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,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.3.5"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "simplecov-rcov", ">= 0"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 YAMAMOTO, Masayuki
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/Rakefile ADDED
@@ -0,0 +1,57 @@
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 = "fsfifo"
18
+ gem.homepage = "http://github.com/mephistobooks/fsfifo"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Fixed size FIFO.}
21
+ gem.description = %Q{Fixed size FIFO, LIFO, and Array.}
22
+ gem.email = "martin.route66.blues+github@gmail.com"
23
+ gem.authors = ["YAMAMOTO, Masayuki"]
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.pattern = 'test/**/test*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ #require 'rcov/rcovtask'
37
+ #Rcov::RcovTask.new do |test|
38
+ # test.libs << 'test'
39
+ # test.pattern = 'test/**/test_*.rb'
40
+ # test.verbose = true
41
+ # test.rcov_opts << '--exclude "gems/*"'
42
+ #end
43
+ #require 'simplecov'
44
+ #require 'simplecov-rcov'
45
+ #SimpleCov.start if ENV["COVERAGE"] # to do this, type COVERAGE=true rake test
46
+
47
+ task :default => :test
48
+
49
+ require 'rdoc/task'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "fsfifo #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/ReadMe.md ADDED
@@ -0,0 +1,74 @@
1
+ # fsfifo ReadMe
2
+
3
+ ## Fixed Size FIFO, LIFO, Array lib.
4
+
5
+ fsfifo.rb defines FSFIFO, which is the fixed size FIFO (Queue).
6
+
7
+ This extends the built-in Array so you can easily understand it. (fsfifo.rb also defines FSLIFO and FSArray, and FSArray is the most general class)
8
+
9
+
10
+ ## Usage
11
+
12
+ Do ```[sudo] gem install fsfifo``` and ```require 'fsfifo'``` for preparation.
13
+
14
+ ### new
15
+
16
+ Just ```FSFIFO.new(4)``` or ```FSFIFO.new( size: 4 )``` and then you can create a FIFO whose size is 4:
17
+
18
+ ```
19
+ >>fsf = FSFIFO.new( 4 )
20
+ => []
21
+ >> fsf.size
22
+ => 0
23
+ >> fsf.fifosize
24
+ => 4
25
+ ```
26
+
27
+ ### size
28
+ Here, FSFIFO#size means the number of elements and FSFIFO#fifosize means the size of FIFO. You can also change the size of FIFO by FSFIFO#resize.
29
+
30
+ ### operation
31
+
32
+ And you can push objects like this:
33
+
34
+ ```
35
+ >> fsf.push( 1 )
36
+ => [1]
37
+
38
+ >> fsf.push( 2 )
39
+ => [1, 2]
40
+
41
+ >> fsf.push( 3 )
42
+ => [1, 2, 3]
43
+
44
+ >> fsf.push( 4 )
45
+ => [1, 2, 3, 4]
46
+
47
+ >> fsf.push( 5 )
48
+ => [2, 3, 4, 5]
49
+ ```
50
+
51
+ You can also shift and resize.
52
+
53
+ ```
54
+ >> fsf
55
+ => [2, 3, 4, 5]
56
+ >> fsf.shift
57
+ => 2
58
+ >> fsf
59
+ => [3, 4, 5]
60
+ >> fsf.resize( 2 )
61
+ => [3]
62
+ >> fsf
63
+ => [4, 5]
64
+ >> fsf.push( 6 )
65
+ => [5, 6]
66
+ ```
67
+
68
+
69
+ ## License
70
+
71
+ MIT License.
72
+
73
+
74
+ # End
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/fsfifo.rb ADDED
@@ -0,0 +1,134 @@
1
+ #
2
+ # filename: fs_fifo.rb
3
+ #
4
+ #
5
+
6
+
7
+ #
8
+ # Fixed Size FIFO, LIFO, and Array.
9
+ #
10
+ #
11
+ class FSArray < Array
12
+
13
+ #
14
+ # FSArray#new( n )
15
+ # FSArray#new( size: n, of_enbl: true, of_proc: Proc.new{...} )
16
+ #
17
+ def initialize( args = {} )
18
+
19
+ if args.class == Fixnum
20
+ args = { size: args, wm_size: args }
21
+ end
22
+
23
+ @opt = {
24
+ :size => 1,
25
+ :of_enbl => false,
26
+ :of_proc => Proc.new{},
27
+ :uf_enbl => false,
28
+ :uf_proc => Proc.new{},
29
+ :push_enbl => false, #TODO.
30
+ :push_proc => Proc.new{},
31
+ :wm_size => 1,
32
+ :wm_enbl => false,
33
+ :wm_proc => Proc.new{},
34
+ }.merge( args )
35
+
36
+ @shifted = nil #TODO.
37
+
38
+ #
39
+ super( )
40
+
41
+ end
42
+
43
+ #
44
+ # Over/underflow callback methods getter/setter, etc.
45
+ #
46
+ def of_proc; @opt[:of_proc]; end
47
+ def of_proc=( newp ); @opt[:of_proc] = newp; end
48
+ def of_enable; @opt[:of_enbl] = true; end
49
+ def of_disable; @opt[:of_enbl] = false; end
50
+ def of_enable?; @opt[:of_enbl] ; end
51
+
52
+ def uf_proc; @opt[:uf_proc]; end
53
+ def uf_proc=( newp ); @opt[:uf_proc] = newp; end
54
+ def uf_enable; @opt[:uf_enbl] = true; end
55
+ def uf_disable; @opt[:uf_enbl] = false; end
56
+ def uf_enable?; @opt[:uf_enbl] ; end
57
+
58
+ #
59
+ # FIFO and data size-related methods.
60
+ #
61
+ def fifosize; @opt[:size]; end
62
+ # Array#size indicates the number of elements in fifo.
63
+ def resize( n )
64
+ ret = []
65
+ @opt[:wm_size] = n if @opt[:size] == @opt[:wm_size]
66
+ @opt[:size] = n
67
+ ret << self.shift_org while self.size > n
68
+
69
+ return ret
70
+ end
71
+
72
+ #
73
+ alias :shift_org :shift
74
+ alias :push_org :push
75
+
76
+ #
77
+ # shift and push - main methods of FSFIFO.
78
+ #
79
+ def shift
80
+ ret = shift_org
81
+
82
+ # callback method when under flow is enabled.
83
+ if self.size == 0
84
+ if @opt[:uf_enbl]
85
+ @opt[:uf_proc].call
86
+ end
87
+ end
88
+
89
+ return ret
90
+ end
91
+
92
+ def push( obj )
93
+
94
+ #
95
+ push_org( obj )
96
+
97
+ #
98
+ of_called = false
99
+ wm_called = false
100
+ while self.size > @opt[:size]
101
+ #
102
+ shift_org
103
+
104
+ # overflow callback.
105
+ if @opt[:of_enbl] and not(of_called)
106
+ @opt[:of_proc].call
107
+ of_called = true
108
+ end
109
+
110
+ # TODO.watermark callback.
111
+ if @opt[:wm_enbl] and not(wm_called)
112
+ @opt[:wm_proc].call
113
+ wm_called = true
114
+ else
115
+ #raise ""
116
+ end
117
+ end
118
+
119
+ return self
120
+ end
121
+
122
+ end
123
+
124
+
125
+ class FSFIFO < FSArray
126
+ undef pop
127
+ end
128
+
129
+ class FSLIFO < FSArray
130
+ undef shift
131
+ end
132
+
133
+
134
+ #### endof filename: fixed_array.rb
data/test/helper.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ require 'simplecov-rcov'
3
+ SimpleCov.start if ENV["COVERAGE"] # to do this, type COVERAGE=true rake test
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+ begin
8
+ Bundler.setup(:default, :development)
9
+ rescue Bundler::BundlerError => e
10
+ $stderr.puts e.message
11
+ $stderr.puts "Run `bundle install` to install missing gems"
12
+ exit e.status_code
13
+ end
14
+ require 'test/unit'
15
+ require 'shoulda'
16
+
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+ require 'fsfifo'
20
+
21
+ class Test::Unit::TestCase
22
+ end
@@ -0,0 +1,205 @@
1
+ #
2
+ # filename: test-fs_array.rb
3
+ #
4
+ #
5
+
6
+ # Unit Test Lib.
7
+ #require "rubygems"
8
+ #gem "test-unit"
9
+ #require "test/unit"
10
+ require 'helper'
11
+ require 'test/unit'
12
+
13
+ # required lib.
14
+ #require 'something'
15
+
16
+ # Testee.
17
+ require "fsfifo"
18
+
19
+
20
+ # Test Cases.
21
+ class TestFSArray < Test::Unit::TestCase
22
+
23
+ def setup
24
+ @fifo = FSFIFO.new
25
+
26
+ @of_proc_called = false
27
+ @uf_proc_called = false
28
+
29
+ @fifo.of_proc = Proc.new { p "of called."; @of_proc_called ^= true }
30
+ @fifo.uf_proc = Proc.new { p "uf called."; @uf_proc_called ^= true }
31
+ end
32
+
33
+ def teardown
34
+
35
+ end
36
+
37
+ ####
38
+
39
+ def test_new
40
+
41
+ ff = FSFIFO.new( size: 10 )
42
+ assert_equal( 0, ff.size )
43
+ assert_equal( 10, ff.fifosize )
44
+
45
+ end
46
+
47
+ def test_size
48
+
49
+ assert_equal( 0, @fifo.size )
50
+ assert_equal( 1, @fifo.fifosize )
51
+
52
+ end
53
+
54
+ def test_resize
55
+
56
+ assert_equal( 0, @fifo.size )
57
+ assert_equal( 1, @fifo.fifosize )
58
+
59
+ #
60
+ @fifo.resize(10)
61
+ assert_equal( 0, @fifo.size )
62
+ assert_equal( 10, @fifo.fifosize )
63
+
64
+ end
65
+
66
+ ####
67
+
68
+ def test_push_and_shift
69
+
70
+ #
71
+ assert_equal( 0, @fifo.size )
72
+ assert_equal( 1, @fifo.fifosize )
73
+
74
+ #
75
+ @fifo.push( 'a' )
76
+ assert_equal( 1, @fifo.size )
77
+ assert_equal( 1, @fifo.fifosize )
78
+ assert_equal( 'a', @fifo[0] )
79
+
80
+ ret = @fifo.shift
81
+ assert_equal( 0, @fifo.size )
82
+ assert_equal( 1, @fifo.fifosize )
83
+ assert_equal( [], @fifo )
84
+ assert_equal( 'a', ret )
85
+
86
+ @fifo.push( 'c' )
87
+ assert_equal( 1, @fifo.size )
88
+ assert_equal( 1, @fifo.fifosize )
89
+ assert_equal( 'c', @fifo[0] )
90
+
91
+ end
92
+
93
+ def test_resize_and_push
94
+
95
+ #
96
+ @fifo.resize( 4 )
97
+ assert_equal( 0, @fifo.size )
98
+ assert_equal( 4, @fifo.fifosize )
99
+
100
+ #
101
+ ret = @fifo.push( 'm' )
102
+ assert_equal( 1, @fifo.size )
103
+ assert_equal( 4, @fifo.fifosize )
104
+ assert_equal( 'm', ret.join )
105
+
106
+ @fifo.push( 'o' )
107
+ assert_equal( 2, @fifo.size )
108
+ assert_equal( 4, @fifo.fifosize )
109
+ assert_equal( 'mo', @fifo.join )
110
+
111
+ @fifo.push( 'e' )
112
+ assert_equal( 3, @fifo.size )
113
+ assert_equal( 4, @fifo.fifosize )
114
+ assert_equal( 'moe', @fifo.join )
115
+
116
+ @fifo.push( 'm' )
117
+ assert_equal( 4, @fifo.size )
118
+ assert_equal( 4, @fifo.fifosize )
119
+ assert_equal( 'moem', @fifo.join )
120
+
121
+ @fifo.push( 'o' )
122
+ assert_equal( 4, @fifo.size )
123
+ assert_equal( 4, @fifo.fifosize )
124
+ assert_equal( 'oemo', @fifo.join )
125
+
126
+ @fifo.push( 'e' )
127
+ assert_equal( 4, @fifo.size )
128
+ assert_equal( 4, @fifo.fifosize )
129
+ assert_equal( 'emoe', @fifo.join )
130
+
131
+ end
132
+
133
+ def test_resize_push_and_resize
134
+
135
+ #
136
+ @fifo.resize( 4 )
137
+ assert_equal( 0, @fifo.size )
138
+ assert_equal( 4, @fifo.fifosize )
139
+
140
+ #
141
+ "moemoe".split(/\B/).each do |e|
142
+ @fifo.push( e )
143
+ end
144
+ assert_equal( 4, @fifo.size )
145
+ assert_equal( 4, @fifo.fifosize )
146
+ assert_equal( 'emoe', @fifo.join )
147
+
148
+ #
149
+ @fifo.resize(2)
150
+ assert_equal( 2, @fifo.size )
151
+ assert_equal( 2, @fifo.fifosize )
152
+ assert_equal( 'oe', @fifo.join )
153
+
154
+ #
155
+ @fifo.resize( 4 )
156
+ assert_equal( 2, @fifo.size )
157
+ assert_equal( 4, @fifo.fifosize )
158
+
159
+ end
160
+
161
+ ####
162
+
163
+ def test_of
164
+
165
+ @fifo.of_enable
166
+ assert_equal( true, @fifo.of_enable? )
167
+
168
+ @fifo.push( 1 )
169
+ @fifo.push( 2 )
170
+ assert_equal( true, @of_proc_called )
171
+
172
+ @fifo.push( 3 )
173
+ assert_equal( false, @of_proc_called )
174
+
175
+ @fifo.of_disable
176
+ @fifo.push( 4 )
177
+ assert_equal( false, @of_proc_called )
178
+
179
+ end
180
+
181
+ def test_uf
182
+
183
+ @fifo.uf_enable
184
+ assert_equal( true, @fifo.uf_enable? )
185
+
186
+ @fifo.push( 1 )
187
+ @fifo.shift
188
+ assert_equal( true, @uf_proc_called )
189
+
190
+ @fifo.push( 2 )
191
+ @fifo.shift
192
+ assert_equal( false, @uf_proc_called )
193
+
194
+ @fifo.push( 3 )
195
+ @fifo.uf_disable
196
+ @fifo.shift
197
+ assert_equal( false, @uf_proc_called )
198
+
199
+ end
200
+
201
+
202
+ end
203
+
204
+
205
+ #### endof filename: test-fs_array.rb
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fsfifo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - YAMAMOTO, Masayuki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov-rcov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Fixed size FIFO, LIFO, and Array.
84
+ email: martin.route66.blues+github@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ files:
90
+ - .document
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - Rakefile
94
+ - ReadMe.md
95
+ - VERSION
96
+ - lib/fsfifo.rb
97
+ - test/helper.rb
98
+ - test/test_fsfifo.rb
99
+ homepage: http://github.com/mephistobooks/fsfifo
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.0.6
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Fixed size FIFO.
123
+ test_files: []