Skip to content

Commit

Permalink
attempt to fix SequenceExtensions.ElementsAt (it didn't quite work)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 8, 2024
1 parent af62b8b commit 722ee63
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions CSharp.Nixill/src/Utils/Extensions/SequenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public static IEnumerable<T> ElementsAt<T>(this IEnumerable<T> items, params Ind

List<int> toStoreNegative = indices
.Where(i => i.IsFromEnd)
.Select(i => i.Value)
.Select(i => i.GetOffset(buffer.BufferSize))
.Distinct()
.Order()
.ToList();
Dictionary<int, T> elementsAtNegative = [];

foreach ((T item, int index) in buffer.Reverse().WithIndex())
foreach ((T item, int index) in buffer.WithIndex())
{
if (index + 1 == toStoreNegative[0])
{
Expand All @@ -153,7 +153,7 @@ public static IEnumerable<T> ElementsAt<T>(this IEnumerable<T> items, params Ind
while (indexes.Count > 0)
{
Index i = indexes.Pop();
if (i.IsFromEnd) yield return elementsAtNegative[i.Value];
if (i.IsFromEnd) yield return elementsAtNegative[i.GetOffset(buffer.BufferSize)];
else yield return elementsAt[i.Value];
}
}
Expand Down Expand Up @@ -267,7 +267,7 @@ static void MakeListAdd<T>(ref List<T>? list, T item)
list.Add(item);
}

static IEnumerable<T> ExceptElementsAt<T>(IEnumerable<T> items, params Index[] indices)
public static IEnumerable<T> ExceptElementsAt<T>(this IEnumerable<T> items, params Index[] indices)
{
List<Index> positiveIndices = indices.Where(i => !i.IsFromEnd).OrderBy(i => i.Value).Distinct().ToList();
List<Index> negativeIndices = indices.Where(i => i.IsFromEnd).OrderByDescending(i => i.Value).Distinct().ToList();
Expand All @@ -279,7 +279,7 @@ static IEnumerable<T> ExceptElementsAt<T>(IEnumerable<T> items, params Index[] i
(bool bumped, (T bumpedItem, int bumpedIndex)) = buffer.Add((item, index));
if (bumped)
{
if (bumpedIndex == positiveIndices[0].Value)
if (positiveIndices.Count != 0 && bumpedIndex == positiveIndices[0].Value)
{
positiveIndices.RemoveAt(0);
}
Expand All @@ -292,7 +292,7 @@ static IEnumerable<T> ExceptElementsAt<T>(IEnumerable<T> items, params Index[] i

foreach (((T item, int wrongIndex), int index) in buffer.WithIndex())
{
if (index == negativeIndices[0].GetOffset(buffer.BufferSize))
if (negativeIndices.Count != 0 && index == negativeIndices[0].GetOffset(buffer.BufferSize))
{
negativeIndices.Pop();
}
Expand Down

0 comments on commit 722ee63

Please sign in to comment.