using System.Collections.Generic;
using System.Linq;
namespace BizHawk.Common
{
#if false
/// Sorts using a reorderable list of predicates.
///
public sealed class MultiPredicateSort
{
private readonly int _count;
/// TODO would an array be faster?
private readonly List<(string ID, bool IsDesc)> _order;
private readonly IReadOnlyDictionary> _predicates;
public MultiPredicateSort(IReadOnlyDictionary> predicates)
{
_count = predicates.Count;
if (_count == 0) throw new ArgumentException("must have at least 1 predicate", nameof(predicates));
_order = predicates.Select(kvp => (kvp.Key, false)).ToList();
_predicates = predicates;
}
public List AppliedTo(IReadOnlyCollection list)
{
var temp = _order[0].IsDesc
? list.OrderByDescending(_predicates[_order[0].ID])
: list.OrderBy(_predicates[_order[0].ID]);
for (var i = 1; i < _count; i++)
{
temp = _order[i].IsDesc
? temp.ThenByDescending(_predicates[_order[i].ID])
: temp.ThenBy(_predicates[_order[i].ID]);
}
return temp.ToList();
}
}
#endif
/// Sorts using a single primary predicate, with subsorts using the remaining predicates in order.
#if false
///
#endif
public sealed class RigidMultiPredicateSort
{
private readonly IReadOnlyDictionary> _predicates;
public RigidMultiPredicateSort(IReadOnlyDictionary> predicates)
{
if (predicates.Count == 0) throw new ArgumentException("must have at least 1 predicate", nameof(predicates));
_predicates = predicates;
}
/// sorts using asc/desc (by ), then by the remaining predicates, all asc
public List AppliedTo(IReadOnlyCollection list, string idOfFirst, bool firstIsDesc = false)
{
var temp = firstIsDesc
? list.OrderByDescending(_predicates[idOfFirst])
: list.OrderBy(_predicates[idOfFirst]);
foreach (var (id, pred) in _predicates)
{
if (id == idOfFirst) continue;
temp = temp.ThenBy(pred);
}
return temp.ToList();
}
public List AppliedTo(IReadOnlyCollection list, string idOfFirst, IReadOnlyDictionary isDescMap)
{
var temp = isDescMap[idOfFirst]
? list.OrderByDescending(_predicates[idOfFirst])
: list.OrderBy(_predicates[idOfFirst]);
foreach (var (id, pred) in _predicates)
{
if (id == idOfFirst) continue;
temp = isDescMap[id] ? temp.ThenByDescending(pred) : temp.ThenBy(pred);
}
return temp.ToList();
}
}
}