Changeset 15017 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack
- Timestamp:
- 06/01/17 09:28:34 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/IPushStack.cs
r14914 r15017 4 4 5 5 public interface IPushStack<T> : ICollection<T>, IPushStack { 6 T Top { get; }6 T Top { get; set; } 7 7 T TopOrDefault { get; } 8 8 T Bottom { get; } … … 23 23 void Insert(int index, T item); 24 24 T ReverseElementAt(int offset); 25 void SetTop(T value);26 25 T Pop(); 27 26 T[] Pop(int count); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/InterpreterStackStringifier.cs
r14952 r15017 3 3 4 4 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Stack { 5 using System.Globalization; 6 5 7 using HeuristicLab.Problems.ProgramSynthesis.Push.Constants; 8 using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; 6 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; 7 10 8 public class InterpreterStackStringifier {11 public static class InterpreterStackStringifier { 9 12 10 public static IEnumerable<string> StringifyStack( IPushInterpreter interpreter, StackTypes type) {13 public static IEnumerable<string> StringifyStack(this IPushInterpreter interpreter, StackTypes type) { 11 14 switch (type) { 12 15 case StackTypes.Boolean: return StringifyLiteralStack(interpreter.BooleanStack); 13 16 case StackTypes.Integer: return StringifyLiteralStack(interpreter.IntegerStack); 14 case StackTypes.Float: return Stringify LiteralStack(interpreter.FloatStack);15 case StackTypes.Char: return Stringify LiteralStack(interpreter.CharStack);16 case StackTypes.String: return StringifyLiteralStack(interpreter.StringStack, "\"", "\"");17 case StackTypes.Float: return StringifyFloatStack(interpreter.FloatStack, interpreter.Configuration.FloatStringFormat); 18 case StackTypes.Char: return StringifyCharStack(interpreter.CharStack); 19 case StackTypes.String: return StringifyLiteralStack(interpreter.StringStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr); 17 20 case StackTypes.Name: return StringifyLiteralStack(interpreter.NameStack); 18 21 case StackTypes.Code: return StringifyLiteralStack(interpreter.CodeStack); 19 22 case StackTypes.Exec: return StringifyLiteralStack(interpreter.ExecStack); 20 case StackTypes.Print: return StringifyLiteralStack(interpreter.PrintStack );23 case StackTypes.Print: return StringifyLiteralStack(interpreter.PrintStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr, false); 21 24 22 25 case StackTypes.BooleanVector: return StringifyVectorStack(interpreter.BooleanVectorStack); 23 26 case StackTypes.IntegerVector: return StringifyVectorStack(interpreter.IntegerVectorStack); 24 case StackTypes.FloatVector: return Stringify VectorStack(interpreter.FloatVectorStack);25 case StackTypes.StringVector: return StringifyVectorStack(interpreter.StringVectorStack, "\"", "\"");27 case StackTypes.FloatVector: return StringifyFloatVectorStack(interpreter.FloatVectorStack, interpreter.Configuration.FloatStringFormat); 28 case StackTypes.StringVector: return StringifyVectorStack(interpreter.StringVectorStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr); 26 29 27 30 default: return Enumerable.Empty<string>(); … … 29 32 } 30 33 31 private static IEnumerable<string> StringifyLiteralStack<T>(IPushStack<T> stack, string prefix = "", string postfix = "") { 32 return stack.Reverse().Select(x => prefix + x.ToString().Replace(PushEnvironment.NewLine, "\\n") + postfix); 34 private static IEnumerable<string> StringifyCharStack(IPushStack<char> stack) { 35 var result = stack.AsEnumerable(); 36 37 return result.Select(x => PushEnvironment.CharSymbol + x.Printify() + PushEnvironment.CharSymbol); 33 38 } 34 39 35 private static IEnumerable<string> StringifyVectorStack<T>(IPushStack<List<T>> stack, string prefix = "", string postfix = "") { 40 private static IEnumerable<string> StringifyFloatStack(IPushStack<double> stack, string format) { 41 var result = stack.AsEnumerable(); 42 43 return result.Select(x => x.ToString(format, CultureInfo.InvariantCulture)); 44 } 45 46 private static IEnumerable<string> StringifyLiteralStack<T>(IPushStack<T> stack, string prefix = "", string postfix = "", bool reverse = true) { 47 var result = stack.AsEnumerable(); 48 49 if (reverse) 50 result = result.Reverse(); 51 52 return result.Select(x => prefix + x.ToString() + postfix); 53 } 54 55 private static IEnumerable<string> StringifyFloatVectorStack(IPushStack<IReadOnlyList<double>> stack, string format) { 56 var separator = PushEnvironment.VectorSeparatorSymbol.ToString(); 57 return stack 58 .Reverse() 59 .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => _.ToString(format, CultureInfo.InvariantCulture))) + PushEnvironment.VectorEndSymbol); 60 } 61 62 private static IEnumerable<string> StringifyVectorStack<T>(IPushStack<IReadOnlyList<T>> stack, string prefix = "", string postfix = "") { 36 63 var separator = PushEnvironment.VectorSeparatorSymbol.ToString(); 37 64 return stack -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PushStack.cs
r14914 r15017 43 43 return data[Count - 1]; 44 44 } 45 46 set 47 { 48 data[Count - 1] = value; 49 } 45 50 } 46 51 … … 119 124 public T ReverseElementAt(int offset) { 120 125 return data[offset]; 121 }122 123 public void SetTop(T value) {124 data[Count - 1] = value;125 126 } 126 127 … … 276 277 277 278 public override string ToString() { 278 var data = this.data as IEnumerable<T>; 279 return string.Join(Delimiter, data.Reverse()); 279 return string.Join(Delimiter, data.Select(x => x.ToString()).Reverse()); 280 280 } 281 281
Note: See TracChangeset
for help on using the changeset viewer.