Reformat stdlib: collections
#KT-5558
This commit is contained in:
@@ -120,7 +120,7 @@ public actual abstract class AbstractMutableList<E> protected actual constructor
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
check(last != -1) { "Call next() or previous() before removing element from the iterator."}
|
||||
check(last != -1) { "Call next() or previous() before removing element from the iterator." }
|
||||
|
||||
removeAt(last)
|
||||
index = last
|
||||
@@ -158,7 +158,7 @@ public actual abstract class AbstractMutableList<E> protected actual constructor
|
||||
}
|
||||
|
||||
override fun set(element: E) {
|
||||
check(last != -1) { "Call next() or previous() before updating element value with the iterator."}
|
||||
check(last != -1) { "Call next() or previous() before updating element value with the iterator." }
|
||||
this@AbstractMutableList[last] = element
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,38 +46,39 @@ public actual abstract class AbstractMutableMap<K, V> protected actual construct
|
||||
}
|
||||
|
||||
private var _keys: MutableSet<K>? = null
|
||||
override val keys: MutableSet<K> get() {
|
||||
if (_keys == null) {
|
||||
_keys = object : AbstractMutableSet<K>() {
|
||||
override fun add(element: K): Boolean = throw UnsupportedOperationException("Add is not supported on keys")
|
||||
override fun clear() {
|
||||
this@AbstractMutableMap.clear()
|
||||
}
|
||||
|
||||
override operator fun contains(element: K): Boolean = containsKey(element)
|
||||
|
||||
override operator fun iterator(): MutableIterator<K> {
|
||||
val entryIterator = entries.iterator()
|
||||
return object : MutableIterator<K> {
|
||||
override fun hasNext(): Boolean = entryIterator.hasNext()
|
||||
override fun next(): K = entryIterator.next().key
|
||||
override fun remove() = entryIterator.remove()
|
||||
override val keys: MutableSet<K>
|
||||
get() {
|
||||
if (_keys == null) {
|
||||
_keys = object : AbstractMutableSet<K>() {
|
||||
override fun add(element: K): Boolean = throw UnsupportedOperationException("Add is not supported on keys")
|
||||
override fun clear() {
|
||||
this@AbstractMutableMap.clear()
|
||||
}
|
||||
}
|
||||
|
||||
override fun remove(element: K): Boolean {
|
||||
if (containsKey(element)) {
|
||||
this@AbstractMutableMap.remove(element)
|
||||
return true
|
||||
override operator fun contains(element: K): Boolean = containsKey(element)
|
||||
|
||||
override operator fun iterator(): MutableIterator<K> {
|
||||
val entryIterator = entries.iterator()
|
||||
return object : MutableIterator<K> {
|
||||
override fun hasNext(): Boolean = entryIterator.hasNext()
|
||||
override fun next(): K = entryIterator.next().key
|
||||
override fun remove() = entryIterator.remove()
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override val size: Int get() = this@AbstractMutableMap.size
|
||||
override fun remove(element: K): Boolean {
|
||||
if (containsKey(element)) {
|
||||
this@AbstractMutableMap.remove(element)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override val size: Int get() = this@AbstractMutableMap.size
|
||||
}
|
||||
}
|
||||
return _keys!!
|
||||
}
|
||||
return _keys!!
|
||||
}
|
||||
|
||||
actual abstract override fun put(key: K, value: V): V?
|
||||
|
||||
@@ -88,36 +89,38 @@ public actual abstract class AbstractMutableMap<K, V> protected actual construct
|
||||
}
|
||||
|
||||
private var _values: MutableCollection<V>? = null
|
||||
override val values: MutableCollection<V> get() {
|
||||
if (_values == null) {
|
||||
_values = object : AbstractMutableCollection<V>() {
|
||||
override fun add(element: V): Boolean = throw UnsupportedOperationException("Add is not supported on values")
|
||||
override fun clear() = this@AbstractMutableMap.clear()
|
||||
override val values: MutableCollection<V>
|
||||
get() {
|
||||
if (_values == null) {
|
||||
_values = object : AbstractMutableCollection<V>() {
|
||||
override fun add(element: V): Boolean = throw UnsupportedOperationException("Add is not supported on values")
|
||||
override fun clear() = this@AbstractMutableMap.clear()
|
||||
|
||||
override operator fun contains(element: V): Boolean = containsValue(element)
|
||||
override operator fun contains(element: V): Boolean = containsValue(element)
|
||||
|
||||
override operator fun iterator(): MutableIterator<V> {
|
||||
val entryIterator = entries.iterator()
|
||||
return object : MutableIterator<V> {
|
||||
override fun hasNext(): Boolean = entryIterator.hasNext()
|
||||
override fun next(): V = entryIterator.next().value
|
||||
override fun remove() = entryIterator.remove()
|
||||
override operator fun iterator(): MutableIterator<V> {
|
||||
val entryIterator = entries.iterator()
|
||||
return object : MutableIterator<V> {
|
||||
override fun hasNext(): Boolean = entryIterator.hasNext()
|
||||
override fun next(): V = entryIterator.next().value
|
||||
override fun remove() = entryIterator.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override val size: Int get() = this@AbstractMutableMap.size
|
||||
override val size: Int get() = this@AbstractMutableMap.size
|
||||
|
||||
// TODO: should we implement them this way? Currently it's unspecified in JVM
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Collection<*>) return false
|
||||
return AbstractList.orderedEquals(this, other)
|
||||
// TODO: should we implement them this way? Currently it's unspecified in JVM
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Collection<*>) return false
|
||||
return AbstractList.orderedEquals(this, other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int = AbstractList.orderedHashCode(this)
|
||||
}
|
||||
override fun hashCode(): Int = AbstractList.orderedHashCode(this)
|
||||
}
|
||||
return _values!!
|
||||
}
|
||||
return _values!!
|
||||
}
|
||||
|
||||
override fun remove(key: K): V? {
|
||||
val iter = entries.iterator()
|
||||
|
||||
@@ -24,6 +24,7 @@ public actual open class ArrayList<E> internal constructor(private var array: Ar
|
||||
* @param initialCapacity initial capacity (ignored)
|
||||
*/
|
||||
public actual constructor(initialCapacity: Int) : this(emptyArray()) {}
|
||||
|
||||
/**
|
||||
* Creates an [ArrayList] filled from the [elements] collection.
|
||||
*/
|
||||
@@ -31,6 +32,7 @@ public actual open class ArrayList<E> internal constructor(private var array: Ar
|
||||
|
||||
/** Does nothing in this ArrayList implementation. */
|
||||
public actual fun trimToSize() {}
|
||||
|
||||
/** Does nothing in this ArrayList implementation. */
|
||||
public actual fun ensureCapacity(minCapacity: Int) {}
|
||||
|
||||
|
||||
@@ -92,12 +92,13 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
|
||||
actual override fun containsValue(value: V): Boolean = internalMap.any { equality.equals(it.value, value) }
|
||||
|
||||
private var _entries: MutableSet<MutableMap.MutableEntry<K, V>>? = null
|
||||
actual override val entries: MutableSet<MutableMap.MutableEntry<K, V>> get() {
|
||||
if (_entries == null) {
|
||||
_entries = createEntrySet()
|
||||
actual override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
get() {
|
||||
if (_entries == null) {
|
||||
_entries = createEntrySet()
|
||||
}
|
||||
return _entries!!
|
||||
}
|
||||
return _entries!!
|
||||
}
|
||||
|
||||
protected open fun createEntrySet(): MutableSet<MutableMap.MutableEntry<K, V>> = EntrySet()
|
||||
|
||||
|
||||
@@ -36,21 +36,18 @@ internal class InternalHashCodeMap<K, V>(override val equality: EqualityComparat
|
||||
if (chainOrEntry == null) {
|
||||
// This is a new chain, put it to the map.
|
||||
backingMap[hashCode] = SimpleEntry(key, value)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (chainOrEntry !is Array<*>) {
|
||||
// It is an entry
|
||||
val entry: SimpleEntry<K, V> = chainOrEntry
|
||||
if (equality.equals(entry.key, key)) {
|
||||
return entry.setValue(value)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
backingMap[hashCode] = arrayOf(entry, SimpleEntry(key, value))
|
||||
size++
|
||||
return null
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Chain already exists, perhaps key also exists.
|
||||
val chain: Array<MutableEntry<K, V>> = chainOrEntry
|
||||
val entry = chain.findEntryInChain(key)
|
||||
@@ -74,12 +71,10 @@ internal class InternalHashCodeMap<K, V>(override val equality: EqualityComparat
|
||||
deleteProperty(backingMap, hashCode)
|
||||
size--
|
||||
return entry.value
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val chain: Array<MutableEntry<K, V>> = chainOrEntry
|
||||
for (index in chain.indices) {
|
||||
val entry = chain[index]
|
||||
@@ -116,19 +111,17 @@ internal class InternalHashCodeMap<K, V>(override val equality: EqualityComparat
|
||||
val entry: MutableEntry<K, V> = chainOrEntry
|
||||
if (equality.equals(entry.key, key)) {
|
||||
return entry
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val chain: Array<MutableEntry<K, V>> = chainOrEntry
|
||||
return chain.findEntryInChain(key)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Array<MutableEntry<K, V>>.findEntryInChain(key: K): MutableEntry<K, V>? =
|
||||
firstOrNull { entry -> equality.equals(entry.key, key) }
|
||||
firstOrNull { entry -> equality.equals(entry.key, key) }
|
||||
|
||||
override fun iterator(): MutableIterator<MutableEntry<K, V>> {
|
||||
|
||||
@@ -155,8 +148,7 @@ internal class InternalHashCodeMap<K, V>(override val equality: EqualityComparat
|
||||
isChain = chainOrEntry is Array<*>
|
||||
itemIndex = 0
|
||||
return 0
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
chainOrEntry = null
|
||||
return 1
|
||||
}
|
||||
@@ -172,8 +164,7 @@ internal class InternalHashCodeMap<K, V>(override val equality: EqualityComparat
|
||||
if (!hasNext()) throw NoSuchElementException()
|
||||
val lastEntry = if (isChain) {
|
||||
chainOrEntry.unsafeCast<Array<MutableEntry<K, V>>>()[itemIndex]
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
chainOrEntry.unsafeCast<MutableEntry<K, V>>()
|
||||
}
|
||||
this.lastEntry = lastEntry
|
||||
|
||||
@@ -52,8 +52,7 @@ internal class InternalStringMap<K, V>(override val equality: EqualityComparator
|
||||
size++
|
||||
// structureChanged(host)
|
||||
return null
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// valueMod++
|
||||
return oldValue.unsafeCast<V>()
|
||||
}
|
||||
@@ -67,8 +66,7 @@ internal class InternalStringMap<K, V>(override val equality: EqualityComparator
|
||||
size--
|
||||
// structureChanged(host)
|
||||
return value.unsafeCast<V>()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// valueMod++
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -135,8 +135,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
|
||||
if (this.next === this) {
|
||||
// if this is single element, remove head
|
||||
head = null
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (head === this) {
|
||||
// if this is first element, move head to next
|
||||
head = next
|
||||
@@ -158,7 +157,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
|
||||
/**
|
||||
* Constructs an empty [LinkedHashMap] instance.
|
||||
*/
|
||||
actual constructor() : super() {
|
||||
actual constructor() : super() {
|
||||
map = HashMap<K, ChainEntry<K, V>>()
|
||||
}
|
||||
|
||||
@@ -225,8 +224,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
|
||||
map.put(key, newEntry)
|
||||
newEntry.addToEnd()
|
||||
return null
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return old.setValue(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
|
||||
actual constructor(elements: Collection<E>) : super(LinkedHashMap<E, Any>()) {
|
||||
addAll(elements)
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new empty [LinkedHashSet].
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user