MATH-1265

Implement "Iterable".
This commit is contained in:
Gilles 2015-09-04 16:11:41 +02:00
parent 1f7b1c12ff
commit 18a027b1f0
3 changed files with 37 additions and 1 deletions

View File

@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>
<release version="4.0" date="XXXX-XX-XX" description="">
<action dev="erans" type="add" issue="MATH-1265"> <!-- backported to 3.6 -->
"NeuronSquareMesh2D" (package "o.a.c.m.ml.neuralnet.twod") implements "Iterable".
</action>
<action dev="erans" type="add" issue="MATH-1264"> <!-- backported to 3.6 -->
"MapUtils" (package "o.a.c.m.ml.neuralnet"): Method to sort units according to distance
from a given vector.

View File

@ -41,7 +41,9 @@ import org.apache.commons.math4.ml.neuralnet.SquareNeighbourhood;
* @see org.apache.commons.math4.ml.neuralnet.sofm
* @since 3.3
*/
public class NeuronSquareMesh2D implements Serializable {
public class NeuronSquareMesh2D
implements Iterable<Neuron>,
Serializable {
/** Serial version ID */
private static final long serialVersionUID = 1L;
/** Underlying network. */
@ -195,6 +197,11 @@ public class NeuronSquareMesh2D implements Serializable {
createLinks();
}
/** {@inheritDoc} */
public Iterator<Neuron> iterator() {
return network.iterator();
}
/**
* Retrieves the underlying network.
* A reference is returned (enabling, for example, the network to be

View File

@ -845,4 +845,30 @@ public class NeuronSquareMesh2DTest {
NeuronSquareMesh2D.HorizontalDirection.RIGHT,
NeuronSquareMesh2D.VerticalDirection.DOWN).getIdentifier());
}
@Test
public void testIterator() {
final FeatureInitializer[] initArray = { init };
final NeuronSquareMesh2D map = new NeuronSquareMesh2D(3, true,
3, true,
SquareNeighbourhood.VON_NEUMANN,
initArray);
final Set<Neuron> fromMap = new HashSet<Neuron>();
for (Neuron n : map) {
fromMap.add(n);
}
final Network net = map.getNetwork();
final Set<Neuron> fromNet = new HashSet<Neuron>();
for (Neuron n : net) {
fromNet.add(n);
}
for (Neuron n : fromMap) {
Assert.assertTrue(fromNet.contains(n));
}
for (Neuron n : fromNet) {
Assert.assertTrue(fromMap.contains(n));
}
}
}