-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathHelpers.cs
47 lines (40 loc) · 1.43 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections.Generic;
using DataStructures.Lists;
namespace Algorithms.Common;
public static class Helpers
{
/// <summary>
/// Swaps two values in an IList<T> collection given their indexes.
/// </summary>
public static void Swap<T>(this IList<T> list, int firstIndex, int secondIndex)
{
if (list.Count < 2 || firstIndex == secondIndex) //This check is not required but Partition function may make many calls so its for perf reason
return;
var temp = list[firstIndex];
list[firstIndex] = list[secondIndex];
list[secondIndex] = temp;
}
/// <summary>
/// Swaps two values in an ArrayList<T> collection given their indexes.
/// </summary>
public static void Swap<T>(this ArrayList<T> list, int firstIndex, int secondIndex)
{
if (list.Count < 2 || firstIndex == secondIndex) //This check is not required but Partition function may make many calls so its for perf reason
return;
var temp = list[firstIndex];
list[firstIndex] = list[secondIndex];
list[secondIndex] = temp;
}
/// <summary>
/// Populates a collection with a specific value.
/// </summary>
public static void Populate<T>(this IList<T> collection, T value)
{
if (collection == null)
return;
for (int i = 0; i < collection.Count; i++)
{
collection[i] = value;
}
}
}