Skip to content

Commit 09b8eab

Browse files
sync back some stuff
1 parent 579a797 commit 09b8eab

6 files changed

Lines changed: 62 additions & 46 deletions

File tree

0 Bytes
Loading
0 Bytes
Loading

ExtendedSystemObjects/ImmutableLookupMap.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,21 @@ IEnumerator IEnumerable.GetEnumerator()
125125
/// <exception cref="KeyNotFoundException">The key {key} was not found in the lookup map.</exception>
126126
public TValue Get(TKey key)
127127
{
128-
var capacity = _keys.Length;
129-
var baseHash = GetHash(key, capacity);
128+
var hash = GetHash(key, _keys.Length);
129+
var originalHash = hash;
130130

131-
for (int i = 0; i < capacity; i++)
131+
while (_keyPresence[hash])
132132
{
133-
var hash = (baseHash + i * i) % capacity;
134-
135-
if (!_keyPresence[hash])
136-
break; // Key not found
137-
138133
if (_keys[hash].Equals(key))
134+
{
139135
return _values[hash];
136+
}
137+
138+
hash = (hash + 1) % _keys.Length; // Linear probing
139+
if (hash == originalHash)
140+
{
141+
break; // Full cycle, key not found
142+
}
140143
}
141144

142145
throw new KeyNotFoundException(ExtendedSystemObjectsResources.ErrorValueNotFound);
@@ -150,21 +153,22 @@ public TValue Get(TKey key)
150153
/// <returns>The value amd bool check if it exists.</returns>
151154
public bool TryGetValue(TKey key, out TValue value)
152155
{
153-
var capacity = _keys.Length;
154-
var baseHash = GetHash(key, capacity);
156+
var hash = GetHash(key, _keys.Length);
157+
var originalHash = hash;
155158

156-
for (int i = 0; i < capacity; i++)
159+
while (_keyPresence[hash])
157160
{
158-
var hash = (baseHash + i * i) % capacity;
159-
160-
if (!_keyPresence[hash])
161-
break; // Key not found
162-
163161
if (_keys[hash].Equals(key))
164162
{
165163
value = _values[hash];
166164
return true;
167165
}
166+
167+
hash = (hash + 1) % _keys.Length; // Linear probing
168+
if (hash == originalHash)
169+
{
170+
break; // Full cycle, key not found
171+
}
168172
}
169173

170174
value = default;
@@ -182,8 +186,7 @@ public bool TryGetValue(TKey key, out TValue value)
182186
[MethodImpl(MethodImplOptions.AggressiveInlining)]
183187
private static int GetHash(TKey key, int capacity)
184188
{
185-
// Mask sign bit to avoid negative numbers
186-
return (key.GetHashCode() & 0x7FFFFFFF) % capacity;
189+
return Math.Abs(key.GetHashCode() % capacity);
187190
}
188191

189192
/// <summary>

ExtendedSystemObjects/IntArray.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

9+
// ReSharper disable UnusedMember.Global
10+
// ReSharper disable UnusedType.Global
11+
// ReSharper disable MemberCanBeInternal
12+
913
using System;
1014
using System.Runtime.CompilerServices;
1115
using System.Runtime.InteropServices;
1216

1317
namespace ExtendedSystemObjects
1418
{
19+
/// <inheritdoc />
1520
/// <summary>
1621
/// Represents a high-performance, low-overhead array of integers
1722
/// backed by unmanaged memory. Designed for performance-critical
1823
/// scenarios where garbage collection overhead must be avoided.
1924
/// </summary>
20-
/// <seealso cref="System.IDisposable" />
21-
public unsafe class IntArray : IDisposable
25+
/// <seealso cref="T:System.IDisposable" />
26+
public sealed unsafe class IntArray : IDisposable
2227
{
2328
/// <summary>
2429
/// The buffer
@@ -87,9 +92,9 @@ public void RemoveAt(int index)
8792
#if DEBUG
8893
if (index < 0 || index >= _length) throw new IndexOutOfRangeException();
8994
#endif
90-
int* ptr = (int*)_buffer;
95+
var ptr = (int*)_buffer;
9196

92-
for (int i = index; i < _length - 1; i++)
97+
for (var i = index; i < _length - 1; i++)
9398
{
9499
ptr[i] = ptr[i + 1];
95100
}
@@ -114,8 +119,8 @@ public void Resize(int newSize)
114119
/// </summary>
115120
public void Clear()
116121
{
117-
int* ptr = (int*)_buffer;
118-
for (int i = 0; i < _length; i++)
122+
var ptr = (int*)_buffer;
123+
for (var i = 0; i < _length; i++)
119124
{
120125
ptr[i] = 0;
121126
}
@@ -135,6 +140,7 @@ public void Clear()
135140
Dispose();
136141
}
137142

143+
/// <inheritdoc />
138144
/// <summary>
139145
/// Frees the unmanaged memory held by the array.
140146
/// After disposal, the instance should not be used.

ExtendedSystemObjects/IntList.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

9+
// ReSharper disable UnusedMember.Global
10+
// ReSharper disable UnusedType.Global
11+
// ReSharper disable MemberCanBePrivate.Global
12+
// ReSharper disable MemberCanBeInternal
13+
914
using System;
1015
using System.Runtime.InteropServices;
1116

1217
namespace ExtendedSystemObjects
1318
{
19+
/// <inheritdoc />
1420
/// <summary>
1521
/// A high-performance list of integers backed by unmanaged memory.
1622
/// Supports fast adding, popping, and random access with minimal overhead.
1723
/// Designed for scenarios where manual memory management is needed.
1824
/// </summary>
19-
/// <seealso cref="System.IDisposable" />
20-
public unsafe class IntList : IDisposable
25+
/// <seealso cref="T:System.IDisposable" />
26+
public sealed unsafe class IntList : IDisposable
2127
{
2228
/// <summary>
2329
/// The buffer
2430
/// </summary>
2531
private IntPtr _buffer;
2632

27-
/// <summary>
28-
/// The count
29-
/// </summary>
30-
private int _count;
31-
3233
/// <summary>
3334
/// The capacity
3435
/// </summary>
@@ -37,7 +38,7 @@ public unsafe class IntList : IDisposable
3738
/// <summary>
3839
/// Gets the number of elements contained in the <see cref="IntList"/>.
3940
/// </summary>
40-
public int Count => _count;
41+
public int Count { get; private set; }
4142

4243
/// <summary>
4344
/// Pointer to the unmanaged buffer holding the integer elements.
@@ -70,8 +71,8 @@ public void Push(int value)
7071
/// <param name="value">The integer value to add.</param>
7172
public void Add(int value)
7273
{
73-
EnsureCapacity(_count + 1);
74-
_ptr[_count++] = value;
74+
EnsureCapacity(Count + 1);
75+
_ptr[Count++] = value;
7576
}
7677

7778
/// <summary>
@@ -81,9 +82,10 @@ public void Add(int value)
8182
/// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception>
8283
public int Pop()
8384
{
84-
if (_count == 0)
85+
if (Count == 0)
8586
throw new InvalidOperationException("Stack empty");
86-
return _ptr[--_count];
87+
88+
return _ptr[--Count];
8789
}
8890

8991
/// <summary>
@@ -93,9 +95,10 @@ public int Pop()
9395
/// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception>
9496
public int Peek()
9597
{
96-
if (_count == 0)
98+
if (Count == 0)
9799
throw new InvalidOperationException("Stack empty");
98-
return _ptr[_count - 1];
100+
101+
return _ptr[Count - 1];
99102
}
100103

101104
/// <summary>
@@ -109,14 +112,14 @@ public int this[int i]
109112
get
110113
{
111114
#if DEBUG
112-
if (i < 0 || i >= _count) throw new IndexOutOfRangeException();
115+
if (i < 0 || i >= Count) throw new IndexOutOfRangeException();
113116
#endif
114117
return _ptr[i];
115118
}
116119
set
117120
{
118121
#if DEBUG
119-
if (i < 0 || i >= _count) throw new IndexOutOfRangeException();
122+
if (i < 0 || i >= Count) throw new IndexOutOfRangeException();
120123
#endif
121124
_ptr[i] = value;
122125
}
@@ -131,7 +134,7 @@ private void EnsureCapacity(int min)
131134
{
132135
if (min <= _capacity) return;
133136

134-
int newCapacity = _capacity * 2;
137+
var newCapacity = _capacity * 2;
135138
if (newCapacity < min)
136139
newCapacity = min;
137140

@@ -145,15 +148,15 @@ private void EnsureCapacity(int min)
145148
/// </summary>
146149
public void Clear()
147150
{
148-
_count = 0;
151+
Count = 0;
149152
}
150153

151154
/// <summary>
152155
/// Returns a span over the valid elements of the list.
153156
/// Allows fast, safe access to the underlying data.
154157
/// </summary>
155158
/// <returns>A <see cref="Span{Int32}"/> representing the list's contents.</returns>
156-
public Span<int> AsSpan() => new((void*)_buffer, _count);
159+
public Span<int> AsSpan() => new((void*)_buffer, Count);
157160

158161
/// <summary>
159162
/// Finalizes an instance of the <see cref="IntList"/> class, releasing unmanaged resources.
@@ -163,8 +166,9 @@ public void Clear()
163166
Dispose();
164167
}
165168

169+
/// <inheritdoc />
166170
/// <summary>
167-
/// Frees unmanaged resources used by the <see cref="IntList"/>.
171+
/// Frees unmanaged resources used by the <see cref="T:ExtendedSystemObjects.IntList" />.
168172
/// After calling this method, the instance should not be used.
169173
/// </summary>
170174
public void Dispose()
@@ -174,7 +178,8 @@ public void Dispose()
174178
Marshal.FreeHGlobal(_buffer);
175179
_buffer = IntPtr.Zero;
176180
}
177-
_count = 0;
181+
182+
Count = 0;
178183
_capacity = 0;
179184
}
180185
}

Interpreter/IrtHandleExtensionInternal.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ private void HandleInternalCommand(ExtensionCommands extension, int key)
152152
irtInternal.ProcessInput(IrtConst.InternalHelpWithParameter, command.Command);
153153
}
154154

155+
UserFeedback feedback = null;
156+
155157
//if none is available use the standard one from Internal
156158
//if not we use the provided one
157-
if (_userFeedback?.TryGetValue(extension.FeedBackId, out var feedback) != true)
159+
if (_userFeedback?.TryGetValue(extension.FeedBackId, out feedback) != true)
158160
{
159161
feedback = IrtConst.InternalFeedback[-1];
160162
}

0 commit comments

Comments
 (0)