forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Enums.cs
66 lines (62 loc) · 2.34 KB
/
Enums.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
using System;
using System.Collections.Generic;
namespace TweakScale
{
internal abstract class Enums<T> where T : class
{
/// <summary>
/// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
/// </summary>
/// <typeparam name="TEnumType">The type of the enumeration.</typeparam>
/// <param name="value">A string containing the name or value to convert.</param>
/// <exception cref="System.ArgumentNullException">value is null</exception>
/// <exception cref="System.ArgumentException">value is either an empty string or only contains white space.-or- value is a name, but not one of the named constants defined for the enumeration.</exception>
/// <exception cref="System.OverflowException">value is outside the range of the underlying type of EnumType</exception>
/// <returns>An object of type enumType whose value is represented by value.</returns>
static public TEnumType Parse<TEnumType>(string value) where TEnumType : T
{
return (TEnumType)Enum.Parse(typeof(TEnumType), value);
}
}
abstract class Enums : Enums<Enum>
{
}
static partial class ExtensionMethods
{
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> list)
{
var enumerator = list.GetEnumerator();
if (!enumerator.MoveNext())
yield break;
var curr = enumerator.Current;
while (enumerator.MoveNext())
{
yield return curr;
curr = enumerator.Current;
}
}
public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> list, int n)
{
var enumerator = list.GetEnumerator();
var buffer = new T[n];
var idx = 0;
while (enumerator.MoveNext() && idx < n)
{
buffer[idx] = enumerator.Current;
idx++;
}
idx = 0;
do
{
yield return buffer[idx];
buffer[idx] = enumerator.Current;
idx++;
if (idx >= n)
{
idx = 0;
}
}
while (enumerator.MoveNext());
}
}
}