Package gnu.lists
Class AbstractSequence<E>
- java.lang.Object
-
- gnu.lists.AbstractSequence<E>
-
- Direct Known Subclasses:
Arrays.BuiltArray
,ExtSequence
,IndirectIndexedSeq
,IString
,Range
,SimpleVector
,SubSequence
,TransformedArray
,TreeList
,Values
public abstract class AbstractSequence<E> extends Object
A collection of default methods for implementing sequence-like classes. Additionally, a sequence may have zero or more attributes, which are name-value pairs. A sequence may also have a named "type". These extensions are to support XML functionality - it might be cleaner to move them to a sub-class of Sequence or some interface. Many of the protected methods in Sequence (such as nextIndex) are only intended to be called from SeqPosition or TreePosition, see those.
-
-
Field Summary
Fields Modifier and Type Field Description static int[]
noInts
-
Constructor Summary
Constructors Constructor Description AbstractSequence()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, E o)
See java.util.List.boolean
add(E o)
See java.util.Collection.boolean
addAll(int index, Collection<? extends E> c)
See java.util.Collection.boolean
addAll(Collection<? extends E> c)
See java.util.Collection.protected int
addPos(int ipos, E value)
Add a value at a specified Pos.Array<E>
asImmutable()
protected RuntimeException
badRank(int i)
int
boundedHash(int seed, int limit)
protected void
checkCanWrite()
protected void
checkRank(int i)
void
clear()
int
compare(int ipos1, int ipos2)
Compare two positions, and indicate their relative order.static int
compare(AbstractSequence seq1, int pos1, AbstractSequence seq2, int pos2)
This is used for the XML concept of "document order".int
compare(SeqPosition i1, SeqPosition i2)
void
consume(int fromIndex, int toIndex, Consumer out)
void
consume(Consumer out)
boolean
consumeNext(int ipos, Consumer out)
Copy an element specified by a position pair to a Consumer.void
consumePosRange(int iposStart, int iposEnd, Consumer out)
boolean
contains(Object o)
See java.util.List.boolean
containsAll(Collection<?> c)
See java.util.List.int
copyPos(int ipos)
Make a copy of a position int.int
createPos(int index, boolean isAfter)
Generate a position at a given index.int
createRelativePos(int pos, int delta, boolean isAfter)
int
effectiveIndex()
int
effectiveIndex(int index)
int
effectiveIndex(int[] indexes)
int
effectiveIndex(int i, int j)
int
effectiveIndex(int i, int j, int k, int... rest)
Enumeration<E>
elements()
int
endPos()
boolean
equals(int ipos1, int ipos2)
Compare two positions, and indicate if they are the same position.boolean
equals(Object o)
void
fill(int fromIndex, int toIndex, E value)
void
fill(E value)
void
fillPosRange(int fromPos, int toPos, E value)
int
firstAttributePos(int ipos)
Like firstChildPos.int
firstChildPos(int ipos)
Get position before first child (of the element following position).int
firstChildPos(int ipos, ItemPredicate predicate)
protected int
fromEndIndex(int ipos)
E
get()
E
get(int i)
E
get(int[] indexes)
E
get(int i, int j)
E
get(int i, int j, int k, int... rest)
Object
getAttribute(int index)
int
getAttributeLength()
boolean
getBooleanRaw(int index)
byte
getByteRaw(int index)
char
getCharRaw(int index)
protected int
getContainingSequenceSize(int ipos)
Get the size of the (sub-) sequence containing a given position.double
getDoubleRaw(int index)
int
getElementKind()
float
getFloatRaw(int index)
protected int
getIndexDifference(int ipos1, int ipos0)
Get offset of (ipos1) relative to (ipos0).int
getInt()
int
getInt(int i)
int
getInt(int[] indexes)
int
getInt(int i, int j)
int
getInt(int i, int j, int k, int... rest)
int
getIntRaw(int index)
SeqPosition<E,AbstractSequence<E>>
getIterator()
SeqPosition<E,AbstractSequence<E>>
getIterator(int index)
SeqPosition<E,AbstractSequence<E>>
getIteratorAtPos(int ipos)
long
getLongRaw(int index)
int
getLowBound(int dim)
int
getNextKind(int ipos)
String
getNextTypeName(int ipos)
E
getNextTypeObject(int ipos)
Object
getPosNext(int ipos)
Get the element following the specified position.Object
getPosPrevious(int ipos)
Get the element before the specified position.E
getRaw(int index)
E
getRowMajor(int index)
short
getShortRaw(int index)
int
getSize()
int
getSize(int dim)
protected boolean
gotoAttributesStart(TreePosition pos)
boolean
gotoChildrenStart(TreePosition pos)
Set position before first child (of the element following position).protected boolean
gotoParent(TreePosition pos)
int
hashCode()
boolean
hasNext(int ipos)
protected boolean
hasPrevious(int ipos)
Called by SeqPosition.hasPrevious.int
indexOf(Object o)
See java.util.List.protected boolean
isAfterPos(int ipos)
Tests whether the position has the "isAfter" property.boolean
isEmpty()
Iterator<E>
iterator()
int
lastIndexOf(Object o)
See java.util.List.ListIterator<E>
listIterator()
ListIterator<E>
listIterator(int index)
protected int
nextIndex(int ipos)
Get the offset from the beginning corresponding to a position cookie.int
nextIndex(SeqPosition pos)
int
nextMatching(int startPos, ItemPredicate type, int endPos, boolean descend)
Get next matching child or descendent (ignoring attributes).int
nextPos(int ipos)
Return the next position following the argument.int
parentPos(int ipos)
Get position of parent.int
previousPos(int ipos)
Return the previous position following the argument.int
rank()
protected void
releasePos(int ipos)
Reclaim any resources used by the given position int.E
remove(int index)
boolean
remove(Object o)
boolean
removeAll(Collection<?> c)
void
removePos(int ipos, int count)
Remove one or more elements.protected void
removePosRange(int ipos0, int ipos1)
Remove a range where each end-point is a position in a container.boolean
retainAll(Collection<?> c)
void
set(int[] indexes, E value)
E
set(int index, E value)
void
setAt(int index, E value)
protected void
setBuffer(Object obj)
protected void
setPosNext(int ipos, E value)
protected void
setPosPrevious(int ipos, E value)
void
setRaw(int index, E value)
Given an "effective index", set selected element.int
size()
int
stableCompare(AbstractSequence other)
This is used for the XML concept of "document order".int
startPos()
List<E>
subList(int fromIx, int toIx)
Sequence
subSequence(SeqPosition start, SeqPosition end)
protected Sequence<E>
subSequencePos(int ipos0, int ipos1)
Object[]
toArray()
<T> T[]
toArray(T[] arr)
String
toString()
void
toString(String sep, StringBuffer sbuf)
protected RuntimeException
unsupported(String text)
static RuntimeException
unsupportedException(String text)
-
-
-
Method Detail
-
size
public int size()
-
getSize
public int getSize()
-
isEmpty
public boolean isEmpty()
-
rank
public int rank()
-
checkRank
protected void checkRank(int i)
-
badRank
protected RuntimeException badRank(int i)
-
get
public E get()
-
get
public E get(int i)
-
get
public E get(int i, int j)
-
get
public E get(int i, int j, int k, int... rest)
-
get
public E get(int[] indexes)
-
checkCanWrite
protected void checkCanWrite()
-
getRowMajor
public E getRowMajor(int index)
-
effectiveIndex
public int effectiveIndex()
-
effectiveIndex
public int effectiveIndex(int index)
-
effectiveIndex
public int effectiveIndex(int i, int j)
-
effectiveIndex
public int effectiveIndex(int i, int j, int k, int... rest)
-
effectiveIndex
public int effectiveIndex(int[] indexes)
-
getRaw
public E getRaw(int index)
-
setBuffer
protected void setBuffer(Object obj)
-
setRaw
public void setRaw(int index, E value)
Given an "effective index", set selected element.
-
getBooleanRaw
public boolean getBooleanRaw(int index)
-
getCharRaw
public char getCharRaw(int index)
-
getByteRaw
public byte getByteRaw(int index)
-
getShortRaw
public short getShortRaw(int index)
-
getIntRaw
public int getIntRaw(int index)
-
getLongRaw
public long getLongRaw(int index)
-
getFloatRaw
public float getFloatRaw(int index)
-
getDoubleRaw
public double getDoubleRaw(int index)
-
getInt
public int getInt()
-
getInt
public int getInt(int i)
-
getInt
public int getInt(int i, int j)
-
getInt
public int getInt(int i, int j, int k, int... rest)
-
getInt
public int getInt(int[] indexes)
-
set
public void set(int[] indexes, E value)
-
getLowBound
public int getLowBound(int dim)
-
getSize
public int getSize(int dim)
-
getElementKind
public int getElementKind()
-
unsupported
protected RuntimeException unsupported(String text)
-
unsupportedException
public static RuntimeException unsupportedException(String text)
-
setAt
public void setAt(int index, E value)
-
fill
public void fill(E value)
-
fillPosRange
public void fillPosRange(int fromPos, int toPos, E value)
-
fill
public void fill(int fromIndex, int toIndex, E value)
-
indexOf
public int indexOf(Object o)
See java.util.List.
-
lastIndexOf
public int lastIndexOf(Object o)
See java.util.List.
-
nextMatching
public int nextMatching(int startPos, ItemPredicate type, int endPos, boolean descend)
Get next matching child or descendent (ignoring attributes).- Parameters:
startPos
- starting positiontype
- a test (predicate) to apply to selected elementsendPos
- stop before endPosdescend
- if true do depth-first traversal.- Returns:
- poistion of next match or 0 if none found
-
contains
public boolean contains(Object o)
See java.util.List.
-
containsAll
public boolean containsAll(Collection<?> c)
See java.util.List.
-
elements
public final Enumeration<E> elements()
-
getIterator
public final SeqPosition<E,AbstractSequence<E>> getIterator()
-
getIterator
public SeqPosition<E,AbstractSequence<E>> getIterator(int index)
-
getIteratorAtPos
public SeqPosition<E,AbstractSequence<E>> getIteratorAtPos(int ipos)
-
listIterator
public final ListIterator<E> listIterator()
-
listIterator
public final ListIterator<E> listIterator(int index)
-
addPos
protected int addPos(int ipos, E value)
Add a value at a specified Pos.- Returns:
- the updated Pos, which is after the inserted value..
-
add
public boolean add(E o)
See java.util.Collection.
-
add
public void add(int index, E o)
See java.util.List.
-
addAll
public boolean addAll(Collection<? extends E> c)
See java.util.Collection.
-
addAll
public boolean addAll(int index, Collection<? extends E> c)
See java.util.Collection.
-
removePos
public void removePos(int ipos, int count)
Remove one or more elements.- Parameters:
ipos
- position where elements should be removedcount
- if non-negative, remove that number of elements following (poses, posNumber); if negative the negative of the number of elements to remove before (poses, posNumber).- Throws:
IndexOutOfBoundsException
- if(count >= 0 ? (index < 0 || index + count > size()) : (index + count < 0 || index > size()))
, whereindex == nextIndex(ipos, xpos)
.
-
removePosRange
protected void removePosRange(int ipos0, int ipos1)
Remove a range where each end-point is a position in a container.- Parameters:
ipos0
- start of range, as a poistionipos1
- end of range- Throws:
IndexOutOfBoundsException
- ifnextIndex(ipos0) > nextIndex(ipos1)
ornextIndex(ipos0) < 0
ornextIndex(ipos1) > size()
.
-
remove
public E remove(int index)
-
remove
public boolean remove(Object o)
-
removeAll
public boolean removeAll(Collection<?> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
clear
public void clear()
-
isAfterPos
protected boolean isAfterPos(int ipos)
Tests whether the position has the "isAfter" property. I.e. if something is inserted at the position, will the iterator end up being after the new data?
-
createPos
public int createPos(int index, boolean isAfter)
Generate a position at a given index. The result is a position cookie that must be free'd with releasePos.- Parameters:
index
- offset from beginning of desired positionisAfter
- should the position have the isAfter property- Throws:
IndexOutOfBoundsException
- if index is out of bounds
-
createRelativePos
public int createRelativePos(int pos, int delta, boolean isAfter)
-
startPos
public int startPos()
-
endPos
public int endPos()
-
releasePos
protected void releasePos(int ipos)
Reclaim any resources used by the given position int.- Parameters:
ipos
- the Pos being free'd.
-
copyPos
public int copyPos(int ipos)
Make a copy of a position int. For simple positions returns the argument. However, if the positions are magic cookies that are actively managed by the sequence (as opposed to for example a simple index), then making a copy may need to increment a reference count, or maybe allocate a new position cookie. In any case, the new position is initialized to the same offset (and isAfter property) as the original.- Parameters:
ipos
- the position being copied.- Returns:
- the new position
-
getIndexDifference
protected int getIndexDifference(int ipos1, int ipos0)
Get offset of (ipos1) relative to (ipos0).
-
nextIndex
protected int nextIndex(int ipos)
Get the offset from the beginning corresponding to a position cookie.
-
fromEndIndex
protected int fromEndIndex(int ipos)
-
getContainingSequenceSize
protected int getContainingSequenceSize(int ipos)
Get the size of the (sub-) sequence containing a given position. Normally the same as size(), but may be different if this Sequence is a tree and the position points at an interior node.
-
hasNext
public boolean hasNext(int ipos)
-
getNextKind
public int getNextKind(int ipos)
-
getNextTypeName
public String getNextTypeName(int ipos)
-
getNextTypeObject
public E getNextTypeObject(int ipos)
-
hasPrevious
protected boolean hasPrevious(int ipos)
Called by SeqPosition.hasPrevious.
-
nextPos
public int nextPos(int ipos)
Return the next position following the argument. The new position has the isAfter property. The argument is implicitly released (as in releasePos). Returns 0 if we are already at end of file.
-
previousPos
public int previousPos(int ipos)
Return the previous position following the argument. The new position has the isBefore property. The argument is implicitly released (as in releasePos). Returns -1 if we are already at beginning of file.
-
gotoChildrenStart
public final boolean gotoChildrenStart(TreePosition pos)
Set position before first child (of the element following position).- Returns:
- true if there is a child sequence (which might be empty); false if current position is end of sequence or following element is atomic (cannot have children).
-
firstChildPos
public int firstChildPos(int ipos)
Get position before first child (of the element following position).- Parameters:
ipos
- parent position. It is not released by this method.- Returns:
- non-zero position cookie if there is a child sequence (which might be empty); zero if current position is end of sequence or following element is atomic (cannot have children).
-
firstChildPos
public int firstChildPos(int ipos, ItemPredicate predicate)
-
firstAttributePos
public int firstAttributePos(int ipos)
Like firstChildPos. Problem: Should this stop before we get to children? I think so, but that requires changes to TreeList.
-
parentPos
public int parentPos(int ipos)
Get position of parent.- Parameters:
ipos
- child position. It is not released by this method.- Returns:
- the p os of the parent, or endPos() is there is no known parent.
-
gotoParent
protected boolean gotoParent(TreePosition pos)
-
getAttributeLength
public int getAttributeLength()
-
getAttribute
public Object getAttribute(int index)
-
gotoAttributesStart
protected boolean gotoAttributesStart(TreePosition pos)
-
getPosNext
public Object getPosNext(int ipos)
Get the element following the specified position.- Parameters:
ipos
- the specified position.- Returns:
- the following element, or eofValue if there is none. Called by SeqPosition.getNext. FIXME Should change eof handling so return type can be E.
-
getPosPrevious
public Object getPosPrevious(int ipos)
Get the element before the specified position.- Parameters:
ipos
- the specified position.- Returns:
- the following element, or eofValue if there is none. FIXME Should change eof handling so return type can be E.
-
setPosNext
protected void setPosNext(int ipos, E value)
-
setPosPrevious
protected void setPosPrevious(int ipos, E value)
-
nextIndex
public final int nextIndex(SeqPosition pos)
-
equals
public boolean equals(int ipos1, int ipos2)
Compare two positions, and indicate if they are the same position.
-
compare
public int compare(int ipos1, int ipos2)
Compare two positions, and indicate their relative order.
-
compare
public final int compare(SeqPosition i1, SeqPosition i2)
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] arr)
-
stableCompare
public int stableCompare(AbstractSequence other)
This is used for the XML concept of "document order".
-
compare
public static int compare(AbstractSequence seq1, int pos1, AbstractSequence seq2, int pos2)
This is used for the XML concept of "document order". It is overridden in gnu.xml.NodeTree for a more robust implementation.
-
boundedHash
public int boundedHash(int seed, int limit)
-
subSequence
public Sequence subSequence(SeqPosition start, SeqPosition end)
-
consumeNext
public boolean consumeNext(int ipos, Consumer out)
Copy an element specified by a position pair to a Consumer.- Returns:
- if hasNext(ipos).
-
consumePosRange
public void consumePosRange(int iposStart, int iposEnd, Consumer out)
-
consume
public void consume(int fromIndex, int toIndex, Consumer out)
-
consume
public void consume(Consumer out)
-
toString
public void toString(String sep, StringBuffer sbuf)
-
-