using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hydro.CommonBase { [Serializable] public class DRange { public DRange(double min, double max) { Min = min; Max = max; } public DRange(DRange range) { if (range==null) { Min = 0; Max = 0; } else { Min = range.Min; Max = range.Max; } } public DRange() { Min = 0; Max = 0; } public double Min { get; set; } public double Max { get; set; } public double Length { get { return Max - Min; } } public bool IsInside(double x) { return x >= Min && x <= Max; } public bool isValid { get { return Min <= Max; } } public void Union(DRange range) { if (range.Min < Min) { Min = range.Min; } if (range.Max > Max) { Max = range.Max; } } public static DRange Union(DRange range1, DRange range2) { DRange range = new DRange(range1); range.Union(range2); return range; } public override string ToString() { return $"{Min:0.000}-{Max:0.000}"; } } public class IndexedDictionary : Dictionary { private List keys = new List(); public TValue this[int index] { get { return this[keys[index]]; } set { this[keys[index]] = value; } } public new void Add(TKey key, TValue value) { base.Add(key, value); keys.Add(key); } public new bool Remove(TKey key) { if (base.Remove(key)) { keys.Remove(key); return true; } return false; } } public class TStringList { private List _list = new List(); private Dictionary _objectDict = new Dictionary(); public int Count => _list.Count; public TStringList() { } public TStringList(IEnumerable list) { foreach (var o in list) { string key = ""; if (o != null) { key = o.ToString(); } string key0 = key; int i = 1; while (_objectDict.ContainsKey(key0)) { key0 = key + i++; } this._list.Add(key0); _objectDict.Add(key0, o); } // = list; //_list.ForEach(l => _objectDict.Add(l,null)); } public Dictionary ToDict() { return _objectDict; } // 添加元素和关联的对象 public void Add(string s, object obj = null) { _list.Add(s); _objectDict[s] = obj; } public void Remove(int index) { _objectDict.Remove(_list[index]); _list.RemoveAt(index); } // 获取元素关联的对象 public object Objects(int index) => _objectDict[_list[index]]; // 设置文本,以换行符分隔的字符串 public void SetText(string text) { _list.Clear(); _objectDict.Clear(); string[] lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { _list.Add(line); _objectDict[line] = null; } } public void Free() { _list.Clear(); _objectDict.Clear(); } public void Clear() { Free(); } public TStringList Values { get { List list = new List(); foreach (string s in _list) { list.Add(_objectDict[s]); } return new TStringList(list); } } public object this[int index] { get { //return _list[index].ToString(); return _objectDict[_list[index]]; } set { _objectDict[_list[index]] = value; } } // Assign 方法,将另一个 TStringList 的内容复制到当前列表 public void Assign(TStringList sourceList) { Clear(); foreach (string item in sourceList._list) { Add(item); } } } [Serializable] public class SaveDict: ConcurrentDictionary { //ConcurrentDictionary _dict; public SaveDict():base() { //_dict = new ConcurrentDictionary(); } public SaveDict(IEnumerable> list):base(list) { //_dict = new ConcurrentDictionary(list); } } }