Singularity/Library/PackageCache/com.unity.shadergraph@12.1.11/Editor/Util/ListUtilities.cs

52 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-05-06 14:45:45 -04:00
using System;
using System.Collections.Generic;
namespace UnityEditor.ShaderGraph
{
static class ListUtilities
{
// Ideally, we should build a non-yield return, struct version of Slice
public static IEnumerable<T> Slice<T>(this List<T> list, int start, int end)
{
for (int i = start; i < end; i++)
yield return list[i];
}
public static int RemoveAllFromRange<T>(this List<T> list, Predicate<T> match, int startIndex, int count)
{
// match behavior of RemoveRange
if ((startIndex < 0) || (count < 0))
throw new ArgumentOutOfRangeException();
int endIndex = startIndex + count;
if (endIndex > list.Count)
throw new ArgumentException();
int readIndex = startIndex;
int writeIndex = startIndex;
while (readIndex < endIndex)
{
T element = list[readIndex];
bool remove = match(element);
if (!remove)
{
// skip some work if nothing removed (especially if T is a large struct)
if (writeIndex < readIndex)
list[writeIndex] = element;
writeIndex++;
}
readIndex++;
}
// once we're done, we can remove the entries at the end in one operation
int numberRemoved = readIndex - writeIndex;
if (numberRemoved > 0)
{
list.RemoveRange(writeIndex, numberRemoved);
}
return numberRemoved;
}
}
}