kodkod.util.ints
Class ArraySequence<V>

java.lang.Object
  extended by kodkod.util.ints.AbstractSparseSequence<V>
      extended by kodkod.util.ints.ArraySequence<V>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<IndexedEntry<V>>, SparseSequence<V>

public final class ArraySequence<V>
extends AbstractSparseSequence<V>
implements java.lang.Cloneable

An implementation of a sparse sequence based on an array. This implementation can be used only when the indices of the sequence are known in advance. The indices with which an ArraySequence is construct remain fixed throughout. The put operation fails whenever its index argument is not one of the sequence's pre-set indices, and iteration is proportional to the number of the pre-set indices. This sequence does not allow null values.

Author:
Emina Torlak
specfield:
indeces: set int
entries: indeces -> lone (V - null)

Constructor Summary
ArraySequence(IntSet indices)
          Constructs an array sequence that contains the given indeces.
ArraySequence(SparseSequence<? extends V> s)
          Constructs a new array sequence with the same index/value mappings as the given sequence.
 
Method Summary
 IndexedEntry<V> ceil(int index)
          If an entry for the given index exists, it is returned.
 void clear()
          Removes all entries from this sequences.
 ArraySequence<V> clone()
          Returns a copy of this sparse sequence.
 boolean containsIndex(int index)
          Returns true if this sparse sequence has an entry for the given index; otherwise returns false.
 IndexedEntry<V> first()
          Returns the entry with the smallest index.
 IndexedEntry<V> floor(int index)
          If an entry for the given index exists, it is returned.
 V get(int index)
          Returns the value to which this sequence maps the given index.
 boolean isEmpty()
          Returns true if this sequence is empty; otherwise returns false.
 java.util.Iterator<IndexedEntry<V>> iterator(int from, int to)
          Returns an iterator over the entries in this sequence, whose indeces are between from and to.
 IndexedEntry<V> last()
          Returns the entry with the largest index.
 V put(int index, V value)
          Puts the given value at the specified index.
 V remove(int index)
          Removes the entry with the given index, if it exists, and returns the value previously stored at the index.
 int size()
          Returns the number of entries in this sequence.
 
Methods inherited from class kodkod.util.ints.AbstractSparseSequence
contains, equals, hashCode, indices, iterator, putAll, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArraySequence

public ArraySequence(IntSet indices)
Constructs an array sequence that contains the given indeces.

Throws:
java.lang.NullPointerException - - indeces = null
effects:
this.indeces' = indeces && no this.entries'

ArraySequence

public ArraySequence(SparseSequence<? extends V> s)
Constructs a new array sequence with the same index/value mappings as the given sequence.

Throws:
java.lang.NullPointerException - - s = null || null in s
effects:
this.entries' = s.entries
Method Detail

size

public int size()
Returns the number of entries in this sequence.

Specified by:
size in interface SparseSequence<V>
Returns:
#this.entries
See Also:
SparseSequence.size()

isEmpty

public boolean isEmpty()
Returns true if this sequence is empty; otherwise returns false.

Specified by:
isEmpty in interface SparseSequence<V>
Overrides:
isEmpty in class AbstractSparseSequence<V>
Returns:
no this.entries
See Also:
SparseSequence.isEmpty()

clear

public void clear()
Removes all entries from this sequences. This method obtains an iterator over the sequences and calls remove() after each call to next(). Removes all entries from this sequences.

Specified by:
clear in interface SparseSequence<V>
Overrides:
clear in class AbstractSparseSequence<V>
See Also:
SparseSequence.clear()

put

public V put(int index,
             V value)
Puts the given value at the specified index. If the sequence already mapped the index to a value, the previous value is replaced with the new one and returned.

Specified by:
put in interface SparseSequence<V>
Overrides:
put in class AbstractSparseSequence<V>
Returns:
this.entries[index]
Throws:
java.lang.IndexOutOfBoundsException - - index !in this.indeces
java.lang.NullPointerException - - value = null
See Also:
SparseSequence.put(int, Object)
effects:
this.entries' = this.entries + index->value

get

public V get(int index)
Returns the value to which this sequence maps the given index. If the index is not mapped, null is returned.

Specified by:
get in interface SparseSequence<V>
Returns:
this.entries[index]
See Also:
SparseSequence.get(int)

remove

public V remove(int index)
Removes the entry with the given index, if it exists, and returns the value previously stored at the index. If the sequence had no previous mapping for the index, null is returned.

Specified by:
remove in interface SparseSequence<V>
Overrides:
remove in class AbstractSparseSequence<V>
Returns:
this.entries[index]
See Also:
SparseSequence.remove(int)
effects:
this.entries' = this.entries - index->E

containsIndex

public boolean containsIndex(int index)
Returns true if this sparse sequence has an entry for the given index; otherwise returns false.

Specified by:
containsIndex in interface SparseSequence<V>
Overrides:
containsIndex in class AbstractSparseSequence<V>
Returns:
index in this.indeces
See Also:
SparseSequence.containsIndex(int)

iterator

public java.util.Iterator<IndexedEntry<V>> iterator(int from,
                                                    int to)
Returns an iterator over the entries in this sequence, whose indeces are between from and to. If from < to, the entries are returned in the ascending order of indeces. Otherwise, they are returned in the descending order of indeces.

Specified by:
iterator in interface SparseSequence<V>
Returns:
an iterator over the entries in this sequence whose indeces are between from and to. Formally, if from < to, then the first and last entries returned by the iterator are this.ceil(from) and this.floor(to). Otherwise, they are this.floor(from) and this.ceil(to).
See Also:
SparseSequence.iterator(int, int)

first

public IndexedEntry<V> first()
Returns the entry with the smallest index. If the sequence is empty, returns null.

Specified by:
first in interface SparseSequence<V>
Overrides:
first in class AbstractSparseSequence<V>
Returns:
{e: IndexedEntry | e.index = min(this.entries.E) && e.value = this.entries[e.index] }
See Also:
SparseSequence.first()

last

public IndexedEntry<V> last()
Returns the entry with the largest index. If the sequence is empty, returns null.

Specified by:
last in interface SparseSequence<V>
Overrides:
last in class AbstractSparseSequence<V>
Returns:
{e: IndexedEntry | e.index = max(this.entries.E) && e.value = this.entries[e.index] }
See Also:
SparseSequence.last()

ceil

public IndexedEntry<V> ceil(int index)
If an entry for the given index exists, it is returned. Otherwise, successor(index) is returned.

Specified by:
ceil in interface SparseSequence<V>
Overrides:
ceil in class AbstractSparseSequence<V>
Returns:
this.containsIndex(index) => {e: IndexedEntry | e.index = index && e.value = this.entries[index] }, successor(index)
See Also:
SparseSequence.ceil(int)

floor

public IndexedEntry<V> floor(int index)
If an entry for the given index exists, it is returned. Otherwise, predecessor(index) is returned.

Specified by:
floor in interface SparseSequence<V>
Overrides:
floor in class AbstractSparseSequence<V>
Returns:
this.containsIndex(index) => {e: IndexedEntry | e.index = index && e.value = this.entries[index] }, predecessor(index)
See Also:
SparseSequence.floor(int)

clone

public ArraySequence<V> clone()
Returns a copy of this sparse sequence. The copy is independent of this sequence.

Specified by:
clone in interface SparseSequence<V>
Overrides:
clone in class AbstractSparseSequence<V>
Returns:
a copy of this sparse sequence.
See Also:
SparseSequence.clone()