Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PrintStack.cs @ 17773

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

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

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