I am blanking right now, is there any faster way of retrieving an item from an array by the value of one of its properties than simply iterating through each value and checking?

ie:
foreach(Something s in collection){
if(s.value == valueToCheckAgainst)
return s;
}

Mainly concerned because said collection could be quite large. Order is not guaranteed (specifically, this is dealing with a ComponentDataArray in Unity ECS)

This is for retrieving tiles in a grid. Currently they're stored in a linear array, with values returned by their coordinate positions converted to the linear array's index, but I'm trying to think of a solution that would work if each tile was an entity, and the order of entities could constantly be changing.

One thing I just thought of, have a native hash map that I can check against that indexes by the position. Bonus, saves the trouble of having to convert

@lyon Since we’re talking about spatial data and I _think_ you’re trying to generalize this outside of storing the entire 2D grid, a quadtree is probably what you’re looking for: en.m.wikipedia.org/wiki/Quadtr

Some other possible approaches here are B+ trees (for entity-relational data) or applying a Bloom filter to the entire set if it is very large. If your grid is very small, using a hash approach like you mention is also optimal.

Sign in to participate in the conversation
Awoo Space

Awoo.space is a Mastodon instance where members can rely on a team of moderators to help resolve conflict, and limits federation with other instances using a specific access list to minimize abuse.

While mature content is allowed here, we strongly believe in being able to choose to engage with content on your own terms, so please make sure to put mature and potentially sensitive content behind the CW feature with enough description that people know what it's about.

Before signing up, please read our community guidelines. While it's a very broad swath of topics it covers, please do your best! We believe that as long as you're putting forth genuine effort to limit harm you might cause – even if you haven't read the document – you'll be okay!