IAccessorArray.cs 3.6 KB

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