diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a244745..d2ad1d36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ _See also: [the official LibGDX changelog](https://github.com/libgdx/libgdx/blob - **[UPDATE]** Updated to Kotlin 1.4.0. - **[UPDATE]** Updated to Kotlin Coroutines 1.3.9. - **[FEATURE]** (`ktx-scene2d`) Added `image` builders for `NinePatch`, `TextureRegion`, `Texture` and `Drawable`. +- **[FEATURE]** (`ktx-collections`) Added alias `GdxArrayMap` for gds ArrayMap and `set` operator function to use square brackets assignment. #### 1.9.11-b1 diff --git a/collections/src/main/kotlin/ktx/collections/maps.kt b/collections/src/main/kotlin/ktx/collections/maps.kt index 1fb338e1..57ed239e 100644 --- a/collections/src/main/kotlin/ktx/collections/maps.kt +++ b/collections/src/main/kotlin/ktx/collections/maps.kt @@ -2,6 +2,7 @@ package ktx.collections +import com.badlogic.gdx.utils.ArrayMap import com.badlogic.gdx.utils.IdentityMap import com.badlogic.gdx.utils.IntFloatMap import com.badlogic.gdx.utils.IntIntMap @@ -15,6 +16,9 @@ import com.badlogic.gdx.utils.ObjectSet /** Alias for [com.badlogic.gdx.utils.ObjectMap]. Added for consistency with other collections and factory methods. */ typealias GdxMap = ObjectMap +/** Alias for [com.badlogic.gdx.utils.ArrayMap]. Added for consistency with other collections and factory methods. */ +typealias GdxArrayMap = ArrayMap + /** * Default LibGDX map size used by most constructors. */ @@ -293,6 +297,13 @@ operator fun IntMap<*>.contains(key: Int): Boolean = this.containsKey(key) */ operator fun IntMap.set(key: Int, value: Value): Value? = this.put(key, value) +/** + * @param key the passed value will be linked with this key. + * @param value will be stored in the map, accessible by the passed key. + * @return index of the key and value in the underlying arrays. + */ +operator fun GdxArrayMap.set(key: Key, value: Value): Int = this.put(key, value) + /** * Allows to destruct [ObjectMap.Entry] into key and value components. * @return [ObjectMap.Entry.key] diff --git a/collections/src/test/kotlin/ktx/collections/MapsTest.kt b/collections/src/test/kotlin/ktx/collections/MapsTest.kt index 1f9d7ea5..716111d3 100644 --- a/collections/src/test/kotlin/ktx/collections/MapsTest.kt +++ b/collections/src/test/kotlin/ktx/collections/MapsTest.kt @@ -380,4 +380,17 @@ class MapsTest { assertEquals(GdxArray.with(1, 2, 2, 3, 3, 3), result) } + + @Test + fun `should add new entry to the GdxArrayMap using square brackets assignment`() { + val gdxArrayMap = GdxArrayMap() + gdxArrayMap[1] = "One" + gdxArrayMap[2] = "Two" + gdxArrayMap.put(3, "Three") + gdxArrayMap.put(4, "Four") + assertEquals("One", gdxArrayMap[1]) + assertEquals("Two", gdxArrayMap[2]) + assertEquals("Three", gdxArrayMap[3]) + assertEquals("Four", gdxArrayMap[4]) + } }