Class 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 a Predicate, 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.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface org.protelis.lang.datatype.Tuple

        flatMap
    • 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 to
        length - 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).
        Specified by:
        append in interface Tuple
        Parameters:
        element - Element to be added
        Returns:
        A new tuple with equal to the current plus the new element added to the end
      • compareTo

        public int compareTo​(Tuple o)
        Specified by:
        compareTo in interface java.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.
        Specified by:
        contains in interface Tuple
        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"
      • 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 interface Tuple
        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 class java.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.
        Specified by:
        filter in interface Tuple
        Parameters:
        ctx - the current execution context
        fun - the 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 a Predicate, returns a new tuple containing only the elements that match the predicate.
        Specified by:
        filter in interface Tuple
        Parameters:
        fun - the Predicate
        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.
        Specified by:
        flatMap in interface Tuple
        Parameters:
        fun - the mapping function
        Returns:
        a new tuple whose elements are the elements generated by the application of the provided funcion to each element of the tuple, in order.
      • 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 interface Tuple
        Parameters:
        ctx - the current execution context
        defVal - the base value of the computation
        fun - the FunctionDefinition 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.
        Specified by:
        fold in interface Tuple
        Parameters:
        initial - the base value of the computation
        fun - the BinaryOperator that will be applied to each couple of values
        Returns:
        a single value of this tuple, or the default value if empty
      • 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.
        Specified by:
        get in interface Tuple
        Parameters:
        i - Zero-based index of the object to be retrieved
        Returns:
        The object at index i
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • head

        public java.lang.Object head()
        Specified by:
        head in interface Tuple
        Returns:
        the first element of the tuple if present, throws an exception otherwise
      • 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.
        Specified by:
        indexof in interface Tuple
        Parameters:
        element - The object to be searched for
        Returns:
        The first index where the search object is found, or -1 if not 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.
        Specified by:
        insert in interface Tuple
        Parameters:
        i - Zero-based index at which the element is to be added
        element - Element to be added
        Returns:
        A new tuple with equal to the current plus the new element added at the given index
      • 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 interface Tuple
        Parameters:
        t - tuple to take intersection with
        Returns:
        a new tuple containing elements appearing in both tuples, in arbitrary order
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface Tuple
        Returns:
        Returns true iff the size of the tuple is zero
      • iterator

        @Nonnull
        public java.util.Iterator<java.lang.Object> iterator()
        Specified by:
        iterator in interface java.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.
        Specified by:
        map in interface Tuple
        Parameters:
        ctx - the current execution context
        fun - the function to apply
        Returns:
        a new tuple containing the values
      • 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.
        Specified by:
        map in interface Tuple
        Parameters:
        fun - the function to apply
        Returns:
        a new tuple containing the values
      • max

        public java.lang.Object max​(java.lang.Object def)
        Specified by:
        max in interface Tuple
        Parameters:
        def - the return in case max is not defined
        Returns:
        the biggest element of this tuple, if defined, or default otherwise
      • 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 interface Tuple
        Parameters:
        tuple - tuple to be added
        Returns:
        a new tuple
      • min

        public java.lang.Object min​(java.lang.Object def)
        Specified by:
        min in interface Tuple
        Parameters:
        def - the return in case min is not defined
        Returns:
        the smallest element of this tuple, if defined, or default otherwise
      • 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).
        Specified by:
        prepend in interface Tuple
        Parameters:
        element - Element to be added
        Returns:
        A new tuple with equal to the current plus the new element added to the start
      • 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.
        Specified by:
        reduce in interface Tuple
        Parameters:
        ctx - the current execution context
        defVal - the value that will be returned if the tuple is empty
        fun - the BinaryOperator that will be applied to each couple of values
        Returns:
        a single value of this tuple, or the default value if empty
      • 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.
        Specified by:
        reduce in interface Tuple
        Parameters:
        defVal - the value that will be returned if the tuple is empty
        fun - the BinaryOperator that will be applied to each couple of values
        Returns:
        a single value of this tuple, or the default value if empty
      • set

        public Tuple set​(int i,
                         java.lang.Object element)
        Description copied from interface: Tuple
        Replace an element in a tuple.
        Specified by:
        set in interface Tuple
        Parameters:
        i - Zero-based index at which the element is to be replaced
        element - Element to replace the current element
        Returns:
        A new tuple with the element at the ith index replaced by "element"
      • size

        public int size()
        Specified by:
        size in interface Tuple
        Returns:
        Number of elements in the tuple
      • sort

        public Tuple sort()
        Specified by:
        sort in interface Tuple
        Returns:
        a sorted version of this tuple
      • subtract

        public Tuple subtract​(Tuple t)
        Description copied from interface: Tuple
        Set arithmetic: compute which elements in this tuple are not in another tuple.
        Specified by:
        subtract in interface Tuple
        Parameters:
        t - tuple to subtract
        Returns:
        a new tuple containing elements not in t, in arbitrary order
      • 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.
        Specified by:
        subTuple in interface Tuple
        Parameters:
        i - start index
        j - end index
        Returns:
        a new tuple containing elements i through j-1
      • 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 interface Tuple
        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 interface Tuple
        Parameters:
        i - Number of elements to take
        Returns:
        a new tuple containing elements 0 through i-1
      • tail

        public Tuple tail()
        Specified by:
        tail in interface Tuple
        Returns:
        a tuple with all the element of this tuple except the first
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface Tuple
        Returns:
        an array with a copy of the contents of the tuple
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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.
        Specified by:
        union in interface Tuple
        Parameters:
        t - tuple to take union with
        Returns:
        a new tuple containing elements appearing in either tuple, in arbitrary order
      • 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].
        Specified by:
        unwrap in interface Tuple
        Parameters:
        i - index of sub-element to substitute
        Returns:
        a new tuple with substituted elements
      • 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]]
        Specified by:
        zip in interface Tuple
        Parameters:
        other - the tuple to zip this one with
        Returns:
        A tuple of pairs (tuples of two elements) containing elements with the same index in this tuple and the provided one.