0
我有一个3d布尔数组,其维数如下: bool myArray [streamCount] [dayCount] [minuteCount]; 其中 dayCount = 500,streamCount = 11,000且minuteCount = 400;使用位打包来模拟3d数组的功能c
我想通过使用位打包来显着缩小此数组的内存要求。 我需要保留随机访问任何值的能力,就像我现在使用3d数组一样。
下面是我设计的(脑死亡)方案。它存在的问题是要找到值,我需要设置if语句。有没有更简单的方法来做到这一点?
#define STREAM_COUNT 11000
#define DAY_COUNT 500
typedef struct s_minuteStorage
{
unsigned char a: 1;
unsigned char b: 1;
unsigned char c : 1;
unsigned char d : 1;
unsigned char e: 1;
unsigned char f: 1;
unsigned char g : 1;
unsigned char h : 1;
} minuteStorage;
typedef struct s_itemStorage
{
minuteStorage Minutes[STREAM_COUNT][50];
} itemStorage;
itemStorage *Items;
void allocStorage(void)
{
Items = (itemStorage *) ecalloc(DAY_COUNT, 1);
}
int getMinuteValue(int minuteIndex, int dayIndex, int streamIndex)
{
int minuteArrayIndex = minuteIndex/8;
int remainder = minuteIndex % 8;
int value;
if (remainder == 0)
value = Items[dayIndex].Minutes[streamIndex][minuteArrayIndex].a;
if (remainder == 1)
value = Items[dayIndex].Minutes[streamIndex][minuteArrayIndex].b;
if (remainder == 2)
value = Items[dayIndex].Minutes[streamIndex][minuteArrayIndex].c;
// etc
return(value);
}
谢谢你,要好得多。要设置值,它会是:(Items [dayIndex] .Minutes [streamIndex] [minuteArrayIndex] << remaining)| = 1? – PaeneInsula 2014-08-28 02:43:17
@ user994179:不完全。试试'm | = 1 << remaining'。 – 2014-08-28 02:57:35
在大多数架构中int会比char更快吗? – 2014-08-28 05:19:30