otg-bitmap 1.0.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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ bin/
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.sublime-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in otg-bitmap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 James Gregory, On the Game
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Bitmap
2
+
3
+ Dead simple Bitmap implementation in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'otg-bitmap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install otg-bitmap
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ bmp = Bitmap.new(5)
23
+ bmp.on(0)
24
+ bmp.on(3)
25
+ bmp.to_s
26
+ # => {10010}
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/bitmap.rb ADDED
@@ -0,0 +1,78 @@
1
+ class Bitmap
2
+ VERSION = '1.0.0'
3
+
4
+ def initialize(size=0)
5
+ @map = {}
6
+
7
+ size.times do |i|
8
+ @map[i] = false
9
+ end
10
+ end
11
+
12
+ def clone
13
+ bmp = Bitmap.new @map.size
14
+ self.each do |index,value|
15
+ bmp[index] = value
16
+ end
17
+ bmp
18
+ end
19
+
20
+ def [] index
21
+ !!@map[index]
22
+ end
23
+
24
+ def []= index, value
25
+ @map[index] = !!value
26
+ self
27
+ end
28
+
29
+ def each(&block)
30
+ @map.each(&block)
31
+ self
32
+ end
33
+
34
+ def count(which=nil)
35
+ case which
36
+ when :on, true
37
+ @map.values.select { |x| !!x }.size
38
+ when :off, false
39
+ @map.values.select { |x| !x }.size
40
+ else
41
+ @map.values.size
42
+ end
43
+ end
44
+ alias_method :size, :count
45
+
46
+ def on(index)
47
+ @map[index] = true
48
+ self
49
+ end
50
+
51
+ def off(index)
52
+ @map[index] = false
53
+ self
54
+ end
55
+
56
+ def +(other)
57
+ bmp = clone
58
+
59
+ other.each do |index,value|
60
+ bmp.on index if value
61
+ end
62
+
63
+ bmp
64
+ end
65
+
66
+ def all?
67
+ @map.values.all? { |value| !!value }
68
+ end
69
+
70
+ def to_s
71
+ values = []
72
+ self.each do |index,value|
73
+ values[index] = value
74
+ end
75
+ values = values.map { |x| x ? 1 : 0 }.join('')
76
+ "{#{values}}"
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bitmap'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'otg-bitmap'
8
+ gem.version = Bitmap::VERSION
9
+ gem.authors = ['James Gregory', 'On the Game']
10
+ gem.email = ['james@onthegame.com.au']
11
+ gem.description = 'Dead simple Ruby Bitmap implementation, used for flags etc...'
12
+ gem.summary = 'Bitmap/bitset Ruby implementation'
13
+ gem.homepage = 'http://www.onthegame.com.au/about/opensource'
14
+
15
+ gem.add_development_dependency 'rspec'
16
+ gem.add_development_dependency 'rake'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,60 @@
1
+ describe Bitmap do
2
+ describe '#clone' do
3
+ subject { Bitmap.new.on(1) }
4
+
5
+ it 'should not have the same object id' do
6
+ subject.object_id.should_not eq subject.clone.object_id
7
+ end
8
+
9
+ its(:to_s) { should eq subject.clone.to_s }
10
+ end
11
+
12
+ describe '#+' do
13
+ before do
14
+ @bmp1 = Bitmap.new.on(0).on(1)
15
+ @bmp2 = Bitmap.new.on(0).on(3)
16
+ end
17
+
18
+ it 'should merge the bitmaps' do
19
+ (@bmp1 + @bmp2).to_s.should eq '{1101}'
20
+ end
21
+ end
22
+
23
+ describe '#count' do
24
+ describe :on do
25
+ it 'should count only the on bits' do
26
+ Bitmap.new(3).on(0).on(1).count(:on).should eq 2
27
+ end
28
+ end
29
+
30
+ describe :off do
31
+ it 'should count only the off bits' do
32
+ Bitmap.new(3).on(0).on(1).count(:off).should eq 1
33
+ end
34
+ end
35
+
36
+ it 'should count the bits' do
37
+ Bitmap.new(3).on(0).on(1).count.should eq 3
38
+ end
39
+ end
40
+
41
+ describe '#all?' do
42
+ describe 'when all are on' do
43
+ it 'should be true' do
44
+ Bitmap.new.on(0).on(1).all?.should eq true
45
+ end
46
+ end
47
+
48
+ describe 'when all are off' do
49
+ it 'should be true' do
50
+ Bitmap.new.off(0).off(1).all?.should eq false
51
+ end
52
+ end
53
+
54
+ describe 'when some are on' do
55
+ it 'should be true' do
56
+ Bitmap.new.off(0).on(1).all?.should eq false
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: otg-bitmap
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Gregory
9
+ - On the Game
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-03-07 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Dead simple Ruby Bitmap implementation, used for flags etc...
48
+ email:
49
+ - james@onthegame.com.au
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/bitmap.rb
60
+ - otg-bitmap.gemspec
61
+ - spec/bitmap_spec.rb
62
+ homepage: http://www.onthegame.com.au/about/opensource
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: 4231147258415211171
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: 4231147258415211171
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.25
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Bitmap/bitset Ruby implementation
92
+ test_files:
93
+ - spec/bitmap_spec.rb