Modernizing double loops in C#?
In C#, concerning an IEnumerable, is there a better way to handle searching? The code is currently using a double for loop to search in a "radius" around the character.
I was thinking something along the lines of:
location.Objects is a SerializableDictionary<Vector2, object>
Is it possible to use a Where()/Intersects()/Any()/other()? Maybe by testing overlap with a Rectangle?
Rectangle bubba = new Rectangle((int)location.X, (int)location.Y, radius, radius);
IEnumerable<T> shortlist = location.Objects.Where(?????);
9 Replies
is this in some specific engine?
what do you expect the maximum radius to be?
Probably no more than 10. It's for a StarewValley mod, but I'm just trying to figure out C# better with this excercise. 🙂
the fix would be to store the objects more efficiently to begin with, like in a quadtree
but you probably don't have control over that
is there an observed performance issue with the current solution?
Or possibly a way to enumerate our "square of coordinates" without the double loop.
Nope! 🙂 As I said, I'm just interested in different ways to do this.
the double loop in of itself isn't a bad thing, you're going to be searching the same number of cells regardless
unless you completely change the search method which would require changing how the engine stores objects
Just thought it might be more readable with some "where" or "in" or "intersect"
the only major problem i'd fix is using
TryGetValue
instead of containskey and indexing
so you cut your lookups in half
besides that i don't see much point in code golfing with linqNot the answer to this mental excercise, but thanks for the tip! Much appreciated
Jajajajajaja
Generate vectors with a generator:
There you go.
Linq Golfing.