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