Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problem.ProgramSynthesis.BenchmarkSuite/ExampleArgumentConverter.cs @ 15160

Last change on this file since 15160 was 15017, checked in by pkimmesw, 7 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 3.0 KB
Line 
1namespace HeuristicLab.BenchmarkSuite {
2  using System;
3  using System.Collections.Generic;
4  using System.Globalization;
5  using System.IO;
6  using System.Linq;
7
8  public static class ExampleArgumentConverter {
9    private static readonly char[] ArrayValueSeparator = { ' ', ';' };
10    private static readonly char[] ArraySymbolTrim = { '[', ']', '(', ')' };
11
12    public static double[] ConvertDoubles(string[] input) {
13      return input.Select(ConvertDouble).ToArray();
14    }
15
16    public static double[] ConvertDoubles(string input) {
17      return ConvertMultiple(input, str => double.Parse(str, NumberStyles.Float, CultureInfo.InvariantCulture));
18    }
19
20    public static double ConvertDouble(string input) {
21      return double.Parse(input, CultureInfo.InvariantCulture);
22    }
23
24    public static long[] ConvertIntegers(string[] input) {
25      return input.Select(ConvertInteger).ToArray();
26    }
27
28    public static long[] ConvertIntegers(string input) {
29      return ConvertMultiple(input, long.Parse);
30    }
31
32    public static long ConvertInteger(string input) {
33
34      return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input);
35    }
36
37    public static string[] ConvertStringVector(string input) {
38      var length = input.Length - 1;
39      var strs = new List<string>();
40
41      for (var i = 1; i < length; i++) {
42        // collect string
43        var c = input[i];
44
45        if (c == ' ') continue;
46        if (c != '\"') throw new InvalidDataException("Unable to parse string vector");
47
48        var start = i + 1;
49        do {
50          i++;
51          c = input[i];
52        } while (i < length - 1  &&
53               !(c == '\"' && input[i + 1] == ' ') &&
54               !(i == length - 2 && c == '\"' && input[i + 1] == ']'));
55
56
57        var str = input.Substring(start, i - start);
58        strs.Add(str);
59      }
60
61      return strs.ToArray();
62    }
63
64    public static T[] ConvertMultiple<T>(string input, Func<string, T> converter) {
65      return input
66       .Trim(ArraySymbolTrim)
67       .Split(ArrayValueSeparator)
68       .Where(s => !string.IsNullOrWhiteSpace(s))
69       .Select(converter)
70       .ToArray();
71    }
72
73    public static IEnumerable<string> SplitByNewLine(string input) {
74      return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None);
75    }
76
77    private static string[] trueFormats = { "true", "t", "1" };
78    private static string[] falseFormats = { "false", "f", "0" };
79
80    public static bool ConvertBoolean(string input) {
81      var str = input.ToLower();
82
83      if (trueFormats.Contains(str)) return true;
84      if (falseFormats.Contains(str)) return false;
85      throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str));
86    }
87
88    public static bool[] ConvertBooleans(string input) {
89      return input
90        .Trim(ArraySymbolTrim)
91        .Split(ArrayValueSeparator)
92        .Where(s => !string.IsNullOrWhiteSpace(s))
93        .Select(ConvertBoolean)
94        .ToArray();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.