如何将4D动态分配的数组更改为1D向量?

时间:2016-12-24 21:31:15

标签: c++ memory

这是我原始代码的一部分,代码太大了,无法全部放在这里, 无论如何我的问题只与Sads 4D矩阵有关, 我不想像我在previous question

中建议的那样使用int ****
int main()
{
   //4D matrix
    int**** Sads = new int***[inputImage->HeightLines];
    for (size_t i = 0; i < inputImage->HeightLines; i++)
    {
        Sads[i] = new int**[inputImage->WidthColumns];
        for (size_t j = 0; j < inputImage->WidthColumns; j++)
        {
            Sads[i][j] = new int*[W_SIZE];
            for (size_t k = 0; k < W_SIZE; k++)
            {
                Sads[i][j][k] = new int[W_SIZE];
            }
        }
    }
     ProcessRowsLoop(20, 1904, Sads);
}
void ProcessRowsLoop(int m_support, int m_height, int**** sads)
{
 for (int row_in = m_support - 1; row_in < m_Height_in; row_in += BNLM_OUT_SZ)
    {
       ProcessRow( &Sads[indexRow]);
    }
}

 void ProcessRow(int**** sads)
{
    int m_SAD_00[W_SIZE][W_SIZE];
    int m_SAD_01[W_SIZE][W_SIZE];
    int m_SAD_10[W_SIZE][W_SIZE];
    int m_SAD_11[W_SIZE][W_SIZE];

    RunAlgo(m_support, m_SAD_00, m_SAD_01, m_SAD_10, m_SAD_11, m_CP_00, m_CP_01, m_CP_10, m_CP_11, m_ColumnSADUp, m_ColumnSADDown);

        for (size_t i = 0; i < W_SIZE; i++)
        {
            for (size_t j = 0; j < W_SIZE; j++)
            {
                Sads[0][m_col_out][i][j] = (m_SAD_00[i][j] + color_penalty_weight * m_CP_00[i][j]) / (sqrt(m_sigma_patch[0][0] / pow(mnm, 2)));
                Sads[0][m_col_out + 1][i][j] = (m_SAD_01[i][j] + color_penalty_weight * m_CP_01[i][j]) / (sqrt(m_sigma_patch[0][1] / pow(mnm, 2)));
                Sads[1][m_col_out][i][j] = (m_SAD_10[i][j] + color_penalty_weight + m_CP_10[i][j]) / (sqrt(m_sigma_patch[1][0] / pow(mnm, 2)));
                Sads[1][m_col_out + 1][i][j] = (m_SAD_11[i][j] + color_penalty_weight + m_CP_11[i][j]) / (sqrt(m_sigma_patch[1][1] / pow(mnm, 2)));
            }
        }

}

在我的新代码中,我想用

替换4D矩阵int **** Sads
struct VectorFourD
{
private:
    int _width, _height;
    int _w_size;
    std::vector<int> _vec;
public:
    VectorFourD(int width, int height, int size) : _width(width), _height(height), _w_size(size), _vec(totalSize())
    {

    }
    auto totalSize() const-> int
    {
        return _width * _height * _w_size * _w_size;
    }
    int* at(int a)
    {
        return _vec.data() + (a * _height * _w_size * _w_size);
    }
    int* at(int a, int b)
    {
        return at(a) + (b * _w_size * _w_size);
    }
    int *at(int a, int b, int c)
    {
        return at(a, b) + (c* _w_size);
    }
    int& at(int a, int b, int c, int d)
    {
        return *(at(a, b, c) + d);
    }
};

你可以看到我在函数processrow()

中同时迭代两行

跑过

Sads[0][m_col_out][i][j], Sads[0][m_col_out + 1][i][j], 
Sads[1][m_col_out][i][j], Sads[1][m_col_out + 1][i][j]

与此同时,我的问题是如何更改我的代码以使用新的4dvector

int main()
{
 VectorFourD SadsVec = VectorFourD(inputImage->HeightLines, inputImage->WidthColumns, W_SIZE);
     ProcessRowsLoop(20, 1904, SadsVec);
}

也会更改功能void ProcessRowsLoop(int m_support, int m_height, VectorFourD* SadsVec)
但是我不知道怎么从这里继续,你能帮忙吗?

0 个答案:

没有答案