mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
SortedSet<T>.Trim extension
This commit is contained in:
parent
4d891fc5a1
commit
590c26d807
@ -53,5 +53,16 @@ public static bool TryDequeue<T>(this Queue<T> source, out T element)
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
// helper function to trim a set to a max length
|
||||
public static void Trim<T>(this SortedSet<T> set, int maxLength)
|
||||
{
|
||||
// make sure we don't deadlock for negative length
|
||||
if (maxLength < 0) return;
|
||||
|
||||
// remove while count > max
|
||||
while (set.Count > maxLength)
|
||||
set.Remove(set.Max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,5 +22,31 @@ public void CopyToList()
|
||||
source.CopyTo(destination);
|
||||
Assert.That(destination.SequenceEqual(source), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SortedSet_Trim()
|
||||
{
|
||||
SortedSet<int> set = new SortedSet<int>{1, 2, 3, 4, 5};
|
||||
|
||||
// larger than count
|
||||
set.Trim(6);
|
||||
Assert.That(set.SequenceEqual(new []{1, 2, 3, 4, 5}));
|
||||
|
||||
// exactly count
|
||||
set.Trim(5);
|
||||
Assert.That(set.SequenceEqual(new []{1, 2, 3, 4, 5}));
|
||||
|
||||
// smaller than count
|
||||
set.Trim(3);
|
||||
Assert.That(set.SequenceEqual(new []{1, 2, 3}));
|
||||
|
||||
// negative should not deadlock
|
||||
set.Trim(-1);
|
||||
Assert.That(set.SequenceEqual(new []{1, 2, 3}));
|
||||
|
||||
// zero
|
||||
set.Trim(0);
|
||||
Assert.That(set.SequenceEqual(new int[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user