// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
// Ported from: https://github.com/SixLabors/Fonts/
using System;
using System.Runtime.CompilerServices;
namespace Avalonia.Utilities
{
///
/// Provides a mapped view of an underlying slice, selecting arbitrary indices
/// from the source array.
///
/// The type of item contained in the underlying array.
internal readonly struct MappedArraySlice
where T : struct
{
private readonly ArraySlice _data;
private readonly ArraySlice _map;
///
/// Initializes a new instance of the struct.
///
/// The data slice.
/// The map slice.
public MappedArraySlice(in ArraySlice data, in ArraySlice map)
{
#if DEBUG
if (map.Length.CompareTo(data.Length) > 0)
{
throw new ArgumentOutOfRangeException(nameof(map));
}
#endif
_data = data;
_map = map;
}
///
/// Gets the number of items in the map.
///
public int Length => _map.Length;
///
/// Returns a reference to specified element of the slice.
///
/// The index of the element to return.
/// The .
///
/// Thrown when index less than 0 or index greater than or equal to .
///
public ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _data[_map[index]];
}
}
}