forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnArray.cs
More file actions
125 lines (105 loc) · 4.32 KB
/
AnArray.cs
File metadata and controls
125 lines (105 loc) · 4.32 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
namespace NumSharp.UnitTest.Selection
{
public partial class Array
{
/// <summary>Initializes a new instance of the <see cref="T:System.Object"></see> class.</summary>
public Array(List<Array> values)
{
Values = values;
}
public Array(string val)
{
IsValue = true;
Value = val ?? throw new ArgumentNullException(nameof(val));
}
public void Add(Array child)
{
Values.Add(child);
}
public bool IsValue;
public string Value;
public List<Array> Values { get; set; }
[DebuggerDisplay("{Depth} - {Match}")]
private class ExpressionTrack
{
public Guid Id { get; } = Guid.NewGuid();
private Match _match;
public string AssignedVariable { get; set; }
public Match Match
{
get => _match;
set
{
_match = value;
Index = value.Index;
Length = value.Length;
}
}
public int Depth { get; set; }
public int Index { get; set; }
public int Length { get; set; }
}
public static Array Parse(string @string)
{
const string arrayElementsSeperationRegex = @"\s{1,}";
const string BracketDepthPattern2 = @"\[([^\[\]]*)\]";
var arrayStr = @string;
//here we perform an algorithm similar to shunting-yard
//var parsed = new Dictionary<Guid, Data>();
//var parsedMap = new Dictionary<string, string>();
var tracks = new List<ExpressionTrack>();
string @in = arrayStr;
int depth = 0;
int i = 0;
var parent = new Array(new List<Array>());
Array _last = null;
var uniqueid = new string(Guid.NewGuid().ToString("N").SkipWhile(char.IsDigit).ToArray());
while (true)
{
var matches = Regex.Matches(@in, BracketDepthPattern2, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant);
if (matches.Count == 0)
break;
foreach (Match expressionMatch in matches.Cast<Match>().Reverse())
{
//iterate matches, last to first
//expressionMatch.DisplayMatchResults();
var expression = StripBrackets(expressionMatch.Value);
var expressionTrack = new ExpressionTrack() {Depth = depth, Match = expressionMatch};
tracks.Add(expressionTrack);
//the following might be useless, left here in-case problems rise up.
//for (int j = 0; j < tracks.Count - 1; j++) {
// expression = expression.Replace(
// tracks[j].Match.Value,
// tracks[j].AssignedVariable);
//}
var input = StripBrackets(expression);
var _parsed = Regex.Split(input, arrayElementsSeperationRegex, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant)
.ToList();
//_last = new Array(_parsed);
parent.Add(_last);
var key = $"__{uniqueid}{i++}";
//variables.Add(key, arr);
expressionTrack.AssignedVariable = key; //parsedMap[expressionTrack.Match.Value] =
@in = @in.Replace(expressionMatch.Value, expressionTrack.AssignedVariable);
}
}
//remove used variables
//foreach (var k in variables.Keys.ToArray().Where(k => k.StartsWith($"__{uniqueid}")))
//{
// variables.Remove(k);
//}
return _last;
string StripBrackets(string input)
{
if (input.StartsWith("[") && input.EndsWith("]"))
input = input.Substring(1, input.Length - 2);
return input;
}
}
}
}