bitmap 0.0.1

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.
Files changed (2) hide show
  1. data/lib/bitmap.rb +62 -0
  2. metadata +65 -0
@@ -0,0 +1,62 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
6
+ #
7
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9
+ #
10
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
11
+ #++
12
+
13
+ class Bitmap < Hash
14
+ class Value
15
+ def initialize (value, names)
16
+ @value = value.to_i
17
+ @names = names
18
+ end
19
+
20
+ def has? (name)
21
+ @names.member?(name)
22
+ end
23
+
24
+ def to_a
25
+ @names
26
+ end
27
+
28
+ def to_i
29
+ @value
30
+ end
31
+ end
32
+
33
+ def initialize (data)
34
+ merge!(data)
35
+ freeze
36
+ end
37
+
38
+ def [] (*args)
39
+ args.flatten!
40
+ args.compact!
41
+
42
+ if args.length == 1 && args.first.is_a?(Integer)
43
+ Value.new(args.first, args.first.to_s(2).reverse.chars.each_with_index.map {|bit, index|
44
+ next if bit.to_i.zero?
45
+
46
+ index("#{bit}#{'0' * index}".to_i(2)) or raise ArgumentError, "unknown bit at #{index}"
47
+ }.compact)
48
+ else
49
+ Value.new(args.map {|arg|
50
+ super(arg) or super(arg.to_s.to_sym) or raise ArgumentError, "unknown symbol #{arg}"
51
+ }.inject {|a, b|
52
+ a | b
53
+ }, args)
54
+ end
55
+ end
56
+
57
+ def all
58
+ Value.new(values.inject {|a, b|
59
+ a | b
60
+ }, keys)
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitmap
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - meh.
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-06 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description:
22
+ email: meh@paranoici.org
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/bitmap.rb
31
+ homepage: http://github.com/meh/ruby-bitmap
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.6
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Simple and stupid bitmap implementation (bitmap as in bitset, not bitmap image)
64
+ test_files: []
65
+