IndirectIndexedSeq.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) 2015 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ../../COPYING.
  3. package gnu.lists;
  4. import java.util.List;
  5. /** Wrap a List (or an indexed selection of it) as a Sequence. */
  6. public class IndirectIndexedSeq<E>
  7. extends AbstractSequence<E> implements Sequence<E>, Array<E> {
  8. List<E> base;
  9. IntSequence indexes;
  10. public IndirectIndexedSeq(List<E> base, IntSequence indexes) {
  11. this.base = base;
  12. this.indexes = indexes;
  13. }
  14. @Override
  15. public int size() {
  16. return indexes.size();
  17. }
  18. public int getElementKind() {
  19. return (base instanceof AbstractSequence
  20. ? ((AbstractSequence) base).getElementKind()
  21. : null);
  22. }
  23. public int getBufferLength() { return base.size(); }
  24. @Override
  25. public int effectiveIndex(int i) { return indexes.getInt(i); }
  26. @Override
  27. public E get(int index) {
  28. return base.get(indexes.getInt(index));
  29. }
  30. @Override
  31. public E set(int index, E value) {
  32. return base.set(indexes.getInt(index), value);
  33. }
  34. @Override
  35. public E getRaw(int i) { return base.get(i); }
  36. @Override
  37. public void setRaw(int index, E value) { base.set(index, value); }
  38. public void copyBuffer(int length) { throw unsupported("copyBuffer"); }
  39. }