-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathHelpers.cs
69 lines (59 loc) · 2.42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using DataStructures.Lists;
namespace DataStructures.Common
{
public static class Helpers
{
/// <summary>
/// Swap 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>
/// Swap 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>
/// Centralize a text.
/// </summary>
public static string PadCenter(this string text, int newWidth, char fillerCharacter = ' ')
{
if (string.IsNullOrEmpty(text))
return text;
int length = text.Length;
int charactersToPad = newWidth - length;
if (charactersToPad < 0) throw new ArgumentException("New width must be greater than string length.", "newWidth");
int padLeft = charactersToPad / 2 + charactersToPad % 2;
//add a space to the left if the string is an odd number
int padRight = charactersToPad / 2;
return new String(fillerCharacter, padLeft) + text + new String(fillerCharacter, padRight);
}
/// <summary>
/// Populates the specified two-dimensional with a default value.
/// </summary>
public static void Populate<T>(this T[,] array, int rows, int columns, T defaultValue = default(T))
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
array[i, j] = defaultValue;
}
}
}
}
}