using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
namespace UnityEngine.Rendering
{
///
/// A set of extension methods for collections
///
public static class SwapCollectionExtensions
{
///
/// Tries to remove a range of elements from the list in the given range.
///
/// The list to remove the range
/// From index
/// To index
/// The exception raised by the implementation
/// The value type stored on the list
/// True if succeed, false otherwise
[CollectionAccess(CollectionAccessType.ModifyExistingContent)]
[MustUseReturnValue]
public static bool TrySwap([DisallowNull] this IList list, int from, int to, [NotNullWhen(false)] out Exception error)
{
error = null;
if (list == null)
{
error = new ArgumentNullException(nameof(list));
}
else
{
if (from < 0 || from >= list.Count)
error = new ArgumentOutOfRangeException(nameof(from));
if (to < 0 || to >= list.Count)
error = new ArgumentOutOfRangeException(nameof(to));
}
if (error != null)
return false;
// https://tearth.dev/posts/performance-of-the-different-ways-to-swap-two-values/
(list[to], list[from]) = (list[from], list[to]);
return true;
}
}
}