Implementing a Lookup Table

后端 未结 3 983
执念已碎
执念已碎 2021-01-07 10:43

I am working on a custom data structure and I am currently in the beta testing process: The data will be stored within an array and this array can be represented as a 4D, 2D

3条回答
  •  别那么骄傲
    2021-01-07 10:58

    Start by changing HEX_ to an array. Then, look at individual blocks and turn them into loops. Since your .A and .B are mutually exclusive, we may handle them separately.

    Here's a "function" you may use to help initialize A:

    void InitializeA(CrossReference arr[])
    {
        InitializeABlock(arr, 0x00, 0x00);
        InitializeABlock(arr, 0x10, 0x00);
        InitializeABlock(arr, 0x20, 0x04);
        InitializeABlock(arr, 0x30, 0x04);
        InitializeABlock(arr, 0x40, 0x00);
        InitializeABlock(arr, 0x50, 0x00);
        InitializeABlock(arr, 0x60, 0x04);
        InitializeABlock(arr, 0x70, 0x04);
    
        InitializeABlock(arr, 0x80, 0x08);
        InitializeABlock(arr, 0x90, 0x08);
        InitializeABlock(arr, 0xA0, 0x10);
        InitializeABlock(arr, 0xB0, 0x10);
        InitializeABlock(arr, 0xC0, 0x08);
        InitializeABlock(arr, 0xD0, 0x08);
        InitializeABlock(arr, 0xE0, 0x10);
        InitializeABlock(arr, 0xF0, 0x10);
    }
    
    void InitializeABlock(CrossReference arr[], int idxStart, int hexStart)
    {
        for(int j = hexStart; j < hexStart + 4; j++)
        {
            int baseIdx = idxStart + 4 * (j - hexStart);
    
            for(int i = 0; i < 4; i++)
            {
                arr[baseIdx + i].A = j;
            }
        }
    }
    

    The implementation for B is very similar:

    void InitializeB(CrossReference arr[])
    {
        InitializeBBlock(arr, 0x00, 0x00);
        InitializeBBlock(arr, 0x10, 0x00);
        InitializeBBlock(arr, 0x20, 0x04);
        InitializeBBlock(arr, 0x30, 0x04);
        InitializeBBlock(arr, 0x40, 0x00);
        InitializeBBlock(arr, 0x50, 0x00);
        InitializeBBlock(arr, 0x60, 0x04);
        InitializeBBlock(arr, 0x70, 0x04);
    
        InitializeBBlock(arr, 0x80, 0x08);
        InitializeBBlock(arr, 0x90, 0x08);
        InitializeBBlock(arr, 0xA0, 0x10);
        InitializeBBlock(arr, 0xB0, 0x10);
        InitializeBBlock(arr, 0xC0, 0x08);
        InitializeBBlock(arr, 0xD0, 0x08);
        InitializeBBlock(arr, 0xE0, 0x10);
        InitializeBBlock(arr, 0xF0, 0x10);
    }
    
    void InitializeBBlock(CrossReference arr[], int idxStart, int hexStart)
    {
        for(int j = idxStart; j < idxStart + 4; j++)
        {
            int baseIdx = idxStart + 4 * (j - idxStart);
    
            for(int i = 0; i < 4; i++)
            {
                arr[baseIdx + i].A = i + hexStart;
            }
        }
    }
    

    You may, of course, compress this further. ;)

提交回复
热议问题