wasSharp – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Linq; |
||
10 | |||
11 | namespace wasSharp.Collections.Generic |
||
12 | { |
||
13 | /////////////////////////////////////////////////////////////////////////// |
||
14 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
15 | /////////////////////////////////////////////////////////////////////////// |
||
16 | /// <summary> |
||
17 | /// A collection that maps ranges to values with O(1) complexity |
||
18 | /// lookups and O(n) insertions. |
||
19 | /// </summary> |
||
20 | /// <typeparam name="T">the type of value to store</typeparam> |
||
21 | public class RangeCollection<T> : IEnumerable |
||
22 | { |
||
23 | private readonly Dictionary<int, T> map; |
||
24 | |||
25 | public RangeCollection(int min, int max) |
||
26 | { |
||
27 | map = new Dictionary<int, T>(max - min); |
||
28 | } |
||
29 | |||
30 | public T this[int x] |
||
31 | { |
||
32 | get |
||
33 | { |
||
34 | T value; |
||
35 | return map.TryGetValue(x, out value) ? value : default(T); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public IEnumerator GetEnumerator() |
||
40 | { |
||
27 | office | 41 | return ((IEnumerable)map).GetEnumerator(); |
10 | office | 42 | } |
43 | |||
44 | /// <summary> |
||
45 | /// Map a value to a range. |
||
46 | /// </summary> |
||
47 | /// <param name="Value">the value for the range</param> |
||
48 | /// <param name="min">the minimal range</param> |
||
49 | /// <param name="max">the maximal range</param> |
||
50 | public void Add(T Value, int min, int max) |
||
51 | { |
||
52 | foreach (var i in Enumerable.Range(min, max - min + 1)) |
||
53 | { |
||
54 | map.Add(i, Value); |
||
55 | } |
||
56 | } |
||
57 | } |
||
27 | office | 58 | } |