Class: Bitmap

Inherits:
Hash
  • Object
show all
Defined in:
lib/bitmap.rb

Overview

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                 Version 2, December 2004

Copyleft meh. [http://meh.paranoid.pk | [email protected]]

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Defined Under Namespace

Classes: Value

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Bitmap

Returns a new instance of Bitmap.



56
57
58
59
# File 'lib/bitmap.rb', line 56

def initialize (data)
  merge!(data)
  freeze
end

Instance Method Details

#[](*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bitmap.rb', line 61

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?

      key("#{bit}#{'0' * index}".to_i(2)) or raise ArgumentError, "unknown bit at #{index}"
    }.compact)
  else
    Value.new(self, args.map {|arg|
      super(arg) or super(arg.to_s.to_sym) or raise ArgumentError, "unknown symbol #{arg}"
    }.inject {|a, b|
      a | b
    }, args)
  end
end

#allObject



80
81
82
83
84
# File 'lib/bitmap.rb', line 80

def all
  Value.new(self, values.inject {|a, b|
    a | b
  }, keys)
end