/// <summary> /// Gets the <see cref="Track" /> at a given index in this <see cref="TrackList" />. /// </summary> /// <param name="index">The index of which to get the <see cref="Track" />.</param> /// <returns>The <see cref="Track" /> at the given <paramref name="index" />.</returns> public Track this[int index] { get { // Bounds check if (index < 0 || index >= Count) { throw new IndexOutOfRangeException(); } // Looking in cached parts TrackListPart part = _parts.FirstOrDefault(p => p.Offset <= index && p.Offset + p.Tracks.Count() > index); if (part != null) { return(part.Tracks.ElementAt(index - part.Offset)); } // Fetch parts untill we have the right part TrackListPart current = _parts.Last(); while (current.Offset + current.Tracks.Count() < index) { current = current.Next; _parts.Add(current); } return(current.Tracks.ElementAt(index - current.Offset)); } }
private readonly List <TrackListPart> _parts = new List <TrackListPart>(); //Cache of all parts #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="TrackList" />. /// </summary> /// <param name="first">The first part of the list.</param> internal TrackList(TrackListPart first) { while (first.Offset != 0) { _parts.Insert(0, first); first = first.Previous; } _parts.Insert(0, first); }