Code cleanup.
parent
6341127c77
commit
fb1c30b035
|
@ -50,7 +50,7 @@ import kotlin.math.min
|
|||
*
|
||||
* @author Nathan Sweet
|
||||
*/
|
||||
class ArrayMap<K: Any, V> : MutableMap<K, V?>, MutableIterable<Entry<K, V?>> {
|
||||
class ArrayMap<K: Any, V> : MutableMap<K, V?>{
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -670,10 +670,6 @@ class ArrayMap<K: Any, V> : MutableMap<K, V?>, MutableIterable<Entry<K, V?>> {
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<K, V?>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V?>>
|
||||
get() = entries() as MutableSet<MutableMap.MutableEntry<K, V?>>
|
||||
|
|
|
@ -93,6 +93,7 @@ class BinaryHeap<T : BinaryHeap.Node?> @JvmOverloads constructor(capacity: Int =
|
|||
* a max heap). */
|
||||
fun peek(): T? {
|
||||
check(size != 0) { "The heap is empty." }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return nodes[0] as T?
|
||||
}
|
||||
|
||||
|
@ -106,6 +107,7 @@ class BinaryHeap<T : BinaryHeap.Node?> @JvmOverloads constructor(capacity: Int =
|
|||
down(0)
|
||||
}
|
||||
else nodes[0] = null
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return removed as T?
|
||||
}
|
||||
|
||||
|
@ -143,6 +145,7 @@ class BinaryHeap<T : BinaryHeap.Node?> @JvmOverloads constructor(capacity: Int =
|
|||
if ((value < oldValue) xor isMaxHeap) up(node.index) else down(node.index)
|
||||
}
|
||||
|
||||
@Suppress("NAME_SHADOWING")
|
||||
private fun up(index: Int) {
|
||||
var index = index
|
||||
val nodes = nodes
|
||||
|
@ -162,6 +165,7 @@ class BinaryHeap<T : BinaryHeap.Node?> @JvmOverloads constructor(capacity: Int =
|
|||
node.index = index
|
||||
}
|
||||
|
||||
@Suppress("NAME_SHADOWING")
|
||||
private fun down(index: Int) {
|
||||
var index = index
|
||||
val nodes = nodes
|
||||
|
@ -207,9 +211,8 @@ class BinaryHeap<T : BinaryHeap.Node?> @JvmOverloads constructor(capacity: Int =
|
|||
node.index = index
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (obj !is BinaryHeap<*>) return false
|
||||
val other = obj
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is BinaryHeap<*>) return false
|
||||
if (other.size != size) return false
|
||||
val nodes1 = nodes
|
||||
val nodes2 = other.nodes
|
||||
|
|
|
@ -142,12 +142,12 @@ class BinarySearch<T>(private val eval: Evaluator<T>, private val indexed: Index
|
|||
else {
|
||||
-1
|
||||
}
|
||||
|
||||
else -> throw AssertionError(bias)
|
||||
}
|
||||
|
||||
}
|
||||
val mid = start + range / 2
|
||||
val vm = eval.getValue(indexed[mid])
|
||||
|
||||
return if (value >= vm) {
|
||||
search(mid, end, value, bias)
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ import kotlin.math.min
|
|||
*
|
||||
* @author Nathan Sweet
|
||||
*/
|
||||
class ExpandingArray<T> : Iterable<T> {
|
||||
class ExpandingArray<T> : MutableIterable<T> {
|
||||
/**
|
||||
* Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed
|
||||
* if the [ExpandingArray.Array] constructor was used.
|
||||
|
@ -473,9 +473,12 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
return size > 0
|
||||
}
|
||||
|
||||
val isEmpty: Boolean
|
||||
/** Returns true if the array is empty. */
|
||||
get() = size == 0
|
||||
/**
|
||||
* Returns true if the array is empty.
|
||||
*/
|
||||
fun isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
Arrays.fill(items, 0, size, null)
|
||||
|
@ -520,7 +523,9 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
return items
|
||||
}
|
||||
|
||||
/** Creates a new backing array with the specified size containing the current items. */
|
||||
/**
|
||||
* Creates a new backing array with the specified size containing the current items.
|
||||
*/
|
||||
protected fun resize(newSize: Int): Array<T?> {
|
||||
val items = items
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
@ -608,7 +613,8 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
*
|
||||
* If [Collections.allocateIterators] is false, the same iterator instance is returned each time this method is called.
|
||||
*
|
||||
* Use the [ArrayIterator] constructor for nested or multithreaded iteration. */
|
||||
* Use the [ArrayIterator] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
override fun iterator(): ArrayIterator<T> {
|
||||
if (allocateIterators) return ArrayIterator(this, true)
|
||||
if (iterable == null) iterable = ArrayIterable(this)
|
||||
|
@ -620,7 +626,8 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
*
|
||||
* If [Collections.allocateIterators] is false, the same iterable instance is returned each time this method is called.
|
||||
*
|
||||
* Use the [Predicate.PredicateIterable] constructor for nested or multithreaded iteration. */
|
||||
* Use the [Predicate.PredicateIterable] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
fun select(predicate: Predicate<T>?): Iterable<T> {
|
||||
if (allocateIterators) return PredicateIterable(this, predicate)
|
||||
if (predicateIterable == null) {
|
||||
|
@ -799,7 +806,8 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
this.allowRemove = allowRemove
|
||||
}
|
||||
|
||||
/** @see Collections.allocateIterators
|
||||
/**
|
||||
* @see Collections.allocateIterators
|
||||
*/
|
||||
override fun iterator(): ArrayIterator<T> {
|
||||
if (allocateIterators) return ArrayIterator(array, allowRemove)
|
||||
|
@ -807,8 +815,8 @@ class ExpandingArray<T> : Iterable<T> {
|
|||
// lastAcquire.getBuffer().setLength(0);
|
||||
// new Throwable().printStackTrace(new java.io.PrintWriter(lastAcquire));
|
||||
if (iterator1 == null) {
|
||||
iterator1 = ArrayIterator(array, allowRemove) as ArrayIterator<T>?
|
||||
iterator2 = ArrayIterator(array, allowRemove) as ArrayIterator<T>?
|
||||
iterator1 = ArrayIterator(array, allowRemove)
|
||||
iterator2 = ArrayIterator(array, allowRemove)
|
||||
// iterator1.iterable = this;
|
||||
// iterator2.iterable = this;
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
class IntFloatMap : MutableMap<Int, Float>, MutableIterable<IntFloatMap.Entry> {
|
||||
class IntFloatMap : MutableMap<Int, Float> {
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -509,9 +509,6 @@ class IntFloatMap : MutableMap<Int, Float>, MutableIterable<IntFloatMap.Entry> {
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator for the entries in the map. Remove is supported.
|
||||
|
|
|
@ -54,7 +54,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
class IntIntMap : MutableMap<Int, Int>, MutableIterable<IntIntMap.Entry> {
|
||||
class IntIntMap : MutableMap<Int, Int> {
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -509,9 +509,6 @@ class IntIntMap : MutableMap<Int, Int>, MutableIterable<IntIntMap.Entry> {
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator for the entries in the map. Remove is supported.
|
||||
|
|
|
@ -51,10 +51,11 @@ import java.util.*
|
|||
*
|
||||
* This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci
|
||||
* hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see [Malte Skarupke's blog post](https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/)). Linear probing continues to work even when all hashCodes collide, just more slowly.
|
||||
*
|
||||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
open class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
||||
open class IntMap<V> : MutableMap<Int, V> {
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -556,10 +557,6 @@ open class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<V?>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator for the entries in the map. Remove is supported.
|
||||
*
|
||||
|
|
|
@ -425,10 +425,10 @@ class LockFreeBiMap<K: Any, V: Any> : MutableMap<K, V>, Cloneable, Serializable
|
|||
|
||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||
private val forwardREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeBiMap::class.java, HashMap::class.java, "forwardHashMap"
|
||||
LockFreeBiMap::class.java, MutableMap::class.java, "forwardHashMap"
|
||||
)
|
||||
private val reverseREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeBiMap::class.java, HashMap::class.java, "reverseHashMap"
|
||||
LockFreeBiMap::class.java, MutableMap::class.java, "reverseHashMap"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ import java.util.concurrent.atomic.*
|
|||
class LockFreeHashMap<K: Any, V> : MutableMap<K, V?>, Cloneable, Serializable {
|
||||
|
||||
@Volatile
|
||||
private var hashMap: HashMap<K, V?>
|
||||
private var hashMap: MutableMap<K, V?>
|
||||
|
||||
|
||||
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
|
||||
|
|
|
@ -68,7 +68,7 @@ class LockFreeIntBiMap<V: Any> : MutableMap<Int, V>, Cloneable, Serializable {
|
|||
get() = forwardHashMap.size
|
||||
|
||||
/**
|
||||
* Removes all of the mappings from this bimap.
|
||||
* Removes all the mappings from this bimap.
|
||||
* The bimap will be empty after this call returns.
|
||||
*/
|
||||
@Synchronized
|
||||
|
@ -95,7 +95,7 @@ class LockFreeIntBiMap<V: Any> : MutableMap<Int, V>, Cloneable, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Replaces all of the mappings from the specified map to this bimap.
|
||||
* Replaces all the mappings from the specified map to this bimap.
|
||||
* These mappings will replace any mappings that this map had for
|
||||
* any of the keys currently in the specified map.
|
||||
*
|
||||
|
@ -225,7 +225,7 @@ class LockFreeIntBiMap<V: Any> : MutableMap<Int, V>, Cloneable, Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Copies all of the mappings from the specified map to this map.
|
||||
* Copies all the mappings from the specified map to this map.
|
||||
* These mappings will replace any mappings that this map had for
|
||||
* any of the keys currently in the specified map.
|
||||
*
|
||||
|
|
|
@ -428,10 +428,10 @@ class LockFreeObjectBiMap<K: Any, V: Any> : MutableMap<K, V>, Cloneable, Seriali
|
|||
|
||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||
private val forwardREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeObjectBiMap::class.java, HashMap::class.java, "forwardHashMap"
|
||||
LockFreeObjectBiMap::class.java, MutableMap::class.java, "forwardHashMap"
|
||||
)
|
||||
private val reverseREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeObjectBiMap::class.java, HashMap::class.java, "reverseHashMap"
|
||||
LockFreeObjectBiMap::class.java, MutableMap::class.java, "reverseHashMap"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ class LockFreeObjectIntBiMap<K: Any> : MutableMap<K, Int>, Cloneable, Serializab
|
|||
get() = forwardHashMap.size
|
||||
|
||||
/**
|
||||
* Removes all of the mappings from this bimap.
|
||||
* Removes all the mappings from this bimap.
|
||||
* The bimap will be empty after this call returns.
|
||||
*/
|
||||
@Synchronized
|
||||
|
@ -96,7 +96,7 @@ class LockFreeObjectIntBiMap<K: Any> : MutableMap<K, Int>, Cloneable, Serializab
|
|||
}
|
||||
|
||||
/**
|
||||
* Replaces all of the mappings from the specified map to this bimap.
|
||||
* Replaces all the mappings from the specified map to this bimap.
|
||||
* These mappings will replace any mappings that this map had for
|
||||
* any of the keys currently in the specified map.
|
||||
*
|
||||
|
@ -130,7 +130,7 @@ class LockFreeObjectIntBiMap<K: Any> : MutableMap<K, Int>, Cloneable, Serializab
|
|||
}
|
||||
|
||||
/**
|
||||
* Replaces all of the mappings from the specified map to this bimap.
|
||||
* Replaces all the mappings from the specified map to this bimap.
|
||||
* These mappings will replace any mappings that this map had for
|
||||
* any of the keys currently in the specified map. This is an alternate
|
||||
* form of [.replaceAll] replaceAll(K, V) that will silently
|
||||
|
|
|
@ -54,7 +54,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
class LongMap<V> : MutableMap<Long, V>, MutableIterable<LongMap.Entry<V?>> {
|
||||
class LongMap<V> : MutableMap<Long, V> {
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -556,10 +556,6 @@ class LongMap<V> : MutableMap<Long, V>, MutableIterable<LongMap.Entry<V?>> {
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<V?>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator for the entries in the map. Remove is supported.
|
||||
*
|
||||
|
|
|
@ -59,7 +59,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
open class ObjectFloatMap<K: Any> : MutableMap<K, Float>, MutableIterable<MutableMap.MutableEntry<K, Float>> {
|
||||
open class ObjectFloatMap<K: Any> : MutableMap<K, Float> {
|
||||
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
|
@ -561,10 +561,6 @@ open class ObjectFloatMap<K: Any> : MutableMap<K, Float>, MutableIterable<Mutabl
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<K>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, Float>>
|
||||
get() = entries() as MutableSet<MutableMap.MutableEntry<K, Float>>
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
open class ObjectIntMap<K: Any> : MutableMap<K, Int>, MutableIterable<MutableMap.MutableEntry<K, Int>> {
|
||||
open class ObjectIntMap<K: Any> : MutableMap<K, Int> {
|
||||
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
|
@ -506,10 +506,6 @@ open class ObjectIntMap<K: Any> : MutableMap<K, Int>, MutableIterable<MutableMap
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<K>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, Int>>
|
||||
get() = entries() as MutableSet<MutableMap.MutableEntry<K, Int>>
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
open class ObjectMap<K: Any, V> : MutableMap<K, V>, MutableIterable<MutableMap.MutableEntry<K, V?>> {
|
||||
open class ObjectMap<K: Any, V> : MutableMap<K, V> {
|
||||
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
|
@ -523,10 +523,6 @@ open class ObjectMap<K: Any, V> : MutableMap<K, V>, MutableIterable<MutableMap.M
|
|||
return buffer.toString()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<Entry<K, V?>> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
get() = entries() as MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
package dorkbox.collections
|
||||
|
||||
import dorkbox.collections.Collections.allocateIterators
|
||||
import java.util.Comparator
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -205,10 +205,6 @@ class OrderedMap<K, V> : ObjectMap<K, V?> where K : Any, K : Comparable<K> {
|
|||
return keys_
|
||||
}
|
||||
|
||||
override fun iterator(): Entries<K, V?> {
|
||||
return entries()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator for the entries in the map. Remove is supported.
|
||||
*
|
||||
|
@ -241,6 +237,7 @@ class OrderedMap<K, V> : ObjectMap<K, V?> where K : Any, K : Comparable<K> {
|
|||
*
|
||||
* Use the [OrderedMapValues] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun values(): Values<V?> {
|
||||
if (allocateIterators) return OrderedMapValues(this as OrderedMap<K, V?>)
|
||||
if (values1 == null) {
|
||||
|
@ -284,6 +281,7 @@ class OrderedMap<K, V> : ObjectMap<K, V?> where K : Any, K : Comparable<K> {
|
|||
return keys2 as Keys<K>
|
||||
}
|
||||
|
||||
@Suppress("KotlinConstantConditions")
|
||||
override fun toString(separator: String, braces: Boolean): String {
|
||||
if (size == 0) return if (braces) "{}" else ""
|
||||
|
||||
|
@ -369,8 +367,8 @@ class OrderedMap<K, V> : ObjectMap<K, V?> where K : Any, K : Comparable<K> {
|
|||
currentIndex = -1
|
||||
}
|
||||
|
||||
@Suppress("USELESS_CAST", "UNCHECKED_CAST")
|
||||
override fun toArray(): Array<K> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return Array(keys.size - nextIndex) { next() as Any } as Array<K>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -106,9 +106,9 @@ class OrderedSet<T> : ObjectSet<T> where T : Any, T : Comparable<T> {
|
|||
items.sortWith(comparator)
|
||||
}
|
||||
|
||||
override fun add(key: T): Boolean {
|
||||
if (!super.add(key)) return false
|
||||
items.add(key)
|
||||
override fun add(element: T): Boolean {
|
||||
if (!super.add(element)) return false
|
||||
items.add(element)
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -119,9 +119,9 @@ class OrderedSet<T> : ObjectSet<T> where T : Any, T : Comparable<T> {
|
|||
fun add(key: T, index: Int): Boolean {
|
||||
if (!super.add(key)) {
|
||||
var oldIndex = -1
|
||||
items.forEachIndexed { index, item ->
|
||||
items.forEachIndexed { i, item ->
|
||||
if (item === key) {
|
||||
oldIndex = index
|
||||
oldIndex = i
|
||||
return@forEachIndexed
|
||||
}
|
||||
}
|
||||
|
@ -274,8 +274,8 @@ class OrderedSet<T> : ObjectSet<T> where T : Any, T : Comparable<T> {
|
|||
(set as OrderedSet<*>).removeIndex(nextIndex)
|
||||
}
|
||||
|
||||
@Suppress("USELESS_CAST", "UNCHECKED_CAST")
|
||||
override fun toArray(): Array<T> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return Array(set.size - nextIndex) { next() as Any } as Array<T>
|
||||
}
|
||||
|
||||
|
|
|
@ -117,6 +117,7 @@ interface Predicate<T> {
|
|||
else {
|
||||
iterator!!.set(iterable!!.iterator() as MutableIterator<T>, predicate)
|
||||
}
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return iterator as MutableIterator<T>
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ import kotlin.arrayOf
|
|||
import kotlin.arrayOfNulls
|
||||
import kotlin.intArrayOf
|
||||
import kotlin.run
|
||||
import kotlin.toString
|
||||
|
||||
/** Tests for the collection classes. Currently, only equals() and hashCode() methods are tested. */
|
||||
class CollectionsTest {
|
||||
|
@ -191,12 +190,12 @@ $b"""
|
|||
}
|
||||
|
||||
// perform an iteration test
|
||||
var anotherMap = copy(map)
|
||||
var it = (anotherMap as MutableIterable<*>).iterator()
|
||||
var anotherMap = copy(map)as MutableMap<*,*>
|
||||
var it = anotherMap.iterator()
|
||||
|
||||
var iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
val entry = it.next()!!
|
||||
val entry = it.next()
|
||||
iterationCount++
|
||||
}
|
||||
assertEquals(iterationCount, keys.size)
|
||||
|
@ -206,11 +205,11 @@ $b"""
|
|||
var i = 0
|
||||
val n = keys.size
|
||||
while (i < n) {
|
||||
anotherMap = copy(map)
|
||||
it = (anotherMap as MutableIterable<*>).iterator()
|
||||
anotherMap = copy(map) as MutableMap<*,*>
|
||||
it = anotherMap.iterator()
|
||||
iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
val entry = it.next()!!
|
||||
val entry = it.next()
|
||||
if (iterationCount == i) {
|
||||
it.remove()
|
||||
}
|
||||
|
@ -274,8 +273,8 @@ $b"""
|
|||
}
|
||||
|
||||
// perform an iteration test
|
||||
var anotherMap = copy(map)
|
||||
var it = (anotherMap as Iterable<*>).iterator()
|
||||
var anotherMap = copy(map) as MutableMap<*,*>
|
||||
var it = anotherMap.iterator()
|
||||
var iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
|
@ -287,8 +286,8 @@ $b"""
|
|||
var i = 0
|
||||
val n = keys.size
|
||||
while (i < n) {
|
||||
anotherMap = copy(map)
|
||||
it = (anotherMap as MutableIterable<*>).iterator()
|
||||
anotherMap = copy(map) as MutableMap<*,*>
|
||||
it = anotherMap.iterator()
|
||||
iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
|
@ -331,8 +330,8 @@ $b"""
|
|||
}
|
||||
|
||||
// perform an iteration test
|
||||
var anotherMap = copy(map)
|
||||
var it = (anotherMap as MutableIterable<*>).iterator()
|
||||
var anotherMap = copy(map) as MutableMap<*,*>
|
||||
var it = anotherMap.iterator()
|
||||
var iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
|
@ -344,8 +343,8 @@ $b"""
|
|||
var i = 0
|
||||
val n = keys.size
|
||||
while (i < n) {
|
||||
anotherMap = copy(map)
|
||||
it = (anotherMap as MutableIterable<*>).iterator()
|
||||
anotherMap = copy(map) as MutableMap<*,*>
|
||||
it = anotherMap.iterator()
|
||||
iterationCount = 0
|
||||
while (it.hasNext()) {
|
||||
it.next()
|
||||
|
|
|
@ -38,6 +38,15 @@ class IntTests {
|
|||
return map
|
||||
}
|
||||
|
||||
private fun map3(): LockFreeIntMap<String?> {
|
||||
val map = LockFreeIntMap<String?>()
|
||||
map[1] = "1"
|
||||
map[2] = "2"
|
||||
map[3] = null
|
||||
|
||||
return map
|
||||
}
|
||||
|
||||
private fun set(): IntSet {
|
||||
val set = IntSet()
|
||||
set.add(1)
|
||||
|
@ -60,7 +69,7 @@ class IntTests {
|
|||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
map.retainAll { it.key == 1}
|
||||
map.entries.retainAll { it.key == 1}
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
|
@ -204,7 +213,7 @@ class IntTests {
|
|||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
map.retainAll { it.key == 1}
|
||||
map.entries.retainAll { it.key == 1}
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == 1)
|
||||
|
||||
|
@ -334,6 +343,149 @@ class IntTests {
|
|||
assertTrue(entries.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLFIntMap() {
|
||||
val map = map3()
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
assertTrue(map[2] == "2")
|
||||
map.remove(2)
|
||||
assertTrue(map.size == 2)
|
||||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
map.entries.retainAll { it.key == 1}
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
|
||||
map.clear()
|
||||
|
||||
assertTrue(map.isEmpty())
|
||||
assertTrue(map.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLFIntMapKeys() {
|
||||
val map = map3()
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
val keys = map.keys()
|
||||
assertTrue(keys.size == 3)
|
||||
|
||||
assertTrue(map[2] == "2")
|
||||
keys.remove(2)
|
||||
assertTrue(map.size == 2)
|
||||
assertTrue(keys.size == 2)
|
||||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
val keep = listOf(1)
|
||||
keys.retainAll(keep)
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
keys.clear()
|
||||
|
||||
assertTrue(map.isEmpty())
|
||||
assertTrue(keys.isEmpty())
|
||||
assertTrue(map.size == 0)
|
||||
assertTrue(keys.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLFIntMapValues() {
|
||||
val map = map3()
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
val values = map.values()
|
||||
assertTrue(values.size == 3)
|
||||
|
||||
values.remove("2")
|
||||
assertTrue(map.size == 2)
|
||||
assertTrue(values.size == 2)
|
||||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
val keep = listOf("1")
|
||||
values.retainAll(keep)
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
values.clear()
|
||||
|
||||
assertTrue(map.isEmpty())
|
||||
assertTrue(values.isEmpty())
|
||||
assertTrue(map.size == 0)
|
||||
assertTrue(values.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLFIntMapEntries() {
|
||||
val map = map3()
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
val entries = map.entries()
|
||||
assertTrue(entries.size == 3)
|
||||
|
||||
var toRemove: IntMap.Entry<String?>? = null
|
||||
val iter = entries.iterator()
|
||||
while (iter.hasNext()) {
|
||||
val entry = iter.next()
|
||||
if (entry.key == 2) {
|
||||
toRemove = entry
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
assertTrue(toRemove?.key == 2)
|
||||
entries.remove(toRemove)
|
||||
assertTrue(map.size == 2)
|
||||
assertTrue(entries.size == 2)
|
||||
|
||||
assertTrue(map[2] == null)
|
||||
|
||||
val keepEntry = entries.iterator().next()
|
||||
|
||||
val keep = listOf(keepEntry)
|
||||
entries.retainAll(keep)
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
entries.clear()
|
||||
|
||||
assertTrue(map.isEmpty())
|
||||
assertTrue(entries.isEmpty())
|
||||
assertTrue(map.size == 0)
|
||||
assertTrue(entries.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLFIntMapEntries2() {
|
||||
val map = map3()
|
||||
assertTrue(map.size == 3)
|
||||
|
||||
val entries = map.entries()
|
||||
val keepEntry = IntMap.Entry<String?>()
|
||||
keepEntry.key = 1
|
||||
keepEntry.value = "1"
|
||||
|
||||
val keep = listOf(keepEntry)
|
||||
entries.retainAll(keep)
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map[1] == "1")
|
||||
|
||||
entries.clear()
|
||||
|
||||
assertTrue(map.isEmpty())
|
||||
assertTrue(entries.isEmpty())
|
||||
assertTrue(map.size == 0)
|
||||
assertTrue(entries.size == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testIntSet() {
|
||||
val set = set()
|
||||
|
|
|
@ -51,7 +51,7 @@ class ObjectTests {
|
|||
|
||||
assertTrue(map["2"] == null)
|
||||
|
||||
map.retainAll { it.key == "1"}
|
||||
map.entries.retainAll { it.key == "1"}
|
||||
assertTrue(map.size == 1)
|
||||
assertTrue(map["1"] == 1)
|
||||
|
||||
|
|
Loading…
Reference in New Issue