forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
97 lines (82 loc) · 2.9 KB
/
Extensions.cs
File metadata and controls
97 lines (82 loc) · 2.9 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
namespace TensorStack.Common
{
public static class Extensions
{
/// <summary>
/// Converts to long.
/// </summary>
/// <param name="array">The array.</param>
/// <returns></returns>
public static long[] ToLong(this ReadOnlySpan<int> array)
{
return Array.ConvertAll(array.ToArray(), Convert.ToInt64);
}
/// <summary>
/// Converts the string representation of a number to an integer.
/// </summary>
/// <param name="array">The array.</param>
/// <returns></returns>
public static int[] ToInt(this long[] array)
{
return Array.ConvertAll(array, Convert.ToInt32);
}
/// <summary>
/// Converts to intsafe.
/// </summary>
/// <param name="array">The array.</param>
/// <returns></returns>
/// <exception cref="OverflowException">$"Value {value} at index {i} is outside the range of an int.</exception>
public static int[] ToIntSafe(this long[] array)
{
var result = GC.AllocateUninitializedArray<int>(array.Length);
for (int i = 0; i < array.Length; i++)
{
long value = array[i];
if (value < int.MinValue || value > int.MaxValue)
value = 0;
result[i] = (int)value;
}
return result;
}
/// <summary>
/// Converts to long.
/// </summary>
/// <param name="array">The array.</param>
/// <returns></returns>
public static long[] ToLong(this int[] array)
{
return Array.ConvertAll(array, Convert.ToInt64);
}
/// <summary>
/// Determines whether the the source sequence is null or empty
/// </summary>
/// <typeparam name="TSource">Type of elements in <paramref name="source" /> sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <returns>
/// <c>true</c> if the source sequence is null or empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsNullOrEmpty<TSource>(this IEnumerable<TSource> source)
{
return source == null || !source.Any();
}
/// <summary>
/// Get the item at the specifed index.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <param name="item">The item.</param>
/// <returns>System.Int32.</returns>
public static int IndexOf<T>(this IReadOnlyList<T> list, T item) where T : IEquatable<T>
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Equals(item))
return i;
}
return -1;
}
}
}