Skip to content

Instantly share code, notes, and snippets.

@kimyoutora
Created February 15, 2012 01:27
Show Gist options
  • Save kimyoutora/1832329 to your computer and use it in GitHub Desktop.
Save kimyoutora/1832329 to your computer and use it in GitHub Desktop.
Matching Bitmask Nodes in Tree
class BitMaskNode
attr_accessor :children, :name
attr_reader :mask
def initialize(name, bitmask)
@name = name
@mask = bitmask
@children = []
end
def has_mask?(bitmask)
(mask ^ bitmask).zero?
end
def all(bitmask)
matches = []
matches << self.name if has_mask?(bitmask)
children.each do |child|
matches += child.all(bitmask)
end
matches
end
end
A = BitMaskNode.new('A', 0b1110)
B = BitMaskNode.new('B', 0b1011)
C = BitMaskNode.new('C', 0b1111)
D = BitMaskNode.new('D', 0b1101)
L = BitMaskNode.new('L', 0b0011)
F = BitMaskNode.new('F', 0b0110)
Z = BitMaskNode.new('Z', 0b0001)
X = BitMaskNode.new('X', 0b0101)
A.children = [B,C,D,L,F]
B.children = [Z,X]
puts A.all(0b1110).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment