SubSequence.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2001, 2002, 2003 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.lists;
  4. /** A sequence consisting of a sub-range of the elements of a base sequence.
  5. * The start and end positions are position triples (on the same sequence).
  6. */
  7. public class SubSequence<E>
  8. extends AbstractSequence<E> implements Sequence<E>
  9. {
  10. /** Normally the Sequence this a sub-sequence of.
  11. * Actually the sequence that provides context for the
  12. * start and end position pairs. */
  13. AbstractSequence<E> base;
  14. /** Integer part of start position. */
  15. int ipos0;
  16. /** Integer part of end position. */
  17. int ipos1;
  18. public SubSequence()
  19. {
  20. }
  21. public SubSequence(AbstractSequence<E> base, int startPos, int endPos)
  22. {
  23. this.base = base;
  24. this.ipos0 = startPos;
  25. this.ipos1 = endPos;
  26. }
  27. public SubSequence(AbstractSequence<E> base)
  28. {
  29. this.base = base;
  30. }
  31. @Override
  32. public E get(int index)
  33. {
  34. if (index < 0 || index >= size())
  35. throw new IndexOutOfBoundsException();
  36. int start = base.nextIndex(ipos0);
  37. return base.get(start + index);
  38. }
  39. public int size()
  40. {
  41. return base.getIndexDifference(ipos1, ipos0);
  42. }
  43. public void removePosRange(int istart, int iend)
  44. {
  45. base.removePosRange(istart == 0 ? ipos0 : istart == -1 ? ipos1 : istart,
  46. iend == -1 ? ipos1 : iend == 0 ? ipos0 : iend);
  47. }
  48. protected boolean isAfterPos(int ipos)
  49. { return base.isAfterPos(ipos); }
  50. public int createPos(int offset, boolean isAfter)
  51. {
  52. return base.createRelativePos(ipos0, offset, isAfter);
  53. }
  54. public int createRelativePos(int pos, int offset, boolean isAfter)
  55. {
  56. return base.createRelativePos(pos, offset, isAfter);
  57. }
  58. protected int getIndexDifference(int ipos1, int ipos0)
  59. {
  60. return base.getIndexDifference(ipos1, ipos0);
  61. }
  62. public void releasePos(int ipos)
  63. {
  64. base.releasePos(ipos);
  65. }
  66. protected int nextIndex (int ipos)
  67. {
  68. return getIndexDifference(ipos, ipos0);
  69. }
  70. public int compare (int ipos1, int ipos2)
  71. {
  72. return base.compare(ipos1, ipos2);
  73. }
  74. public Object getPosNext(int ipos)
  75. {
  76. if (base.compare(ipos, ipos1) >= 0)
  77. return eofValue;
  78. return base.getPosNext(ipos);
  79. }
  80. public int getNextKind(int ipos)
  81. {
  82. if (base.compare(ipos, ipos1) >= 0)
  83. return EOF_VALUE;
  84. return base.getNextKind(ipos);
  85. }
  86. public int startPos () { return ipos0; }
  87. public int endPos () { return ipos1; }
  88. public Object getPosPrevious(int ipos)
  89. {
  90. if (base.compare(ipos, ipos0) <= 0)
  91. return eofValue;
  92. return base.getPosPrevious(ipos);
  93. }
  94. @Override
  95. protected Sequence<E> subSequencePos(int ipos0, int ipos1)
  96. {
  97. return new SubSequence<E>(base, ipos0, ipos1);
  98. }
  99. public void clear()
  100. {
  101. removePosRange(ipos0, ipos1);
  102. }
  103. public void finalize()
  104. {
  105. base.releasePos(ipos0);
  106. base.releasePos(ipos1);
  107. }
  108. }