Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CollectionExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 namespace SiliconStudio.Core.Extensions
7 {
8  /// <summary>
9  /// An extension class for various types of collection.
10  /// </summary>
11  public static class CollectionExtensions
12  {
13  /// <summary>
14  /// Remove an item by swapping it with the last item and removing it from the last position. This function prevents to shift values from the list on removal but does not maintain order.
15  /// </summary>
16  /// <param name="list">The list.</param>
17  /// <param name="item">The item to remove.</param>
18  public static void SwapRemove<T>(this IList<T> list, T item)
19  {
20  int index = list.IndexOf(item);
21  if (index < 0)
22  return;
23 
24  list.SwapRemoveAt(index);
25  }
26 
27  /// <summary>
28  /// Remove an item by swapping it with the last item and removing it from the last position. This function prevents to shift values from the list on removal but does not maintain order.
29  /// </summary>
30  /// <param name="list">The list.</param>
31  /// <param name="index">Index of the item to remove.</param>
32  public static void SwapRemoveAt<T>(this IList<T> list, int index)
33  {
34  if (index < 0 || index >= list.Count) throw new ArgumentOutOfRangeException("index");
35 
36  if (index < list.Count - 1)
37  {
38  list[index] = list[list.Count - 1];
39  }
40 
41  list.RemoveAt(list.Count - 1);
42  }
43  }
44 }
An extension class for various types of collection.