|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: ExtendedSystemObjects |
| 4 | + * FILE: ExtendedSystemObjects/IntList.cs |
| 5 | + * PURPOSE: A high-performance List implementation with reduced features. Limited to integer Values. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | + |
| 12 | +namespace ExtendedSystemObjects |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// A high-performance list of integers backed by unmanaged memory. |
| 16 | + /// Supports fast adding, popping, and random access with minimal overhead. |
| 17 | + /// Designed for scenarios where manual memory management is needed. |
| 18 | + /// </summary> |
| 19 | + /// <seealso cref="System.IDisposable" /> |
| 20 | + public unsafe class IntList : IDisposable |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The buffer |
| 24 | + /// </summary> |
| 25 | + private IntPtr _buffer; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The count |
| 29 | + /// </summary> |
| 30 | + private int _count; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The capacity |
| 34 | + /// </summary> |
| 35 | + private int _capacity; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets the number of elements contained in the <see cref="IntList"/>. |
| 39 | + /// </summary> |
| 40 | + public int Count => _count; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Pointer to the unmanaged buffer holding the integer elements. |
| 44 | + /// </summary> |
| 45 | + private int* _ptr; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="IntList"/> class with the specified initial capacity. |
| 49 | + /// </summary> |
| 50 | + /// <param name="initialCapacity">The initial number of elements the list can hold without resizing. Default is 16.</param> |
| 51 | + public IntList(int initialCapacity = 16) |
| 52 | + { |
| 53 | + _capacity = initialCapacity > 0 ? initialCapacity : 16; |
| 54 | + _buffer = Marshal.AllocHGlobal(_capacity * sizeof(int)); |
| 55 | + _ptr = (int*)_buffer; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Pushes the specified value. |
| 60 | + /// </summary> |
| 61 | + /// <param name="value">The value.</param> |
| 62 | + public void Push(int value) |
| 63 | + { |
| 64 | + Add(value); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Adds an integer value to the end of the list, resizing if necessary. |
| 69 | + /// </summary> |
| 70 | + /// <param name="value">The integer value to add.</param> |
| 71 | + public void Add(int value) |
| 72 | + { |
| 73 | + EnsureCapacity(_count + 1); |
| 74 | + _ptr[_count++] = value; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Removes and returns the last element from the list. |
| 79 | + /// </summary> |
| 80 | + /// <returns>The last integer element in the list.</returns> |
| 81 | + /// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception> |
| 82 | + public int Pop() |
| 83 | + { |
| 84 | + if (_count == 0) |
| 85 | + throw new InvalidOperationException("Stack empty"); |
| 86 | + return _ptr[--_count]; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Returns the last element without removing it from the list. |
| 91 | + /// </summary> |
| 92 | + /// <returns>The last integer element in the list.</returns> |
| 93 | + /// <exception cref="InvalidOperationException">Thrown when the list is empty.</exception> |
| 94 | + public int Peek() |
| 95 | + { |
| 96 | + if (_count == 0) |
| 97 | + throw new InvalidOperationException("Stack empty"); |
| 98 | + return _ptr[_count - 1]; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets or sets the element at the specified index. |
| 103 | + /// </summary> |
| 104 | + /// <param name="i">The zero-based index of the element to get or set.</param> |
| 105 | + /// <returns>The element at the specified index.</returns> |
| 106 | + /// <exception cref="IndexOutOfRangeException">Thrown in debug builds if the index is out of bounds.</exception> |
| 107 | + public int this[int i] |
| 108 | + { |
| 109 | + get |
| 110 | + { |
| 111 | +#if DEBUG |
| 112 | + if (i < 0 || i >= _count) throw new IndexOutOfRangeException(); |
| 113 | +#endif |
| 114 | + return _ptr[i]; |
| 115 | + } |
| 116 | + set |
| 117 | + { |
| 118 | +#if DEBUG |
| 119 | + if (i < 0 || i >= _count) throw new IndexOutOfRangeException(); |
| 120 | +#endif |
| 121 | + _ptr[i] = value; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Ensures the capacity of the internal buffer is at least the specified minimum size. |
| 127 | + /// Resizes the buffer if necessary by doubling its capacity or setting it to the minimum required size. |
| 128 | + /// </summary> |
| 129 | + /// <param name="min">The minimum capacity required.</param> |
| 130 | + private void EnsureCapacity(int min) |
| 131 | + { |
| 132 | + if (min <= _capacity) return; |
| 133 | + |
| 134 | + int newCapacity = _capacity * 2; |
| 135 | + if (newCapacity < min) |
| 136 | + newCapacity = min; |
| 137 | + |
| 138 | + _buffer = Marshal.ReAllocHGlobal(_buffer, (IntPtr)(newCapacity * sizeof(int))); |
| 139 | + _ptr = (int*)_buffer; |
| 140 | + _capacity = newCapacity; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Removes all elements from the list. The capacity remains unchanged. |
| 145 | + /// </summary> |
| 146 | + public void Clear() |
| 147 | + { |
| 148 | + _count = 0; |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Returns a span over the valid elements of the list. |
| 153 | + /// Allows fast, safe access to the underlying data. |
| 154 | + /// </summary> |
| 155 | + /// <returns>A <see cref="Span{Int32}"/> representing the list's contents.</returns> |
| 156 | + public Span<int> AsSpan() => new((void*)_buffer, _count); |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Finalizes an instance of the <see cref="IntList"/> class, releasing unmanaged resources. |
| 160 | + /// </summary> |
| 161 | + ~IntList() |
| 162 | + { |
| 163 | + Dispose(); |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Frees unmanaged resources used by the <see cref="IntList"/>. |
| 168 | + /// After calling this method, the instance should not be used. |
| 169 | + /// </summary> |
| 170 | + public void Dispose() |
| 171 | + { |
| 172 | + if (_buffer != IntPtr.Zero) |
| 173 | + { |
| 174 | + Marshal.FreeHGlobal(_buffer); |
| 175 | + _buffer = IntPtr.Zero; |
| 176 | + } |
| 177 | + _count = 0; |
| 178 | + _capacity = 0; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments