Skip to content

Instantly share code, notes, and snippets.

@dhwaneetbhatt
Last active September 1, 2022 16:24
Show Gist options
  • Save dhwaneetbhatt/dff8350b173155fccd93d25379693bb1 to your computer and use it in GitHub Desktop.
Save dhwaneetbhatt/dff8350b173155fccd93d25379693bb1 to your computer and use it in GitHub Desktop.
List and array iterators for iterating over a matrix
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.google.common.base.Preconditions;
/**
* Provides data from a matrix represented using a 2D array
*
* @author Dhwaneet Bhatt
* @since Aug 17, 2016
*/
public class ArrayMatrixIterator<T> implements Iterator<T> {
private T[][] dataset;
private int rowIndex;
private int columnIndex;
public ArrayMatrixIterator(T[][] dataset) {
Preconditions.checkNotNull(dataset);
this.dataset = dataset;
}
@Override
public boolean hasNext() {
if (rowIndex >= dataset.length)
return false;
if (columnIndex >= dataset[rowIndex].length &&
(rowIndex >= dataset.length || rowIndex == dataset.length - 1))
return false;
return true;
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (columnIndex >= dataset[rowIndex].length) {
rowIndex++;
columnIndex = 0;
}
return dataset[rowIndex][columnIndex++];
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
import java.util.NoSuchElementException;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Dhwaneet Bhatt
* @since Aug 17, 2016
*/
public class ArrayMatrixIteratorTest {
@Test
public void testGetData() {
String[] row1 = {"0", "1", "2", "3"};
String[] row2 = {"4", "5", "6", "7"};
String[] row3 = {"8", "9", "10", "11"};
String[] row4 = {"12", "13", "14", "15"};
String[][] dataset = {row1, row2, row3, row4};
ArrayMatrixIterator<String> provider = new ArrayMatrixIterator<>(dataset);
int i = 0;
while (provider.hasNext()) {
String data = provider.next();
Assert.assertNotNull(data);
Assert.assertEquals(Integer.parseInt(data), i++);
}
Assert.assertFalse(provider.hasNext());
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testNoSuchElementException() {
String[] row1 = {"0"};
String[] row2 = {"1"};
String[][] dataset = {row1, row2};
ArrayMatrixIterator<String> provider = new ArrayMatrixIterator<>(dataset);
Assert.assertEquals(provider.next(), "0");
Assert.assertEquals(provider.next(), "1");
Assert.assertFalse(provider.hasNext());
provider.next();
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testReturnFalseForEmptyDataset() {
String[][] dataset = new String[0][0];
ArrayMatrixIterator<String> provider = new ArrayMatrixIterator<>(dataset);
Assert.assertFalse(provider.hasNext());
provider.next();
}
}
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import com.google.common.base.Preconditions;
/**
* Provides data from a matrix represented using nested lists of depth 2
*
* @author Dhwaneet Bhatt
* @since Aug 17, 2016
*/
public class ListMatrixIterator<T> implements Iterator<T> {
private List<List<T>> dataset;
private Iterator<List<T>> rowIterator;
private Iterator<T> columnIterator;
public ListMatrixIterator(List<List<T>> dataset) {
Preconditions.checkNotNull(dataset);
this.dataset = dataset;
this.rowIterator = dataset.iterator();
}
private boolean noDataInColumn() {
return this.columnIterator == null || !this.columnIterator.hasNext();
}
@Override
public boolean hasNext() {
if (this.dataset.isEmpty())
return false;
if (noDataInColumn())
return this.rowIterator.hasNext();
return this.columnIterator.hasNext();
}
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (noDataInColumn()) {
this.columnIterator = this.rowIterator.next().iterator();
}
return this.columnIterator.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
import java.util.List;
import java.util.NoSuchElementException;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.common.collect.Lists;
/**
* @author Dhwaneet Bhatt
* @since Aug 17, 2016
*/
@SuppressWarnings("unchecked")
public class ListMatrixIteratorTest {
@Test
public void testGetData() {
List<String> row1 = Lists.newArrayList("0", "1", "2", "3");
List<String> row2 = Lists.newArrayList("4", "5", "6", "7");
List<String> row3 = Lists.newArrayList("8", "9", "10", "11");
List<String> row4 = Lists.newArrayList("12", "13", "14", "15");
List<List<String>> dataset = Lists.newArrayList(row1, row2, row3, row4);
ListMatrixIterator<String> provider = new ListMatrixIterator<>(dataset);
int i = 0;
while (provider.hasNext()) {
String data = provider.next();
Assert.assertNotNull(data);
Assert.assertEquals(Integer.parseInt(data), i++);
}
Assert.assertFalse(provider.hasNext());
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testNoSuchElementException() {
List<String> row1 = Lists.newArrayList("0");
List<String> row2 = Lists.newArrayList("1");
List<List<String>> dataset = Lists.newArrayList(row1, row2);
ListMatrixIterator<String> provider = new ListMatrixIterator<>(dataset);
Assert.assertEquals(provider.next(), "0");
Assert.assertEquals(provider.next(), "1");
Assert.assertFalse(provider.hasNext());
provider.next();
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testReturnFalseForEmptyDataset() {
List<List<String>> dataset = Lists.newArrayList();
ListMatrixIterator<String> provider = new ListMatrixIterator<>(dataset);
Assert.assertFalse(provider.hasNext());
provider.next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment