private void AddToGrid(T item, Point cell) { List <T> l; Grid.TryGetValue(cell, out l); if (l == null) { if (ListOfItemQueue.Count > 0) { l = ListOfItemQueue.Dequeue(); } else { l = new List <T>(); } l.Add(item); Grid.Add(cell, l); } else { if (!l.Contains(item)) { l.Add(item); } } }
/// <summary> /// Removes all items from the given cell. /// If the items don't occupy another cell, they are removed as well. /// </summary> /// <param name="cell">The cell to remove items from</param> public void Remove(Point cell) { lock (lockObject) { Point c = Clamp(cell); List <T> l; Grid.TryGetValue(c, out l); if (l != null) { foreach (T i in l) { List <Point> pl; ItemDictionary.TryGetValue(i, out pl); if (pl != null) { pl.Remove(c); if (pl.Count == 0) { ListOfPointQueue.Enqueue(pl); ItemDictionary.Remove(i); } } } l.Clear(); ListOfItemQueue.Enqueue(l); Grid.Remove(cell); } } }