Skip to content

Commit

Permalink
Merge pull request #19 from nventive/dev/dr/IndexOfList
Browse files Browse the repository at this point in the history
Add IndexOf on IList which accepts an IEqualiyComparer
  • Loading branch information
dr1rrb authored Jun 19, 2018
2 parents 8206eb9 + ebf39c3 commit 48ebb1e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Uno.Core.Tests/Collections/ListExtensionsFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// ******************************************************************
// Copyright � 2015-2018 nventive inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ******************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions;

namespace Uno.Core.Tests.Collections
{
[TestClass]
public class ListExtensionsFixture
{
private static readonly int[] list = new [] { 1, 2, 3};

[TestMethod]
public void AsReadOnly()
{
IList<int> list2 = new List<int>(list);

Assert.IsTrue(list2.AsReadOnly().IsReadOnly);
}

[TestMethod]
public void Adapt()
{
IList<string> adapter = list.Adapt<int, string>();

Assert.IsNotNull(adapter);
}

[TestMethod]
public void IndexOf_WithComparer()
{
var @true = new MyComparer((l, r) => true);
var @false = new MyComparer((l, r) => false);

Assert.AreEqual(0, new[] {1, 2, 3}.IndexOf(2, @true));
Assert.AreEqual(1, new[] {1, 2, 3}.IndexOf(2, EqualityComparer<object>.Default));
Assert.AreEqual(1, new[] {1, 2, 3}.IndexOf(2, default(IEqualityComparer)));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(2, @false));

Assert.AreEqual(0, new[] {1, 2, 3}.IndexOf(-1, @true));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, EqualityComparer<object>.Default));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, default(IEqualityComparer)));
Assert.AreEqual(-1, new[] {1, 2, 3}.IndexOf(-1, @false));
}

private class MyComparer : IEqualityComparer
{
private readonly Func<object, object, bool> _equals;

public MyComparer(Func<object, object, bool> equals) => _equals = @equals;

public bool Equals(object left, object right) => _equals(left, right);

public int GetHashCode(object obj) => throw new System.NotImplementedException();
}
}
}
25 changes: 25 additions & 0 deletions src/Uno.Core/Collections/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,30 @@ public static T FindNearestItem<T>(this IReadOnlyList<T> list, Func<T, bool> pre
.ToDivergentEnumerable(startingAt)
.FirstOrDefault(predicate);
}

/// <summary>
/// Determines the index of a specific item in the <see cref="IList"/>.
/// </summary>
/// <param name="list">The source list to look into.</param>
/// <param name="value">The object to locate in the <see cref="IList"/>.</param>
/// <param name="comparer">The comparer to use to locate the <paramref name="value" />.</param>
/// <returns>The index of value if found in the list; otherwise, -1.</returns>
public static int IndexOf(this IList list, object value, IEqualityComparer comparer)
{
if (comparer == null)
{
return list.IndexOf(value);
}

for (var i = 0; i < list.Count; i++)
{
if (comparer.Equals(value, list[i]))
{
return i;
}
}

return -1;
}
}
}

0 comments on commit 48ebb1e

Please sign in to comment.