using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace SharpGLTF.Memory { /// /// Special accessor to wrap over a base accessor and a sparse accessor /// /// [System.Diagnostics.DebuggerDisplay("Sparse {typeof(T).Name} Accessor {Count}")] public struct SparseArray : IEncodedArray where T : unmanaged { #region lifecycle public SparseArray(IEncodedArray bottom, IEncodedArray top, IntegerArray topMapping) { _BottomItems = bottom; _TopItems = top; _Mapping = new Dictionary(); for (int val = 0; val < topMapping.Count; ++val) { var key = (int)topMapping[val]; _Mapping[key] = val; } } #endregion #region data [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] private readonly IEncodedArray _BottomItems; [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] private readonly IEncodedArray _TopItems; [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] private readonly Dictionary _Mapping; [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)] private T[] _DebugItems => this.ToArray(); #endregion #region API [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] public int Count => _BottomItems.Count; public T this[int index] { get => _Mapping.TryGetValue(index, out int topIndex) ? _TopItems[topIndex] : _BottomItems[index]; set { if (_Mapping.TryGetValue(index, out int topIndex)) _TopItems[topIndex] = value; } } public void CopyTo(ArraySegment dst) { EncodedArrayUtils.Copy(this, dst); } public (T, T) GetBounds() { throw new NotImplementedException(); } public IEnumerator GetEnumerator() { return new EncodedArrayEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator(this); } #endregion } }