Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayMap does not support square bracket assignment #176

Closed
limbusdev opened this issue Jan 24, 2019 · 6 comments
Closed

ArrayMap does not support square bracket assignment #176

limbusdev opened this issue Jan 24, 2019 · 6 comments
Labels
collections Issues of the ktx-collections module
Milestone

Comments

@limbusdev
Copy link

Hi,
great extension to libGDX :) thank you for that.

My question: how can square brackets be used with libKTX ArrayMaps?

what I wanted to to:

val myMap : ArrayMap<String, Label>
myMap = ArrayMap()
myMap["one"] = new Label(...)

But there is no Set operator defined. Am I doing something wrong, or isn't it just implemented yet?

Cheers

@jcornaz
Copy link
Contributor

jcornaz commented Jan 24, 2019

You have to create an extension function for it:

operator fun <K, V> ArrayMap<K, V>.set(key: K, value: V) {
  put(key, value)
}

Eventually it could be added to ktx...

@limbusdev
Copy link
Author

yeah, hoped it's already been there - thank you :)

@czyzby czyzby added collections Issues of the ktx-collections module good first issue Simple tasks that do not require advanced knowledge of KTX or LibGDX labels Jan 24, 2019
@czyzby czyzby added this to the 1.9.9 milestone Jan 24, 2019
@czyzby
Copy link
Member

czyzby commented Jan 24, 2019

LibGDX introduced a lot of custom collections without a common interface, which makes adding such extensions very redundant. We focused on the most common collections. As of writing this, ktx-collections adds utility methods for ObjectMap and IdentityMap, but I believe ArrayMap was omitted.

@limbusdev
Copy link
Author

limbusdev commented Jan 25, 2019

Yeah, but it had been said, that the libgdx implementations are more efficient and use less storage. Nevermind, I did what jcornaz explained and extended it myself. Thank you.

@czyzby
Copy link
Member

czyzby commented Feb 5, 2019

@limbusdev FYI, from the documentation:

An ordered or unordered map of objects. This implementation uses arrays to store the keys and values, which means #getKey(Object, boolean) do a comparison for each key in the map. This is slower than a typical hash map implementation, but may be acceptable for small maps and has the benefits that keys and values can be accessed by index, which makes iteration fast.

ArrayMap is rarely a good choice, since it loses the main advantage of a hash map. If garbage collection is an issue and you want to rely on LibGDX collections, you should use ObjectMap instead.

I still think we could add some basic utility methods for ArrayMap, but it's a rather niche collection.

@czyzby czyzby modified the milestones: 1.9.9, backlog Jul 22, 2019
@libktx libktx deleted a comment from woneryf Dec 25, 2019
@libktx libktx deleted a comment Dec 25, 2019
@libktx libktx deleted a comment Dec 25, 2019
@libktx libktx deleted a comment Dec 25, 2019
@czyzby
Copy link
Member

czyzby commented Dec 25, 2019

Removed some spam.

czyzby added a commit that referenced this issue Sep 9, 2020
…ent_for_array_map

Added square bracket assignment support for ArrayMap. #176
@czyzby czyzby removed the good first issue Simple tasks that do not require advanced knowledge of KTX or LibGDX label Sep 9, 2020
@czyzby czyzby closed this as completed Sep 9, 2020
@czyzby czyzby modified the milestones: backlog, 1.9.11 Sep 9, 2020
czyzby added a commit that referenced this issue Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Issues of the ktx-collections module
Projects
None yet
Development

No branches or pull requests

3 participants