Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Add a rolling queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Jan 20, 2024
1 parent 790c875 commit caa9dc8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Common/Collections/RollingQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections;

namespace Voxel.Common.Collections;

public class RollingQueue<T> : IEnumerable<HashSet<T>>, IEnumerable {
public HashSet<T> this[int index] {
get => CreateIfEmpty(index);
set => AppendEach(index, value);
}

private Dictionary<int, HashSet<T>> internalHolder = [];

public HashSet<T> Shift() {
var output = this[0];

var next = new Dictionary<int, HashSet<T>>();

foreach (var elem in internalHolder) {
var newKey = elem.Key-1;
if (newKey >= 0)
next[newKey] = elem.Value;
}

internalHolder = next;

return output;
}

public void Append(int index, T value)
=> this[index].Add(value);

public void AppendEach(int index, HashSet<T> value)
=> this[index].UnionWith(value);

public IEnumerator<HashSet<T>> GetEnumerator()
=> throw new NotImplementedException();

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();

private HashSet<T> CreateIfEmpty(int index) {
if (!internalHolder.ContainsKey(index))
internalHolder[index] = [];
return internalHolder[index];
}
}

0 comments on commit caa9dc8

Please sign in to comment.