This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
790c875
commit caa9dc8
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |