diff options
| author | cvium <clausvium@gmail.com> | 2022-01-07 10:23:22 +0100 |
|---|---|---|
| committer | cvium <clausvium@gmail.com> | 2022-01-07 10:23:22 +0100 |
| commit | c658a883a2bc84b46ed73d209d2983e8a324cdce (patch) | |
| tree | dabdbb5ac224e202d5433e7062e0c1b6872d1af7 /src/Jellyfin.Extensions/ReadOnlyListExtension.cs | |
| parent | 2899b77cd58456470b8dd4d01d3a8c525a9b5911 (diff) | |
| parent | 6b4f5a86631e5bde93dae88553380c7ffd99b8e4 (diff) | |
Merge branch 'master' into keyframe_extraction_v1
# Conflicts:
# Jellyfin.Api/Controllers/DynamicHlsController.cs
# MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs
# MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
Diffstat (limited to 'src/Jellyfin.Extensions/ReadOnlyListExtension.cs')
| -rw-r--r-- | src/Jellyfin.Extensions/ReadOnlyListExtension.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/Jellyfin.Extensions/ReadOnlyListExtension.cs b/src/Jellyfin.Extensions/ReadOnlyListExtension.cs new file mode 100644 index 000000000..7785cfb49 --- /dev/null +++ b/src/Jellyfin.Extensions/ReadOnlyListExtension.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace Jellyfin.Extensions +{ + /// <summary> + /// Static extensions for the <see cref="IReadOnlyList{T}"/> interface. + /// </summary> + public static class ReadOnlyListExtension + { + /// <summary> + /// Finds the index of the desired item. + /// </summary> + /// <param name="source">The source list.</param> + /// <param name="value">The value to fine.</param> + /// <typeparam name="T">The type of item to find.</typeparam> + /// <returns>Index if found, else -1.</returns> + public static int IndexOf<T>(this IReadOnlyList<T> source, T value) + { + if (source is IList<T> list) + { + return list.IndexOf(value); + } + + for (int i = 0; i < source.Count; i++) + { + if (Equals(value, source[i])) + { + return i; + } + } + + return -1; + } + + /// <summary> + /// Finds the index of the predicate. + /// </summary> + /// <param name="source">The source list.</param> + /// <param name="match">The value to find.</param> + /// <typeparam name="T">The type of item to find.</typeparam> + /// <returns>Index if found, else -1.</returns> + public static int FindIndex<T>(this IReadOnlyList<T> source, Predicate<T> match) + { + if (source is List<T> list) + { + return list.FindIndex(match); + } + + for (int i = 0; i < source.Count; i++) + { + if (match(source[i])) + { + return i; + } + } + + return -1; + } + } +} |
