Skip to content

Commit 95ffdec

Browse files
author
LoneWandererProductions
committed
smaller cleanups
1 parent 277e185 commit 95ffdec

2 files changed

Lines changed: 16 additions & 47 deletions

File tree

ExtendedSystemObjects/ExtendedDictionary.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ public static bool IsValueDistinct<TKey, TValue>(this Dictionary<TKey, TValue> d
229229
/// <exception cref="ValueNotFoundException"><paramref name="value" /> not found.</exception>
230230
public static TKey GetFirstKeyByValue<TKey, TValue>(this IDictionary<TKey, TValue> dic, TValue value)
231231
{
232-
foreach (var pair in dic.Where(pair => value.Equals(pair.Value)))
232+
foreach (var pair in dic)
233233
{
234-
return pair.Key;
234+
if (EqualityComparer<TValue>.Default.Equals(pair.Value, value))
235+
return pair.Key;
235236
}
236237

237238
throw new ValueNotFoundException(SharedResources.ErrorValueNotFound);

ExtendedSystemObjects/MultiArray.cs

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
using System;
15+
using System.Runtime.InteropServices;
1516
using System.Text;
1617
using ExtendedSystemObjects.Helper;
1718

@@ -103,20 +104,15 @@ public static unsafe string ToText<TValue>(this TValue[,] array) where TValue :
103104
/// <returns>Copy of the called array</returns>
104105
public static unsafe TValue[,] Duplicate<TValue>(this TValue[,] array) where TValue : unmanaged
105106
{
106-
// allocates/creates a duplicate of a matrix.
107-
var result = new TValue[array.GetLength(0), array.GetLength(1)];
107+
var rows = array.GetLength(0);
108+
var cols = array.GetLength(1);
109+
var result = new TValue[rows, cols];
108110

109-
fixed (TValue* one = result, two = array)
111+
fixed (TValue* src = array, dest = result)
110112
{
111-
for (var i = 0; i < array.GetLength(0); i++)
112-
for (var j = 0; j < array.GetLength(1); j++)
113-
{
114-
var cursor = i + (j * array.GetLength(1));
115-
116-
one[cursor] = two[cursor];
117-
}
113+
long bytes = (long)rows * cols * sizeof(TValue);
114+
Buffer.MemoryCopy(src, dest, bytes, bytes);
118115
}
119-
120116
return result;
121117
}
122118

@@ -127,32 +123,12 @@ public static unsafe string ToText<TValue>(this TValue[,] array) where TValue :
127123
/// <param name="array">The array.</param>
128124
/// <param name="compare">The compare target.</param>
129125
/// <returns>Equal or not</returns>
130-
public static unsafe bool Equal<TValue>(this TValue[,] array, TValue[,] compare) where TValue : unmanaged
126+
public static bool Equal<TValue>(this TValue[,] array, TValue[,] compare) where TValue : unmanaged
131127
{
132-
if (array.GetLength(0) != compare.GetLength(0))
133-
{
134-
return false;
135-
}
128+
if (array.GetLength(0) != compare.GetLength(0) ||
129+
array.GetLength(1) != compare.GetLength(1)) return false;
136130

137-
if (array.GetLength(1) != compare.GetLength(1))
138-
{
139-
return false;
140-
}
141-
142-
var length = array.GetLength(0) * array.GetLength(1);
143-
144-
fixed (TValue* one = array, two = compare)
145-
{
146-
for (var i = 0; i < length; ++i)
147-
{
148-
if (!one[i].Equals(two[i]))
149-
{
150-
return false;
151-
}
152-
}
153-
}
154-
155-
return true;
131+
return array.ToSpan().SequenceEqual(compare.ToSpan());
156132
}
157133

158134
/// <summary>
@@ -161,17 +137,9 @@ public static unsafe bool Equal<TValue>(this TValue[,] array, TValue[,] compare)
161137
/// <typeparam name="TValue">The type of the value.</typeparam>
162138
/// <param name="array">The array.</param>
163139
/// <returns>A Multi array as span Type.</returns>
164-
public static unsafe Span<TValue> ToSpan<TValue>(this TValue[,] array) where TValue : unmanaged
140+
public static Span<TValue> ToSpan<TValue>(this TValue[,] array) where TValue : unmanaged
165141
{
166-
var length = array.GetLength(0) * array.GetLength(1);
167-
Span<TValue> result;
168-
169-
fixed (TValue* a = array)
170-
{
171-
result = new Span<TValue>(a, length);
172-
}
173-
174-
return result;
142+
return MemoryMarshal.CreateSpan(ref array[0, 0], array.Length);
175143
}
176144

177145
/// <summary>

0 commit comments

Comments
 (0)