-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSolverTools.cs
More file actions
123 lines (113 loc) · 2.34 KB
/
Copy pathSolverTools.cs
File metadata and controls
123 lines (113 loc) · 2.34 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
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace AK {
public static class SolverTools
{
public struct IntPair
{
public int first;
public int second;
public IntPair(int first, int second)
{
this.first = first;
this.second = second;
}
}
private static bool IsWhiteSpaceChar(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
public static string RemoveWhiteSpace(string formula)
{
int l = formula.Length;
StringBuilder sb = new StringBuilder(l);
for (int i=0;i<l;i++)
{
char c = formula[i];
if (c=='\'')
{
sb.Append(c);
for (int j=i+1;j<l;j++)
{
char d = formula[j];
if (d=='\'' && formula[j-1] != '\\')
{
i++;
break;
}
i++;
sb.Append(d);
}
}
if (!IsWhiteSpaceChar(c))
{
sb.Append(c);
}
}
return sb.ToString();
}
public static List<IntPair> ParseParameters(string formula, int begin, int end)
{
List<IntPair> r = new List<IntPair>();
int currentParamBegin = -1;
int depth = 0;
for (int i=begin;i<end;i++) {
if (formula[i] == '(') {
if (depth == 0) {
// First parameters
currentParamBegin = i+1;
}
depth++;
}
else if (formula[i] == ')') {
depth--;
if (depth == 0) {
r.Add (new IntPair(currentParamBegin,i));
}
}
else if (formula[i] == ',' && depth == 1) {
r.Add (new IntPair(currentParamBegin,i));
currentParamBegin = i+1;
}
}
return r;
}
public static int CountParameters(string formula,int begin,int end) {
int depth = 0;
int r = 1;
for (int i=begin;i<end;i++) {
if (formula[i] == '(') {
depth++;
}
else if (formula[i] == ')') {
depth--;
}
else if (formula[i] == ',' && depth == 1) {
r++;
}
}
return r;
}
public static int ParseUntilEndOfExponent(string formula, int begin, int end) {
int currentDepth = 0;
for (int i=begin;i<end;i++) {
if (formula[i] == '(') {
currentDepth++;
}
else if (formula[i] == ')') {
currentDepth--;
if (currentDepth == -1)
return i;
}
else if (currentDepth==0) {
if (i>begin && formula[i]=='-')
return i;
else if (i>begin && formula[i]=='+')
return i;
}
}
return end;
}
}
}