Skip to content

Commit

Permalink
Updated license text in headers.
Browse files Browse the repository at this point in the history
Using some C# 6.0 features.
  • Loading branch information
psilo committed Apr 24, 2016
1 parent 7d406f0 commit 66b8b53
Show file tree
Hide file tree
Showing 10 changed files with 1,005 additions and 876 deletions.
27 changes: 27 additions & 0 deletions CollisionGrid.sln.DotSettings
@@ -0,0 +1,27 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">*************************************************************************** &#xD;
This is free and unencumbered software released into the public domain.&#xD;
&#xD;
Anyone is free to copy, modify, publish, use, compile, sell, or&#xD;
distribute this software, either in source code form or as a compiled&#xD;
binary, for any purpose, commercial or non-commercial, and by any&#xD;
means.&#xD;
&#xD;
In jurisdictions that recognize copyright laws, the author or authors&#xD;
of this software dedicate any and all copyright interest in the&#xD;
software to the public domain. We make this dedication for the benefit&#xD;
of the public at large and to the detriment of our heirs and&#xD;
successors. We intend this dedication to be an overt act of&#xD;
relinquishment in perpetuity of all present and future rights to this&#xD;
software under copyright law.&#xD;
&#xD;
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,&#xD;
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF&#xD;
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.&#xD;
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR&#xD;
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,&#xD;
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR&#xD;
OTHER DEALINGS IN THE SOFTWARE.&#xD;
&#xD;
For more information, please refer to &lt;http://unlicense.org&gt;&#xD;
***************************************************************************</s:String></wpf:ResourceDictionary>
344 changes: 178 additions & 166 deletions CollisionGrid/CollisionGrid.Point.cs
@@ -1,183 +1,195 @@
// ***************************************************************************
// Copyright (c) 2016 by Unterrainer Informatik OG.
// This source is licensed to Unterrainer Informatik OG.
// All rights reserved.
//
// In other words:
// YOU MUST NOT COPY, USE, CHANGE OR REDISTRIBUTE ANY ART, MUSIC, CODE OR
// OTHER DATA, CONTAINED WITHIN THESE DIRECTORIES WITHOUT THE EXPRESS
// PERMISSION OF Unterrainer Informatik OG.
// ---------------------------------------------------------------------------
// Programmer: G U,
// Created: 2016-03-30
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org>
// ***************************************************************************

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace CollisionGrid
{
public partial class CollisionGrid<T>
{
public T[] Get(Point cell)
{
lock (lockObject)
{
List<T> contents;
Grid.TryGetValue(Clamp(cell), out contents);
if (contents == null)
{
return new T[0];
}
return contents.ToArray();
}
}
public partial class CollisionGrid<T>
{
public T[] Get(Point cell)
{
lock (lockObject)
{
List<T> contents;
Grid.TryGetValue(Clamp(cell), out contents);
if (contents == null)
{
return new T[0];
}
return contents.ToArray();
}
}

/// <summary>
/// Gets the first item encountered on the given cell.
/// </summary>
/// <param name="cell">The cell to search</param>
/// <returns>The item or default(T)</returns>
public T First(Point cell)
{
lock (lockObject)
{
List<T> contents;
Grid.TryGetValue(Clamp(cell), out contents);
if (contents != null && contents.Count > 0)
{
return contents[0];
}
return default(T);
}
}
/// <summary>
/// Gets the first item encountered on the given cell.
/// </summary>
/// <param name="cell">The cell to search</param>
/// <returns>The item or default(T)</returns>
public T First(Point cell)
{
lock (lockObject)
{
List<T> contents;
Grid.TryGetValue(Clamp(cell), out contents);
if (contents != null && contents.Count > 0)
{
return contents[0];
}
return default(T);
}
}

/// <summary>
/// Adds a given item to a given cell.
/// If the cell already contains the item, it is not added a second time.
/// </summary>
/// <param name="item">The item to add</param>
/// <param name="cell">The cell to add the item to</param>
public void Add(T item, Point cell)
{
lock (lockObject)
{
Point c = Clamp(cell);
AddToGrid(item, c);
AddToItems(item, c);
}
}
/// <summary>
/// Adds a given item to a given cell.
/// If the cell already contains the item, it is not added a second time.
/// </summary>
/// <param name="item">The item to add</param>
/// <param name="cell">The cell to add the item to</param>
public void Add(T item, Point cell)
{
lock (lockObject)
{
var c = Clamp(cell);
AddToGrid(item, c);
AddToItems(item, c);
}
}

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);
}
}
}
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);
}
}
}

private void AddToItems(T item, Point cell)
{
List<Point> pl;
ItemDictionary.TryGetValue(item, out pl);
if (pl == null)
{
if (ListOfPointQueue.Count > 0)
{
pl = ListOfPointQueue.Dequeue();
}
else
{
pl = new List<Point>();
}
pl.Add(cell);
ItemDictionary.Add(item, pl);
}
else
{
if (!pl.Contains(cell))
{
pl.Add(cell);
}
}
}
private void AddToItems(T item, Point cell)
{
List<Point> pl;
ItemDictionary.TryGetValue(item, out pl);
if (pl == null)
{
if (ListOfPointQueue.Count > 0)
{
pl = ListOfPointQueue.Dequeue();
}
else
{
pl = new List<Point>();
}
pl.Add(cell);
ItemDictionary.Add(item, pl);
}
else
{
if (!pl.Contains(cell))
{
pl.Add(cell);
}
}
}

/// <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);
/// <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)
{
var 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);
}
}
}
if (l != null)
{
foreach (var 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);
}
}
}

/// <summary>
/// Removes all occurrences of the given item and re-adds it at the new given cell.
/// If the item hasn't been in the grid before, this will just add it.
/// </summary>
/// <param name="item">The item to move</param>
/// <param name="cell">The cell to move it to</param>
public void Move(T item, Point cell)
{
lock (lockObject)
{
Remove(item);
Add(item, cell);
}
}
/// <summary>
/// Removes all occurrences of the given item and re-adds it at the new given cell.
/// If the item hasn't been in the grid before, this will just add it.
/// </summary>
/// <param name="item">The item to move</param>
/// <param name="cell">The cell to move it to</param>
public void Move(T item, Point cell)
{
lock (lockObject)
{
Remove(item);
Add(item, cell);
}
}

public bool IsEmpty(Point cell)
{
lock (lockObject)
{
return Get(cell).Length == 0;
}
}
}
public bool IsEmpty(Point cell)
{
lock (lockObject)
{
return Get(cell).Length == 0;
}
}
}
}

0 comments on commit 66b8b53

Please sign in to comment.