Skip to content

Commit

Permalink
Fixes #2985 by adding a possible index to add operations to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Jul 2, 2020
1 parent d5359c0 commit ad6dbdf
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 20 deletions.
7 changes: 6 additions & 1 deletion msi.gama.core/src/msi/gama/util/IContainer.java
Expand Up @@ -104,7 +104,12 @@ public interface Modifiable<KeyType, ValueType> {

// Then, methods for "all" operations
// Adds the values if possible, without replacing existing ones
void addValues(IScope scope, IContainer<?, ?> values);
// AD July 2020: Addition of the index (see #2985)
void addValues(IScope scope, Object index, IContainer<?, ?> values);

default void addValues(final IScope scope, final IContainer<?, ?> values) {
addValues(scope, null, values);
}

// Adds this value to all slots (if this operation is available),
// otherwise replaces the values with this one
Expand Down
10 changes: 8 additions & 2 deletions msi.gama.core/src/msi/gama/util/IList.java
Expand Up @@ -125,9 +125,15 @@ default void setValueAtIndex(final IScope scope, final Object index, final E val
set(buildIndex(scope, index), buildValue(scope, value));
}

// AD July 2020: Addition of the index (see #2985)
@Override
default void addValues(final IScope scope, final IContainer values) {
addAll(buildValues(scope, values));
default void addValues(final IScope scope, final Object index, final IContainer values) {
if (index == null) {
addAll(buildValues(scope, values));
} else {
final int i = buildIndex(scope, index);
addAll(i, buildValues(scope, values));
}
}

@Override
Expand Down
14 changes: 11 additions & 3 deletions msi.gama.core/src/msi/gama/util/IMap.java
Expand Up @@ -9,6 +9,7 @@
import java.util.Spliterator;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;

import msi.gama.common.interfaces.BiConsumerWithPruning;
Expand Down Expand Up @@ -103,10 +104,17 @@ default void addValueAtIndex(final IScope scope, final Object index, final V val
*
* @see msi.gama.util.IContainer#addAll(msi.gama.runtime.IScope, msi.gama.util.IContainer)
*/
// AD July 2020: Addition of the index (see #2985)
@Override
default void addValues(final IScope scope, final IContainer/* <?, GamaPair<K, V>> */ values) {
for (final Object o : values.iterable(scope)) {
addValue(scope, (V) o);
default void addValues(final IScope scope, final Object index, final IContainer/* <?, GamaPair<K, V>> */ values) {
// If an index is specified, we add only the last object
if (index != null) {
final Iterable list = values.iterable(scope);
setValueAtIndex(scope, index, (V) Iterables.getLast(list));
} else {
for (final Object o : values.iterable(scope)) {
addValue(scope, (V) o);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions msi.gama.core/src/msi/gama/util/file/GamaFile.java
Expand Up @@ -214,9 +214,10 @@ public void setValueAtIndex(final IScope scope, final Object index, final Object
// Then, methods for "all" operations
// Adds the values if possible, without replacing existing ones
@Override
public void addValues(final IScope scope, final IContainer values) {
public void addValues(final IScope scope, final Object index, final IContainer values) {
// Addition of the index (see #2985)
fillBuffer(scope);
getBuffer().addValues(scope, values);
getBuffer().addValues(scope, index, values);
}

// Adds this value to all slots (if this operation is available), otherwise
Expand Down
13 changes: 7 additions & 6 deletions msi.gama.core/src/msi/gama/util/graph/GamaGraph.java
Expand Up @@ -327,16 +327,17 @@ public void setValueAtIndex(final IScope scope, final Object index,
}

@Override
public void addValues(final IScope scope, final IContainer values) {
public void addValues(final IScope scope, final Object index, final IContainer values) {
// Index is not used here as it does not make sense for graphs (see #2985)
if (values instanceof GamaGraph) {
for (final Object o : ((GamaGraph) values).edgeSet()) {
addEdge(o);
}
return;
}
for (final Object o : values.iterable(scope)) {
if (o instanceof msi.gaml.operators.Graphs.GraphObjectToAdd) {
addValue(scope, (msi.gaml.operators.Graphs.GraphObjectToAdd) o);
} else {
for (final Object o : values.iterable(scope)) {
if (o instanceof msi.gaml.operators.Graphs.GraphObjectToAdd) {
addValue(scope, (msi.gaml.operators.Graphs.GraphObjectToAdd) o);
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions msi.gama.core/src/msi/gama/util/matrix/GamaMatrix.java
Expand Up @@ -386,8 +386,8 @@ public final IMatrix copy(final IScope scope) {
}

public static boolean isFlat(final List val) {
for (int i = 0; i < val.size(); i++) {
if (val.get(i) instanceof List) { return false; }
for (final Object element : val) {
if (element instanceof List) { return false; }
}
return true;
}
Expand Down Expand Up @@ -544,8 +544,11 @@ public void setValueAtIndex(final IScope scope, final Object index, final T valu

// Then, methods for "all" operations
// Adds the values if possible, without replacing existing ones
// AD July 2020: Addition of the index (see #2985)
@Override
public void addValues(final IScope scope, final IContainer values) {}
public void addValues(final IScope scope, final Object index, final IContainer values) {
// Nothing to do for matrices
}

// Adds this value to all slots (if this operation is available), otherwise
// replaces the values with this one
Expand Down
6 changes: 3 additions & 3 deletions msi.gama.core/src/msi/gaml/statements/AddStatement.java
Expand Up @@ -271,8 +271,7 @@ public void validateIndexAndContentTypes(final String keyword, final IDescriptio
final IExpression item = cd.getFacetExpr(ITEM);
final IExpression list = cd.getFacetExpr(TO);
final IExpression allFacet = cd.getFacetExpr(ALL);
if (item == null)
return;
if (item == null) { return; }
if (allFacet != null && allFacet.isConst() && "true".equals(allFacet.literalValue())) {
if (!item.getGamlType().isContainer()) {
cd.warning(
Expand Down Expand Up @@ -338,7 +337,8 @@ protected void apply(final IScope scope, final Object object, final Object posit
}
} else {
if (object instanceof IContainer) {
container.addValues(scope, (IContainer<?, ?>) object);
// AD July 2020: Addition of the position (see #2985)
container.addValues(scope, position, (IContainer<?, ?>) object);
}
}
}
Expand Down

0 comments on commit ad6dbdf

Please sign in to comment.