Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7240


Ignore:
Timestamp:
Aug 28, 2010, 6:58:30 PM (14 years ago)
Author:
rgrieder
Message:

C++ forbids non constant sized stack arrays.
Please check whether this is functionally equivalent (I cannot test the original version).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/util/StringUtils.cc

    r7238 r7240  
    538538        size_t cols = str1.size() + 1;
    539539        size_t rows = str2.size() + 1;
    540         int matrix[rows][cols];
     540        int* matrix = new int[rows * cols];
    541541
    542542        for (size_t r = 0; r < rows; ++r)
    543543            for (size_t c = 0; c < cols; ++c)
    544                 matrix[r][c] = 0;
     544                matrix[r*cols + c] = 0;
    545545
    546546        for (size_t i = 1; i < cols; ++i)
    547             matrix[0][i] = i;
     547            matrix[0*cols + i] = i;
    548548        for (size_t i = 1; i < rows; ++i)
    549             matrix[i][0] = i;
     549            matrix[i*cols + 0] = i;
    550550
    551551        for (size_t r = 1; r < rows; ++r)
    552552            for (size_t c = 1; c < cols; ++c)
    553                 matrix[r][c] = (str1[c-1] != str2[r-1]);
     553                matrix[r*cols + c] = (str1[c-1] != str2[r-1]);
    554554
    555555        for (size_t r = 1; r < rows; ++r)
    556556            for (size_t c = 1; c < cols; ++c)
    557                 matrix[r][c] = std::min(std::min(matrix[r-1][c] + 1, matrix[r][c-1] + 1), matrix[r-1][c-1] + (str1[c-1] != str2[r-1]));
    558 
    559         return matrix[rows-1][cols-1];
     557                matrix[r*cols + c] = std::min(std::min(matrix[(r-1)*cols + c] + 1,
     558                                                       matrix[r*cols + c-1] + 1),
     559                                              matrix[(r-1)*cols + c-1] + (str1[c-1] != str2[r-1]));
     560
     561        int result = matrix[(rows-1)*cols + cols-1];
     562        delete[] matrix;
     563        return result;
    560564    }
    561565}
Note: See TracChangeset for help on using the changeset viewer.