Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PrintStack.cs @ 16101

Last change on this file since 16101 was 15341, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Improved Performance

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