Skip to content

Commit

Permalink
simplify IAST#forEach(start, end, consumer)
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Sep 29, 2024
1 parent 3bba791 commit a89d87e
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private static final class Do extends AbstractCoreFunctionEvaluator {
public IExpr evaluate(final IAST ast, EvalEngine engine) {
try {
final java.util.List<IIterator<IExpr>> iterList = new ArrayList<IIterator<IExpr>>();
ast.forEach(2, ast.size(), (x, i) -> iterList.add(Iterator.create((IAST) x, i, engine)));
ast.forEach2((x, i) -> iterList.add(Iterator.create((IAST) x, i, engine)));
final DoIterator generator = new DoIterator(iterList, engine);
return generator.doIt(ast.arg1());
} catch (final NoEvalException | ClassCastException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ public IASTMutable evalArgs(final IAST ast, final int attributes, boolean numeri
numericMode = fNumericMode;
try {
final boolean nMode = localNumericMode;
ast.forEach(2, astSize, (arg, i) -> {
ast.forEach2((arg, i) -> {
if (!arg.isUnevaluated()) {
selectNumericMode(attributes, ISymbol.NHOLDREST, nMode);
evalArg(rlist, ast, arg, i, isNumericFunction);
Expand All @@ -950,7 +950,7 @@ public IASTMutable evalArgs(final IAST ast, final int attributes, boolean numeri
numericMode = fNumericMode;
try {
selectNumericMode(attributes, ISymbol.NHOLDREST, localNumericMode);
ast.forEach(2, astSize, (arg, i) -> {
ast.forEach2((arg, i) -> {
if (arg.isAST(S.Evaluate)) {
evalArg(rlist, ast, arg, i, isNumericFunction);
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ private IExpr evalNoAttributes(IASTMutable mutableAST) {
final boolean localNumericMode = fNumericMode;
final boolean argNumericMode = isNumericArg(mutableAST);
IASTMutable[] rlist = new IASTMutable[] {F.NIL};
mutableAST.forEach(1, astSize, (arg, i) -> {
mutableAST.forEach((arg, i) -> {
if (!arg.isUnevaluated()) {
fNumericMode = localNumericMode;
evalArg(rlist, mutableAST, arg, i, argNumericMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public void forEach(ObjIntConsumer<? super IExpr> action) {
// do nothing
}

@Override
public void forEach2(ObjIntConsumer<? super IExpr> action) {
// do nothing
}

/** {@inheritDoc} */
@Override
public int indexOf(final IExpr expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
}

@Override
public void forEach2(ObjIntConsumer<? super IExpr> action) {
// do nothing
}

/** {@inheritDoc} */
@Override
public int indexOf(final IExpr expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
action.accept(arg2, 2);
}

@Override
public void forEach2(ObjIntConsumer<? super IExpr> action) {
action.accept(arg2, 2);
}

@Override
public int indexOf(final IExpr expr) {
if (arg1.equals(expr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,19 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public final void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
action.accept(arg2, 2);
action.accept(arg3, 3);
}

@Override
public final void forEach2(ObjIntConsumer<? super IExpr> action) {
action.accept(arg2, 2);
action.accept(arg3, 3);
}

@Override
public int indexOf(final IExpr expr) {
if (arg1.equals(expr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ public final IReal evalReal() {
public IExpr evalEvaluate(EvalEngine engine) {
IASTMutable[] rlist = new IASTMutable[] {F.NIL};
if (!isHoldAllCompleteAST()) {
forEach(1, size(), (x, i) -> {
forEach((x, i) -> {
if (x.isAST(S.Evaluate)) {
engine.evalArg(rlist, this, x, i, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,16 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public final void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
}

@Override
public void forEach2(ObjIntConsumer<? super IExpr> action) {
// do nothing
}

/** {@inheritDoc} */
@Override
public int indexOf(final IExpr expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,21 +1195,21 @@ public void forEach(Consumer<? super IExpr> action, int startOffset) {

/** {@inheritDoc} */
@Override
public void forEach(int startOffset, int endOffset, Consumer<? super IExpr> action) {
public void forEach(int startOffset, final int endOffset, final Consumer<? super IExpr> action) {
if (startOffset < endOffset) {
switch (startOffset) {
case 0:
action.accept(head());
if (startOffset + 1 < endOffset) {
if (++startOffset < endOffset) {
action.accept(arg1);
if (startOffset + 2 < endOffset) {
if (++startOffset < endOffset) {
action.accept(arg2);
}
}
break;
case 1:
action.accept(arg1);
if (startOffset + 1 < endOffset) {
if (++startOffset < endOffset) {
action.accept(arg2);
}
break;
Expand All @@ -1224,34 +1224,44 @@ public void forEach(int startOffset, int endOffset, Consumer<? super IExpr> acti
}

@Override
public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
public void forEach(int start, final int end, ObjIntConsumer<? super IExpr> action) {
if (start < end) {
switch (start) {
case 0:
action.accept(head(), 0);
if (start + 1 < end) {
if (++start < end) {
action.accept(arg1, 1);
if (start + 2 < end) {
if (++start < end) {
action.accept(arg2, 2);
}
}
break;
case 1:
action.accept(arg1, 1);
if (start + 1 < end) {
if (++start < end) {
action.accept(arg2, 2);
}
break;
case 2:
action.accept(arg2, 2);
break;
default:
throw new IndexOutOfBoundsException(
"Index: " + Integer.valueOf(start) + ", Size: 3");
throw new IndexOutOfBoundsException("Index: " + Integer.valueOf(start) + ", Size: 3");
}
}
}

@Override
public final void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
action.accept(arg2, 2);
}

@Override
public final void forEach2(ObjIntConsumer<? super IExpr> action) {
action.accept(arg2, 2);
}

@Override
public IExpr get(int location) {
switch (location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,19 @@ public void forEach(int start, int end, ObjIntConsumer<? super IExpr> action) {
}
}

@Override
public final void forEach(ObjIntConsumer<? super IExpr> action) {
action.accept(arg1, 1);
action.accept(arg2, 2);
action.accept(arg3, 3);
}

@Override
public final void forEach2(ObjIntConsumer<? super IExpr> action) {
action.accept(arg2, 2);
action.accept(arg3, 3);
}

/** {@inheritDoc} */
@Override
public int indexOf(final IExpr expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,14 +919,24 @@ default void forEach(int start, int end, ObjIntConsumer<? super IExpr> consumer)
}
}

/**
* Consume all <code>value-elements</code> generated by the given function from index
* <code>2</code> inclusive to <code>size()</code> exclusive.
*
* @param consumer function which accepts the elements
*/
default void forEach2(final ObjIntConsumer<? super IExpr> consumer) {
forEach(2, size(), consumer);
}

/**
* Consume all <code>value-elements</code> generated by the given function from index
* <code>1</code> inclusive.
*
* @param end end index (exclusive)
* @param consumer function which accepts the elements
*/
default void forEach(int end, ObjIntConsumer<? super IExpr> consumer) {
default void forEach(int end, final ObjIntConsumer<? super IExpr> consumer) {
forEach(1, end, consumer);
}

Expand Down

0 comments on commit a89d87e

Please sign in to comment.