bitfifo 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bitfifo.gemspec +28 -0
- data/lib/bitfifo/version.rb +3 -0
- data/lib/bitfifo.rb +76 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd36212289b1bb01aafb67ac001e9a4f511b4534
|
4
|
+
data.tar.gz: eac39808f5e906f7c49ad38592dee9ce8b55f80d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97afebebf73bcd2b6cafc4ed3eba1ccb242de35f1e64d2308b97897d8d7b64b36b6132731d2d191bf2be0c04efb9a0cf12e428a797ae7e75ac3f3ed5aa5a51f7
|
7
|
+
data.tar.gz: 7a405b407246b9a8aa04245be63bfa090fc8007d50dd2aec67804546617c52d82502e649452073975b8e7a3460386e74fe8f454535ab7d251e7844c0bd704b23
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at bryan@bdlsys.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Bryan Colvin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Bitfifo
|
2
|
+
|
3
|
+
Bitfifo implements a FIFO for 1 or more bits of an Integer.
|
4
|
+
This will be needed for a compression and encryption system.
|
5
|
+
Could be used for any variable length integer based problems including base64 encode/decode.
|
6
|
+
Includes bit reversals for LSB first or MSB first systems.
|
7
|
+
No need to worry about overfill as a Bignum is used to store bits.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'bitfifo'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install bitfifo
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
A FIFO (first in first out) for variable length integers is a necessary component for data compression systems.
|
28
|
+
Basically, what you put is what you get in the same order. See the example below:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
|
32
|
+
# single bits:
|
33
|
+
fifo = Bitfifo.new
|
34
|
+
fifo.put true # converts true/false to 1/0
|
35
|
+
fifo.get # returns 1
|
36
|
+
|
37
|
+
# multiple bits:
|
38
|
+
fifo = Bitfifo.new
|
39
|
+
fifo.put 99, 7 # store 7 bits of the number 99
|
40
|
+
fifo.put 3,5 # store 5 bits of the number 3
|
41
|
+
fifo.put 0xffff # stores lsb bit only, others masked
|
42
|
+
fifo.count # >> 13 the number of stored bits
|
43
|
+
fifo.get 7 # >> 99
|
44
|
+
fifo.get 5 # >> 3
|
45
|
+
|
46
|
+
# reverse bits:
|
47
|
+
fifo = Bitfifo.new
|
48
|
+
fifo.rput 0b11010101, 8 # store 213 in reverse bit order
|
49
|
+
fifo.get 8 # >> 0b10101011 or 171
|
50
|
+
Bitfifo.reverse(171,8) # >> 213
|
51
|
+
|
52
|
+
# what you reverse put, you reverse get
|
53
|
+
fifo = Bitfifo.new
|
54
|
+
fifo.rput 99, 7 # store 7 bits of the number 99
|
55
|
+
fifo.rput 3,5 # store 5 bits of the number 3
|
56
|
+
fifo.rget 7 # >> 99
|
57
|
+
fifo.rget 5 # >> 3
|
58
|
+
```
|
59
|
+
|
60
|
+
The current methods are as follows:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# instance methods:
|
64
|
+
#count ... returns the number of stored bits
|
65
|
+
#size ... same thing as count
|
66
|
+
#put(data, nbits=nil) ... stores `nbits` of data or a single bit if `nbits` not specified
|
67
|
+
#rput(data, nbits=nil) ... reverses bit order of data, then calls #put
|
68
|
+
#get(nbits=1) ... retrieves nbits of data
|
69
|
+
#rget(nbits=1) ... retrieves nbits of data in reverse bit order
|
70
|
+
#empty? ... true if there is no data in FIFO
|
71
|
+
#empty ... clears all data from FIFO, returns what was cleared.
|
72
|
+
|
73
|
+
# class method:
|
74
|
+
# Bitfifo.reverse(data, nbits) ... returns reversed bit order of nbits worth of data. Other bits are masked.
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
I need to control this for the time being
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
90
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bitfifo"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/bitfifo.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bitfifo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bitfifo"
|
8
|
+
spec.version = Bitfifo::VERSION
|
9
|
+
spec.authors = ["Bryan Colvin"]
|
10
|
+
spec.email = ["bryan@bdlsys.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{FIFO system for 1 or more bits}
|
13
|
+
spec.description = %q{Bitfifo implements a FIFO for 1 or more bits of an Integer.
|
14
|
+
This will be needed for a compression and encryption system.
|
15
|
+
Could be used for any variable length integer based problems including base64 encode/decode.
|
16
|
+
Includes bit reversals for LSB first or MSB first systems.
|
17
|
+
No need to worry about overfill as a Bignum is used to store bits.}
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/lib/bitfifo.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "bitfifo/version"
|
2
|
+
|
3
|
+
class Bitfifo
|
4
|
+
def self.reverse(data,nb)
|
5
|
+
data &= (1 << nb) - 1
|
6
|
+
rtn = 0
|
7
|
+
nb.times do |ii|
|
8
|
+
rtn <<= 1
|
9
|
+
rtn |= (data & 1)
|
10
|
+
data >>= 1
|
11
|
+
end
|
12
|
+
rtn
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@bits = 0
|
17
|
+
@size = 0
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def put(data, nb=nil)
|
22
|
+
if nb.nil?
|
23
|
+
data=1 if data==true
|
24
|
+
data=0 if data==false
|
25
|
+
@bits <<= 1
|
26
|
+
@bits |= data & 1
|
27
|
+
@size += 1
|
28
|
+
else
|
29
|
+
mask = (1 << nb) - 1
|
30
|
+
data &= mask
|
31
|
+
@bits <<= nb
|
32
|
+
@bits |= data
|
33
|
+
@size += nb
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def get(nb=1)
|
38
|
+
mask = (1 << nb) - 1
|
39
|
+
rtn = (@bits & (mask << (@size-nb))) >> (@size-nb)
|
40
|
+
@size -= nb
|
41
|
+
if @size < 0
|
42
|
+
@size=0
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
rtn
|
46
|
+
end
|
47
|
+
|
48
|
+
def rput(data, nb=nil)
|
49
|
+
return put(data) if nb.nil?
|
50
|
+
put(self.class.reverse(data,nb),nb)
|
51
|
+
end
|
52
|
+
|
53
|
+
def rget(nb=1)
|
54
|
+
self.class.reverse(get(nb),nb)
|
55
|
+
end
|
56
|
+
|
57
|
+
def size
|
58
|
+
@size
|
59
|
+
end
|
60
|
+
|
61
|
+
def count
|
62
|
+
@size
|
63
|
+
end
|
64
|
+
|
65
|
+
def empty
|
66
|
+
rtn = @bits
|
67
|
+
@bits = 0
|
68
|
+
@size = 0
|
69
|
+
rtn
|
70
|
+
end
|
71
|
+
|
72
|
+
def empty?
|
73
|
+
@size==0
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitfifo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryan Colvin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |-
|
56
|
+
Bitfifo implements a FIFO for 1 or more bits of an Integer.
|
57
|
+
This will be needed for a compression and encryption system.
|
58
|
+
Could be used for any variable length integer based problems including base64 encode/decode.
|
59
|
+
Includes bit reversals for LSB first or MSB first systems.
|
60
|
+
No need to worry about overfill as a Bignum is used to store bits.
|
61
|
+
email:
|
62
|
+
- bryan@bdlsys.com
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- ".gitignore"
|
68
|
+
- ".rspec"
|
69
|
+
- ".travis.yml"
|
70
|
+
- CODE_OF_CONDUCT.md
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/console
|
76
|
+
- bin/setup
|
77
|
+
- bitfifo.gemspec
|
78
|
+
- lib/bitfifo.rb
|
79
|
+
- lib/bitfifo/version.rb
|
80
|
+
homepage:
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: FIFO system for 1 or more bits
|
104
|
+
test_files: []
|