Fixed entry initialization errors
parent
db15e3ea52
commit
e053a1222e
|
@ -790,15 +790,10 @@ class ArrayMap<K: Any, V> : MutableMap<K, V?>{
|
|||
|
||||
class Entries<K: Any, V>(private val map: ArrayMap<K, V?>) : MutableSet<Entry<K, V?>>, MutableIterator<Entry<K, V?>> {
|
||||
|
||||
private lateinit var entry: Entry<K, V?>
|
||||
private var entry: Entry<K, V?>? = null
|
||||
internal var index = 0
|
||||
internal var valid = true
|
||||
|
||||
init {
|
||||
if (hasNext()) {
|
||||
entry = Entry(map)
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (!valid) throw RuntimeException("#iterator() cannot be used nested.")
|
||||
|
@ -889,9 +884,14 @@ class ArrayMap<K: Any, V> : MutableMap<K, V?>{
|
|||
if (index >= map.size_) throw NoSuchElementException(index.toString())
|
||||
if (!valid) throw RuntimeException("#iterator() cannot be used nested.")
|
||||
|
||||
entry.key = map.keyTable[index]!!
|
||||
entry.value = map.valueTable[index++]
|
||||
return entry
|
||||
if (entry == null) {
|
||||
entry = Entry(map.keyTable[index]!!, map.valueTable[index++], map)
|
||||
} else {
|
||||
entry!!.key = map.keyTable[index]!!
|
||||
entry!!.value = map.valueTable[index++]
|
||||
}
|
||||
|
||||
return entry!!
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
|
@ -904,10 +904,10 @@ class ArrayMap<K: Any, V> : MutableMap<K, V?>{
|
|||
}
|
||||
}
|
||||
|
||||
class Entry<K: Any, V>(val map: ArrayMap<K, V>) : MutableMap.MutableEntry<K, V?> {
|
||||
class Entry<K: Any, V>(key: K, value: V, val map: ArrayMap<K, V>) : MutableMap.MutableEntry<K, V?> {
|
||||
// we know there will be at least one
|
||||
override var key: K = map.keyTable[0]!!
|
||||
override var value: V? = map.valueTable[0]
|
||||
override var key: K = key
|
||||
override var value: V? = value
|
||||
|
||||
override fun setValue(newValue: V?): V? {
|
||||
val oldValue = value
|
||||
|
|
|
@ -609,10 +609,9 @@ open class ObjectMap<K: Any, V> : MutableMap<K, V?> {
|
|||
return keys2!!
|
||||
}
|
||||
|
||||
class Entry<K: Any, V>(val map: ObjectMap<K, V?>, index: Int) : MutableMap.MutableEntry<K, V?> {
|
||||
// we know there will be at least one
|
||||
override var key: K = map.keyTable[index]!!
|
||||
override var value: V? = map.valueTable[index]
|
||||
class Entry<K: Any, V>(key: K, value: V, val map: ObjectMap<K, V?>) : MutableMap.MutableEntry<K, V?> {
|
||||
override var key: K = key
|
||||
override var value: V? = value
|
||||
|
||||
override fun setValue(newValue: V?): V? {
|
||||
val oldValue = value
|
||||
|
@ -684,25 +683,24 @@ open class ObjectMap<K: Any, V> : MutableMap<K, V?> {
|
|||
}
|
||||
|
||||
open class Entries<K: Any, V>(map: ObjectMap<K, V?>) : MutableSet<Entry<K, V?>>, MapIterator<K, V?, Entry<K, V?>>(map) {
|
||||
internal lateinit var entry: Entry<K, V?>
|
||||
|
||||
init {
|
||||
if (hasNext) {
|
||||
findNextIndex()
|
||||
entry = Entry(map, nextIndex)
|
||||
}
|
||||
}
|
||||
internal var entry: Entry<K, V?>? = null
|
||||
|
||||
/** Note the same entry instance is returned each time this method is called. */
|
||||
override fun next(): Entry<K, V?> {
|
||||
if (!hasNext) throw NoSuchElementException()
|
||||
if (!valid) throw RuntimeException("#iterator() cannot be used nested.")
|
||||
val keyTable = map.keyTable
|
||||
entry.key = keyTable[nextIndex]!!
|
||||
entry.value = map.valueTable[nextIndex]
|
||||
|
||||
if (entry == null) {
|
||||
entry = Entry(keyTable[nextIndex]!!, map.valueTable[nextIndex], map)
|
||||
} else {
|
||||
entry!!.key = keyTable[nextIndex]!!
|
||||
entry!!.value = map.valueTable[nextIndex]
|
||||
}
|
||||
|
||||
currentIndex = nextIndex
|
||||
findNextIndex()
|
||||
return entry
|
||||
return entry!!
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
|
@ -778,7 +776,7 @@ open class ObjectMap<K: Any, V> : MutableMap<K, V?> {
|
|||
}
|
||||
|
||||
override fun remove(element: Entry<K, V?>): Boolean {
|
||||
val removed = map.remove(entry.key) != null
|
||||
val removed = map.remove(entry!!.key) != null
|
||||
reset()
|
||||
return removed
|
||||
}
|
||||
|
|
|
@ -328,16 +328,23 @@ class OrderedMap<K, V> : ObjectMap<K, V> where K : Any, K : Comparable<K> {
|
|||
if (!hasNext) throw NoSuchElementException()
|
||||
if (!valid) throw RuntimeException("#iterator() cannot be used nested.")
|
||||
currentIndex = nextIndex
|
||||
entry.key = keys[nextIndex]
|
||||
entry.value = map.get(entry.key)
|
||||
|
||||
val key = keys[nextIndex]
|
||||
if (entry == null) {
|
||||
entry = Entry(key, map.get(key), map)
|
||||
} else {
|
||||
entry!!.key = key
|
||||
entry!!.value = map.get(key)
|
||||
}
|
||||
|
||||
nextIndex++
|
||||
hasNext = nextIndex < map.size
|
||||
return entry
|
||||
return entry!!
|
||||
}
|
||||
|
||||
override fun remove() {
|
||||
check(currentIndex >= 0) { "next must be called before remove." }
|
||||
map.remove(entry.key)
|
||||
map.remove(entry!!.key)
|
||||
nextIndex--
|
||||
currentIndex = -1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue