我用 NArray 实现了一个位数组,但是我对 bits_on 方法的速度不是很满意。目前我有:
# Method that returns the number of bits set "on" in a bit array.
def bits_on
bits_on = 0
self.byte_array.each do |byte|
bits_on += @count_array[byte]
end
bits_on
end
byte_array
是一个 NArray.byte() 类型,并且@count_array
是这样构建的:
# Method that returns an array where the element index value is
# the number of bits set for that index value.
def init_count_array
count_array = []
(0 ... (2 ** BitsInChar)).each do |i|
count_array << bits_in_char(i)
end
count_array
end
想法?
干杯,
马丁