1
0
mirror of synced 2024-11-23 22:41:01 +01:00

Try use ordered set for recent songs

This commit is contained in:
asesidaa 2022-09-08 20:51:51 +08:00
parent eff5ac478f
commit e3d8da41bd
2 changed files with 75 additions and 4 deletions

View 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);
}
}

View File

@ -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);
}