Try use ordered set for recent songs
This commit is contained in:
parent
eff5ac478f
commit
e3d8da41bd
73
TaikoLocalServer/Common/OrderedSet.cs
Normal file
73
TaikoLocalServer/Common/OrderedSet.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace TaikoLocalServer.Common;
|
||||
|
||||
public class OrderedSet<T> : ICollection<T> where T : notnull
|
||||
{
|
||||
private readonly IDictionary<T, LinkedListNode<T>> dictionary;
|
||||
private readonly LinkedList<T> linkedList;
|
||||
|
||||
public OrderedSet()
|
||||
: this(EqualityComparer<T>.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public OrderedSet(IEqualityComparer<T> comparer)
|
||||
{
|
||||
dictionary = new Dictionary<T, LinkedListNode<T>>(comparer);
|
||||
linkedList = new LinkedList<T>();
|
||||
}
|
||||
|
||||
public int Count => dictionary.Count;
|
||||
|
||||
public virtual bool IsReadOnly => dictionary.IsReadOnly;
|
||||
|
||||
void ICollection<T>.Add(T item)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
|
||||
public bool Add(T item)
|
||||
{
|
||||
if (dictionary.ContainsKey(item)) return false;
|
||||
var node = linkedList.AddLast(item);
|
||||
dictionary.Add(item, node);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
linkedList.Clear();
|
||||
dictionary.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(T? item)
|
||||
{
|
||||
if (item == null) return false;
|
||||
var found = dictionary.TryGetValue(item, out var node);
|
||||
if (!found) return false;
|
||||
dictionary.Remove(item);
|
||||
if (node != null) linkedList.Remove(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return linkedList.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public bool Contains(T? item)
|
||||
{
|
||||
return item != null && dictionary.ContainsKey(item);
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
linkedList.CopyTo(array, arrayIndex);
|
||||
}
|
||||
}
|
@ -51,8 +51,8 @@ public class UserDataController : BaseController<UserDataController>
|
||||
.Select(datum => datum.SongId)
|
||||
.ToArray();
|
||||
|
||||
// Use custom implementation as distictby cannot guarantee preserved element
|
||||
var recentSet = new SortedSet<uint>();
|
||||
// Use custom implementation as distinctby cannot guarantee preserved element
|
||||
var recentSet = new OrderedSet<uint>();
|
||||
foreach (var id in recentSongs)
|
||||
{
|
||||
recentSet.Add(id);
|
||||
@ -103,8 +103,6 @@ public class UserDataController : BaseController<UserDataController>
|
||||
AryRecentSongNoes = recentSongs,
|
||||
NotesPosition = userData.NotesPosition
|
||||
};
|
||||
|
||||
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user