-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.cs
370 lines (302 loc) · 12.4 KB
/
Data.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using AbMath.Utilities;
namespace AbMath.Calculator
{
public partial class RPN
{
public class DataStore
{
private readonly List<string> _meta_functions;
private readonly Dictionary<string, Function> _functions;
private readonly Dictionary<string, Operator> _operators;
private readonly Dictionary<string, string> _aliases;
private readonly Dictionary<double, string> _autoFormat;
private readonly List<string> _leftbracket;
private readonly List<string> _rightbracket;
private List<TimeRecord> _time;
private readonly Dictionary<string, string> _variableStore;
protected internal object LockObject = new object();
protected internal Logger Logger;
/// <summary>
/// A list of all the functions that are supported
/// by this calculator.
/// </summary>
public IReadOnlyDictionary<string,Function> Functions => _functions;
public IReadOnlyList<string> MetaFunctions => _meta_functions;
/// <summary>
/// A list of all operators that are supported
/// by this calculator.
/// </summary>
public IReadOnlyDictionary<string,Operator> Operators => _operators;
/// <summary>
/// A dictionary of expressions that the calculator
/// treats as equivalent.
/// </summary>
public IReadOnlyDictionary<string, string> Aliases => _aliases;
/// <summary>
/// A dictionary of numerical constants and known
/// representations of them
/// </summary>
public IReadOnlyDictionary<double,string> Format => _autoFormat;
/// <summary>
/// A list of all strings that would
/// start a function or work as grouping symbols
/// </summary>
public IReadOnlyList<string> LeftBracket => _leftbracket;
/// <summary>
/// A list of all strings that end function calls and
/// terminate grouping symbols
/// </summary>
public IReadOnlyList<string> RightBracket => _rightbracket;
/// <summary>
/// A list of variables that the calculator
/// has found in an expression
/// </summary>
public IReadOnlyList<string> Variables => Polish.Where(t => t.Type == Type.Variable).Select(t => t.Value).Distinct().ToList();
public IReadOnlyList<TimeRecord> Time => _time;
/// <summary>
/// The equation passed to the calculator
/// </summary>
public string Equation;
public string SimplifiedEquation;
public Token[] Polish { get; set; }
/// <summary>
/// Whether an expression contains variables
/// </summary>
public bool ContainsVariables => Polish.Any(t => t.Type == Type.Variable);
/// <summary>
/// Whether an expression contains a evaluator
/// such as =, > , > or a combination of those
/// operators.
/// </summary>
public bool ContainsEquation =>
Equation.Contains("=") || Equation.Contains(">") || Equation.Contains("<") ;
public double TotalMilliseconds => this.Time.Where(t => !t.Type.Contains(".")).Sum(t => t.ElapsedMilliseconds);
public double TotalSteps => this.Time.Where(t => !t.Type.Contains(".")).Sum(t => t.ElapsedTicks);
#region Config
/// <summary>
/// If true, auto generated debug tables
/// will write data in markdown format
/// </summary>
public bool MarkdownTables;
public bool AllowMismatchedParentheses;
/// <summary>
/// If true, the program will write tables to the
/// log
/// </summary>
public volatile bool DebugMode;
/// <summary>
/// Implicit multiplication in some interpretations
/// of order of operations has a higher priority
/// compared to that of division.
/// Set this to true to enable that feature.
/// </summary>
public bool ImplicitMultiplicationPriority;
#endregion
/// <summary>
/// Determines the default format of CLI Tables
/// based on the value of MarkdownTables
/// </summary>
public Format DefaultFormat => (MarkdownTables) ? Utilities.Format.MarkDown : Utilities.Format.Default ;
public DataStore(string equation)
{
Equation = equation;
_functions = new Dictionary<string, Function>();
_meta_functions = new List<string>(5);
_operators = new Dictionary<string, Operator>();
_aliases = new Dictionary<string, string>();
_autoFormat = new Dictionary<double, string>();
_leftbracket = new List<string>() { "(", "{", "[" };
_rightbracket = new List<string>() { ")", "}", "]", "," };
_time = new List<TimeRecord>(4);
_variableStore = new Dictionary<string, string>();
Logger = new Logger();
}
public void ClearTimeRecords()
{
_time.Clear();
}
public void SetEquation(string equation)
{
Equation = equation;
}
public void AddTimeRecord(string type, Stopwatch stopwatch)
{
AddTimeRecord(new TimeRecord
{
ElapsedMilliseconds = stopwatch.ElapsedMilliseconds,
ElapsedTicks = stopwatch.ElapsedTicks,
Type = type,
Count = 1
});
}
public void AddTimeRecord(TimeRecord time)
{
if (_time.Count > 0 && _time[_time.Count - 1].Type == time.Type)
{
TimeRecord prev = _time[_time.Count - 1];
prev.ElapsedMilliseconds += time.ElapsedMilliseconds;
prev.ElapsedTicks += time.ElapsedTicks;
prev.Count += 1;
_time[_time.Count - 1] = prev;
}
//If a Type contains a period it denotes that it should always be merged.
else if (time.Type.Contains(".") && _time.Any(t => t.Type == time.Type) )
{
int index = _time.FindIndex(t => t.Type == time.Type);
TimeRecord prev = _time[index];
prev.ElapsedMilliseconds += time.ElapsedMilliseconds;
prev.ElapsedTicks += time.ElapsedTicks;
prev.Count += 1;
_time[index] = prev;
}
else
{
_time.Add(time);
}
}
public Tables<string> TimeRecords()
{
Tables<string> times = new Tables<string>(new Config()
{
Format = DefaultFormat,
Title = "Time"
});
times.Add(new Schema("Type"));
times.Add(new Schema("# Calls"));
times.Add(new Schema("Time (ms)"));
times.Add(new Schema("Ticks"));
times.Add(new Schema("% Milliseconds"));
times.Add(new Schema("% Ticks"));
double miliseconds = TotalMilliseconds;
double steps = TotalSteps;
for (int i = 0; i < _time.Count; i++)
{
RPN.TimeRecord TR = _time[i];
times.Add(new string[]
{
TR.Type,
TR.Count.ToString(),
TR.ElapsedMilliseconds.ToString(),
TR.ElapsedTicks.ToString("N0"),
Math.Round((100 * TR.ElapsedMilliseconds / miliseconds), 2).ToString(),
(100 * TR.ElapsedTicks / steps).ToString("N0")
});
}
times.Add(new string[]
{
"Total", "", miliseconds.ToString("N0") , steps.ToString("N0"), " ", " "
});
times.Add(new string[] {Equation, "", "", "", "", "" });
return times;
}
public DataStore AddLeftBracket(string value)
{
_leftbracket.Add(value);
return this;
}
public DataStore AddLeftBracket(string[] value)
{
_leftbracket.AddRange(value);
return this;
}
public DataStore AddRightBracket(string value)
{
_rightbracket.Add(value);
return this;
}
public DataStore AddRightBracket(string[] value)
{
_rightbracket.AddRange(value);
return this;
}
public DataStore AddAlias(string key, string value)
{
_aliases.Add(key, value);
return this;
}
public DataStore AddStore(string variable,string value)
{
if (_variableStore.ContainsKey(variable))
{
_variableStore[variable] = value;
return this;
}
_variableStore.Add(variable, value);
return this;
}
public DataStore AddFunction(string key, Function func)
{
_functions.Add(key, func);
return this;
}
public DataStore AddMetaFunction(string key, Function func)
{
_functions.Add(key, func);
_meta_functions.Add(key);
return this;
}
public DataStore AddMetaFunction(string key)
{
_meta_functions.Add(key);
return this;
}
public DataStore AddOperator(string key, Operator ops)
{
_operators.Add(key, ops);
return this;
}
public DataStore AddFormat(double number, string format)
{
_autoFormat.Add(number,format);
return this;
}
public DataStore RemoveFunction(string function)
{
_functions.Remove(function);
return this;
}
public bool IsOperator(string value)
{
return Operators.ContainsKey(value);
}
public bool IsUnary(string value)
{
return IsOperator(value) && (value == "-" || value == "−" || value == "+");
}
public bool IsNumber(string value)
{
return double.TryParse(value, out double data);
}
public bool IsFunction(string value)
{
return Functions.ContainsKey(value);
}
public bool IsVariable(string value)
{
return value != "." && !(IsOperator(value) || IsFunction(value) || IsLeftBracket(value) || IsRightBracket(value) || IsNumber(value));
}
public bool IsLeftBracket(string value)
{
return LeftBracket.Contains(value);
}
public bool IsRightBracket(string value)
{
return RightBracket.Contains(value);
}
public Type Resolve(string value)
{
if (string.IsNullOrEmpty(value) || value == ".") { return Type.Null; }
if (IsNumber(value)) { return Type.Number; }
if (IsOperator(value)) { return Type.Operator; }
if (IsFunction(value)) { return Type.Function; }
if (IsLeftBracket(value)) { return Type.LParen; }
if (IsRightBracket(value)) { return Type.RParen; }
return Type.Variable;
}
}
}
}