001 package org.ujmp.core.util; 002 003 import java.util.Iterator; 004 005 import org.ujmp.core.Coordinates; 006 import org.ujmp.core.exceptions.MatrixException; 007 008 public class CoordinateIterator implements Iterable<long[]> { 009 010 private long[] size = null; 011 012 public CoordinateIterator(long... size) { 013 this.size = size; 014 } 015 016 public Iterator<long[]> iterator() { 017 return new It(size); 018 } 019 020 class It implements Iterator<long[]> { 021 long[] cursor = null; 022 023 long[] size = null; 024 025 long[] lastEntry = null; 026 027 boolean isNotEmpty = false; 028 029 public It(long... size) { 030 this.size = size; 031 this.lastEntry = Coordinates.minusOne(size); 032 this.cursor = new long[size.length]; 033 cursor[cursor.length - 1]--; 034 isNotEmpty = Coordinates.product(size) != 0; 035 } 036 037 public boolean hasNext() { 038 return !Coordinates.equals(lastEntry, cursor) && isNotEmpty; 039 } 040 041 public long[] next() { 042 increment(cursor.length - 1); 043 return cursor; 044 } 045 046 private void increment(int dim) { 047 cursor[dim]++; 048 if (cursor[dim] == size[dim]) { 049 cursor[dim] = 0; 050 increment(dim - 1); 051 } 052 } 053 054 public void remove() { 055 throw new MatrixException("not implemented"); 056 } 057 } 058 059 }