1 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
2 | using System;
|
---|
3 | using System.Linq;
|
---|
4 |
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 |
|
---|
8 | public class StringGenerator {
|
---|
9 | public const int DefaultRandomNameLength = 10;
|
---|
10 |
|
---|
11 | private static readonly char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
|
---|
12 | private static readonly char[] lowercaseChars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
|
---|
13 | private static readonly char[] uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
|
---|
14 | private static readonly char[] digitChars = "1234567890".ToCharArray();
|
---|
15 | private const char space = ' ';
|
---|
16 |
|
---|
17 | private int index;
|
---|
18 |
|
---|
19 | public string GetNext() {
|
---|
20 | var length = index < chars.Length ? 1 : (int)Math.Log(index, chars.Length) + 1;
|
---|
21 | var result = new char[length];
|
---|
22 | var tmp = index;
|
---|
23 | index++;
|
---|
24 |
|
---|
25 | for (var i = length; i > 0; i--) {
|
---|
26 | var a = (int)Math.Pow(chars.Length, i - 1);
|
---|
27 | var b = tmp / a;
|
---|
28 |
|
---|
29 | result[length - i] = chars[b];
|
---|
30 |
|
---|
31 | tmp -= a * b;
|
---|
32 | }
|
---|
33 |
|
---|
34 | return new string(result);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static string RandomName(IRandom random) {
|
---|
38 | return RandomName(DefaultRandomNameLength, random);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static string RandomName(int length = DefaultRandomNameLength, IRandom random = null) {
|
---|
42 | random = random ?? new FastRandom();
|
---|
43 | var name = new char[length];
|
---|
44 |
|
---|
45 | for (var i = 0; i < length; i++)
|
---|
46 | name[i] = chars[random.Next(chars.Length)];
|
---|
47 |
|
---|
48 | return new string(name);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static string RandomString(bool allowLowercase, bool allowUppercase, bool allowSpace, double spaceProbability, int length, IRandom random = null) {
|
---|
52 | if (!allowLowercase && !allowUppercase) return string.Empty;
|
---|
53 |
|
---|
54 | random = random ?? new FastRandom();
|
---|
55 | var name = new char[length];
|
---|
56 |
|
---|
57 | var sourceChars = Enumerable.Empty<char>();
|
---|
58 | if (allowLowercase) sourceChars = sourceChars.Concat(lowercaseChars);
|
---|
59 | if (allowUppercase) sourceChars = sourceChars.Concat(uppercaseChars);
|
---|
60 |
|
---|
61 | var chars = sourceChars.ToList();
|
---|
62 |
|
---|
63 | for (var i = 0; i < name.Length; i++) {
|
---|
64 | name[i] = allowSpace && spaceProbability >= random.NextDouble()
|
---|
65 | ? space
|
---|
66 | : chars[random.Next(0, chars.Count)];
|
---|
67 | }
|
---|
68 |
|
---|
69 | return new string(name);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static char RandomChar(int from, int to, IRandom random = null) {
|
---|
73 | if (from > to) throw new InvalidOperationException();
|
---|
74 |
|
---|
75 | random = random ?? new FastRandom();
|
---|
76 |
|
---|
77 | return Convert.ToChar(from == to ? from : random.Next(from, to));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | } |
---|