Collections
.Net has already implemented the most common Collections, Nez adds several flexible collections, which can help you optimize your game.
Deque#
The deque is a Queue where you can add and remove items at the front as well as at the end with a time complexity of O(1).
Usage#
Add and AddBack#
The Add and AddBack functions allows you to add Items to the back of the Deque
The queue now looks like this
back [3,2,1] front
If we had executed this instead
AddFront#
The AddFront function allows you to add Items to the front of the Deque.
The queue now looks like this
back [1,2,3] front
If we had executed this instead
Implementation#
The Deque is implemented with a circular array
The following insertions
Result in the following buffer
| Index | 0 | 1 | 2 | 3 | 4 | 5 | .. | 15 |
|---|---|---|---|---|---|---|---|---|
| Value | a | b | c |
If we then run RemoveFront
The buffer will look like this
| Index | 0 | 1 | 2 | 3 | 4 | 5 | .. | 15 |
|---|---|---|---|---|---|---|---|---|
| Value | b | c |
If we then run AddFront twice
the front index continues on the other side of the buffer
The buffer will look like this
| Index | 0 | 1 | 2 | 3 | 4 | 5 | .. | 15 |
|---|---|---|---|---|---|---|---|---|
| Value | d | b | c | e |
By keeping track of the front and the back, the items do not have to be continually shifted around, which makes the add and remove complexity O(1).
FastList#
The FastList is a wrapper around an Array that auto-expands it when it reaches capacity.
Usage#
Adding items#
You can add items with Add
And multiple items at once with AddRange
Removing items#
You can remove items based on value.
This requires traversing the list to find the item and shifting which results in a complexity of O(n)
You can also remove items based on index, but this still requires the elements to be shifted which results in a complexity of O(n).
The fastest method is RemoveAtWithSwap. Here the item is swapped with the item at the end of the list and then deleted, which results in a complexity of O(1).
The disadvantage of this method is that the order is no longer correct.
Iterating#
When iterating the List, it is important that you use FastList.Length instead of the Buffer length
Pair#
A Pair is a simple mutable structure for managing a two objects
Usage#
Creating a pair
Unlike Tuples, Pairs are mutable
You can use the Clear method to set pair items to null
Alternatives#
Tuple
As an alternative to Pair, you can also use the dotnet Tuples
One limitation of Tuples is that they are immutable.
ValueTuples
You can also use ValueTuples like this.
Or use the C# 7.0 declaration
ValueTuples are value types though, so they are passed by value instead of reference. You can partially get around this by using the ref keyword.
Overview for each situation
| Mutable | Immutable | |
|---|---|---|
| ReferenceType | Pair | Tuple |
| ValueType | ValueTuple |
PriorityQueue#
The PriorityQueue is a data structure that allows you to prioritize elements very efficiently. This is useful for example for algorithms like A* and Dijkstra's Shortest Path.
Usage#
For this example, I am using SimplePriorityQueue.
The first parameter of Enqueue is the value. The second parameter is the priority.
For this example, I'll use the same value.
As you can see, the values are coming out sorted from the PriorityQueue
For understanding the internal workings of the PriorityQueue, I recommend you to read this.