繁体   English   中英

使用Packed Arrays进行Woking,访问数据

[英]Woking with Packed Arrays, accessing data

我正在使用一个大型图像阵列,所以我把它打包了。 为了访问数组中的像素,我实现了两种方法。

def get_p(a)
  data=a.unpack('S9s')
end
def put_p(array,index_a,value)
  index=index_a[0]
  k=array.unpack('S9s')
  k[index]=value
  k.pack('S9s')
end

它有效,但我想知道是否有更优雅的方式来做到这一点。 使我的代码看起来与我的标准数组函数不同。

If get_p(image_data[i][j+1])[BLOB]==0

VS

if image_data[i][j+1][BLOB]==0

此外,不知道是否有人关心,但解压缩似乎没有在任何地方记录,我很幸运在这里找到一个参考,但它花了一些时间。

你可以像下面这样的课程:

class PackedArray

  def initialize(array)
    @packed_array = array.pack('S9s')
  end

  def [](key)
    data = @packed_array.unpack('S9s')
    data[key]
  end

  def []=(key, val)
    k = @packed_array.unpack('S9s')
    k[key]=val
    @packed_array = k.pack('S9s')
  end
end

然后,使用image_data[i][j]的实例填充image_data[i][j] 例如

for i in [0..image_data.size]
  for j in [0..image_data[i].size]
    image_data[i][j] = new PackedArray(image_data[i][j])  
  end
end

最后你可以简单地使用:

if image_data[i][j+1][BLOB] == 0

无需手动打包/拆包。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM