Class ZenoChain<T>
- Type Parameters:
T
- the type of the items in the list
- All Implemented Interfaces:
Iterable<T>
This is an immutable data structure; updating operations create a new
ZenoChain
leaving the original unchanged.
In effect the ZenoChain is a tree with a constant depth of 3, implemented as a list of lists.
For a list built by appending to the end, the size of sublists goes as follows as the list grows: (1).. (32).. (32,1).. (32,32).. (64,1).. (64,32).. (64,32,1).. (64,32,32).. (64,64,1).. (64,64,32).. (128,32,1).. (128,64,1).. (128,64,32,1).. For a list of 20,000 items we get 10 sublists with sizes (8192, 4096, 4096, 2048, 1024, 256, 128, 64, 64, 32). The exact numbers don't matter, the important thing is that the number of sublists is log(N) with shorter sublists at the end of the sequence where append/prepend operations take place.
When two lists are concatenated, the two master lists are first concatenated, followed by a consolidation to combine short lists now appearing near the middle of the structure, to reduce the number of sublists.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppend an item to this list.Append a sequence of items.Concatenate two ZenoChains to form a new ZenoChain, leaving the original operands unchangedget
(int n) Get the item at position n, zero-basedInsert an item at position n, zero-basedboolean
isEmpty()
Ask if the list is emptyboolean
Ask if the list is a singletoniterator()
Iterate over the itemsPrepend an item.remove
(int n) Remove the item at position n, zero-basedReplace the item at position n, zero-basedDiagnostic display of aZenoChain
, exposing its internal structureint
size()
Get the size of the listsubList
(int start, int end) Get a sublist of this list.toString()
Get a string representation of the ZenoChain.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ZenoChain
public ZenoChain()Create an empty sequence
-
-
Method Details
-
add
Append an item to this list. This is an immutable operation; the original list is unchanged- Parameters:
item
- the item to be appended- Returns:
- the list that results from the append operation
-
prepend
Prepend an item. This is an immutable operation; the original list is unchanged.- Parameters:
item
- the item to be added at the start of the sequence- Returns:
- the list resulting from the prepend operation
-
addAll
Append a sequence of items. This is an immutable operation; the original list is unchanged.- Parameters:
items
- the sequence of items to be appended- Returns:
- the concatenated sequence
-
concat
Concatenate two ZenoChains to form a new ZenoChain, leaving the original operands unchanged- Parameters:
other
- the ZenoChain to be concatenated after this one- Returns:
- a new ZenoChain whose elements are the elements of this ZenoChain followed by the elements of the other ZenoChain.
-
replace
Replace the item at position n, zero-based- Parameters:
n
- the requested indexvalue
- the replacement value- Returns:
- the new ZenoChain
- Throws:
IndexOutOfBoundsException
- if n is negative or beyond the end of the list
-
remove
Remove the item at position n, zero-based- Parameters:
n
- the requested index- Returns:
- the new ZenoChain
- Throws:
IndexOutOfBoundsException
- if n is negative or beyond the end of the list
-
insert
Insert an item at position n, zero-based- Parameters:
n
- the requested indexvalue
- the replacement value- Returns:
- the new ZenoChain
- Throws:
IndexOutOfBoundsException
- if n is negative or beyond the end of the list
-
get
Get the item at position n, zero-based- Parameters:
n
- the requested index- Returns:
- the item at position n
- Throws:
IndexOutOfBoundsException
- if n is negative or beyond the end of the list
-
subList
Get a sublist of this list.Note that unlike
List.subList(int, int)
, the returned list is not "backed" by the original list; changes to the returned list will not affect the original list in any way. (This is inevitable, since both lists are immutable).- Parameters:
start
- the zero-based start position of the required sublist, inclusiveend
- the zero-based end position of the required sublist, exclusive- Returns:
- the sublist, as a new ZenoChain
- Throws:
IndexOutOfBoundsException
- under the same conditions as forList.subList(int, int)
: (start < 0 || end > size || start > end)
-
size
public int size()Get the size of the list- Returns:
- the size of the list
-
isEmpty
public boolean isEmpty()Ask if the list is empty- Returns:
- true if the size is zero
-
isSingleton
public boolean isSingleton()Ask if the list is a singleton- Returns:
- true if the size is one
-
iterator
Iterate over the items -
toString
Get a string representation of the ZenoChain. -
showMetrics
Diagnostic display of aZenoChain
, exposing its internal structure- Returns:
- a string that shows the sizes of the segments, for example (64,32,4) for a
ZenoChain
of length 100.
-