Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/17 01:15:25 (8 years ago)
Author:
pkimmesw
Message:

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StringExpressions.cs

    r14834 r14875  
    5959    public override bool Eval(IInternalPushInterpreter interpreter) {
    6060      if (interpreter.StringStack.Count < 2 ||
    61         interpreter.StringStack.Top.Length + interpreter.StringStack.ReverseElementAt(1).Length >= interpreter.Configuration.MaxStringLength)
     61        interpreter.StringStack.Top.Length + interpreter.StringStack[1].Length >= interpreter.Configuration.MaxStringLength)
    6262        return false;
    6363
     
    117117
    118118      var str = interpreter.StringStack.Top;
    119       var values = interpreter.IntegerStack.Pop(2);
    120       var first = Math.Min(str.Length - 1, Math.Max(values[0], 0));
    121       var second = Math.Min(str.Length - 1, Math.Max(values[1], first));
     119      var firstValue = interpreter.IntegerStack[1];
     120      var secondValue = interpreter.IntegerStack.Top;
     121      interpreter.IntegerStack.Remove(2);
     122
     123      var first = Math.Min(str.Length - 1, Math.Max(firstValue, 0));
     124      var second = Math.Min(str.Length - 1, Math.Max(secondValue, first));
    122125      var length = second - first;
    123126
     
    293296        return false;
    294297
    295       var strings = interpreter.StringStack.Pop(2);
    296       interpreter.BooleanStack.Push(strings[0].IndexOf(strings[1], StringComparison.Ordinal) >= 0);
     298      var first = interpreter.StringStack[1];
     299      var second = interpreter.StringStack.Top;
     300      interpreter.StringStack.Remove(2);
     301
     302      interpreter.BooleanStack.Push(first.IndexOf(second, StringComparison.Ordinal) >= 0);
    297303      return true;
    298304    }
     
    366372        return false;
    367373
    368       var strings = interpreter.StringStack.Pop(2);
    369 
    370       if (strings[0].Length == 0 || strings[1].Length == 0)
    371         interpreter.StringStack.SetTop(strings[0]);
     374      var first = interpreter.StringStack[1];
     375      var second = interpreter.StringStack.Top;
     376      interpreter.StringStack.Remove(2);
     377
     378      if (first.Length == 0 || second.Length == 0)
     379        interpreter.StringStack.SetTop(first);
    372380      else {
    373         var result = strings[0].Replace(strings[1], interpreter.StringStack.Top);
     381        var result = first.Replace(second, interpreter.StringStack.Top);
    374382        interpreter.StringStack.SetTop(result);
    375383      }
     
    388396        return false;
    389397
    390       var strings = interpreter.StringStack.Pop(2);
    391 
    392       var pos = strings[0].IndexOf(strings[1], StringComparison.Ordinal);
     398      var first = interpreter.StringStack[0];
     399      var second = interpreter.StringStack[1];
     400      var third = interpreter.StringStack[2];
     401      interpreter.StringStack.Remove(2);
     402
     403      var result = ReplaceFirst(third, second, first);
     404      interpreter.StringStack.SetTop(result);
     405      return true;
     406    }
     407
     408    private static string ReplaceFirst(string text, string search, string replace) {
     409      int pos = text.IndexOf(search);
     410      if (pos < 0) {
     411        return text;
     412      }
     413      return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
     414    }
     415  }
     416
     417  /// <summary>
     418  /// In top string on stack, replaces all occurences of second char with first char
     419  /// </summary>
     420  [PushExpression(StackTypes.String, "STRING.REPLACECHAR", StackTypes.Char)]
     421  public class StringReplaceCharExpression : StatelessExpression {
     422    public override bool Eval(IInternalPushInterpreter interpreter) {
     423      if (interpreter.StringStack.IsEmpty ||
     424          interpreter.CharStack.Count < 2)
     425        return false;
     426
     427      var first = interpreter.CharStack[1];
     428      var second = interpreter.CharStack.Top;
     429      interpreter.CharStack.Remove(2);
     430
     431      var result = interpreter.StringStack.Top.Replace(first, second);
     432      interpreter.StringStack.SetTop(result);
     433      return true;
     434    }
     435  }
     436
     437  /// <summary>
     438  /// In top string on stack, replaces first occurence of second char with first char
     439  /// </summary>
     440  [PushExpression(StackTypes.String, "STRING.REPLACEFIRSTCHAR", StackTypes.Char)]
     441  public class StringReplaceLastCharExpression : StatelessExpression {
     442    public override bool Eval(IInternalPushInterpreter interpreter) {
     443      if (interpreter.StringStack.IsEmpty ||
     444          interpreter.CharStack.Count < 2)
     445        return false;
     446
     447      var str = interpreter.StringStack.Top;
     448      var first = interpreter.CharStack[1];
     449      var second = interpreter.CharStack.Top;
     450      interpreter.CharStack.Remove(2);
     451
     452      var pos = str.IndexOf(first);
    393453
    394454      if (pos < 0)
    395455        return true;
    396456
    397       var result = strings[0].Substring(0, pos) +
    398                    interpreter.StringStack.Top +
    399                    strings[0].Substring(pos + strings[1].Length);
    400 
    401       interpreter.StringStack.SetTop(result);
    402       return true;
    403     }
    404   }
    405 
    406   /// <summary>
    407   /// In top string on stack, replaces all occurences of second char with first char
    408   /// </summary>
    409   [PushExpression(StackTypes.String, "STRING.REPLACECHAR", StackTypes.Char)]
    410   public class StringReplaceCharExpression : StatelessExpression {
    411     public override bool Eval(IInternalPushInterpreter interpreter) {
    412       if (interpreter.StringStack.IsEmpty ||
    413           interpreter.CharStack.Count < 2)
    414         return false;
    415 
    416       var chars = interpreter.CharStack.Pop(2);
    417       var result = interpreter.StringStack.Top.Replace(chars[0], chars[1]);
    418       interpreter.StringStack.SetTop(result);
    419       return true;
    420     }
    421   }
    422 
    423   /// <summary>
    424   /// In top string on stack, replaces first occurence of second char with first char
    425   /// </summary>
    426   [PushExpression(StackTypes.String, "STRING.REPLACEFIRSTCHAR", StackTypes.Char)]
    427   public class StringReplaceLastCharExpression : StatelessExpression {
    428     public override bool Eval(IInternalPushInterpreter interpreter) {
    429       if (interpreter.StringStack.IsEmpty ||
    430           interpreter.CharStack.Count < 2)
    431         return false;
    432 
    433       var str = interpreter.StringStack.Top;
    434       var chars = interpreter.CharStack.Pop(2);
    435       var pos = interpreter.StringStack.Top.IndexOf(chars[0]);
    436 
    437       if (pos < 0)
    438         return true;
    439 
    440457      var result = str.Substring(0, pos) +
    441                    chars[1] +
     458                   second +
    442459                   str.Substring(Math.Min(pos + 2, str.Length - 1));
     460
    443461      interpreter.StringStack.SetTop(result);
    444462      return true;
Note: See TracChangeset for help on using the changeset viewer.