mirror of
https://github.com/4sval/FModel.git
synced 2026-03-22 01:34:37 -05:00
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace FModel.Extensions;
|
|
|
|
public static class CollectionExtensions
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Next<T>(this IList<T> collection, T value)
|
|
{
|
|
var i = collection.IndexOf(value) + 1;
|
|
return i >= collection.Count ? collection.First() : collection[i];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Next<T>(this IList<T> collection, int index)
|
|
{
|
|
var i = index + 1;
|
|
return i >= collection.Count ? collection.First() : collection[i];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Previous<T>(this IList<T> collection, T value)
|
|
{
|
|
var i = collection.IndexOf(value) - 1;
|
|
return i < 0 ? collection.Last() : collection[i];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static T Previous<T>(this IList<T> collection, int index)
|
|
{
|
|
var i = index - 1;
|
|
return i < 0 ? collection.Last() : collection[i];
|
|
}
|
|
} |