Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/28/17 09:03:34 (7 years ago)
Author:
pkimmesw
Message:

#2665 Testet Problems, Improved Performance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Extensions/StringExtensions.cs

    r15334 r15341  
    55    public static bool IsNumeric(this string str) {
    66      int n;
    7       return int.TryParse("123", out n);
     7      return int.TryParse(str, out n);
     8    }
     9
     10    /// <summary>
     11    /// https://github.com/DanHarltey/Fastenshtein
     12    /// </summary>
     13    public static int LevenshteinDistance(this string source, string target) {
     14      if (target.Length == 0) {
     15        return source.Length;
     16      }
     17
     18      int[] costs = new int[target.Length];
     19
     20      // Add indexing for insertion to first row
     21      for (int i = 0; i < costs.Length;) {
     22        costs[i] = ++i;
     23      }
     24
     25      for (int i = 0; i < source.Length; i++) {
     26        // cost of the first index
     27        int cost = i;
     28        int addationCost = i;
     29
     30        // cache value for inner loop to avoid index lookup and bonds checking, profiled this is quicker
     31        char value1Char = source[i];
     32
     33        for (int j = 0; j < target.Length; j++) {
     34          int insertionCost = cost;
     35
     36          cost = addationCost;
     37
     38          // assigning this here reduces the array reads we do, improvement of the old version
     39          addationCost = costs[j];
     40
     41          if (value1Char != target[j]) {
     42            if (insertionCost < cost) {
     43              cost = insertionCost;
     44            }
     45
     46            if (addationCost < cost) {
     47              cost = addationCost;
     48            }
     49
     50            ++cost;
     51          }
     52
     53          costs[j] = cost;
     54        }
     55      }
     56
     57      return costs[costs.Length - 1];
    858    }
    959
     
    1161    /// https://www.dotnetperls.com/levenshtein
    1262    /// </summary>
    13     public static int LevenshteinDistance(this string source, string target) {
     63    public static int LevenshteinDistance2(this string source, string target) {
    1464      if (source == null && target == null) return 0;
    1565      if (source == null) return target.Length;
Note: See TracChangeset for help on using the changeset viewer.