|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace SharpGLTF.Memory |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a readable and sometimes writeable(*) array of elements |
| 11 | + /// </summary> |
| 12 | + /// <typeparam name="T"> |
| 13 | + /// For Index Buffers: <see cref="UInt32"/><br/> |
| 14 | + /// For Vertex Buffers: <see cref="System.Numerics.Vector2"/>, <see cref="System.Numerics.Vector3"/>, <see cref="System.Numerics.Vector4"/> and so on.<br/> |
| 15 | + /// For animations and other plain data: <see cref="float"/>, <see cref="System.Numerics.Quaternion"/>, <see cref="System.Numerics.Quaternion"/> and so on.<br/> |
| 16 | + /// </typeparam> |
| 17 | + /// <remarks> |
| 18 | + /// <para> |
| 19 | + /// This interface is exposed by <see cref="Schema2.Accessor"/> to easily encode/decode packed and strided elements |
| 20 | + /// </para> |
| 21 | + /// <para> |
| 22 | + /// Implemented by:<br/> |
| 23 | + /// <see cref="SparseArray{T}"/> (Read Only)<br/> |
| 24 | + /// <see cref="IntegerArray"/><br/> |
| 25 | + /// <see cref="ScalarArray"/><br/> |
| 26 | + /// <see cref="Vector2Array"/><br/> |
| 27 | + /// <see cref="Vector3Array"/><br/> |
| 28 | + /// <see cref="Vector4Array"/><br/> |
| 29 | + /// <see cref="QuaternionArray"/><br/> |
| 30 | + /// <see cref="Matrix2x2Array"/><br/> |
| 31 | + /// <see cref="Matrix3x2Array"/><br/> |
| 32 | + /// <see cref="Matrix3x3Array"/><br/> |
| 33 | + /// <see cref="Matrix4x3Array"/><br/> |
| 34 | + /// <see cref="Matrix4x3Array"/><br/> |
| 35 | + /// </para> |
| 36 | + /// </remarks> |
| 37 | + public interface IAccessorArray<T> : IReadOnlyList<T> , IList<T> |
| 38 | + { |
| 39 | + // Because this.Count and this[index] exist at both at IList<T> and IReadOnlyList<T>, |
| 40 | + // attempting to use them may cause an ambiguity error. This will be fixed in Net10. |
| 41 | + // see: |
| 42 | + // https://github.com/dotnet/runtime/issues/31001#issuecomment-2942678308 |
| 43 | + // https://github.com/dotnet/runtime/pull/115802 |
| 44 | + |
| 45 | + new T this[int index] { get; set; } |
| 46 | + new int Count { get; } |
| 47 | + } |
| 48 | + |
| 49 | + public readonly struct ZeroAccessorArray<T> : IAccessorArray<T> |
| 50 | + { |
| 51 | + static ZeroAccessorArray() |
| 52 | + { |
| 53 | + _Default = default(T); |
| 54 | + if (typeof(T) == typeof(float[])) _Default = (T)(Object)Array.Empty<T>(); |
| 55 | + } |
| 56 | + |
| 57 | + public ZeroAccessorArray(int count) |
| 58 | + { |
| 59 | + Count = count; |
| 60 | + } |
| 61 | + |
| 62 | + private static readonly T _Default; |
| 63 | + |
| 64 | + public bool IsReadOnly => true; |
| 65 | + |
| 66 | + public T this[int index] { get => _Default; set => throw new NotSupportedException(); } |
| 67 | + |
| 68 | + public int Count { get; } |
| 69 | + |
| 70 | + public int IndexOf(T item) |
| 71 | + { |
| 72 | + return Count > 0 && item.Equals(_Default) ? 0 : -1; |
| 73 | + } |
| 74 | + |
| 75 | + public bool Contains(T item) |
| 76 | + { |
| 77 | + return Count > 0 && item.Equals(_Default); |
| 78 | + } |
| 79 | + |
| 80 | + public void CopyTo(T[] array, int arrayIndex) |
| 81 | + { |
| 82 | + for(int i=0; i < Count; ++i) |
| 83 | + { |
| 84 | + array[i + arrayIndex] = default; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public IEnumerator<T> GetEnumerator() |
| 89 | + { |
| 90 | + return Enumerable.Repeat(_Default, Count).GetEnumerator(); |
| 91 | + } |
| 92 | + |
| 93 | + IEnumerator IEnumerable.GetEnumerator() |
| 94 | + { |
| 95 | + return Enumerable.Repeat(_Default, Count).GetEnumerator(); |
| 96 | + } |
| 97 | + |
| 98 | + void IList<T>.Insert(int index, T item) { throw new NotSupportedException(); } |
| 99 | + |
| 100 | + void IList<T>.RemoveAt(int index) { throw new NotSupportedException(); } |
| 101 | + |
| 102 | + void ICollection<T>.Add(T item) { throw new NotSupportedException(); } |
| 103 | + |
| 104 | + void ICollection<T>.Clear() { throw new NotSupportedException(); } |
| 105 | + |
| 106 | + bool ICollection<T>.Remove(T item) { throw new NotSupportedException(); } |
| 107 | + } |
| 108 | +} |
0 commit comments