Skip to content

Commit

Permalink
Minor fixes.
Browse files Browse the repository at this point in the history
Made result of Get(Rectangle) only containing unique Ts.
  • Loading branch information
psilo committed Apr 3, 2016
1 parent e90c550 commit 7d406f0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
8 changes: 4 additions & 4 deletions CollisionGrid/CollisionGrid.Point.cs
Expand Up @@ -98,7 +98,7 @@ private void AddToGrid(T item, Point cell)
private void AddToItems(T item, Point cell)
{
List<Point> pl;
Items.TryGetValue(item, out pl);
ItemDictionary.TryGetValue(item, out pl);
if (pl == null)
{
if (ListOfPointQueue.Count > 0)
Expand All @@ -110,7 +110,7 @@ private void AddToItems(T item, Point cell)
pl = new List<Point>();
}
pl.Add(cell);
Items.Add(item, pl);
ItemDictionary.Add(item, pl);
}
else
{
Expand Down Expand Up @@ -139,14 +139,14 @@ public void Remove(Point cell)
foreach (T i in l)
{
List<Point> pl;
Items.TryGetValue(i, out pl);
ItemDictionary.TryGetValue(i, out pl);
if (pl != null)
{
pl.Remove(c);
if (pl.Count == 0)
{
ListOfPointQueue.Enqueue(pl);
Items.Remove(i);
ItemDictionary.Remove(i);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion CollisionGrid/CollisionGrid.Rectangle.cs
Expand Up @@ -43,7 +43,13 @@ public T[] Get(Rectangle aabb)
result.Clear();
foreach (Point p in lop)
{
result.AddRange(Get(p));
foreach (T i in Get(p))
{
if (!result.Contains(i))
{
result.Add(i);
}
}
}
return result.ToArray();
}
Expand Down
38 changes: 22 additions & 16 deletions CollisionGrid/CollisionGrid.cs
Expand Up @@ -33,7 +33,7 @@ public partial class CollisionGrid<T>
public int NumberOfCellsY { get; private set; }

private Dictionary<Point, List<T>> Grid { get; set; }
private Dictionary<T, List<Point>> Items { get; set; }
private Dictionary<T, List<Point>> ItemDictionary { get; set; }
private Queue<List<Point>> ListOfPointQueue { get; set; }
private Queue<List<T>> ListOfItemQueue { get; set; }

Expand All @@ -46,7 +46,7 @@ public CollisionGrid(float width, float height, int numberOfCellsX, int numberOf
CellWidth = Width/NumberOfCellsX;
CellHeight = Height/NumberOfCellsY;

Items = new Dictionary<T, List<Point>>();
ItemDictionary = new Dictionary<T, List<Point>>();
Grid = new Dictionary<Point, List<T>>();

ListOfPointQueue = new Queue<List<Point>>();
Expand All @@ -58,7 +58,7 @@ public Point[] Get(T item)
lock (lockObject)
{
List<Point> pl;
Items.TryGetValue(item, out pl);
ItemDictionary.TryGetValue(item, out pl);
if (pl == null)
{
return new Point[0];
Expand All @@ -76,7 +76,7 @@ public void Remove(T item)
lock (lockObject)
{
List<Point> pl;
Items.TryGetValue(item, out pl);
ItemDictionary.TryGetValue(item, out pl);
if (pl == null)
{
return;
Expand All @@ -89,7 +89,7 @@ public void Remove(T item)

pl.Clear();
ListOfPointQueue.Enqueue(pl);
Items.Remove(item);
ItemDictionary.Remove(item);
}
}

Expand All @@ -102,25 +102,31 @@ private void RemoveFromGrid(T item, Point cell)
tl.Remove(item);
if (tl.Count == 0)
{
if (ListOfItemQueue.Count == 0)
{
Console.Out.WriteLine("Yippie!");
}
ListOfItemQueue.Enqueue(tl);
Grid.Remove(cell);
}
}
}

public IEnumerable<T> AllItems()
public IEnumerable<T> Items
{
return Items.Keys;
get { return ItemDictionary.Keys; }
}

public IEnumerable<Point> AllOccupiedCells()
public IEnumerable<Point> OccupiedCells
{
return Grid.Keys;
}
get { return Grid.Keys; }
}

public int OccupiedCellCount
{
get { return ItemDictionary.Keys.Count; }
}

public int ItemCount
{
get { return Grid.Keys.Count; }
}

private Rectangle Rectangle(Rect rect)
{
Expand Down Expand Up @@ -177,8 +183,8 @@ public void Dispose()
ListOfItemQueue = null;
Grid.Clear();
Grid = null;
Items.Clear();
Items = null;
ItemDictionary.Clear();
ItemDictionary = null;
}
}
}
Expand Down

0 comments on commit 7d406f0

Please sign in to comment.