gpt4 book ai didi

c++ - 实现查找表

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:31:15 28 4
gpt4 key购买 nike

我正在研究自定义数据结构,目前正在进行 Beta 测试:数据将存储在一个数组中,该数组可以表示为 4D、2D 和 1D 数组。这三个数组在 union 内声明,因为它代表相同的内存寻址。这是我类(class)的声明:

SomeClass.h

#ifndef SomeClass_H
#define SomeClass_H

class SomeClass {
public:
static const unsigned V1D_SIZE; // Single Or Linear Array Representation : Size 256 - 256 Elements
static const unsigned V2D_SIZE; // 2D Array [16][16] : Size 16 - 256 Elements
static const unsigned V4D_SIZE; // 4D Array [4][4][4][4] : Size 4 - 256 Elements

enum Index {
HEX_UNDER_FLOW = -0x01,
HEX_0 = 0x00,
HEX_1 = 0x01,
HEX_2 = 0x02,
HEX_3 = 0x03,
HEX_4 = 0x04,
HEX_5 = 0x05,
HEX_6 = 0x06,
HEX_7 = 0x07,
HEX_8 = 0x08,
HEX_9 = 0x09,
HEX_A = 0x0A,
HEX_B = 0x0B,
HEX_C = 0x0C,
HEX_D = 0x0D,
HEX_E = 0x0E,
HEX_F = 0x0F,
HEX_OVER_FLOW = 0x10
}; // Index

// Will Be Private After Testing.
union {
int m_p256[256];
int m_p16[16][16];
int m_p4[4][4][4][4];
};

private:
struct CrossReference {
Index A;
Index B;

CrossReference() {}
CrossReference( const Index& A_, const Index& B_ ) : A(A_), B(B_) {}
} typedef CR;


// Current Not Used Only Here To Illustrate A Simple Representation - May Use Or Incorporate Later
struct Cubes {
unsigned box; // 0 - 3 - w
unsigned slice; // 0 - 15 - z
unsigned row; // 0 - 15 - y
unsigned coloumn; // 0 - 15 - x
} m_cubes;


CR m_cr[256];

public:
SomeClass();
~SomeClass();

const CR& getIndexTable() const;
void displayIndexTable() const;

private:
// Must Be Called In Constructor First!
void initializeCrossReference();

}; // SomeClass

#endif // SomeClass

我需要生成此 4D 结构的坐标值对的引用查找表以实现其 1D 版本。

为了说明索引遍历,您可以引用这些图像以获得可视化表示:

3D Visual注意:此图像中的 {x,y,z,w} 并不代表内存中的实际 4D 数组:它只是一种可视化!在内存中,4D 数组将如下所示:array[box][slice][row][column] 并且此图像表示 4D 数组中的 1 个框。

2D Visual 1D LookUp注意:此图像是顶部图像中描述的所有 4 个框的 2D 视觉表示。两个图像中的彩色文本对应于相应的索引。

第二张图片是此 4D 数组数据结构的二维平面网格图像表示。我们可以使用此图像生成此查找表的坐标。

此图像的每个四分之一代表 1 个框。左上角(起始)是框 0,右上角框 1,左下角框 2,右下角框 3。每个框,可以用相同的方式分解为每个切片。左上(起始)切片 0,右上切片 1,左下切片 2,右下切片 3。然后在每个切片中工作时,遍历是 row - col,一旦 col = 3,我们向下移动到下一行。因此,在完成单个切片后,我们必须根据当前切片索引跳转我们的位置。盒子也是如此。

下面是几个遍历此表以生成一组十六进制索引坐标值的示例:

array[2][3][0][1] our [A,B] hex value would be [0x0C, 0x05] 
array[1][1][3][3] our [A,B] hex value would be [0x03, 0x0F]

我正在设计一个函数来生成这个查找表。我在这里手动完成了此操作:

void SomeClass::initializeCrossReference() {
//////////////////////////////////
// Upper Half Of Grid Structure //
//////////////////////////////////

// Grid Coordinates //
// Linear Or 1D Array Representation - Mapped From type[16][16] which is a
// 2D Compressed Version of type[4][4][4][4] -
// NOTE: This does not represent 4D Space; it represents a cluster of 4 (3D Cubes).
// In the declaration of the class there are two multi deminsional arrays that
// are shared in memory through a nameless union.

// ROW COL

// BOX - 0
// Slice - 0
// Row - 0 // Column Coordinates
m_cr[0].A = HEX_0; m_cr[0].B = HEX_0; // col 0
m_cr[1].A = HEX_0; m_cr[1].B = HEX_1; // col 1
m_cr[2].A = HEX_0; m_cr[2].B = HEX_2; // col 2
m_cr[3].A = HEX_0; m_cr[3].B = HEX_3; // col 3
// Row - 1
m_cr[4].A = HEX_1; m_cr[4].B = HEX_0;
m_cr[5].A = HEX_1; m_cr[5].B = HEX_1;
m_cr[6].A = HEX_1; m_cr[6].B = HEX_2;
m_cr[7].A = HEX_1; m_cr[7].B = HEX_3;
// Row - 2
m_cr[8].A = HEX_2; m_cr[8].B = HEX_0;
m_cr[9].A = HEX_2; m_cr[9].B = HEX_1;
m_cr[10].A = HEX_2; m_cr[10].B = HEX_2;
m_cr[11].A = HEX_2; m_cr[11].B = HEX_3;
// Row - 3
m_cr[12].A = HEX_3; m_cr[12].B = HEX_0;
m_cr[13].A = HEX_3; m_cr[13].B = HEX_1;
m_cr[14].A = HEX_3; m_cr[14].B = HEX_2;
m_cr[15].A = HEX_3; m_cr[15].B = HEX_3;

// Slice - 1
// Row - 0
m_cr[16].A = HEX_0; m_cr[16].B = HEX_4;
m_cr[17].A = HEX_0; m_cr[17].B = HEX_5;
m_cr[18].A = HEX_0; m_cr[18].B = HEX_6;
m_cr[19].A = HEX_0; m_cr[19].B = HEX_7;
// Row - 1
m_cr[20].A = HEX_1; m_cr[20].B = HEX_4;
m_cr[21].A = HEX_1; m_cr[21].B = HEX_5;
m_cr[22].A = HEX_1; m_cr[22].B = HEX_6;
m_cr[23].A = HEX_1; m_cr[23].B = HEX_7;
// Row - 2;
m_cr[24].A = HEX_2; m_cr[24].B = HEX_4;
m_cr[25].A = HEX_2; m_cr[25].B = HEX_5;
m_cr[26].A = HEX_2; m_cr[26].B = HEX_6;
m_cr[27].A = HEX_2; m_cr[27].B = HEX_7;
// Row - 3
m_cr[28].A = HEX_3; m_cr[28].B = HEX_4;
m_cr[29].A = HEX_3; m_cr[29].B = HEX_5;
m_cr[30].A = HEX_3; m_cr[30].B = HEX_6;
m_cr[31].A = HEX_3; m_cr[31].B = HEX_7;

// Slice - 2
// Row - 0
m_cr[32].A = HEX_4; m_cr[32].B = HEX_0;
m_cr[33].A = HEX_4; m_cr[33].B = HEX_1;
m_cr[34].A = HEX_4; m_cr[34].B = HEX_2;
m_cr[35].A = HEX_4; m_cr[35].B = HEX_3;
// Row - 1;
m_cr[36].A = HEX_5; m_cr[36].B = HEX_0;
m_cr[37].A = HEX_5; m_cr[37].B = HEX_1;
m_cr[38].A = HEX_5; m_cr[38].B = HEX_2;
m_cr[39].A = HEX_5; m_cr[39].B = HEX_3;
// Row - 2;
m_cr[40].A = HEX_6; m_cr[40].B = HEX_0;
m_cr[41].A = HEX_6; m_cr[41].B = HEX_1;
m_cr[42].A = HEX_6; m_cr[42].B = HEX_2;
m_cr[43].A = HEX_6; m_cr[43].B = HEX_3;
// Row - 3
m_cr[44].A = HEX_7; m_cr[44].B = HEX_0;
m_cr[45].A = HEX_7; m_cr[45].B = HEX_1;
m_cr[46].A = HEX_7; m_cr[46].B = HEX_2;
m_cr[47].A = HEX_7; m_cr[47].B = HEX_3;

// Slice - 3
// Row - 0
m_cr[48].A = HEX_4; m_cr[48].B = HEX_4;
m_cr[49].A = HEX_4; m_cr[49].B = HEX_5;
m_cr[50].A = HEX_4; m_cr[50].B = HEX_6;
m_cr[51].A = HEX_4; m_cr[51].B = HEX_7;
// Row - 1
m_cr[52].A = HEX_5; m_cr[52].B = HEX_4;
m_cr[53].A = HEX_5; m_cr[53].B = HEX_5;
m_cr[54].A = HEX_5; m_cr[54].B = HEX_6;
m_cr[55].A = HEX_5; m_cr[55].B = HEX_7;
// Row - 2;
m_cr[56].A = HEX_6; m_cr[56].B = HEX_4;
m_cr[57].A = HEX_6; m_cr[57].B = HEX_5;
m_cr[58].A = HEX_6; m_cr[58].B = HEX_6;
m_cr[59].A = HEX_6; m_cr[59].B = HEX_7;
// Row - 3
m_cr[60].A = HEX_7; m_cr[60].B = HEX_4;
m_cr[61].A = HEX_7; m_cr[61].B = HEX_5;
m_cr[62].A = HEX_7; m_cr[62].B = HEX_6;
m_cr[63].A = HEX_7; m_cr[63].B = HEX_7;

// BOX - 1
// Slice - 0
// Row - 0
m_cr[64].A = HEX_0; m_cr[64].B = HEX_8;
m_cr[65].A = HEX_0; m_cr[65].B = HEX_9;
m_cr[66].A = HEX_0; m_cr[66].B = HEX_A;
m_cr[67].A = HEX_0; m_cr[67].B = HEX_B;
// Row - 1
m_cr[68].A = HEX_1; m_cr[68].B = HEX_8;
m_cr[69].A = HEX_1; m_cr[69].B = HEX_9;
m_cr[70].A = HEX_1; m_cr[70].B = HEX_A;
m_cr[71].A = HEX_1; m_cr[71].B = HEX_B;
// Row - 2
m_cr[72].A = HEX_2; m_cr[72].B = HEX_8;
m_cr[73].A = HEX_2; m_cr[73].B = HEX_9;
m_cr[74].A = HEX_2; m_cr[74].B = HEX_A;
m_cr[75].A = HEX_2; m_cr[75].B = HEX_B;
// Row - 3
m_cr[76].A = HEX_3; m_cr[76].B = HEX_8;
m_cr[77].A = HEX_3; m_cr[77].B = HEX_9;
m_cr[78].A = HEX_3; m_cr[78].B = HEX_A;
m_cr[79].A = HEX_3; m_cr[79].B = HEX_B;

// Slice - 1
// Row - 0
m_cr[80].A = HEX_0; m_cr[80].B = HEX_C;
m_cr[81].A = HEX_0; m_cr[81].B = HEX_D;
m_cr[82].A = HEX_0; m_cr[82].B = HEX_E;
m_cr[83].A = HEX_0; m_cr[83].B = HEX_F;
// Row - 1
m_cr[84].A = HEX_1; m_cr[84].B = HEX_C;
m_cr[85].A = HEX_1; m_cr[85].B = HEX_D;
m_cr[86].A = HEX_1; m_cr[86].B = HEX_E;
m_cr[87].A = HEX_1; m_cr[87].B = HEX_F;
// Row - 2
m_cr[88].A = HEX_2; m_cr[88].B = HEX_C;
m_cr[89].A = HEX_2; m_cr[89].B = HEX_D;
m_cr[90].A = HEX_2; m_cr[90].B = HEX_E;
m_cr[91].A = HEX_2; m_cr[91].B = HEX_F;
// Row - 3
m_cr[92].A = HEX_3; m_cr[92].B = HEX_C;
m_cr[93].A = HEX_3; m_cr[93].B = HEX_D;
m_cr[94].A = HEX_3; m_cr[94].B = HEX_E;
m_cr[95].A = HEX_3; m_cr[95].B = HEX_F;

// Slice - 2
// Row - 0
m_cr[96].A = HEX_4; m_cr[96].B = HEX_8;
m_cr[97].A = HEX_4; m_cr[97].B = HEX_9;
m_cr[98].A = HEX_4; m_cr[98].B = HEX_A;
m_cr[99].A = HEX_4; m_cr[99].B = HEX_B;
// Row - 1
m_cr[100].A = HEX_5; m_cr[100].B = HEX_8;
m_cr[101].A = HEX_5; m_cr[101].B = HEX_9;
m_cr[102].A = HEX_5; m_cr[102].B = HEX_A;
m_cr[103].A = HEX_5; m_cr[103].B = HEX_B;
// Row - 2
m_cr[104].A = HEX_6; m_cr[104].B = HEX_8;
m_cr[105].A = HEX_6; m_cr[105].B = HEX_9;
m_cr[106].A = HEX_6; m_cr[106].B = HEX_A;
m_cr[107].A = HEX_6; m_cr[107].B = HEX_B;
// Row - 3
m_cr[108].A = HEX_7; m_cr[108].B = HEX_8;
m_cr[109].A = HEX_7; m_cr[109].B = HEX_9;
m_cr[110].A = HEX_7; m_cr[110].B = HEX_A;
m_cr[111].A = HEX_7; m_cr[111].B = HEX_B;

// Slice - 3
// Row - 0
m_cr[112].A = HEX_4; m_cr[112].B = HEX_C;
m_cr[113].A = HEX_4; m_cr[113].B = HEX_D;
m_cr[114].A = HEX_4; m_cr[114].B = HEX_E;
m_cr[115].A = HEX_4; m_cr[115].B = HEX_F;
// Row - 1
m_cr[116].A = HEX_5; m_cr[116].B = HEX_C;
m_cr[117].A = HEX_5; m_cr[117].B = HEX_D;
m_cr[118].A = HEX_5; m_cr[118].B = HEX_E;
m_cr[119].A = HEX_5; m_cr[119].B = HEX_F;
// Row - 2
m_cr[120].A = HEX_6; m_cr[120].B = HEX_C;
m_cr[121].A = HEX_6; m_cr[121].B = HEX_D;
m_cr[122].A = HEX_6; m_cr[122].B = HEX_E;
m_cr[123].A = HEX_6; m_cr[123].B = HEX_F;
// Row - 3
m_cr[124].A = HEX_7; m_cr[124].B = HEX_C;
m_cr[125].A = HEX_7; m_cr[125].B = HEX_D;
m_cr[126].A = HEX_7; m_cr[126].B = HEX_E;
m_cr[127].A = HEX_7; m_cr[127].B = HEX_F;

//////////////////////////////////
// Lower Half Of Grid Structure //
//////////////////////////////////

// Box - 2
// Slice - 0
// Row - 0
m_cr[128].A = HEX_8; m_cr[128].B = HEX_0; // col 0
m_cr[129].A = HEX_8; m_cr[129].B = HEX_1; // col 1
m_cr[130].A = HEX_8; m_cr[130].B = HEX_2; // col 2
m_cr[131].A = HEX_8; m_cr[131].B = HEX_3; // col 3
// Row - 1
m_cr[132].A = HEX_9; m_cr[132].B = HEX_0;
m_cr[133].A = HEX_9; m_cr[133].B = HEX_1;
m_cr[134].A = HEX_9; m_cr[134].B = HEX_2;
m_cr[135].A = HEX_9; m_cr[135].B = HEX_3;
// Row - 2
m_cr[136].A = HEX_A; m_cr[136].B = HEX_0;
m_cr[137].A = HEX_A; m_cr[137].B = HEX_1;
m_cr[138].A = HEX_A; m_cr[138].B = HEX_2;
m_cr[139].A = HEX_A; m_cr[139].B = HEX_3;
// Row - 3
m_cr[140].A = HEX_B; m_cr[140].B = HEX_0;
m_cr[141].A = HEX_B; m_cr[141].B = HEX_1;
m_cr[142].A = HEX_B; m_cr[142].B = HEX_2;
m_cr[143].A = HEX_B; m_cr[143].B = HEX_3;

// Slice - 1
// Row - 0
m_cr[144].A = HEX_8; m_cr[144].B = HEX_4;
m_cr[145].A = HEX_8; m_cr[145].B = HEX_5;
m_cr[146].A = HEX_8; m_cr[146].B = HEX_6;
m_cr[147].A = HEX_8; m_cr[147].B = HEX_7;
// Row - 1
m_cr[148].A = HEX_9; m_cr[148].B = HEX_4;
m_cr[149].A = HEX_9; m_cr[149].B = HEX_5;
m_cr[150].A = HEX_9; m_cr[150].B = HEX_6;
m_cr[151].A = HEX_9; m_cr[151].B = HEX_7;
// Row - 2;
m_cr[152].A = HEX_A; m_cr[152].B = HEX_4;
m_cr[153].A = HEX_A; m_cr[153].B = HEX_5;
m_cr[154].A = HEX_A; m_cr[154].B = HEX_6;
m_cr[155].A = HEX_A; m_cr[155].B = HEX_7;
// Row - 3
m_cr[156].A = HEX_B; m_cr[156].B = HEX_4;
m_cr[157].A = HEX_B; m_cr[157].B = HEX_5;
m_cr[158].A = HEX_B; m_cr[158].B = HEX_6;
m_cr[159].A = HEX_B; m_cr[159].B = HEX_7;

// Slice - 2
// Row - 0
m_cr[160].A = HEX_C; m_cr[160].B = HEX_0;
m_cr[161].A = HEX_C; m_cr[161].B = HEX_1;
m_cr[162].A = HEX_C; m_cr[162].B = HEX_2;
m_cr[163].A = HEX_C; m_cr[163].B = HEX_3;
// Row - 1;
m_cr[164].A = HEX_D; m_cr[164].B = HEX_0;
m_cr[165].A = HEX_D; m_cr[165].B = HEX_1;
m_cr[166].A = HEX_D; m_cr[166].B = HEX_2;
m_cr[167].A = HEX_D; m_cr[167].B = HEX_3;
// Row - 2;
m_cr[168].A = HEX_E; m_cr[168].B = HEX_0;
m_cr[169].A = HEX_E; m_cr[169].B = HEX_1;
m_cr[170].A = HEX_E; m_cr[170].B = HEX_2;
m_cr[171].A = HEX_E; m_cr[171].B = HEX_3;
// Row - 3
m_cr[172].A = HEX_F; m_cr[172].B = HEX_0;
m_cr[173].A = HEX_F; m_cr[173].B = HEX_1;
m_cr[174].A = HEX_F; m_cr[174].B = HEX_2;
m_cr[175].A = HEX_F; m_cr[175].B = HEX_3;

// Slice - 3
// Row - 0
m_cr[176].A = HEX_C; m_cr[176].B = HEX_4;
m_cr[177].A = HEX_C; m_cr[177].B = HEX_5;
m_cr[178].A = HEX_C; m_cr[178].B = HEX_6;
m_cr[179].A = HEX_C; m_cr[179].B = HEX_7;
// Row - 1
m_cr[180].A = HEX_D; m_cr[180].B = HEX_4;
m_cr[181].A = HEX_D; m_cr[181].B = HEX_5;
m_cr[182].A = HEX_D; m_cr[182].B = HEX_6;
m_cr[183].A = HEX_D; m_cr[183].B = HEX_7;
// Row - 2;
m_cr[184].A = HEX_E; m_cr[184].B = HEX_4;
m_cr[185].A = HEX_E; m_cr[185].B = HEX_5;
m_cr[186].A = HEX_E; m_cr[186].B = HEX_6;
m_cr[187].A = HEX_E; m_cr[187].B = HEX_7;
// Row - 3
m_cr[188].A = HEX_F; m_cr[188].B = HEX_4;
m_cr[189].A = HEX_F; m_cr[189].B = HEX_5;
m_cr[190].A = HEX_F; m_cr[190].B = HEX_6;
m_cr[191].A = HEX_F; m_cr[191].B = HEX_7;

// BOX - 3
// Slice - 0
// Row - 0
m_cr[192].A = HEX_8; m_cr[192].B = HEX_8;
m_cr[193].A = HEX_8; m_cr[193].B = HEX_9;
m_cr[194].A = HEX_8; m_cr[194].B = HEX_A;
m_cr[195].A = HEX_8; m_cr[195].B = HEX_B;
// Row - 1
m_cr[196].A = HEX_9; m_cr[196].B = HEX_8;
m_cr[197].A = HEX_9; m_cr[197].B = HEX_9;
m_cr[198].A = HEX_9; m_cr[198].B = HEX_A;
m_cr[199].A = HEX_9; m_cr[199].B = HEX_B;
// Row - 2
m_cr[200].A = HEX_A; m_cr[200].B = HEX_8;
m_cr[201].A = HEX_A; m_cr[201].B = HEX_9;
m_cr[202].A = HEX_A; m_cr[202].B = HEX_A;
m_cr[203].A = HEX_A; m_cr[203].B = HEX_B;
// Row - 3
m_cr[204].A = HEX_B; m_cr[204].B = HEX_8;
m_cr[205].A = HEX_B; m_cr[205].B = HEX_9;
m_cr[206].A = HEX_B; m_cr[206].B = HEX_A;
m_cr[207].A = HEX_B; m_cr[207].B = HEX_B;

// Slice - 1
// Row - 0
m_cr[208].A = HEX_8; m_cr[208].B = HEX_C;
m_cr[209].A = HEX_8; m_cr[209].B = HEX_D;
m_cr[210].A = HEX_8; m_cr[210].B = HEX_E;
m_cr[211].A = HEX_8; m_cr[211].B = HEX_F;
// Row - 1
m_cr[212].A = HEX_9; m_cr[212].B = HEX_C;
m_cr[213].A = HEX_9; m_cr[213].B = HEX_D;
m_cr[214].A = HEX_9; m_cr[214].B = HEX_E;
m_cr[215].A = HEX_9; m_cr[215].B = HEX_F;
// Row - 2
m_cr[216].A = HEX_A; m_cr[216].B = HEX_C;
m_cr[217].A = HEX_A; m_cr[217].B = HEX_D;
m_cr[218].A = HEX_A; m_cr[218].B = HEX_E;
m_cr[219].A = HEX_A; m_cr[219].B = HEX_F;
// Row - 3
m_cr[220].A = HEX_B; m_cr[220].B = HEX_C;
m_cr[221].A = HEX_B; m_cr[221].B = HEX_D;
m_cr[222].A = HEX_B; m_cr[222].B = HEX_E;
m_cr[223].A = HEX_B; m_cr[223].B = HEX_F;

// Slice - 2
// Row - 0
m_cr[224].A = HEX_C; m_cr[224].B = HEX_8;
m_cr[225].A = HEX_C; m_cr[225].B = HEX_9;
m_cr[226].A = HEX_C; m_cr[226].B = HEX_A;
m_cr[227].A = HEX_C; m_cr[227].B = HEX_B;
// Row - 1
m_cr[228].A = HEX_D; m_cr[228].B = HEX_8;
m_cr[229].A = HEX_D; m_cr[229].B = HEX_9;
m_cr[230].A = HEX_D; m_cr[230].B = HEX_A;
m_cr[231].A = HEX_D; m_cr[231].B = HEX_B;
// Row - 2
m_cr[232].A = HEX_E; m_cr[232].B = HEX_8;
m_cr[233].A = HEX_E; m_cr[233].B = HEX_9;
m_cr[234].A = HEX_E; m_cr[234].B = HEX_A;
m_cr[235].A = HEX_E; m_cr[235].B = HEX_B;
// Row - 3
m_cr[236].A = HEX_F; m_cr[236].B = HEX_8;
m_cr[237].A = HEX_F; m_cr[237].B = HEX_9;
m_cr[238].A = HEX_F; m_cr[238].B = HEX_A;
m_cr[239].A = HEX_F; m_cr[239].B = HEX_B;

// Slice - 3
// Row - 0
m_cr[240].A = HEX_C; m_cr[240].B = HEX_C;
m_cr[241].A = HEX_C; m_cr[241].B = HEX_D;
m_cr[242].A = HEX_C; m_cr[242].B = HEX_E;
m_cr[243].A = HEX_C; m_cr[243].B = HEX_F;
// Row - 1
m_cr[244].A = HEX_D; m_cr[244].B = HEX_C;
m_cr[245].A = HEX_D; m_cr[245].B = HEX_D;
m_cr[246].A = HEX_D; m_cr[246].B = HEX_E;
m_cr[247].A = HEX_D; m_cr[247].B = HEX_F;
// Row - 2
m_cr[248].A = HEX_E; m_cr[248].B = HEX_C;
m_cr[249].A = HEX_E; m_cr[249].B = HEX_D;
m_cr[250].A = HEX_E; m_cr[250].B = HEX_E;
m_cr[251].A = HEX_E; m_cr[251].B = HEX_F;
// Row - 3
m_cr[252].A = HEX_F; m_cr[252].B = HEX_C;
m_cr[253].A = HEX_F; m_cr[253].B = HEX_D;
m_cr[254].A = HEX_F; m_cr[254].B = HEX_E;
m_cr[255].A = HEX_F; m_cr[255].B = HEX_F;

} // initializeCrossReference

如您所见,这个函数很长,而且可读性不强。我试图找出一种以优雅的方式实现此功能的方法,这就是我被困的地方。我如何使用 for 循环和一些 if 语句(如果需要)在几行代码中生成此模式?

最终,我想将这些 [A,B] 坐标压缩成单个无符号字符,就好像 [A,B] = [0x0C,0x08] 一样,它将作为字节而不是一对枚举存储为 0xC8。

编辑 - 添加一个新图像,这应该有助于理解我如何生成我的坐标对以及它们在我的查找数组中必须处于的确切顺序。

Improved Image

注意:这个问题是我接受的帖子,至于旧问题在这里Custom Data Structure: Implementing an algorithim to generate a lookup table [duplicate]现在是一个死帖,请引用这个作为正确的。昨晚当我试图继续编辑旧浏览器时,我的浏览器出了问题。我本来打算删除它,但出于历史目的,我会把它留在那里,让管理部门决定是否删除它。

最佳答案

例如,像这样:

for (int i = 0; i < 256; ++i) {
int cube = i / 64;
int slice = (i % 64) / 16;
int row = (i % 16) / 4;
int col = i % 4;
m_cr[i].A = cube / 2 * 8 + slice / 2 * 4 + row;
m_cr[i].B = cube % 2 * 8 + slice % 2 * 4 + col;
}

关于c++ - 实现查找表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416290/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com