using System; using System.Collections.Generic; using System.Linq; namespace GFGGame { public class Selection where K : IComparable { public K Min { get; set; } public K Max { get; set; } public T Value { get; set; } private readonly SelectionModel model; public Selection(K min, K max, T value, SelectionModel model = SelectionModel.Right) { Min = min; Max = max; Value = value; this.model = model; } public bool CheckInclude(K checkKey) { switch (model) { case SelectionModel.Right: return Min.CompareTo(checkKey) < 0 && checkKey.CompareTo(Max) <= 0; case SelectionModel.Left: return Min.CompareTo(checkKey) <= 0 && checkKey.CompareTo(Max) < 0; case SelectionModel.AllInclude: return Min.CompareTo(checkKey) <= 0 && checkKey.CompareTo(Max) <= 0; case SelectionModel.AllExclude: return Min.CompareTo(checkKey) < 0 && checkKey.CompareTo(Max) < 0; default: return false; } } public enum SelectionModel { Right, // ( ], 包含右边 Left, // [ ), 包含左边 AllInclude, // [], 两端包含 AllExclude //(), 两端都不包含 } public static List> CreateSelections(List> res, K min, K max, T defaultValue) { List> list = new List>(); // 添加首区间 KeyValuePair firstRes = res[0]; list.Add(new Selection(min, firstRes.Key, defaultValue, SelectionModel.Left)); for (int i = 1; i < res.Count - 1; i++) { list.Add(new Selection(res[i - 1].Key, res[i].Key, res[i].Value, SelectionModel.Left)); } KeyValuePair lastRes = res[res.Count - 1]; // 添加尾区间 list.Add(new Selection(lastRes.Key, max, lastRes.Value, SelectionModel.Left)); return list.OrderBy(x => x.Min).ToList(); } public static List> CreateSelections(List> res, K max) { List> list = new List>(); // 添加首区间 for (int i = 0; i < res.Count; i++) { K maxVal = i == res.Count - 1 ? max : res[i + 1].Key; list.Add(new Selection(res[i].Key, maxVal, res[i].Value, SelectionModel.Left)); } return list.OrderBy(x => x.Min).ToList(); } } }