Package org.protelis.lang.datatype.impl
Class ArrayTupleImpl
- java.lang.Object
-
- org.protelis.lang.datatype.impl.ArrayTupleImpl
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<Tuple>
,java.lang.Iterable<java.lang.Object>
,Tuple
public final class ArrayTupleImpl extends java.lang.Object implements Tuple
Implementation of a Tuple using an array data structure.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ArrayTupleImpl(java.lang.Object... base)
ArrayTupleImpl(java.lang.Object value, int length)
Create an ArrayTupleImpl with all elements initialized to a given value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tuple
append(java.lang.Object element)
Add an element to the end of a tuple.int
compareTo(Tuple o)
boolean
contains(java.lang.Object element)
Tests whether the tuple has an element equal to its argument.boolean
containsAll(java.lang.Iterable<?> element)
Tests whether the tuple has an element equal to its argument.boolean
equals(java.lang.Object o)
Tuple
filter(java.util.function.Predicate<java.lang.Object> fun)
Given a tuple and aPredicate
, returns a new tuple containing only the elements that match the predicate.Tuple
filter(ExecutionContext ctx, FunctionDefinition fun)
Given a tuple and a predicate, returns a new tuple containing only the elements that match the predicate.Tuple
flatMap(java.util.function.Function<java.lang.Object,Tuple> fun)
Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.java.lang.Object
fold(java.lang.Object initial, java.util.function.BinaryOperator<java.lang.Object> fun)
Performs a reduction operation, that maps a tuple to a single value.java.lang.Object
fold(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)
Performs a reduction operation, that maps a tuple to a single value.java.lang.Object
get(double i)
Compatibility method to speed up calls made using doubles.java.lang.Object
get(int i)
Retrieve the object at one of a tuple's indices.java.lang.Object
get(java.lang.Double i)
Compatibility method to speed up calls made using doubles.int
hashCode()
java.lang.Object
head()
int
indexof(java.lang.Object element)
Searches for an element in a tuple, returning its index if found.Tuple
insert(int i, java.lang.Object element)
Add an element to an arbitrary location within a tuple, moving all elements after it down.Tuple
intersection(Tuple t)
Set arithmetic: compute intersection of set of tuple elements with set of elements of another tuple.boolean
isEmpty()
java.util.Iterator<java.lang.Object>
iterator()
Tuple
map(java.util.function.Function<java.lang.Object,java.lang.Object> fun)
Maps a tuple to another same-length tuple by applying a function to each element.Tuple
map(ExecutionContext ctx, FunctionDefinition fun)
Maps a tuple to another same-length tuple by applying a function to each element.java.lang.Object
max(java.lang.Object def)
Tuple
mergeAfter(Tuple tuple)
Append elements of another tuple to the end of this tuple.java.lang.Object
min(java.lang.Object def)
Tuple
prepend(java.lang.Object element)
Add an element to the start of a tuple.java.lang.Object
reduce(java.lang.Object defVal, java.util.function.BinaryOperator<java.lang.Object> fun)
Performs a reduction operation, that maps a tuple to a single value.java.lang.Object
reduce(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)
Performs a reduction operation, that maps a tuple to a single value.Tuple
set(int i, java.lang.Object element)
Replace an element in a tuple.int
size()
Tuple
sort()
Tuple
subtract(Tuple t)
Set arithmetic: compute which elements in this tuple are not in another tuple.ArrayTupleImpl
subTuple(int i, int j)
Produces a new Tuple containing the elements between the start and end indices.ArrayTupleImpl
subTupleEnd(int i)
Produces a new Tuple containing all elements from i onward.ArrayTupleImpl
subTupleStart(int i)
Produces a new Tuple containing the first i elements.Tuple
tail()
java.lang.Object[]
toArray()
java.lang.String
toString()
ArrayTupleImpl
union(Tuple t)
Set arithmetic: compute union of set of tuple elements with set of elements of another tuple.Tuple
unwrap(int i)
For all elements that are tuples, substitute the ith sub-element.Tuple
zip(Tuple other)
Zips two tuples together, creating a Tuple of 2-ples.
-
-
-
Constructor Detail
-
ArrayTupleImpl
public ArrayTupleImpl(java.lang.Object... base)
- Parameters:
base
- the elements
-
ArrayTupleImpl
public ArrayTupleImpl(java.lang.Object value, int length)
Create an ArrayTupleImpl with all elements initialized to a given value.- Parameters:
value
- The value to initialize tolength
- The length of the tuple
-
-
Method Detail
-
append
public Tuple append(java.lang.Object element)
Description copied from interface:Tuple
Add an element to the end of a tuple. Equivalent to insert(size, element).
-
compareTo
public int compareTo(Tuple o)
- Specified by:
compareTo
in interfacejava.lang.Comparable<Tuple>
-
contains
public boolean contains(java.lang.Object element)
Description copied from interface:Tuple
Tests whether the tuple has an element equal to its argument.
-
containsAll
public boolean containsAll(java.lang.Iterable<?> element)
Description copied from interface:Tuple
Tests whether the tuple has an element equal to its argument.- Specified by:
containsAll
in interfaceTuple
- Parameters:
element
- Element to test whether it is equal to any element in the tuple- Returns:
- true if the tuple contains an element equal to "element"
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
filter
public Tuple filter(ExecutionContext ctx, FunctionDefinition fun)
Description copied from interface:Tuple
Given a tuple and a predicate, returns a new tuple containing only the elements that match the predicate.
-
filter
public Tuple filter(java.util.function.Predicate<java.lang.Object> fun)
Description copied from interface:Tuple
Given a tuple and aPredicate
, returns a new tuple containing only the elements that match the predicate.
-
flatMap
public Tuple flatMap(java.util.function.Function<java.lang.Object,Tuple> fun)
Description copied from interface:Tuple
Provided a function generating tuples from the tuple elements, maps this tuple to a new tuple, and then flattens the result into a single tuple.
-
fold
public java.lang.Object fold(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)
Description copied from interface:Tuple
Performs a reduction operation, that maps a tuple to a single value.- Specified by:
fold
in interfaceTuple
- Parameters:
ctx
- the current execution contextdefVal
- the base value of the computationfun
- theFunctionDefinition
that will be applied to each couple of values- Returns:
- a single value of this tuple, or the default value if empty
-
fold
public java.lang.Object fold(java.lang.Object initial, java.util.function.BinaryOperator<java.lang.Object> fun)
Description copied from interface:Tuple
Performs a reduction operation, that maps a tuple to a single value.
-
get
public java.lang.Object get(double i)
Compatibility method to speed up calls made using doubles.- Parameters:
i
- the element position (will be floored to int)- Returns:
- the i-th element
-
get
public java.lang.Object get(java.lang.Double i)
Compatibility method to speed up calls made using doubles.- Parameters:
i
- the element position (will be floored to int)- Returns:
- the i-th element
-
get
public java.lang.Object get(int i)
Description copied from interface:Tuple
Retrieve the object at one of a tuple's indices.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
head
public java.lang.Object head()
-
indexof
public int indexof(java.lang.Object element)
Description copied from interface:Tuple
Searches for an element in a tuple, returning its index if found.
-
insert
public Tuple insert(int i, java.lang.Object element)
Description copied from interface:Tuple
Add an element to an arbitrary location within a tuple, moving all elements after it down.
-
intersection
public Tuple intersection(Tuple t)
Description copied from interface:Tuple
Set arithmetic: compute intersection of set of tuple elements with set of elements of another tuple.- Specified by:
intersection
in interfaceTuple
- Parameters:
t
- tuple to take intersection with- Returns:
- a new tuple containing elements appearing in both tuples, in arbitrary order
-
isEmpty
public boolean isEmpty()
-
iterator
@Nonnull public java.util.Iterator<java.lang.Object> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Object>
-
map
public Tuple map(ExecutionContext ctx, FunctionDefinition fun)
Description copied from interface:Tuple
Maps a tuple to another same-length tuple by applying a function to each element.
-
map
public Tuple map(java.util.function.Function<java.lang.Object,java.lang.Object> fun)
Description copied from interface:Tuple
Maps a tuple to another same-length tuple by applying a function to each element.
-
max
public java.lang.Object max(java.lang.Object def)
-
mergeAfter
public Tuple mergeAfter(Tuple tuple)
Description copied from interface:Tuple
Append elements of another tuple to the end of this tuple.- Specified by:
mergeAfter
in interfaceTuple
- Parameters:
tuple
- tuple to be added- Returns:
- a new tuple
-
min
public java.lang.Object min(java.lang.Object def)
-
prepend
public Tuple prepend(java.lang.Object element)
Description copied from interface:Tuple
Add an element to the start of a tuple. Equivalent to insert(0, element).
-
reduce
public java.lang.Object reduce(ExecutionContext ctx, java.lang.Object defVal, FunctionDefinition fun)
Description copied from interface:Tuple
Performs a reduction operation, that maps a tuple to a single value.
-
reduce
public java.lang.Object reduce(java.lang.Object defVal, java.util.function.BinaryOperator<java.lang.Object> fun)
Description copied from interface:Tuple
Performs a reduction operation, that maps a tuple to a single value.
-
set
public Tuple set(int i, java.lang.Object element)
Description copied from interface:Tuple
Replace an element in a tuple.
-
size
public int size()
-
sort
public Tuple sort()
-
subtract
public Tuple subtract(Tuple t)
Description copied from interface:Tuple
Set arithmetic: compute which elements in this tuple are not in another tuple.
-
subTuple
public ArrayTupleImpl subTuple(int i, int j)
Description copied from interface:Tuple
Produces a new Tuple containing the elements between the start and end indices. The start index is inclusive, the end index exclusive.
-
subTupleEnd
public ArrayTupleImpl subTupleEnd(int i)
Description copied from interface:Tuple
Produces a new Tuple containing all elements from i onward. Equivalent to subTuple(i,Tuple.size()).- Specified by:
subTupleEnd
in interfaceTuple
- Parameters:
i
- First element to take for new tuple- Returns:
- a new tuple containing elements i through size-1
-
subTupleStart
public ArrayTupleImpl subTupleStart(int i)
Description copied from interface:Tuple
Produces a new Tuple containing the first i elements. Equivalent to subTuple(0,i).- Specified by:
subTupleStart
in interfaceTuple
- Parameters:
i
- Number of elements to take- Returns:
- a new tuple containing elements 0 through i-1
-
tail
public Tuple tail()
-
toArray
public java.lang.Object[] toArray()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
union
public ArrayTupleImpl union(Tuple t)
Description copied from interface:Tuple
Set arithmetic: compute union of set of tuple elements with set of elements of another tuple.
-
unwrap
public Tuple unwrap(int i)
Description copied from interface:Tuple
For all elements that are tuples, substitute the ith sub-element. For example: [[1,2],3,[4,5],6].unwrap(1) produces [2,3,5,6].
-
zip
public Tuple zip(Tuple other)
Description copied from interface:Tuple
Zips two tuples together, creating a Tuple of 2-ples. In case the tuple size differs, the zipping tuple will create a tuple whose length matches the smaller one, discarding elements of the longer one: [1, 2, 3].zip([4, 5]) returns [[1, 4], [2, 5]]
-
-