Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/StringBuilderExtensions.cs

Last change on this file was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 9.1 KB
RevLine 
[15771]1namespace HeuristicLab.Problems.ProgramSynthesis {
[15189]2  using System;
3  using System.Diagnostics;
4  using System.Globalization;
5  using System.Text;
6
7  public static class StringBuilderExtension {
8    // These digits are here in a static array to support hex with simple, easily-understandable code.
9    // Since A-Z don't sit next to 0-9 in the ascii table.
10    private static readonly char[] MsDigits =
11      {
12        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
13        'E', 'F'
14      };
15
16    //private static readonly ulong MsDefaultDecimalPlaces = 5; //< Matches standard .NET formatting dp's
17
18    private const char MsDefaultPadChar = '0';
19
20    //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Any base value allowed.
21    public static StringBuilder Concat(
22      this StringBuilder stringBuilder,
23      ulong ulongVal,
24      ulong padAmount,
25      char padChar,
26      ulong baseVal) {
27      Debug.Assert((baseVal > 0) && (baseVal <= 16));
28
29      // Calculate length of integer when written out
30      ulong length = 0;
31      var lengthCalc = ulongVal;
32
33      do {
34        lengthCalc /= baseVal;
35        length++;
36      }
37      while (lengthCalc > 0);
38
39      // Pad out space for writing.
40      stringBuilder.Append(padChar, (int)Math.Max(padAmount, length));
41
42      var strpos = stringBuilder.Length;
43
44      // We're writing backwards, one character at a time.
45      while (length > 0) {
46        strpos--;
47
48        // Lookup from static char array, to cover hex values too
49        stringBuilder[strpos] = MsDigits[ulongVal % baseVal];
50
51        ulongVal /= baseVal;
52        length--;
53      }
54
55      return stringBuilder;
56    }
57
58    //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Assume no padding and base ten.
59    public static StringBuilder Concat(this StringBuilder stringBuilder, ulong ulongVal) {
60      stringBuilder.Concat(ulongVal, 0, MsDefaultPadChar, 10);
61      return stringBuilder;
62    }
63
64    //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Assume base ten.
65    public static StringBuilder Concat(this StringBuilder stringBuilder, ulong ulongVal, ulong padAmount) {
66      stringBuilder.Concat(ulongVal, padAmount, MsDefaultPadChar, 10);
67      return stringBuilder;
68    }
69
70    //! Convert a given unsigned integer value to a string and concatenate onto the stringbuilder. Assume base ten.
71    public static StringBuilder Concat(
72      this StringBuilder stringBuilder,
73      ulong ulongVal,
74      ulong padAmount,
75      char padChar) {
76      stringBuilder.Concat(ulongVal, padAmount, padChar, 10);
77      return stringBuilder;
78    }
79
80    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Any base value allowed.
81    public static StringBuilder Concat(
82      this StringBuilder stringBuilder,
83      long longVal,
84      ulong padAmount,
85      char padChar,
86      ulong baseVal) {
87      Debug.Assert((baseVal > 0) && (baseVal <= 16));
88
89      // Deal with negative numbers
90      if (longVal < 0) {
91        stringBuilder.Append('-');
92        var ulongVal = ulong.MaxValue - (ulong)longVal + 1; //< This is to deal with Int64.MinValue
93        stringBuilder.Concat(ulongVal, padAmount, padChar, baseVal);
94      } else {
95        stringBuilder.Concat((ulong)longVal, padAmount, padChar, baseVal);
96      }
97
98      return stringBuilder;
99    }
100
101    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume no padding and base ten.
102    public static StringBuilder Concat(this StringBuilder stringBuilder, long longVal) {
103      stringBuilder.Concat(longVal, 0, MsDefaultPadChar, 10);
104      return stringBuilder;
105    }
106
107    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume base ten.
108    public static StringBuilder Concat(this StringBuilder stringBuilder, long longVal, ulong padAmount) {
109      stringBuilder.Concat(longVal, padAmount, MsDefaultPadChar, 10);
110      return stringBuilder;
111    }
112
113    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume base ten.
114    public static StringBuilder Concat(
115      this StringBuilder stringBuilder,
116      long longVal,
117      ulong padAmount,
118      char padChar) {
119      stringBuilder.Concat(longVal, padAmount, padChar, 10);
120      return stringBuilder;
121    }
122
123    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Any base value allowed.
124    public static StringBuilder Concat(
125      this StringBuilder stringBuilder,
126      int intVal,
127      ulong padAmount,
128      char padChar,
129      ulong baseVal) {
130      Debug.Assert((baseVal > 0) && (baseVal <= 16));
131
132      // Deal with negative numbers
133      if (intVal < 0) {
134        stringBuilder.Append('-');
135        var ulongVal = ulong.MaxValue - (ulong)intVal + 1; //< This is to deal with Int32.MinValue
136        stringBuilder.Concat(ulongVal, padAmount, padChar, baseVal);
137      } else {
138        stringBuilder.Concat((ulong)intVal, padAmount, padChar, baseVal);
139      }
140
141      return stringBuilder;
142    }
143
144    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume no padding and base ten.
145    public static StringBuilder Concat(this StringBuilder stringBuilder, int intVal) {
146      stringBuilder.Concat(intVal, 0, MsDefaultPadChar, 10);
147      return stringBuilder;
148    }
149
150    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume base ten.
151    public static StringBuilder Concat(this StringBuilder stringBuilder, int intVal, ulong padAmount) {
152      stringBuilder.Concat(intVal, padAmount, MsDefaultPadChar, 10);
153      return stringBuilder;
154    }
155
156    //! Convert a given signed integer value to a string and concatenate onto the stringbuilder. Assume base ten.
157    public static StringBuilder Concat(this StringBuilder stringBuilder, int intVal, ulong padAmount, char padChar) {
158      stringBuilder.Concat(intVal, padAmount, padChar, 10);
159      return stringBuilder;
160    }
161
162    //! Convert a given double value to a string and concatenate onto the stringbuilder
163    public static StringBuilder Concat(
164      this StringBuilder stringBuilder,
165      double doubleVal,
166      uint decimalPlaces,
167      uint padAmount,
168      char padChar,
169      CultureInfo culturInfo = null) {
170      culturInfo = culturInfo ?? CultureInfo.CurrentCulture;
171
172      if (decimalPlaces == 0) {
173        // No decimal places, just round up and print it as an int
174
175        // Agh, Math.Floor() just works on doubles/decimals. Don't want to cast! Let's do this the old-fashioned way.
176        int intVal;
177        if (doubleVal >= 0.0d) intVal = (int)(doubleVal + 0.5d);
178        else intVal = (int)(doubleVal - 0.5d);
179
180        stringBuilder.Concat(intVal, padAmount, padChar, 10);
181      } else {
182        var intPart = (int)doubleVal;
183
184        // First part is easy, just cast to an integer
185        stringBuilder.Concat(intPart, padAmount, padChar, 10);
186
187        // Decimal point
188        var numberDecimalSeparator = Convert.ToChar(culturInfo.NumberFormat.NumberDecimalSeparator);
189        stringBuilder.Append(numberDecimalSeparator);
190
191        // Work out remainder we need to print after the d.p.
192        var remainder = Math.Abs(doubleVal - intPart);
193
194        // Multiply up to become an int that we can print
195        do {
196          remainder *= 10;
197          decimalPlaces--;
198        }
199        while (remainder > (int)remainder && decimalPlaces > 0);
200
201        // Round up. It's guaranteed to be a positive number, so no extra work required here.
202        remainder += 0.5d;
203
204        // All done, print that as an int!
205        stringBuilder.Concat((ulong)remainder, 0, '0', 10);
206      }
207      return stringBuilder;
208    }
209
210    //! Convert a given double value to a string and concatenate onto the stringbuilder. Assumes five decimal places, and no padding.
211    public static StringBuilder Concat(this StringBuilder stringBuilder, double doubleVal, CultureInfo culturInfo = null) {
212      culturInfo = culturInfo ?? CultureInfo.CurrentCulture;
213
214      stringBuilder.Concat(doubleVal, (uint)culturInfo.NumberFormat.NumberDecimalDigits, 0, MsDefaultPadChar, culturInfo);
215      return stringBuilder;
216    }
217
218    //! Convert a given double value to a string and concatenate onto the stringbuilder. Assumes no padding.
219    public static StringBuilder Concat(this StringBuilder stringBuilder, double doubleVal, uint decimalPlaces, CultureInfo culturInfo = null) {
220      stringBuilder.Concat(doubleVal, decimalPlaces, 0, MsDefaultPadChar, culturInfo);
221      return stringBuilder;
222    }
223
224    //! Convert a given double value to a string and concatenate onto the stringbuilder.
225    public static StringBuilder Concat(
226      this StringBuilder stringBuilder,
227      double doubleVal,
228      uint decimalPlaces,
229      uint padAmount,
230      CultureInfo culturInfo = null) {
231
232      stringBuilder.Concat(doubleVal, decimalPlaces, padAmount, MsDefaultPadChar, culturInfo);
233      return stringBuilder;
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.