Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 31, 2010, 3:37:40 AM (14 years ago)
Author:
landauf
Message:

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/util/StringUtils.cc

    r6424 r7284  
    3535
    3636#include <cctype>
     37#include <boost/scoped_array.hpp>
    3738#include "Convert.h"
    3839#include "Math.h"
     
    514515        @return Number of replacements
    515516    */
    516     _UtilExport size_t replaceCharacters(std::string& str, char target, char replacement)
     517    size_t replaceCharacters(std::string& str, char target, char replacement)
    517518    {
    518519        size_t j = 0;
     
    527528        return j;
    528529    }
     530
     531    /**
     532        @brief Calculates the Levenshtein distance between two strings.
     533
     534        The Levenshtein distance is defined by the number of transformations needed to convert str1
     535        into str2. Possible transformations are substituted, added, or removed characters.
     536    */
     537    unsigned int getLevenshteinDistance(const std::string& str1, const std::string& str2)
     538    {
     539        size_t cols = str1.size() + 1;
     540        size_t rows = str2.size() + 1;
     541        boost::scoped_array<int> matrix(new int[rows * cols]);
     542
     543        for (size_t r = 0; r < rows; ++r)
     544            for (size_t c = 0; c < cols; ++c)
     545                matrix[r*cols + c] = 0;
     546
     547        for (size_t i = 1; i < cols; ++i)
     548            matrix[0*cols + i] = i;
     549        for (size_t i = 1; i < rows; ++i)
     550            matrix[i*cols + 0] = i;
     551
     552        for (size_t r = 1; r < rows; ++r)
     553            for (size_t c = 1; c < cols; ++c)
     554                matrix[r*cols + c] = (str1[c-1] != str2[r-1]);
     555
     556        for (size_t r = 1; r < rows; ++r)
     557            for (size_t c = 1; c < cols; ++c)
     558                matrix[r*cols + c] = std::min(std::min(matrix[(r-1)*cols + c] + 1,
     559                                                       matrix[r*cols + c-1] + 1),
     560                                              matrix[(r-1)*cols + c-1] + (str1[c-1] != str2[r-1]));
     561
     562        return matrix[(rows-1)*cols + cols-1];
     563    }
    529564}
Note: See TracChangeset for help on using the changeset viewer.