Jump to content
  • Advertisement
Sign in to follow this  
menyo

Handling many items on a map.

This topic is 685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

Currently I'm storing many objects (> 100.000) in a multidimensional array with ArrayList<Item> keeping all the items on that specific coordinate. This helps me with drawing just the items on screen since I have so many. So unless there is some method I am not aware of for drawing I want to keep this.

 

I want to display item counts and I'm wondering what would be best to do. My items are made of 2 Strings, type and subtype this could be coin and copper/silver/gold or weapon and sword/axe/mace. I would like to display the total types and individual subtype counts but iterating over my array and counting each list takes way too long.

  • HashMap<Type, HashMap<Subtype, Integer>> would work for just the counting. This leaves me with just the count but I don't think I ever need to access the actual item from here.
  • HashMap<Type, HashMap<Subtype, ArrayList<Item>>> would also work and would allow me to access items from the list. To get the totals of a specific type I just need to call size() for each subtype and sum them up. Problem is when removing an item I need to lookup the exact item

Both need some initializing and/or a bit of extra attention when putting the first item of a type in, perhaps even removing values when it goes empty on removal of a item. But this seems to perform extremely well (0ms for a lookup of +-250k items in a pool of 1M. Removes in 3s with the ArrayList but this can be improved with sorting). What i would like to know is if there are any pitfalls to this or perhaps an even better method?

Edited by menyo

Share this post


Link to post
Share on other sites
Advertisement
If you are really going for speed, create an enum of all your items from 0 -> n-1. Then have an array of n size and index it using the type enum. Should perform better than a hashmap.

What the array holds is upto you but could simply be an int for the type count, or a struct holding both a type count and references back to the items.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!