This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SortedMap implement Map interface. Not changing the name yet. (#534
) Some work on https://github.com/korlibs/kds/issues/20
- Loading branch information
Showing
3 changed files
with
107 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kds/src/commonMain/kotlin/com/soywiz/kds/map/MutableMapExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.soywiz.kds.map | ||
|
||
import com.soywiz.kds.* | ||
|
||
interface MutableMapExt<K, V> : MutableMap<K, V> { | ||
override fun isEmpty(): Boolean = size == 0 | ||
|
||
override fun putAll(from: Map<out K, V>) { | ||
from.forEach { set(it.key, it.value) } | ||
} | ||
} | ||
|
||
class MutableEntryExt<K, V>( | ||
val map: MutableMap<K, V>, | ||
override val key: K, | ||
) : MutableMap.MutableEntry<K, V> { | ||
override val value: V get() = map[key]!! | ||
|
||
override fun setValue(newValue: V): V { | ||
val oldValue = value | ||
map[key] = newValue | ||
return oldValue | ||
} | ||
|
||
companion object { | ||
fun <K, V> fromMap(map: MutableMap<K, V>, keys: Collection<K>): MutableSet<MutableMap.MutableEntry<K, V>> { | ||
return keys.map { MutableEntryExt(map, it) }.toMutableSet() | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters