[15189] | 1 | using System.Text;
|
---|
[15771] | 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[15189] | 4 |
|
---|
[15771] | 5 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[15189] | 6 | /// <summary>
|
---|
| 7 | /// Optimized for string concatenation to reduce memory effort
|
---|
| 8 | /// </summary>
|
---|
| 9 | public class PrintStack : IPrintStack {
|
---|
[15289] | 10 | private int lineCount = 0;
|
---|
[15334] | 11 | private int length = 0;
|
---|
| 12 | private IReadOnlyList<string> lines;
|
---|
[15189] | 13 | private readonly StringBuilder stringBuilder = new StringBuilder();
|
---|
| 14 |
|
---|
[15334] | 15 | public PrintStack() {
|
---|
| 16 | IsCurrentLineEmpty = true;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public bool IsCurrentLineEmpty { get; private set; }
|
---|
| 20 |
|
---|
[15189] | 21 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
[15334] | 22 | return Lines.GetEnumerator();
|
---|
[15189] | 23 | }
|
---|
| 24 |
|
---|
| 25 | public void Add(string item) {
|
---|
| 26 | Push(item);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[15771] | 29 | public IReadOnlyList<string> Lines {
|
---|
| 30 | get {
|
---|
[15341] | 31 | if (lines != null && stringBuilder.Length == length)
|
---|
| 32 | return lines;
|
---|
| 33 |
|
---|
[15334] | 34 | length = stringBuilder.Length;
|
---|
| 35 | lines = stringBuilder.ToString().Split(PushEnvironment.NewLine);
|
---|
| 36 |
|
---|
| 37 | return lines;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[15289] | 41 | void IPushStack.Clear() {
|
---|
[15334] | 42 | lineCount = 0;
|
---|
| 43 | length = 0;
|
---|
| 44 | lines = null;
|
---|
| 45 | IsCurrentLineEmpty = true;
|
---|
[15189] | 46 | stringBuilder.Clear();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public void NewLine() {
|
---|
[15289] | 50 | stringBuilder.Append(PushEnvironment.NewLine);
|
---|
[15334] | 51 | IsCurrentLineEmpty = true;
|
---|
[15289] | 52 | lineCount++;
|
---|
[15189] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public void Push<T>(T item) {
|
---|
[15289] | 56 | if (IsEmpty) lineCount = 1;
|
---|
[15334] | 57 | var str = item.ToString();
|
---|
| 58 | stringBuilder.Append(str);
|
---|
| 59 | IsCurrentLineEmpty = IsCurrentLineEmpty && string.IsNullOrEmpty(str);
|
---|
[15189] | 60 | }
|
---|
| 61 |
|
---|
| 62 | public void Push(float item) {
|
---|
[15289] | 63 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 64 | stringBuilder.Concat(item);
|
---|
[15334] | 65 | IsCurrentLineEmpty = false;
|
---|
[15189] | 66 | }
|
---|
| 67 |
|
---|
| 68 | public void Push(double item) {
|
---|
[15289] | 69 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 70 | stringBuilder.Concat(item);
|
---|
[15334] | 71 | IsCurrentLineEmpty = false;
|
---|
[15189] | 72 | }
|
---|
| 73 |
|
---|
| 74 | public void Push(long item) {
|
---|
[15289] | 75 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 76 | stringBuilder.Concat(item);
|
---|
[15334] | 77 | IsCurrentLineEmpty = false;
|
---|
[15189] | 78 | }
|
---|
| 79 |
|
---|
| 80 | public void Push(int item) {
|
---|
[15289] | 81 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 82 | stringBuilder.Concat(item);
|
---|
[15334] | 83 | IsCurrentLineEmpty = false;
|
---|
[15189] | 84 | }
|
---|
| 85 |
|
---|
| 86 | public void Push(char item) {
|
---|
[15289] | 87 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 88 | stringBuilder.Append(item);
|
---|
[15334] | 89 | IsCurrentLineEmpty = false;
|
---|
[15189] | 90 | }
|
---|
| 91 |
|
---|
| 92 | public void Push(bool item) {
|
---|
[15289] | 93 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 94 | stringBuilder.Append(item);
|
---|
[15334] | 95 | IsCurrentLineEmpty = false;
|
---|
[15189] | 96 | }
|
---|
| 97 |
|
---|
| 98 | public void Push(string item) {
|
---|
[15289] | 99 | if (IsEmpty) lineCount = 1;
|
---|
[15189] | 100 | stringBuilder.Append(item);
|
---|
[15334] | 101 | IsCurrentLineEmpty = IsCurrentLineEmpty && string.IsNullOrEmpty(item);
|
---|
[15189] | 102 | }
|
---|
| 103 |
|
---|
| 104 | public void Push(IReadOnlyList<string> items, int startIndex) {
|
---|
| 105 | for (var i = startIndex; i < items.Count; i++)
|
---|
| 106 | stringBuilder.Append(items[i]);
|
---|
[15334] | 107 |
|
---|
| 108 | IsCurrentLineEmpty = false;
|
---|
[15189] | 109 | }
|
---|
| 110 |
|
---|
| 111 | public void Push(IReadOnlyList<string> items) {
|
---|
| 112 | Push(items, 0);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public void Push(IEnumerable<string> items) {
|
---|
| 116 | foreach (var item in items)
|
---|
| 117 | stringBuilder.Append(item);
|
---|
[15334] | 118 |
|
---|
| 119 | IsCurrentLineEmpty = false;
|
---|
[15189] | 120 | }
|
---|
| 121 |
|
---|
| 122 | public IEnumerable<string> AsStrings() {
|
---|
[15334] | 123 | return Lines;
|
---|
[15189] | 124 | }
|
---|
| 125 |
|
---|
| 126 | public IEnumerable<object> AsObjects() {
|
---|
[15334] | 127 | return Lines;
|
---|
[15189] | 128 | }
|
---|
| 129 |
|
---|
[15771] | 130 | int IPushStack.Count {
|
---|
| 131 | get {
|
---|
[15289] | 132 | return lineCount;
|
---|
[15189] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[15289] | 136 | public bool IsEmpty { get { return stringBuilder.Length == 0; } }
|
---|
[15189] | 137 |
|
---|
| 138 | public bool IsEnabled { get; set; }
|
---|
| 139 |
|
---|
[15289] | 140 | public bool IsReadOnly { get { return false; } }
|
---|
| 141 |
|
---|
| 142 | public override string ToString() {
|
---|
| 143 | return stringBuilder.ToString();
|
---|
[15189] | 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|