1
1
package org .matheclipse .core .visit ;
2
2
3
- import java .util .ArrayList ;
4
3
import org .matheclipse .core .eval .EvalEngine ;
5
4
import org .matheclipse .core .eval .exception .ReturnException ;
6
5
import org .matheclipse .core .expression .F ;
@@ -21,8 +20,9 @@ public class VisitorReplacePart extends AbstractVisitor {
21
20
* A list of pattern matchers which should be matched against, for every possible position of an
22
21
* {@link IAST} structure.
23
22
*/
24
- private ArrayList < IPatternMatcher > patternMatcherList ;
23
+ private IPatternMatcher [] patternMatcherList ;
25
24
25
+ private int listLength ;
26
26
/**
27
27
* <code>0</code> or <code>1</code>, depending if option <code>Heads->True</code> is set or the
28
28
* <code>0</code> index position is used in the left-hand-side of a rule.
@@ -49,7 +49,7 @@ public VisitorReplacePart(IAST rule, IExpr.COMPARE_TERNARY heads) {
49
49
}
50
50
if (rule .isListOfRules ()) {
51
51
IAST list = rule ;
52
- this .patternMatcherList = new ArrayList < IPatternMatcher >( list .argSize () + 3 ) ;
52
+ this .patternMatcherList = new IPatternMatcher [ list .argSize ()] ;
53
53
54
54
for (int i = 1 ; i < list .size (); i ++) {
55
55
rule = (IAST ) list .get (i );
@@ -66,7 +66,11 @@ public VisitorReplacePart(IAST rule, IExpr.COMPARE_TERNARY heads) {
66
66
public VisitorReplacePart (IExpr lhs , IExpr rhs , IExpr .COMPARE_TERNARY heads ) {
67
67
super ();
68
68
engine = EvalEngine .get ();
69
- this .patternMatcherList = new ArrayList <IPatternMatcher >(1 );
69
+ if (lhs .isList ()) {
70
+ this .patternMatcherList = new IPatternMatcher [lhs .argSize ()];
71
+ } else {
72
+ this .patternMatcherList = new IPatternMatcher [1 ];
73
+ }
70
74
startOffset = heads == IExpr .COMPARE_TERNARY .TRUE ? 0 : 1 ;
71
75
initPatternMatcher (lhs , rhs , heads );
72
76
if (heads == COMPARE_TERNARY .FALSE ) {
@@ -106,7 +110,7 @@ private void initPatternMatcher(IExpr lhs, IExpr rhs, IExpr.COMPARE_TERNARY head
106
110
}
107
111
IPatternMatcher evalPatternMatcher =
108
112
engine .evalPatternMatcher (F .Sequence (positions ), rhs );
109
- this .patternMatcherList . add ( evalPatternMatcher ) ;
113
+ this .patternMatcherList [ listLength ++] = evalPatternMatcher ;
110
114
}
111
115
} else {
112
116
if (list .argSize () > 0 ) {
@@ -123,7 +127,7 @@ private void initPatternMatcher(IExpr lhs, IExpr rhs, IExpr.COMPARE_TERNARY head
123
127
}
124
128
IPatternMatcher evalPatternMatcher =
125
129
engine .evalPatternMatcher (F .Sequence (positions ), rhs );
126
- this .patternMatcherList . add ( evalPatternMatcher ) ;
130
+ this .patternMatcherList [ listLength ++] = evalPatternMatcher ;
127
131
}
128
132
}
129
133
} else {
@@ -135,17 +139,17 @@ private void initPatternMatcher(IExpr lhs, IExpr rhs, IExpr.COMPARE_TERNARY head
135
139
startOffset = 0 ;
136
140
}
137
141
IPatternMatcher evalPatternMatcher = engine .evalPatternMatcher (F .Sequence (positions ), rhs );
138
- this .patternMatcherList . add ( evalPatternMatcher ) ;
142
+ this .patternMatcherList [ listLength ++] = evalPatternMatcher ;
139
143
140
144
}
141
145
} catch (ReturnException rex ) {
142
146
if (fromPositions .isList ()) {
143
147
IAST list = ((IAST ) fromPositions ).apply (S .Sequence , 1 );
144
148
IPatternMatcher evalPatternMatcher = engine .evalPatternMatcher (list , rhs );
145
- this .patternMatcherList . add ( evalPatternMatcher ) ;
149
+ this .patternMatcherList [ listLength ++] = evalPatternMatcher ;
146
150
} else {
147
151
IPatternMatcher evalPatternMatcher = engine .evalPatternMatcher (fromPositions , rhs );
148
- this .patternMatcherList . add ( evalPatternMatcher ) ;
152
+ this .patternMatcherList [ listLength ++] = evalPatternMatcher ;
149
153
}
150
154
}
151
155
}
@@ -154,8 +158,11 @@ private IExpr visitPatternIndexList(IAST ast, IASTAppendable positions) {
154
158
IASTAppendable result = F .NIL ;
155
159
for (int i = startOffset ; i < ast .size (); i ++) {
156
160
final IInteger position = F .ZZ (i );
157
- for (int j = 0 ; j < patternMatcherList .size (); j ++) {
158
- IPatternMatcher matcher = patternMatcherList .get (j );
161
+ for (int j = 0 ; j < listLength ; j ++) {
162
+ IPatternMatcher matcher = patternMatcherList [j ];
163
+ if (matcher == null ) {
164
+ continue ;
165
+ }
159
166
IASTAppendable positionsToMatch = positions .copyAppendable ();
160
167
positionsToMatch .append (position );
161
168
@@ -251,8 +258,11 @@ private IASTAppendable patternIndexRecursive(IPatternMatcher matcher, IAST ast,
251
258
@ Override
252
259
public IExpr visit (IASTMutable ast ) {
253
260
IASTAppendable positionsToMatch = F .ast (S .Sequence );
254
- for (int j = 0 ; j < patternMatcherList .size (); j ++) {
255
- IPatternMatcher matcher = patternMatcherList .get (j );
261
+ for (int j = 0 ; j < listLength ; j ++) {
262
+ IPatternMatcher matcher = patternMatcherList [j ];
263
+ if (matcher == null ) {
264
+ continue ;
265
+ }
256
266
IExpr lhs = matcher .getLHS ();
257
267
if (lhs .isAST (S .Sequence , 1 )) {
258
268
// empty sequence matches with complete expression
0 commit comments