Class: Bitmap
- Inherits:
-
Object
- Object
- Bitmap
- Defined in:
- lib/bitmap.rb
Overview
-- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- You just DO WHAT THE FUCK YOU WANT TO. ++
Defined Under Namespace
Classes: Value
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #all ⇒ Object
- #fields ⇒ Object
-
#initialize(data) ⇒ Bitmap
constructor
A new instance of Bitmap.
- #inspect ⇒ Object
Constructor Details
#initialize(data) ⇒ Bitmap
Returns a new instance of Bitmap.
54 55 56 57 58 59 60 61 |
# File 'lib/bitmap.rb', line 54 def initialize (data) if data.first.first.is_a? Integer data = Hash[data.map(&:reverse)] end @bits = data @bits.freeze end |
Instance Method Details
#[](*args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bitmap.rb', line 63 def [] (*args) args.flatten! args.compact! if args.length == 1 && args.first.is_a?(Integer) Value.new(self, args.first, args.first.to_s(2).reverse.chars.each_with_index.map {|bit, index| next if bit.to_i.zero? @bits.key("#{bit}#{'0' * index}".to_i(2)) or raise ArgumentError, "unknown bit at #{index}" }.compact) else Value.new(self, args.map {|arg| @bits[arg] or @bits[arg.to_s.to_sym] or raise ArgumentError, "unknown symbol #{arg}" }.inject {|a, b| a | b }, args) end end |
#all ⇒ Object
82 83 84 85 86 |
# File 'lib/bitmap.rb', line 82 def all Value.new(self, @bits.values.inject {|a, b| a | b }, @bits.keys) end |
#fields ⇒ Object
88 89 90 |
# File 'lib/bitmap.rb', line 88 def fields @bits.keys end |
#inspect ⇒ Object
92 93 94 |
# File 'lib/bitmap.rb', line 92 def inspect "#<#{self.class.name}: #{@bits.map { |name, bit| "#{bit.to_s(16)}=#{name}" }.join ' '}>" end |