其他分享
首页 > 其他分享> > c – 实现查找表

c – 实现查找表

作者:互联网

我正在研究自定义数据结构,目前我处于beta测试过程中:数据将存储在一个数组中,这个数组可以表示为4D,2D和amp; 1D阵列.这三个数组在union中声明,因为它表示相同的内存寻址.这是我班上的宣言:

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阵列数据结构的2D平面网格图像表示.我们可以使用此图像生成此查找表的坐标.

该图像的每个四分之一代表一个盒子.左上角(开始)是方框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循环/ s和几个if语句(如果需要)在几行代码中生成此模式?

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

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

Improved Image

注意:这个问题是我接受的帖子,至于Custom Data Structure: Implementing an algorithim to generate a lookup table 这里的旧问题现在是一个死帖,请参考这个是正确的.昨晚我试图继续编辑旧浏览器时,我的浏览器出现问题.我打算删除它,但我会将其留在那里用于历史目的,并让管理部门决定是否删除它.

解决方法:

例如,像这样:

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;
}

标签:lookup-tables,c,algorithm,indexing,lookup
来源: https://codeday.me/bug/20191007/1867183.html