|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +SPDX-License-Identifier: MIT |
| 4 | +
|
| 5 | +Copyright (c) 2025 DevAM |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +*/ |
| 25 | + |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.Runtime.CompilerServices; |
| 29 | + |
| 30 | +namespace LargeCollections.Observable; |
| 31 | + |
| 32 | +/// <summary> |
| 33 | +/// High-performance predicate interface for filtering operations. |
| 34 | +/// Using struct implementations enables JIT devirtualization and inlining for optimal performance. |
| 35 | +/// </summary> |
| 36 | +/// <typeparam name="T">The type of items to filter.</typeparam> |
| 37 | +/// <example> |
| 38 | +/// <code> |
| 39 | +/// struct PositiveFilter : ILargePredicate<int> |
| 40 | +/// { |
| 41 | +/// public bool Invoke(int item) => item > 0; |
| 42 | +/// } |
| 43 | +/// |
| 44 | +/// var filtered = collection.CreateFilteredView(new PositiveFilter()); |
| 45 | +/// </code> |
| 46 | +/// </example> |
| 47 | +public interface ILargePredicate<T> |
| 48 | +{ |
| 49 | + /// <summary> |
| 50 | + /// Evaluates the predicate for the specified item. |
| 51 | + /// </summary> |
| 52 | + /// <param name="item">The item to evaluate.</param> |
| 53 | + /// <returns>true if the item satisfies the predicate; otherwise, false.</returns> |
| 54 | + bool Invoke(T item); |
| 55 | +} |
| 56 | + |
| 57 | +/// <summary> |
| 58 | +/// Wrapper that adapts a <see cref="Func{T, TResult}"/> delegate to the <see cref="ILargePredicate{T}"/> interface. |
| 59 | +/// Use this when you have an existing delegate but want to use the generic predicate overloads. |
| 60 | +/// Note: This wrapper has the same performance as direct delegate usage. |
| 61 | +/// </summary> |
| 62 | +/// <typeparam name="T">The type to filter.</typeparam> |
| 63 | +public readonly struct DelegatePredicate<T> : ILargePredicate<T> |
| 64 | +{ |
| 65 | + private readonly Func<T, bool> _predicate; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Creates a new delegate wrapper predicate. |
| 69 | + /// </summary> |
| 70 | + /// <param name="predicate">The predicate delegate to wrap.</param> |
| 71 | + public DelegatePredicate(Func<T, bool> predicate) |
| 72 | + { |
| 73 | + _predicate = predicate ?? throw new ArgumentNullException(nameof(predicate)); |
| 74 | + } |
| 75 | + |
| 76 | + /// <inheritdoc/> |
| 77 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 78 | + public bool Invoke(T item) => _predicate(item); |
| 79 | +} |
| 80 | + |
| 81 | +/// <summary> |
| 82 | +/// A pass-through predicate that always returns true (no filtering). |
| 83 | +/// The JIT compiler can completely eliminate the predicate check when this struct is used, |
| 84 | +/// resulting in zero overhead compared to not having a predicate at all. |
| 85 | +/// </summary> |
| 86 | +/// <typeparam name="T">The type of items.</typeparam> |
| 87 | +public readonly struct NoFilter<T> : ILargePredicate<T> |
| 88 | +{ |
| 89 | + /// <inheritdoc/> |
| 90 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 91 | + public bool Invoke(T item) => true; |
| 92 | +} |
| 93 | + |
| 94 | +/// <summary> |
| 95 | +/// A pass-through comparer that preserves the original order (no sorting). |
| 96 | +/// The JIT compiler can completely eliminate the sort operation when this struct is used, |
| 97 | +/// resulting in zero overhead compared to not having a comparer at all. |
| 98 | +/// </summary> |
| 99 | +/// <typeparam name="T">The type of items.</typeparam> |
| 100 | +public readonly struct NoSort<T> : IComparer<T> |
| 101 | +{ |
| 102 | + /// <inheritdoc/> |
| 103 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 104 | + public int Compare(T x, T y) => 0; |
| 105 | +} |
| 106 | + |
0 commit comments