More collections. (#80)
This commit is contained in:
+8
@@ -27,3 +27,11 @@ annotation class ExportTypeInfo(val name: String)
|
|||||||
// CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, TYPE, EXPRESSION, FILE, TYPEALIAS)
|
// CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, TYPE, EXPRESSION, FILE, TYPEALIAS)
|
||||||
//@Retention(SOURCE)
|
//@Retention(SOURCE)
|
||||||
public annotation class Suppress(vararg val names: String)
|
public annotation class Suppress(vararg val names: String)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suppresses errors about variance conflict
|
||||||
|
*/
|
||||||
|
@Target(AnnotationTarget.TYPE)
|
||||||
|
//@Retention(SOURCE)
|
||||||
|
//@MustBeDocumented
|
||||||
|
public annotation class UnsafeVariance
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
|
||||||
|
public abstract class AbstractCollection<out E> protected constructor() : Collection<E> {
|
||||||
|
abstract override val size: Int
|
||||||
|
abstract override fun iterator(): Iterator<E>
|
||||||
|
|
||||||
|
/* TODO: uncomment, once can support lambdas.
|
||||||
|
override fun contains(element: @UnsafeVariance E): Boolean = any { it == element }
|
||||||
|
|
||||||
|
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean =
|
||||||
|
elements.all { contains(it) }
|
||||||
|
|
||||||
|
override fun isEmpty(): Boolean = size == 0
|
||||||
|
|
||||||
|
override fun toString(): String = joinToString(", ", "[", "]") {
|
||||||
|
if (it === this) "(this Collection)" else it.toString()
|
||||||
|
} */
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic collection of elements. Methods in this interface support only read-only access to the collection;
|
||||||
|
* read/write access is supported through the [MutableCollection] interface.
|
||||||
|
* @param E the type of elements contained in the collection.
|
||||||
|
*/
|
||||||
|
public interface Collection<out E> : Iterable<E> {
|
||||||
|
// Query Operations
|
||||||
|
/**
|
||||||
|
* Returns the size of the collection.
|
||||||
|
*/
|
||||||
|
public val size: Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the collection is empty (contains no elements), `false` otherwise.
|
||||||
|
*/
|
||||||
|
public fun isEmpty(): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the specified element is contained in this collection.
|
||||||
|
*/
|
||||||
|
public operator fun contains(element: @UnsafeVariance E): Boolean
|
||||||
|
override fun iterator(): Iterator<E>
|
||||||
|
|
||||||
|
// Bulk Operations
|
||||||
|
/**
|
||||||
|
* Checks if all elements in the specified collection are contained in this collection.
|
||||||
|
*/
|
||||||
|
public fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic collection of elements that supports adding and removing elements.
|
||||||
|
*/
|
||||||
|
public interface MutableCollection<E> : Collection<E>, MutableIterable<E> {
|
||||||
|
// Query Operations
|
||||||
|
override fun iterator(): MutableIterator<E>
|
||||||
|
|
||||||
|
// Modification Operations
|
||||||
|
/**
|
||||||
|
* Adds the specified element to the collection.
|
||||||
|
*
|
||||||
|
* @return `true` if the element has been added, `false` if the collection does not support duplicates
|
||||||
|
* and the element is already contained in the collection.
|
||||||
|
*/
|
||||||
|
public fun add(element: E): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a single instance of the specified element from this
|
||||||
|
* collection, if it is present.
|
||||||
|
*
|
||||||
|
* @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
|
||||||
|
*/
|
||||||
|
public fun remove(element: E): Boolean
|
||||||
|
|
||||||
|
// Bulk Modification Operations
|
||||||
|
/**
|
||||||
|
* Adds all of the elements in the specified collection to this collection.
|
||||||
|
*
|
||||||
|
* @return `true` if any of the specified elements was added to the collection, `false` if the collection was not modified.
|
||||||
|
*/
|
||||||
|
public fun addAll(elements: Collection<E>): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all of this collection's elements that are also contained in the specified collection.
|
||||||
|
*
|
||||||
|
* @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
|
||||||
|
*/
|
||||||
|
public fun removeAll(elements: Collection<E>): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retains only the elements in this collection that are contained in the specified collection.
|
||||||
|
*
|
||||||
|
* @return `true` if any element was removed from the collection, `false` if the collection was not modified.
|
||||||
|
*/
|
||||||
|
public fun retainAll(elements: Collection<E>): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all elements from this collection.
|
||||||
|
*/
|
||||||
|
public fun clear(): Unit
|
||||||
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic ordered collection of elements. Methods in this interface support only read-only access to the list;
|
||||||
|
* read/write access is supported through the [MutableList] interface.
|
||||||
|
* @param E the type of elements contained in the list.
|
||||||
|
*/
|
||||||
|
public interface List<out E> : Collection<E> {
|
||||||
|
// Query Operations
|
||||||
|
override val size: Int
|
||||||
|
override fun isEmpty(): Boolean
|
||||||
|
override fun contains(element: @UnsafeVariance E): Boolean
|
||||||
|
override fun iterator(): Iterator<E>
|
||||||
|
|
||||||
|
// Bulk Operations
|
||||||
|
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||||
|
|
||||||
|
// Positional Access Operations
|
||||||
|
/**
|
||||||
|
* Returns the element at the specified index in the list.
|
||||||
|
*/
|
||||||
|
public operator fun get(index: Int): E
|
||||||
|
|
||||||
|
// Search Operations
|
||||||
|
/**
|
||||||
|
* Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
|
||||||
|
* element is not contained in the list.
|
||||||
|
*/
|
||||||
|
public fun indexOf(element: @UnsafeVariance E): Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
|
||||||
|
* element is not contained in the list.
|
||||||
|
*/
|
||||||
|
public fun lastIndexOf(element: @UnsafeVariance E): Int
|
||||||
|
|
||||||
|
// List Iterators
|
||||||
|
/**
|
||||||
|
* Returns a list iterator over the elements in this list (in proper sequence).
|
||||||
|
*/
|
||||||
|
public fun listIterator(): ListIterator<E>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list iterator over the elements in this list (in proper sequence), starting at the specified [index].
|
||||||
|
*/
|
||||||
|
public fun listIterator(index: Int): ListIterator<E>
|
||||||
|
|
||||||
|
// View
|
||||||
|
/**
|
||||||
|
* Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
|
||||||
|
* The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
|
||||||
|
*/
|
||||||
|
public fun subList(fromIndex: Int, toIndex: Int): List<E>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic ordered collection of elements that supports adding and removing elements.
|
||||||
|
* @param E the type of elements contained in the list.
|
||||||
|
*/
|
||||||
|
public interface MutableList<E> : List<E>, MutableCollection<E> {
|
||||||
|
// Modification Operations
|
||||||
|
override fun add(element: E): Boolean
|
||||||
|
override fun remove(element: E): Boolean
|
||||||
|
|
||||||
|
// Bulk Modification Operations
|
||||||
|
override fun addAll(elements: Collection<E>): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts all of the elements in the specified collection [elements] into this list at the specified [index].
|
||||||
|
*
|
||||||
|
* @return `true` if the list was changed as the result of the operation.
|
||||||
|
*/
|
||||||
|
public fun addAll(index: Int, elements: Collection<E>): Boolean
|
||||||
|
override fun removeAll(elements: Collection<E>): Boolean
|
||||||
|
override fun retainAll(elements: Collection<E>): Boolean
|
||||||
|
override fun clear(): Unit
|
||||||
|
|
||||||
|
// Positional Access Operations
|
||||||
|
/**
|
||||||
|
* Replaces the element at the specified position in this list with the specified element.
|
||||||
|
*
|
||||||
|
* @return the element previously at the specified position.
|
||||||
|
*/
|
||||||
|
public operator fun set(index: Int, element: E): E
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts an element into the list at the specified [index].
|
||||||
|
*/
|
||||||
|
public fun add(index: Int, element: E): Unit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an element at the specified [index] from the list.
|
||||||
|
*
|
||||||
|
* @return the element that has been removed.
|
||||||
|
*/
|
||||||
|
public fun removeAt(index: Int): E
|
||||||
|
|
||||||
|
// List Iterators
|
||||||
|
override fun listIterator(): MutableListIterator<E>
|
||||||
|
override fun listIterator(index: Int): MutableListIterator<E>
|
||||||
|
|
||||||
|
// View
|
||||||
|
override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection that holds pairs of objects (keys and values) and supports efficiently retrieving
|
||||||
|
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
|
||||||
|
* Methods in this interface support only read-only access to the map; read-write access is supported through
|
||||||
|
* the [MutableMap] interface.
|
||||||
|
* @param K the type of map keys.
|
||||||
|
* @param V the type of map values.
|
||||||
|
*/
|
||||||
|
public interface Map<K, out V> {
|
||||||
|
// Query Operations
|
||||||
|
/**
|
||||||
|
* Returns the number of key/value pairs in the map.
|
||||||
|
*/
|
||||||
|
public val size: Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the map is empty (contains no elements), `false` otherwise.
|
||||||
|
*/
|
||||||
|
public fun isEmpty(): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the map contains the specified [key].
|
||||||
|
*/
|
||||||
|
public fun containsKey(key: K): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns `true` if the map maps one or more keys to the specified [value].
|
||||||
|
*/
|
||||||
|
public fun containsValue(value: @UnsafeVariance V): Boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value corresponding to the given [key], or `null` if such a key is not present in the map.
|
||||||
|
*/
|
||||||
|
public operator fun get(key: K): V?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value corresponding to the given [key], or [defaultValue] if such a key is not present in the map.
|
||||||
|
*
|
||||||
|
* @since JDK 1.8
|
||||||
|
*/
|
||||||
|
public fun getOrDefault(key: K, defaultValue: @UnsafeVariance V): V
|
||||||
|
|
||||||
|
// Views
|
||||||
|
/**
|
||||||
|
* Returns a [Set] of all keys in this map.
|
||||||
|
*/
|
||||||
|
public val keys: Set<K>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Collection] of all values in this map. Note that this collection may contain duplicate values.
|
||||||
|
*/
|
||||||
|
public val values: Collection<V>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Set] of all key/value pairs in this map.
|
||||||
|
*/
|
||||||
|
public val entries: Set<Map.Entry<K, V>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a key/value pair held by a [Map].
|
||||||
|
*/
|
||||||
|
public interface Entry<out K, out V> {
|
||||||
|
/**
|
||||||
|
* Returns the key of this key/value pair.
|
||||||
|
*/
|
||||||
|
public val key: K
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of this key/value pair.
|
||||||
|
*/
|
||||||
|
public val value: V
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A modifiable collection that holds pairs of objects (keys and values) and supports efficiently retrieving
|
||||||
|
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
|
||||||
|
* @param K the type of map keys.
|
||||||
|
* @param V the type of map values.
|
||||||
|
*/
|
||||||
|
public interface MutableMap<K, V> : Map<K, V> {
|
||||||
|
// Modification Operations
|
||||||
|
/**
|
||||||
|
* Associates the specified [value] with the specified [key] in the map.
|
||||||
|
*
|
||||||
|
* @return the previous value associated with the key, or `null` if the key was not present in the map.
|
||||||
|
*/
|
||||||
|
public fun put(key: K, value: V): V?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the specified key and its corresponding value from this map.
|
||||||
|
*
|
||||||
|
* @return the previous value associated with the key, or `null` if the key was not present in the map.
|
||||||
|
*/
|
||||||
|
public fun remove(key: K): V?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the entry for the specified key only if it is mapped to the specified value.
|
||||||
|
*
|
||||||
|
* @return true if entry was removed
|
||||||
|
*/
|
||||||
|
public fun remove(key: K, value: V): Boolean
|
||||||
|
|
||||||
|
// Bulk Modification Operations
|
||||||
|
/**
|
||||||
|
* Updates this map with key/value pairs from the specified map [from].
|
||||||
|
*/
|
||||||
|
public fun putAll(from: Map<out K, V>): Unit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all elements from this map.
|
||||||
|
*/
|
||||||
|
public fun clear(): Unit
|
||||||
|
|
||||||
|
// Views
|
||||||
|
override val keys: MutableSet<K>
|
||||||
|
override val values: MutableCollection<V>
|
||||||
|
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a key/value pair held by a [MutableMap].
|
||||||
|
*/
|
||||||
|
public interface MutableEntry<K,V>: Map.Entry<K, V> {
|
||||||
|
/**
|
||||||
|
* Changes the value associated with the key of this entry.
|
||||||
|
*
|
||||||
|
* @return the previous value corresponding to the key.
|
||||||
|
*/
|
||||||
|
public fun setValue(newValue: V): V
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
/*
|
||||||
|
private open class ReversedListReadOnly<out T>(private val delegate: List<T>) : AbstractList<T>() {
|
||||||
|
override val size: Int get() = delegate.size
|
||||||
|
override fun get(index: Int): T = delegate[reverseElementIndex(index)]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ReversedList<T>(private val delegate: MutableList<T>) : AbstractMutableList<T>() {
|
||||||
|
override val size: Int get() = delegate.size
|
||||||
|
override fun get(index: Int): T = delegate[reverseElementIndex(index)]
|
||||||
|
|
||||||
|
override fun clear() = delegate.clear()
|
||||||
|
override fun removeAt(index: Int): T = delegate.removeAt(reverseElementIndex(index))
|
||||||
|
|
||||||
|
override fun set(index: Int, element: T): T = delegate.set(reverseElementIndex(index), element)
|
||||||
|
override fun add(index: Int, element: T) {
|
||||||
|
delegate.add(reversePositionIndex(index), element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private fun List<*>.reverseElementIndex(index: Int) = // TODO: Use AbstractList.checkElementIndex: run { AbstractList.checkElementIndex(index, size); lastIndex - index }
|
||||||
|
if (index in 0..size - 1) size - index - 1 else throw IndexOutOfBoundsException("Index $index should be in range [${0..size - 1}].")
|
||||||
|
|
||||||
|
private fun List<*>.reversePositionIndex(index: Int) =
|
||||||
|
if (index in 0..size) size - index else throw IndexOutOfBoundsException("Index $index should be in range [${0..size}].")
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a reversed read-only view of the original List.
|
||||||
|
* All changes made in the original list will be reflected in the reversed one.
|
||||||
|
*/
|
||||||
|
public fun <T> List<T>.asReversed(): List<T> = ReversedListReadOnly(this)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a reversed mutable view of the original mutable List.
|
||||||
|
* All changes made in the original list will be reflected in the reversed one and vice versa.
|
||||||
|
*/
|
||||||
|
public fun <T> MutableList<T>.asReversed(): MutableList<T> = ReversedList(this)
|
||||||
|
*/
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package kotlin.collections
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic unordered collection of elements that does not support duplicate elements.
|
||||||
|
* Methods in this interface support only read-only access to the set;
|
||||||
|
* read/write access is supported through the [MutableSet] interface.
|
||||||
|
* @param E the type of elements contained in the set.
|
||||||
|
*/
|
||||||
|
public interface Set<out E> : Collection<E> {
|
||||||
|
// Query Operations
|
||||||
|
override val size: Int
|
||||||
|
override fun isEmpty(): Boolean
|
||||||
|
override fun contains(element: @UnsafeVariance E): Boolean
|
||||||
|
override fun iterator(): Iterator<E>
|
||||||
|
|
||||||
|
// Bulk Operations
|
||||||
|
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic unordered collection of elements that does not support duplicate elements, and supports
|
||||||
|
* adding and removing elements.
|
||||||
|
* @param E the type of elements contained in the set.
|
||||||
|
*/
|
||||||
|
public interface MutableSet<E> : Set<E>, MutableCollection<E> {
|
||||||
|
// Query Operations
|
||||||
|
override fun iterator(): MutableIterator<E>
|
||||||
|
|
||||||
|
// Modification Operations
|
||||||
|
override fun add(element: E): Boolean
|
||||||
|
override fun remove(element: E): Boolean
|
||||||
|
|
||||||
|
// Bulk Modification Operations
|
||||||
|
override fun addAll(elements: Collection<E>): Boolean
|
||||||
|
override fun removeAll(elements: Collection<E>): Boolean
|
||||||
|
override fun retainAll(elements: Collection<E>): Boolean
|
||||||
|
override fun clear(): Unit
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user