PredicateMap 1.0.1
dotnet add package PredicateMap --version 1.0.1
NuGet\Install-Package PredicateMap -Version 1.0.1
<PackageReference Include="PredicateMap" Version="1.0.1" />
<PackageVersion Include="PredicateMap" Version="1.0.1" />
<PackageReference Include="PredicateMap" />
paket add PredicateMap --version 1.0.1
#r "nuget: PredicateMap, 1.0.1"
#:package PredicateMap@1.0.1
#addin nuget:?package=PredicateMap&version=1.0.1
#tool nuget:?package=PredicateMap&version=1.0.1
PredicateMap
PredicateMap is a high-performance .NET library that allows you to map keys to values using predicates and direct key lookups. It compiles the mapping logic into an optimized expression tree, providing fast lookups even with complex conditions.
Features
- Fast Lookups: Compiles mapping logic into a single delegate using Expression Trees.
- Flexible Mapping: Supports both direct key-to-value mapping and predicate-based mapping.
- Priority Support:
PriorityPredicateMapBuilderallows defining order-sensitive mappings. - Fluent API: Easy-to-use builder pattern and extension methods.
Installation
Install the package via NuGet:
dotnet add package PredicateMap
Usage
Basic Usage with PredicateMapBuilder
Use PredicateMapBuilder when you have a mix of specific keys and general predicates.
using PredicateMap;
using System.Linq.Expressions;
var builder = new PredicateMapBuilder<int, string>()
// Direct key mapping
.Add(1, "One")
.Add(2, "Two")
// Predicate mapping
.Add(x => x > 10, "Greater than ten")
.Add(x => x < 0, "Negative");
IPredicateMap<int, string> map = builder.ToPredicateMap();
if (map.TryGet(1, out var value))
{
Console.WriteLine(value); // Output: One
}
if (map.TryGet(15, out value))
{
Console.WriteLine(value); // Output: Greater than ten
}
Priority Mapping with PriorityPredicateMapBuilder
Use PriorityPredicateMapBuilder when the order of evaluation matters. The first matching predicate or key will return the value.
using PredicateMap;
var builder = new PriorityPredicateMapBuilder<int, string>()
// Order matters here
.Add(x => x % 2 == 0, "Even")
.Add(x => x % 3 == 0, "Divisible by 3")
.Add(6, "Six"); // This will never be reached because 6 is Even
IPredicateMap<int, string> map = builder.ToPredicateMap();
map.TryGet(6, out var val1); // Output: Even
map.TryGet(9, out var val2); // Output: Divisible by 3
Creating from an Enumerable
You can also create a map from any IEnumerable<T> using the ToPredicateMap extension method.
var items = new[] { 1, 2, 3, 4, 5 };
IPredicateMap<int, string> map = items.ToPredicateMap(
keySelector: x => x,
valueSelector: x => x % 2 == 0 ? "Even" : "Odd"
);
Performance
PredicateMap is designed for performance. It compiles the lookup logic into a highly optimized delegate, minimizing overhead during runtime lookups. This makes it suitable for hot paths where performance is critical.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on PredicateMap:
| Package | Downloads |
|---|---|
|
SwiftState
SwiftState is a lightweight, fluent state machine library |
GitHub repositories
This package is not used by any popular GitHub repositories.