/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System.Collections; using System.Collections.Generic; using System.Linq; namespace wasSharp.Collections.Generic { /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// A collection that maps ranges to values with O(1) complexity /// lookups and O(n) insertions. /// /// the type of value to store public class RangeCollection : IEnumerable { private readonly Dictionary map; public RangeCollection(int min, int max) { map = new Dictionary(max - min); } public T this[int x] { get { T value; return map.TryGetValue(x, out value) ? value : default(T); } } public IEnumerator GetEnumerator() { return ((IEnumerable)map).GetEnumerator(); } /// /// Map a value to a range. /// /// the value for the range /// the minimal range /// the maximal range public void Add(T Value, int min, int max) { foreach (var i in Enumerable.Range(min, max - min + 1)) { map.Add(i, Value); } } } }