Skip to content

Commit cfc6fd1

Browse files
committed
Fixes
1 parent 6e19457 commit cfc6fd1

File tree

4 files changed

+90
-40
lines changed

4 files changed

+90
-40
lines changed

Hexa.NET.Utilities/Hexa.NET.Utilities.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<PropertyGroup>
2020
<PackageId>Hexa.NET.Utilities</PackageId>
2121
<AssemblyVersion>1.0.0</AssemblyVersion>
22-
<PackageVersion>2.1.5</PackageVersion>
22+
<PackageVersion>2.1.6</PackageVersion>
2323
<Authors>Juna</Authors>
2424
<AssemblyName>Hexa.NET.Utilities</AssemblyName>
2525
<PackageProjectUrl>https://github.com/HexaEngine/Hexa.NET.Utilities</PackageProjectUrl>

Hexa.NET.Utilities/UnsafeDictionary.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public int Capacity
166166
{
167167
Entry* tombstone = null;
168168
uint index = (uint)(hashCode % capacity);
169-
bool iterate = true;
170-
while (iterate)
169+
bool exit = false;
170+
while (true)
171171
{
172172
Entry* entry = &entries[index];
173173
if (!entry->IsFilled)
@@ -195,8 +195,12 @@ public int Capacity
195195
index++; // this is faster than %.
196196
if (index == capacity)
197197
{
198-
iterate = false;
198+
if (exit)
199+
{
200+
break;
201+
}
199202
index = 0;
203+
exit = true;
200204
}
201205
}
202206

Hexa.NET.Utilities/UnsafeList.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public unsafe struct UnsafeList<T> : IFreeable, IEnumerable<T>, IList<T>, IReadO
1313
{
1414
private const int DefaultCapacity = 4;
1515

16-
private T* pointer;
16+
private T* items;
1717
private int size;
1818
private int capacity;
1919

@@ -32,7 +32,7 @@ public struct Enumerator : IEnumerator<T>
3232
/// <param name="list">The <see cref="UnsafeList{T}"/> to enumerate.</param>
3333
internal Enumerator(UnsafeList<T> list)
3434
{
35-
pointer = list.pointer;
35+
pointer = list.items;
3636
size = list.size;
3737
currentIndex = -1;
3838
}
@@ -120,7 +120,7 @@ public UnsafeList(int capacity)
120120
/// <summary>
121121
/// Gets the pointer to the underlying data array.
122122
/// </summary>
123-
public readonly T* Data => pointer;
123+
public readonly T* Data => items;
124124

125125
/// <summary>
126126
/// Gets a value indicating whether the list is empty.
@@ -130,12 +130,12 @@ public UnsafeList(int capacity)
130130
/// <summary>
131131
/// Gets a pointer to the first element in the list.
132132
/// </summary>
133-
public readonly T* Front => pointer;
133+
public readonly T* Front => items;
134134

135135
/// <summary>
136136
/// Gets a pointer to the last element in the list.
137137
/// </summary>
138-
public readonly T* Back => &pointer[size - 1];
138+
public readonly T* Back => &items[size - 1];
139139

140140
/// <summary>
141141
/// Gets or sets the capacity of the list.
@@ -145,14 +145,14 @@ public int Capacity
145145
readonly get => capacity;
146146
set
147147
{
148-
if (pointer == null)
148+
if (items == null)
149149
{
150-
pointer = AllocT<T>(value);
150+
items = AllocT<T>(value);
151151
capacity = value;
152152
Erase();
153153
return;
154154
}
155-
pointer = ReAllocT(pointer, value);
155+
items = ReAllocT(items, value);
156156
capacity = value;
157157
size = capacity < size ? capacity : size;
158158
}
@@ -166,9 +166,9 @@ public int Capacity
166166
public T this[int index]
167167
{
168168
[MethodImpl(MethodImplOptions.AggressiveInlining)]
169-
get => pointer[index];
169+
get => items[index];
170170
[MethodImpl(MethodImplOptions.AggressiveInlining)]
171-
set => pointer[index] = value;
171+
set => items[index] = value;
172172
}
173173

174174
/// <summary>
@@ -198,7 +198,7 @@ public T At(int index)
198198
/// <returns>A pointer to the element at the specified index.</returns>
199199
public T* GetPointer(int index)
200200
{
201-
return &pointer[index];
201+
return &items[index];
202202
}
203203

204204
/// <summary>
@@ -240,7 +240,7 @@ private void Grow(int capacity)
240240
[MethodImpl(MethodImplOptions.AggressiveInlining)]
241241
public void Reserve(int capacity)
242242
{
243-
if (this.capacity < capacity || pointer == null)
243+
if (this.capacity < capacity || items == null)
244244
{
245245
Grow(capacity);
246246
}
@@ -273,7 +273,7 @@ public void Resize(int newSize)
273273
/// </summary>
274274
public readonly void Erase()
275275
{
276-
ZeroMemoryT(pointer, capacity);
276+
ZeroMemoryT(items, capacity);
277277
}
278278

279279
/// <summary>
@@ -287,7 +287,7 @@ public void PushBack(T item)
287287
if ((uint)size < (uint)capacity)
288288
{
289289
this.size = size + 1;
290-
pointer[size] = item;
290+
items[size] = item;
291291
}
292292
else
293293
{
@@ -302,7 +302,7 @@ private void AddWithResize(T item)
302302
int size = this.size;
303303
Grow(size + 1);
304304
this.size = size + 1;
305-
pointer[size] = item;
305+
items[size] = item;
306306
}
307307

308308
/// <summary>
@@ -321,7 +321,7 @@ public void Add(T item)
321321
[MethodImpl(MethodImplOptions.AggressiveInlining)]
322322
public void PopBack()
323323
{
324-
pointer[size - 1] = default;
324+
items[size - 1] = default;
325325
size--;
326326
}
327327

@@ -336,7 +336,7 @@ public void AppendRange(T[] values)
336336

337337
fixed (T* src = values)
338338
{
339-
Memcpy(src, &pointer[size], capacity * sizeof(T), values.Length * sizeof(T));
339+
Memcpy(src, &items[size], capacity * sizeof(T), values.Length * sizeof(T));
340340
}
341341
size += values.Length;
342342
}
@@ -351,7 +351,7 @@ public void AppendRange(T* values, int count)
351351
{
352352
Reserve(size + count);
353353

354-
Memcpy(values, &pointer[size], capacity * sizeof(T), count * sizeof(T));
354+
Memcpy(values, &items[size], capacity * sizeof(T), count * sizeof(T));
355355

356356
size += count;
357357
}
@@ -382,13 +382,13 @@ public void RemoveAt(int index)
382382
{
383383
if (index == this.size - 1)
384384
{
385-
pointer[this.size - 1] = default;
385+
items[this.size - 1] = default;
386386
this.size--;
387387
return;
388388
}
389389

390390
var size = (this.size - index) * sizeof(T);
391-
Buffer.MemoryCopy(&pointer[index + 1], &pointer[index], size, size);
391+
Buffer.MemoryCopy(&items[index + 1], &items[index], size, size);
392392
this.size--;
393393
}
394394

@@ -403,8 +403,8 @@ public void Insert(int index, T item)
403403
Reserve(this.size + 1);
404404

405405
var size = (this.size - index) * sizeof(T);
406-
Buffer.MemoryCopy(&pointer[index], &pointer[index + 1], size, size);
407-
pointer[index] = item;
406+
Buffer.MemoryCopy(&items[index], &items[index + 1], size, size);
407+
items[index] = item;
408408
this.size++;
409409
}
410410

@@ -427,7 +427,7 @@ public bool Contains(T item)
427427
{
428428
for (int i = 0; i < size; i++)
429429
{
430-
var current = pointer[i];
430+
var current = items[i];
431431

432432
if (EqualityComparer<T>.Default.Equals(current, item))
433433
{
@@ -448,7 +448,7 @@ public int IndexOf(T item)
448448
{
449449
for (int i = 0; i < size; i++)
450450
{
451-
var current = pointer[i];
451+
var current = items[i];
452452

453453
if (EqualityComparer<T>.Default.Equals(current, item))
454454
{
@@ -468,7 +468,7 @@ public int FirstIndexOf(Func<T, bool> comparer)
468468
{
469469
for (int i = 0; i < size; i++)
470470
{
471-
var current = pointer[i];
471+
var current = items[i];
472472

473473
if (comparer(current))
474474
{
@@ -484,7 +484,7 @@ public int FirstIndexOf(Func<T, bool> comparer)
484484
/// </summary>
485485
public readonly void Reverse()
486486
{
487-
new Span<T>(pointer, (int)size).Reverse();
487+
new Span<T>(items, (int)size).Reverse();
488488
}
489489

490490
/// <summary>
@@ -493,8 +493,8 @@ public readonly void Reverse()
493493
/// <param name="list">The list whose content will be moved to this list.</param>
494494
public void Move(UnsafeList<T> list)
495495
{
496-
Free(pointer);
497-
pointer = list.pointer;
496+
Free(items);
497+
items = list.items;
498498
capacity = list.capacity;
499499
size = list.size;
500500
}
@@ -530,7 +530,7 @@ public int InterlockedDecrementCounter()
530530
public int InterlockedPushBack(T value)
531531
{
532532
int index = Interlocked.Increment(ref size);
533-
pointer[index] = value;
533+
items[index] = value;
534534
return index;
535535
}
536536

@@ -542,7 +542,7 @@ public int InterlockedPushBack(T value)
542542
public int InterlockedPopBack()
543543
{
544544
int index = InterlockedDecrementCounter();
545-
pointer[index + 1] = default;
545+
items[index + 1] = default;
546546
return index;
547547
}
548548

@@ -584,9 +584,9 @@ readonly IEnumerator IEnumerable.GetEnumerator()
584584
[MethodImpl(MethodImplOptions.AggressiveInlining)]
585585
public void Release()
586586
{
587-
if (pointer != null)
587+
if (items != null)
588588
{
589-
Free(pointer);
589+
Free(items);
590590
this = default;
591591
}
592592
}
@@ -615,7 +615,7 @@ void ICollection<T>.CopyTo(T[] array, int arrayIndex)
615615
{
616616
fixed (T* dst = array)
617617
{
618-
MemcpyT(&pointer[arrayIndex], dst, array.Length);
618+
MemcpyT(&items[arrayIndex], dst, array.Length);
619619
}
620620
}
621621

@@ -626,7 +626,7 @@ bool ICollection<T>.Remove(T item)
626626

627627
public readonly Span<T> AsSpan()
628628
{
629-
return new(pointer, (int)size);
629+
return new(items, (int)size);
630630
}
631631

632632
public override readonly bool Equals(object? obj)
@@ -636,12 +636,12 @@ public override readonly bool Equals(object? obj)
636636

637637
public readonly bool Equals(UnsafeList<T> other)
638638
{
639-
return pointer == other.pointer;
639+
return items == other.items;
640640
}
641641

642642
public override readonly int GetHashCode()
643643
{
644-
return HashCode.Combine((nint)pointer);
644+
return HashCode.Combine((nint)items);
645645
}
646646

647647
public static bool operator ==(UnsafeList<T> left, UnsafeList<T> right)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Hexa.NET.Utilities
2+
{
3+
public unsafe partial class Utils
4+
{
5+
public static int StrCmp(char* a, char* b)
6+
{
7+
if (a == null)
8+
{
9+
if (b != null)
10+
{
11+
return -1;
12+
}
13+
14+
return 0;
15+
}
16+
17+
if (b == null)
18+
{
19+
return 1;
20+
}
21+
22+
while (*a != 0 && *b != 0)
23+
{
24+
if (*a != *b)
25+
{
26+
return *a - *b;
27+
}
28+
29+
a++;
30+
b++;
31+
}
32+
33+
if (*a == 0 && *b == 0)
34+
{
35+
return 0;
36+
}
37+
38+
if (*a != 0)
39+
{
40+
return 1;
41+
}
42+
43+
return -1;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)