0

我用 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

想法?

干杯,

马丁

4

1 回答 1

1

我不确定我是否正确理解了背景,可能的解决方案是:

def bits_on
  NArray.to_na(@count_array)[self.byte_array].sum
end

对不起,上面是错误的,接下来会起作用:

def bits_on
  index = NArray.int(*self.byte_array.shape)
  index[] = self.byte_array
  NArray.to_na(@count_array)[index].sum
end
于 2011-10-17T15:56:37.367 回答