Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace aweXpect.Options;
Expand Down Expand Up @@ -29,13 +28,13 @@ private sealed class DistinctItemsInOrder
{
private readonly int[] _firstOccurrence;
private readonly Func<int, int, ValueTask<bool>> _isMatch;
private readonly Dictionary<(int SearchedId, int SoughtId), bool> _isMatchCache = new();
private readonly Dictionary<(int SearchedId, int SoughtId), bool> _isMatchCache;
private readonly int[] _lastOccurrence;
private readonly OrderMatch _orderMatch;
private readonly int[] _searched;
private readonly int _searchedCount;
private readonly int _soughtCount;
private HashSet<int> _placedIds = new();
private HashSet<int>? _placedIds;

/// <param name="searched">The id of each searched item.</param>
/// <param name="searchedCount">The number of distinct searched items.</param>
Expand All @@ -50,6 +49,8 @@ public DistinctItemsInOrder(int[] searched, int searchedCount, int soughtCount,
_soughtCount = soughtCount;
_isMatch = isMatch;
_orderMatch = orderMatch;
// Items in the sought order need about one comparison each.
_isMatchCache = new Dictionary<(int SearchedId, int SoughtId), bool>(soughtCount);
_firstOccurrence = new int[searchedCount];
_lastOccurrence = new int[searchedCount];
for (int position = searched.Length - 1; position >= 0; position--)
Expand Down Expand Up @@ -81,7 +82,7 @@ public async ValueTask<bool> IsInOrder()
return false;
}

_placedIds = new HashSet<int>(placement.Select(position => _searched[position]));
_placedIds = GetPlacedIds(placement);
return _orderMatch != OrderMatch.Equal || await IsEveryOtherItemADuplicate();
}

Expand All @@ -101,14 +102,25 @@ public async ValueTask<bool> IsInOrder()

if (await IsContiguousAsSet(start, chosen))
{
_placedIds = new HashSet<int>(chosen.Select(position => _searched[position]));
_placedIds = GetPlacedIds(chosen);
return true;
}
}

return false;
}

private HashSet<int> GetPlacedIds(List<int> positions)
{
HashSet<int> placedIds = new();
foreach (int position in positions)
{
placedIds.Add(_searched[position]);
}

return placedIds;
}

/// <summary>
/// The searched items contain a distinct item that is no sought item, after a successful
/// <see cref="IsInOrder" />.
Expand All @@ -117,7 +129,7 @@ public async ValueTask<bool> HasAdditionalItem()
{
for (int searchedId = 0; searchedId < _searchedCount; searchedId++)
{
if (!_placedIds.Contains(searchedId) && !await MatchesAnySoughtItem(searchedId))
if (_placedIds?.Contains(searchedId) != true && !await MatchesAnySoughtItem(searchedId))
{
return true;
}
Expand Down Expand Up @@ -205,7 +217,7 @@ private async ValueTask<bool> IsEveryOtherItemADuplicate()
{
for (int searchedId = 0; searchedId < _searchedCount; searchedId++)
{
if (!_placedIds.Contains(searchedId) && !await MatchesAnySoughtItem(searchedId))
if (_placedIds?.Contains(searchedId) != true && !await MatchesAnySoughtItem(searchedId))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
#if NET8_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
using System.Threading.Tasks;
using aweXpect.Core;

Expand Down Expand Up @@ -84,10 +87,10 @@ private abstract class SameOrderIgnoreDuplicatesCollectionMatcherBase<T, T2, T3>
private readonly int[]? _dimensions;

private readonly EquivalenceRelations _equivalenceRelations;
private readonly List<int> _firstIndexOfSubjectItem = new();
private readonly List<int> _firstIndexOfSubjectItem;
private readonly bool _ignoreInterspersedItems;
private readonly List<int> _subjectIdAt = new();
private readonly ItemIds<T> _subjectIds = new(null);
private readonly List<int> _subjectIdAt;
private readonly ItemIds<T> _subjectIds;
private bool _areExpectedItemsUnique;
private T3[] _expectedDistinctItems = [];
private int[] _expectedIds = [];
Expand All @@ -99,7 +102,9 @@ private abstract class SameOrderIgnoreDuplicatesCollectionMatcherBase<T, T2, T3>
/// <remarks>
/// The <paramref name="comparer" /> merges equal expected values, when the comparison cannot tell them apart;
/// predicates and expectations cannot be compared with each other, so without it each expected item stands for
/// itself.
/// itself.<br />
/// The subject is expected to have about as many items as the expected collection, so that the collections
/// for its items do not grow item by item.
/// </remarks>
protected SameOrderIgnoreDuplicatesCollectionMatcherBase(EquivalenceRelations equivalenceRelation,
IEnumerable<T3> expected,
Expand All @@ -112,6 +117,9 @@ protected SameOrderIgnoreDuplicatesCollectionMatcherBase(EquivalenceRelations eq
_dimensions = dimensions;
ExpectedItems = expected as T3[] ?? expected.ToArray();
_comparer = comparer;
_firstIndexOfSubjectItem = new List<int>(ExpectedItems.Length);
_subjectIdAt = new List<int>(ExpectedItems.Length);
_subjectIds = new ItemIds<T>(null, ExpectedItems.Length);
}

protected T3[] ExpectedItems { get; }
Expand Down Expand Up @@ -225,7 +233,7 @@ private bool MergesEqualItems(IOptionsEquality<T2> options)
/// </remarks>
private void NumberTheExpectedValues(IEqualityComparer<T3> comparer, bool mergesEqualItems)
{
ItemIds<T3> expectedIds = new(comparer);
ItemIds<T3> expectedIds = new(comparer, ExpectedItems.Length);
_expectedIds = new int[ExpectedItems.Length];
for (int i = 0; i < ExpectedItems.Length; i++)
{
Expand Down Expand Up @@ -356,14 +364,26 @@ protected abstract ValueTask<bool>
/// <summary>
/// Numbers the distinct items in the order of their first occurrence.
/// </summary>
private sealed class ItemIds<TItem>(IEqualityComparer<TItem>? comparer)
private sealed class ItemIds<TItem>(IEqualityComparer<TItem>? comparer, int capacity)
{
private readonly Dictionary<Key, int> _ids = new(new KeyComparer(comparer ?? EqualityComparer<TItem>.Default));
private readonly Dictionary<Key, int> _ids =
new(capacity, new KeyComparer(comparer ?? EqualityComparer<TItem>.Default));

public List<TItem> Items { get; } = new();
public List<TItem> Items { get; } = new(capacity);

public int GetOrAdd(TItem item, out bool isNew)
{
#if NET8_0_OR_GREATER
ref int id = ref CollectionsMarshal.GetValueRefOrAddDefault(_ids, new Key(item), out bool exists);
isNew = !exists;
if (isNew)
{
id = Items.Count;
Items.Add(item);
}

return id;
#else
isNew = !_ids.TryGetValue(new Key(item), out int id);
if (isNew)
{
Expand All @@ -373,6 +393,7 @@ public int GetOrAdd(TItem item, out bool isNew)
}

return id;
#endif
}

/// <summary>
Expand Down
Loading