Skip to content

Commit 5ea9f3a

Browse files
Remove need to allocate new list to find the index
1 parent ad7324c commit 5ea9f3a

3 files changed

Lines changed: 49 additions & 13 deletions

File tree

src/ImageSharp.Web/Commands/CommandCollection.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.Runtime.CompilerServices;
78

89
namespace SixLabors.ImageSharp.Web.Commands
910
{
@@ -59,7 +60,8 @@ public IEnumerable<string> Keys
5960
return item.Value;
6061
}
6162

62-
throw new KeyNotFoundException();
63+
ThrowKeyNotFound();
64+
return default;
6365
}
6466

6567
set
@@ -75,7 +77,43 @@ public IEnumerable<string> Keys
7577
}
7678
}
7779

80+
/// <summary>
81+
/// Searches for an element that matches the conditions defined by the specified
82+
/// predicate, and returns the zero-based index of the first occurrence within the
83+
/// entire <see cref="CommandCollection"/>.
84+
/// </summary>
85+
/// <param name="match">
86+
/// The <see cref="Predicate{T}"/> delegate that defines the conditions of the element to
87+
/// search for.
88+
/// </param>
89+
/// <returns>
90+
/// The zero-based index of the first occurrence of an element that matches the conditions
91+
/// defined by match, if found; otherwise, -1.
92+
/// </returns>
93+
/// <exception cref="ArgumentNullException"><paramref name="match"/> is null.</exception>
94+
public int FindIndex(Predicate<string> match)
95+
{
96+
Guard.NotNull(match, nameof(match));
97+
98+
int index = 0;
99+
foreach (KeyValuePair<string, string> item in this)
100+
{
101+
if (match(item.Key))
102+
{
103+
return index;
104+
}
105+
106+
index++;
107+
}
108+
109+
return -1;
110+
}
111+
78112
/// <inheritdoc/>
113+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79114
protected override string GetKeyForItem(KeyValuePair<string, string> item) => item.Key;
115+
116+
[MethodImpl(MethodImplOptions.NoInlining)]
117+
private static void ThrowKeyNotFound() => throw new KeyNotFoundException();
80118
}
81119
}

src/ImageSharp.Web/Processors/WebProcessingExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ public static FormattedImage Process(
3434
CommandParser commandParser,
3535
CultureInfo culture)
3636
{
37-
var commandKeys = new List<string>(commands.Keys);
38-
39-
foreach (IImageWebProcessor processor in processors.GetBySupportedCommands(commandKeys))
37+
foreach (IImageWebProcessor processor in processors.GetBySupportedCommands(commands))
4038
{
4139
source = processor.Process(source, logger, commands, commandParser, culture);
4240
}
@@ -52,7 +50,7 @@ public static FormattedImage Process(
5250
/// <returns>
5351
/// The sorted proccessors that supports any of the specified commands.
5452
/// </returns>
55-
public static IEnumerable<IImageWebProcessor> GetBySupportedCommands(this IEnumerable<IImageWebProcessor> processors, List<string> commands)
53+
public static IEnumerable<IImageWebProcessor> GetBySupportedCommands(this IEnumerable<IImageWebProcessor> processors, CommandCollection commands)
5654
{
5755
var indexedProcessors = new List<(int Index, IImageWebProcessor Processor)>();
5856

tests/ImageSharp.Web.Tests/Processors/WebProcessingExtensionsTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using System.Collections.Generic;
54
using System.Linq;
5+
using SixLabors.ImageSharp.Web.Commands;
66
using SixLabors.ImageSharp.Web.Processors;
77
using SixLabors.ImageSharp.Web.Tests.DependencyInjection;
88
using Xunit;
@@ -22,14 +22,14 @@ public void WebProcessingExtensions_GetBySupportedCommands()
2222
new MockWebProcessor()
2323
};
2424

25-
var commands = new List<string>
25+
CommandCollection commands = new()
2626
{
27-
ResizeWebProcessor.Width,
28-
QualityWebProcessor.Quality,
29-
ResizeWebProcessor.Height
27+
new(ResizeWebProcessor.Width, null),
28+
new(QualityWebProcessor.Quality, null),
29+
new(ResizeWebProcessor.Height, null)
3030
};
3131

32-
IImageWebProcessor[] supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
32+
IImageWebProcessor[] supportedProcessors = processors.GetBySupportedCommands(commands).ToArray();
3333

3434
Assert.Equal(2, supportedProcessors.Length);
3535
Assert.IsType<ResizeWebProcessor>(supportedProcessors[0]);
@@ -44,9 +44,9 @@ public void WebProcessingExtensions_GetBySupportedCommands_Empty()
4444
new MockWebProcessor()
4545
};
4646

47-
var commands = new List<string>();
47+
CommandCollection commands = new();
4848

49-
IImageWebProcessor[] supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
49+
IImageWebProcessor[] supportedProcessors = processors.GetBySupportedCommands(commands).ToArray();
5050

5151
Assert.Empty(supportedProcessors);
5252
}

0 commit comments

Comments
 (0)