gnu.lists

Class TreeList

Implemented Interfaces:
Consumable, Consumer, PositionConsumer, XConsumer
Known Direct Subclasses:
NodeTree, Values

public class TreeList
extends AbstractSequence
implements XConsumer, PositionConsumer, Consumable

A compact representation of a tree, that is a nested list structure. The data structure can store anything that can be emitted to a Consumer. This data structure is optimized for efficient forwards traversal through the data structure, not random access. It does have an "insertion point"; insertions and deletions are efficient through the use of a buffer gap. It is a reasonable choice for a "DOM" for XML data.

Field Summary

protected static int
BEGIN_ATTRIBUTE_LONG
The beginning of an attribute.
static int
BEGIN_ATTRIBUTE_LONG_SIZE
protected static int
BEGIN_DOCUMENT
Beginning of a document (or top-level value).
protected static int
BEGIN_ELEMENT_LONG
Begin of an element, non-compact form.
protected static int
BEGIN_ELEMENT_SHORT
Beginning of an element, compact form.
protected static int
BEGIN_ELEMENT_SHORT_INDEX_MAX
static int
BEGIN_ENTITY
End of an entity (typically a file, possibly included).
static int
BEGIN_ENTITY_SIZE
protected static int
DOCUMENT_URI
The document-uri property of a node.
static int
END_ATTRIBUTE_SIZE
protected static int
END_DOCUMENT
End of a document.
protected static int
END_ELEMENT_LONG
End of n element, non-compact form.
protected static int
END_ELEMENT_SHORT
End of an element, compact form.
protected static int
END_ENTITY
static int
INT_FOLLOWS
A 32-bit integer, non-compact form.
static int
MAX_CHAR_SHORT
The largest Unicode character that can be encoded in one char.
protected static char
POSITION_PAIR_FOLLOWS
A position triple referenceing some other "nodes".
protected static int
PROCESSING_INSTRUCTION
A processing-instruction node follows.
int
attrStart
If non-zero, gap is in an attribute starting (1 less than) here.
char[]
data
int
docStart
If non-zero, gap is in an document starting (1 less than) here.
int
gapEnd
int
gapStart
Object[]
objects
int
oindex

Constructor Summary

TreeList()
TreeList(TreeList list)
TreeList(TreeList list, int startPosition, int endPosition)
Make a copy of a sub-range of a TreeList.

Method Summary

Consumer
append(CharSequence csq)
Consumer
append(CharSequence csq, int start, int end)
Consumer
append(char c)
void
beginEntity(Object base)
void
clear()
int
compare(int ipos1, int ipos2)
Compare two positions, and indicate their relative order.
void
consume(Consumer out)
void
consume(SeqPosition position)
Consume node at current position.
int
consumeIRange(int startPosition, int endPosition, Consumer out)
boolean
consumeNext(int ipos, Consumer out)
Copy an element specified by a position pair to a Consumer.
void
consumePosRange(int startPos, int endPos, Consumer out)
int
createPos(int index, boolean isAfter)
Generate a position at a given index.
int
createRelativePos(int istart, int offset, boolean isAfter)
Object
documentUriOfPos(int pos)
void
dump()
void
dump(java.io.PrintWriter out)
void
dump(java.io.PrintWriter out, int start, int limit)
void
endAttribute()
End of an attribute or end of an actual parameter.
void
endDocument()
void
endElement()
void
endEntity()
void
ensureSpace(int needed)
int
find(Object arg1)
int
firstAttributePos(int ipos)
Like firstChildPos.
int
firstChildPos(int ipos)
Get position before first child (of the element following position).
Object
get(int index)
See java.util.List.
int
getAttributeCount(int parent)
protected int
getIndexDifference(int ipos1, int ipos0)
Get offset of (ipos1) relative to (ipos0).
protected int
getIntN(int index)
Get a 32-bit int from the data array.
protected long
getLongN(int index)
Get a 64-bit long from the data array.
int
getNextKind(int ipos)
int
getNextKindI(int index)
String
getNextTypeName(int ipos)
Object
getNextTypeObject(int ipos)
Object
getPosNext(int ipos)
Get the element following the specified position.
int
getPosNextInt(int ipos)
Return following value (like getPosNext), as an integer.
Object
getPosPrevious(int ipos)
Get the element before the specified position.
boolean
gotoAttributesStart(TreePosition pos)
int
gotoAttributesStart(int index)
int
gotoChildrenStart(int index)
boolean
hasNext(int ipos)
int
hashCode()
boolean
ignoring()
True if consumer is ignoring rest of element.
boolean
isEmpty()
int
nextDataIndex(int pos)
int
nextMatching(int startPos, ItemPredicate predicate, int endPos, boolean descend)
Get next matching child or descendent (ignoring attributes).
int
nextNodeIndex(int pos, int limit)
Skip all primitive content nodes.
int
nextPos(int position)
Return the next position following the argument.
int
parentOrEntityI(int index)
int
parentOrEntityPos(int ipos)
int
parentPos(int ipos)
Get position of parent.
int
posToDataIndex(int ipos)
void
resizeObjects()
void
setAttributeName(int attrIndex, int nameIndex)
void
setElementName(int elementIndex, int nameIndex)
void
setIntN(int index, int i)
int
size()
See java.util.List.
void
startAttribute(Object attrType)
Write a attribute for the current element.
void
startAttribute(int index)
void
startDocument()
void
startElement(Object type)
void
startElement(int index)
void
statistics()
void
statistics(java.io.PrintWriter out)
int
stringValue(boolean inElement, int index, StringBuffer sbuf)
int
stringValue(int index, StringBuffer sbuf)
void
stringValue(int startIndex, int endIndex, StringBuffer sbuf)
void
toString(String sep, StringBuffer sbuf)
void
write(CharSequence str, int start, int length)
void
write(String str)
void
write(char[] buf, int off, int len)
void
write(int c)
void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeCDATA(char[] chars, int offset, int length)
void
writeComment(String comment, int offset, int length)
void
writeComment(char[] chars, int offset, int length)
void
writeDocumentUri(Object uri)
Write/set the document-uri property of the current document.
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeJoiner()
void
writeLong(long v)
void
writeObject(Object v)
void
writePosition(AbstractSequence seq, int ipos)
Consume a single position pair.
void
writeProcessingInstruction(String target, String content, int offset, int length)
void
writeProcessingInstruction(String target, char[] content, int offset, int length)

Methods inherited from class gnu.lists.AbstractSequence

add, add, addAll, addAll, addPos, clear, compare, compare, compare, consume, consumeNext, consumePosRange, contains, containsAll, copyPos, createPos, createRelativePos, elements, endPos, equals, equals, fill, fill, fillPosRange, firstAttributePos, firstChildPos, firstChildPos, fromEndIndex, get, get, getAttribute, getAttributeLength, getContainingSequenceSize, getEffectiveIndex, getIndexDifference, getIterator, getIterator, getIteratorAtPos, getLowBound, getNextKind, getNextTypeName, getNextTypeObject, getPosNext, getPosPrevious, getSize, gotoAttributesStart, gotoChildrenStart, gotoParent, hasNext, hasPrevious, hashCode, indexOf, isAfterPos, isEmpty, iterator, lastIndexOf, listIterator, listIterator, nextIndex, nextIndex, nextMatching, nextPos, parentPos, previousPos, rank, releasePos, remove, remove, removeAll, removePos, removePosRange, retainAll, set, set, setPosNext, setPosPrevious, size, stableCompare, startPos, subList, subSequence, subSequencePos, toArray, toArray, toString, toString, unsupported, unsupportedException

Field Details

BEGIN_ATTRIBUTE_LONG

protected static final int BEGIN_ATTRIBUTE_LONG
The beginning of an attribute. [BEGIN_ATTRIBUTE_LONG] [index], 2 shorts, where objects[index] is the attribute type name and objects[index+1] is the attribute type object. [end_offset], 2 shorts, giving the location of the following END_ATTRIBUTE. If the attribute straddles the gap, then end_offset is a negative offset relative to data.length. (Therefore allocating more space for the gap does not require adjusting end_offset.) Otherwise, the end_offset is relative to the BEGIN_ATTRIBUTE_LONG word.
Field Value:
61705

BEGIN_ATTRIBUTE_LONG_SIZE

public static final int BEGIN_ATTRIBUTE_LONG_SIZE
Field Value:
5

BEGIN_DOCUMENT

protected static final int BEGIN_DOCUMENT
Beginning of a document (or top-level value). Used to distinguish a document from its element node. [end_offset], 2 shorts, giving the location of the following END_DOCUMENT. If the attribute straddles the gap, then end_offset is a negative offset relative to data.length. (Therefore allocating more space for the gap does not require adjusting end_offset.) Otherwise, the end_offset is relative to the BEGIN_DOCUMENT word. [parent_offset], in 2 shorts. The parent node, or -1 if no parent. Otherwise, a negative value is a relative offset, while a non-negative value is absolute. (Use the latter when gap is between this node and its parent. The parent would normally be a BEGIN_ENTITY.
Field Value:
61712

BEGIN_ELEMENT_LONG

protected static final int BEGIN_ELEMENT_LONG
Begin of an element, non-compact form. [BEGIN_ELEMENT_LONG] [end_offset], in 2 shorts. The position of the matching END_ELEMENT_LONG. If the element straddles the gap, then end_offset is a negative offset relative to data.length. (Therefore allocating more space for the gap does not require adjusting any end_offset.) If the element and and its children are all on the same side of the gap, then end_offset is a positive offset relative to the BEGIN_ELEMENT_LONG word. (Hence shifting an entire element when the gap is moved does not require changing its end_offset.) Note that the space taken by a BEGIN_ELEMENT_LONG is the same that needed for a BEGIN_ELEMENT_SHORT (but a END_ELEMENT_LONG takes much more space than a END_ELEMENT_SHORT). This is to make it easier to convert a BEGIN_ELEMENT_LONG to a BEGIN_ELEMENT_SHORT or vice versa, as needed.
Field Value:
61704

BEGIN_ELEMENT_SHORT

protected static final int BEGIN_ELEMENT_SHORT
Beginning of an element, compact form. [BEGIN_ELEMENT_SHORT + index], where objects[index] is the element's type name and objects[index+1] is the type object. [end_offset], the unsigned offset (from the initial word) to the corresponding END_ELEMENT_SHORT. [parent_offset], the (unsigned absolute value of the) offset to the outer BEGIN_ELEMENT_SHORT/BEGIN_ELEMENT_LONG/BEGIN_DOCUMENT. . (If these is no parent, then parent_offset==0.) This should is used when index <32BEGIN_ELEMENT_SHORT_INDEX_MAX, both end_offset and parent_offset fit in 16 bits, and the element does not straddle the gap.
Field Value:
40960

BEGIN_ELEMENT_SHORT_INDEX_MAX

protected static final int BEGIN_ELEMENT_SHORT_INDEX_MAX
Field Value:
4095

BEGIN_ENTITY

public static final int BEGIN_ENTITY
End of an entity (typically a file, possibly included). [base_uri], 2 short, given an index of a base-uri object [parent_offset], in 2 shorts, encoded as for BEGIN_DOCUMENT.
Field Value:
61714

BEGIN_ENTITY_SIZE

public static final int BEGIN_ENTITY_SIZE
Field Value:
5

DOCUMENT_URI

protected static final int DOCUMENT_URI
The document-uri property of a node. This is not an actual value, but it is a property of the previous document node, or the surrounding node just after a BEGIN_XXX entry. [DOCUMENT_URI] [index]. 2 shorts, where objects[index] is the document-uri value.
Field Value:
61720

END_ATTRIBUTE_SIZE

public static final int END_ATTRIBUTE_SIZE
Field Value:
1

END_DOCUMENT

protected static final int END_DOCUMENT
End of a document.
Field Value:
61713

END_ELEMENT_LONG

protected static final int END_ELEMENT_LONG
End of n element, non-compact form. [END_ELEMENT_LONG] [index], 2 shorts where objects[index] is the element's type name and objects[index+1] is the type object. [begin_offset], in 2 shorts. The position of the matching BEGIN_ELEMENT_LONG. If the element straddles the gap, then begin_offset is the actual index (i.e. relative to the start of data) of the matching BEGIN_ELEMENT_LONG. (Therefore allocating more space for the gap does not require adjusting begin_offset.) If the element does not straddle the gap, then begin_offset is a negative offset relative to the END_ELEMENT_LONG word. (Hence shifting an entire element when the gap is moved does not require changing its begin_offset.) relative to data.length. [parent_offset], in 2 shorts. The position of the outer BEGIN_ELEMENT_LONG, BEGIN_ELEMENT_SHORT or BEGIN_DOCUMENT. If the difference straddles the gap (i.e. either this element straddles the gap or the parent element does and the gap precedes this element), then parent_offset is the actual index of the parent element. Otherwise, then parent_offset is a negative offset relative to the END_ELEMENT_LONG word.
Field Value:
61708

END_ELEMENT_SHORT

protected static final int END_ELEMENT_SHORT
End of an element, compact form. [END_ELEMENT_SHORT] [begin_offset], the unsigned absolute value of the offset to the matching BEGIN. (This is the same as the matching end_offset.)
Field Value:
61707

END_ENTITY

protected static final int END_ENTITY
Field Value:
61715

INT_FOLLOWS

public static final int INT_FOLLOWS
A 32-bit integer, non-compact form. [INT_FOLLOWS] [word1], [word2]: The big-endian bits of the integer.
Field Value:
61698

MAX_CHAR_SHORT

public static final int MAX_CHAR_SHORT
The largest Unicode character that can be encoded in one char.
Field Value:
40959

POSITION_PAIR_FOLLOWS

protected static final char POSITION_PAIR_FOLLOWS
A position triple referenceing some other "nodes". Followed by index of sequence (2 chars), and ipos (2 chars).
Field Value:
'\uf10f'

PROCESSING_INSTRUCTION

protected static final int PROCESSING_INSTRUCTION
A processing-instruction node follows. [PROCESSING_INSTRUCTION] [target] 2 shorts, where objects[target] is the target as a String. [length] 2 shorts. [comment text], (length) number of characters.
Field Value:
61716

attrStart

public int attrStart
If non-zero, gap is in an attribute starting (1 less than) here.

data

public char[] data

docStart

public int docStart
If non-zero, gap is in an document starting (1 less than) here.

gapEnd

public int gapEnd

gapStart

public int gapStart

objects

public Object[] objects

oindex

public int oindex

Constructor Details

TreeList

public TreeList()

TreeList

public TreeList(TreeList list)

TreeList

public TreeList(TreeList list,
                int startPosition,
                int endPosition)
Make a copy of a sub-range of a TreeList.
Parameters:
list - the TreeList to copy
startPosition - start of range, as a raw index in data
endPosition - end of range, as a raw index in data

Method Details

append

public Consumer append(CharSequence csq)

append

public Consumer append(CharSequence csq,
                       int start,
                       int end)

append

public Consumer append(char c)

beginEntity

public void beginEntity(Object base)
Specified by:
beginEntity in interface XConsumer

clear

public void clear()
Overrides:
clear in interface AbstractSequence

compare

public int compare(int ipos1,
                   int ipos2)
Compare two positions, and indicate their relative order.
Overrides:
compare in interface AbstractSequence

consume

public void consume(Consumer out)
Specified by:
consume in interface Consumable
Overrides:
consume in interface AbstractSequence

consume

public void consume(SeqPosition position)
Consume node at current position. The caller may invalidate or change the position after consume returns, so if the consumer wants to save it, it needs to copy it.
Specified by:
consume in interface PositionConsumer

consumeIRange

public int consumeIRange(int startPosition,
                         int endPosition,
                         Consumer out)

consumeNext

public boolean consumeNext(int ipos,
                           Consumer out)
Copy an element specified by a position pair to a Consumer.
Overrides:
consumeNext in interface AbstractSequence
Returns:
if hasNext(ipos).

consumePosRange

public void consumePosRange(int startPos,
                            int endPos,
                            Consumer out)
Overrides:
consumePosRange in interface AbstractSequence

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.
Overrides:
createPos in interface AbstractSequence
Parameters:
index - offset from beginning of desired position
isAfter - should the position have the isAfter property

createRelativePos

public int createRelativePos(int istart,
                             int offset,
                             boolean isAfter)
Overrides:
createRelativePos in interface AbstractSequence

documentUriOfPos

public Object documentUriOfPos(int pos)

dump

public void dump()

dump

public void dump(java.io.PrintWriter out)

dump

public void dump(java.io.PrintWriter out,
                 int start,
                 int limit)

endAttribute

public void endAttribute()
End of an attribute or end of an actual parameter. The former use matches a startAttribute; the latter may not, and can be used to separate parameters in a parameter list. This double duty suggsts the method should at least be re-named.
Specified by:
endAttribute in interface Consumer

endDocument

public void endDocument()
Specified by:
endDocument in interface Consumer

endElement

public void endElement()
Specified by:
endElement in interface Consumer

endEntity

public void endEntity()
Specified by:
endEntity in interface XConsumer

ensureSpace

public void ensureSpace(int needed)

find

public int find(Object arg1)

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.
Overrides:
firstAttributePos in interface AbstractSequence

firstChildPos

public int firstChildPos(int ipos)
Get position before first child (of the element following position).
Overrides:
firstChildPos in interface AbstractSequence
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).

get

public Object get(int index)
See java.util.List.
Overrides:
get in interface AbstractSequence

getAttributeCount

public int getAttributeCount(int parent)

getIndexDifference

protected int getIndexDifference(int ipos1,
                                 int ipos0)
Get offset of (ipos1) relative to (ipos0).
Overrides:
getIndexDifference in interface AbstractSequence

getIntN

protected final int getIntN(int index)
Get a 32-bit int from the data array.

getLongN

protected final long getLongN(int index)
Get a 64-bit long from the data array.

getNextKind

public int getNextKind(int ipos)
Overrides:
getNextKind in interface AbstractSequence

getNextKindI

public int getNextKindI(int index)

getNextTypeName

public String getNextTypeName(int ipos)
Overrides:
getNextTypeName in interface AbstractSequence

getNextTypeObject

public Object getNextTypeObject(int ipos)
Overrides:
getNextTypeObject in interface AbstractSequence

getPosNext

public Object getPosNext(int ipos)
Get the element following the specified position.
Overrides:
getPosNext in interface AbstractSequence
Parameters:
ipos - the specified position.
Returns:
the following element, or eofValue if there is none. Called by SeqPosition.getNext.

getPosNextInt

public int getPosNextInt(int ipos)
Return following value (like getPosNext), as an integer.

getPosPrevious

public Object getPosPrevious(int ipos)
Get the element before the specified position.
Overrides:
getPosPrevious in interface AbstractSequence
Parameters:
ipos - the specified position.
Returns:
the following element, or eofValue if there is none.

gotoAttributesStart

public boolean gotoAttributesStart(TreePosition pos)
Overrides:
gotoAttributesStart in interface AbstractSequence

gotoAttributesStart

public int gotoAttributesStart(int index)

gotoChildrenStart

public final int gotoChildrenStart(int index)

hasNext

public boolean hasNext(int ipos)
Overrides:
hasNext in interface AbstractSequence

hashCode

public int hashCode()
Overrides:
hashCode in interface AbstractSequence

ignoring

public boolean ignoring()
True if consumer is ignoring rest of element. The producer can use this information to skip ahead.
Specified by:
ignoring in interface Consumer

isEmpty

public boolean isEmpty()
Overrides:
isEmpty in interface AbstractSequence

nextDataIndex

public final int nextDataIndex(int pos)

nextMatching

public int nextMatching(int startPos,
                        ItemPredicate predicate,
                        int endPos,
                        boolean descend)
Get next matching child or descendent (ignoring attributes).
Overrides:
nextMatching in interface AbstractSequence
Parameters:
startPos - starting position
endPos - stop before endPos
descend - if true do depth-first traversal.
Returns:
poistion of next match or 0 if none found

nextNodeIndex

public final int nextNodeIndex(int pos,
                               int limit)
Skip all primitive content nodes.

nextPos

public int nextPos(int position)
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.
Overrides:
nextPos in interface AbstractSequence

parentOrEntityI

public int parentOrEntityI(int index)

parentOrEntityPos

public int parentOrEntityPos(int ipos)

parentPos

public int parentPos(int ipos)
Get position of parent.
Overrides:
parentPos in interface AbstractSequence
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.

posToDataIndex

public final int posToDataIndex(int ipos)

resizeObjects

public final void resizeObjects()

setAttributeName

public void setAttributeName(int attrIndex,
                             int nameIndex)

setElementName

public void setElementName(int elementIndex,
                           int nameIndex)

setIntN

public final void setIntN(int index,
                          int i)

size

public int size()
See java.util.List.
Overrides:
size in interface AbstractSequence

startAttribute

public void startAttribute(Object attrType)
Write a attribute for the current element. This is only allowed immediately after a startElement.
Specified by:
startAttribute in interface Consumer

startAttribute

public void startAttribute(int index)

startDocument

public void startDocument()
Specified by:
startDocument in interface Consumer

startElement

public void startElement(Object type)
Specified by:
startElement in interface Consumer

startElement

public void startElement(int index)

statistics

public void statistics()

statistics

public void statistics(java.io.PrintWriter out)

stringValue

public int stringValue(boolean inElement,
                       int index,
                       StringBuffer sbuf)

stringValue

public int stringValue(int index,
                       StringBuffer sbuf)

stringValue

public void stringValue(int startIndex,
                        int endIndex,
                        StringBuffer sbuf)

toString

public void toString(String sep,
                     StringBuffer sbuf)
Overrides:
toString in interface AbstractSequence

write

public void write(CharSequence str,
                  int start,
                  int length)
Specified by:
write in interface Consumer

write

public void write(String str)
Specified by:
write in interface Consumer

write

public void write(char[] buf,
                  int off,
                  int len)
Specified by:
write in interface Consumer

write

public void write(int c)
Specified by:
write in interface Consumer

writeBoolean

public void writeBoolean(boolean v)
Specified by:
writeBoolean in interface Consumer

writeByte

public void writeByte(int v)

writeCDATA

public void writeCDATA(char[] chars,
                       int offset,
                       int length)
Specified by:
writeCDATA in interface XConsumer

writeComment

public void writeComment(String comment,
                         int offset,
                         int length)

writeComment

public void writeComment(char[] chars,
                         int offset,
                         int length)
Specified by:
writeComment in interface XConsumer

writeDocumentUri

public void writeDocumentUri(Object uri)
Write/set the document-uri property of the current document. Only allowed immediately following startDocument.

writeDouble

public void writeDouble(double v)
Specified by:
writeDouble in interface Consumer

writeFloat

public void writeFloat(float v)
Specified by:
writeFloat in interface Consumer

writeInt

public void writeInt(int v)
Specified by:
writeInt in interface Consumer

writeJoiner

public void writeJoiner()

writeLong

public void writeLong(long v)
Specified by:
writeLong in interface Consumer

writeObject

public void writeObject(Object v)
Specified by:
writeObject in interface Consumer

writePosition

public void writePosition(AbstractSequence seq,
                          int ipos)
Consume a single position pair. This PositionConsumer may assume the sequence does no reference management; i.e. that copyPos is trivial and releasePos is a no-op. If that is not the case, use consume(TreePosition) instead.
Specified by:
writePosition in interface PositionConsumer

writeProcessingInstruction

public void writeProcessingInstruction(String target,
                                       String content,
                                       int offset,
                                       int length)

writeProcessingInstruction

public void writeProcessingInstruction(String target,
                                       char[] content,
                                       int offset,
                                       int length)
Specified by:
writeProcessingInstruction in interface XConsumer