Skip to content

Commit

Permalink
Added square bracket assignment support for ArrayMap. libktx#176
Browse files Browse the repository at this point in the history
  • Loading branch information
rsolmano committed Sep 9, 2020
1 parent 92384e5 commit 6dab28d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions collections/src/main/kotlin/ktx/collections/maps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Key, Value> = ObjectMap<Key, Value>

/** Alias for [com.badlogic.gdx.utils.ArrayMap]. Added for consistency with other collections and factory methods. */
typealias GdxArrayMap<Key, Value> = ArrayMap<Key, Value>

/**
* Default LibGDX map size used by most constructors.
*/
Expand Down Expand Up @@ -293,6 +297,13 @@ operator fun IntMap<*>.contains(key: Int): Boolean = this.containsKey(key)
*/
operator fun <Value> IntMap<Value>.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 <Key, Value> GdxArrayMap<Key, Value>.set(key: Key, value: Value): Int = this.put(key, value)

/**
* Allows to destruct [ObjectMap.Entry] into key and value components.
* @return [ObjectMap.Entry.key]
Expand Down
13 changes: 13 additions & 0 deletions collections/src/test/kotlin/ktx/collections/MapsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, String>()
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])
}
}

0 comments on commit 6dab28d

Please sign in to comment.