Skip to content

Commit

Permalink
Merge pull request #984 from smoogipooo/fix-container-contains
Browse files Browse the repository at this point in the history
Fix container contains
  • Loading branch information
Tom94 authored Aug 22, 2017
2 parents 00be5c7 + df3ddd4 commit 74f644b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
13 changes: 13 additions & 0 deletions osu.Framework.Tests/Lists/TestSortedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,18 @@ public void TestClear()
Assert.IsFalse(list.Any());
Assert.AreEqual(0, list.Count);
}

[Test]
public void TestIndexOf()
{
var list = new SortedList<int>(Comparer<int>.Default)
{
10,
10,
10,
};

Assert.IsTrue(list.IndexOf(10) >= 0);
}
}
}
5 changes: 2 additions & 3 deletions osu.Framework/Graphics/Containers/CompositeDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.Threading;
using OpenTK;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.OpenGL;
Expand Down Expand Up @@ -277,7 +276,7 @@ protected internal void ClearInternal(bool disposeChildren = true)
/// Used to assign a monotonically increasing ID to children as they are added. This member is
/// incremented whenever a child is added.
/// </summary>
private static long currentChildID;
private ulong currentChildID;

/// <summary>
/// Adds a child to <see cref="InternalChildren"/>.
Expand All @@ -294,7 +293,7 @@ protected internal virtual void AddInternal(Drawable drawable)
if (drawable.ChildID != 0)
throw new InvalidOperationException("May not add a drawable to multiple containers.");

drawable.ChildID = Interlocked.Increment(ref currentChildID);
drawable.ChildID = ++currentChildID;

if (drawable.LoadState >= LoadState.Ready)
drawable.Parent = this;
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Drawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected virtual void LoadComplete()
/// ID is unique within the <see cref="Parent"/> <see cref="CompositeDrawable"/>.
/// The primary use case of this ID is stable sorting of Drawables with equal <see cref="Depth"/>.
/// </summary>
public long ChildID { get; internal set; }
public ulong ChildID { get; internal set; }

/// <summary>
/// Whether this drawable has been added to a parent <see cref="CompositeDrawable"/>. Note that this does NOT imply that
Expand Down
6 changes: 5 additions & 1 deletion osu.Framework/Lists/SortedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public int RemoveAll(Predicate<T> match)

public int BinarySearch(T value) => list.BinarySearch(value, Comparer);

public int IndexOf(T value) => list.BinarySearch(value, Comparer);
public int IndexOf(T value)
{
int index = list.BinarySearch(value, Comparer);
return index >= 0 && list[index].Equals(value) ? index : -1;
}

public void CopyTo(T[] array, int arrayIndex) => list.CopyTo(array, arrayIndex);

Expand Down

0 comments on commit 74f644b

Please sign in to comment.