// RichTextKit // Copyright © 2019-2020 Topten Software. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); you may // not use this product except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. using System.Runtime.CompilerServices; namespace Topten.RichTextKit.Utils { /// /// Provides a mapped view of an underlying slice array, selecting arbitrary indicies /// from the source array /// /// The element type of the underlying array public struct MappedSlice { /// /// Constructs a new mapped array /// /// The data to be mapped /// The index map public MappedSlice(Slice data, Slice mapping) { _data = data; _mapping = mapping; } Slice _data; Slice _mapping; /// /// Get the underlying slice for this mapped array /// public Slice Underlying => _data; /// /// Get the index mapping for this mapped array /// public Slice Mapping => _mapping; /// /// Gets the number of elements in this mapping /// public int Length => _mapping.Length; /// /// Gets a reference to a mapped element /// /// The mapped index to be retrieved /// A reference to the element public ref T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return ref _data[_mapping[index]]; } } /// /// Get the content of this mapped slice as an array /// /// The content as an array public T[] ToArray() { var arr = new T[Length]; for (int i = 0; i < Length; i++) { arr[i] = this[i]; } return arr; } } }