FlattenedArray.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package gnu.lists;
  2. /** View an array as a vector, with the former's elements in row-major order. */
  3. public class FlattenedArray<E> extends TransformedArray<E> implements AVector<E>
  4. {
  5. private final int size;
  6. private final int brank;
  7. public FlattenedArray(Array<E> base) {
  8. super(base);
  9. size = base.getSize();
  10. brank = base.rank();
  11. }
  12. @Override
  13. public int size() { return size; }
  14. @Override
  15. public int getSize(int dim) {
  16. if (dim != 0)
  17. badRank(dim);
  18. return size;
  19. }
  20. @Override
  21. public int effectiveIndex(int i) {
  22. return Arrays.rowMajorToEffectiveIndex(base, i);
  23. }
  24. /** Created a shared flattened view of the argument.
  25. */
  26. public static <E> AVector<E> flatten(Array<E> array) {
  27. if (array instanceof AVector)
  28. return (AVector) array;
  29. if (array instanceof GeneralArray) {
  30. GeneralArray<E> garr = (GeneralArray<E>) array;
  31. if (garr.simple && garr.base instanceof AVector)
  32. return (AVector<E>) garr.base;
  33. }
  34. return new FlattenedArray(array);
  35. }
  36. }