| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace GFGGame
- {
- public class Selection<K, T> where K : IComparable<K>
- {
- 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<Selection<K, T>> CreateSelections(List<KeyValuePair<K, T>> res, K min, K max, T defaultValue)
- {
- List<Selection<K, T>> list = new List<Selection<K, T>>();
- // 添加首区间
- KeyValuePair<K, T> firstRes = res[0];
- list.Add(new Selection<K, T>(min, firstRes.Key, defaultValue, SelectionModel.Left));
- for (int i = 1; i < res.Count - 1; i++)
- {
- list.Add(new Selection<K, T>(res[i - 1].Key, res[i].Key, res[i].Value, SelectionModel.Left));
- }
- KeyValuePair<K, T> lastRes = res[res.Count - 1];
- // 添加尾区间
- list.Add(new Selection<K, T>(lastRes.Key, max, lastRes.Value, SelectionModel.Left));
- return list.OrderBy(x => x.Min).ToList();
- }
- public static List<Selection<K, T>> CreateSelections(List<KeyValuePair<K, T>> res, K max)
- {
- List<Selection<K, T>> list = new List<Selection<K, T>>();
- // 添加首区间
- for (int i = 0; i < res.Count; i++)
- {
- K maxVal = i == res.Count - 1 ? max : res[i + 1].Key;
- list.Add(new Selection<K, T>(res[i].Key, maxVal, res[i].Value, SelectionModel.Left));
- }
- return list.OrderBy(x => x.Min).ToList();
- }
- }
- }
|