Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Swap.Extensions.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using JetBrains.Annotations;
  5. namespace UnityEngine.Rendering
  6. {
  7. /// <summary>
  8. /// A set of extension methods for collections
  9. /// </summary>
  10. public static class SwapCollectionExtensions
  11. {
  12. /// <summary>
  13. /// Tries to remove a range of elements from the list in the given range.
  14. /// </summary>
  15. /// <param name="list">The list to remove the range</param>
  16. /// <param name="from">From index</param>
  17. /// <param name="to">To index</param>
  18. /// <param name="error">The exception raised by the implementation</param>
  19. /// <typeparam name="TValue">The value type stored on the list</typeparam>
  20. /// <returns>True if succeed, false otherwise</returns>
  21. [CollectionAccess(CollectionAccessType.ModifyExistingContent)]
  22. [MustUseReturnValue]
  23. public static bool TrySwap<TValue>([DisallowNull] this IList<TValue> list, int from, int to, [NotNullWhen(false)] out Exception error)
  24. {
  25. error = null;
  26. if (list == null)
  27. {
  28. error = new ArgumentNullException(nameof(list));
  29. }
  30. else
  31. {
  32. if (from < 0 || from >= list.Count)
  33. error = new ArgumentOutOfRangeException(nameof(from));
  34. if (to < 0 || to >= list.Count)
  35. error = new ArgumentOutOfRangeException(nameof(to));
  36. }
  37. if (error != null)
  38. return false;
  39. // https://tearth.dev/posts/performance-of-the-different-ways-to-swap-two-values/
  40. (list[to], list[from]) = (list[from], list[to]);
  41. return true;
  42. }
  43. }
  44. }