/Main/src/DynamicDataDisplay.Maps/Servers/FileServers/ReadonlyTileCache.cs
C# | 105 lines | 84 code | 21 blank | 0 comment | 7 complexity | 52745d420a0029b8c3edcc09e118e144 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Collections; 6using System.Diagnostics; 7 8namespace Microsoft.Research.DynamicDataDisplay.Charts.Maps 9{ 10 [Serializable] 11 [DebuggerDisplay("Count = {Count}")] 12 public sealed class ReadonlyTileCache 13 { 14 private readonly Dictionary<TileIndex, ulong> cache = new Dictionary<TileIndex, ulong>(new TileIndex.TileIndexEqualityComparer()); 15 16 public ReadonlyTileCache() { } 17 18 public ReadonlyTileCache(Dictionary<TileIndex, ulong> dictionary) 19 { 20 this.cache = dictionary; 21 22 CalcMinMaxLevels(); 23 } 24 25 private const int levelDelta = 3; 26 private const int levelSize = 8; // 3 << 2 27 28 public void CalcMinMaxLevels() 29 { 30 minLevel = Int32.MaxValue; 31 maxLevel = Int32.MinValue; 32 33 foreach (var key in cache.Keys) 34 { 35 int level = (int)key.Level + levelDelta; 36 37 if (minLevel > level) 38 minLevel = level; 39 if (maxLevel < level) 40 maxLevel = level; 41 } 42 } 43 44 private int minLevel = 0; 45 public int MinLevel 46 { 47 get { return minLevel; } 48 } 49 50 private int maxLevel = Int32.MaxValue; 51 public int MaxLevel 52 { 53 get { return maxLevel; } 54 } 55 56 public void Add(TileIndex id, bool exists) 57 { 58 ulong cacheLine = 0; 59 TileIndex parent = id.GetLowerTile(levelDelta); 60 61 cache.TryGetValue(parent, out cacheLine); 62 63 int x = id.X - parent.X * levelSize; 64 int y = id.Y - parent.Y * levelSize; 65 66 int position = x * levelSize + y; 67 if (exists) 68 { 69 cacheLine |= ((ulong)1) << position; 70 } 71 else 72 { 73 cacheLine &= ~(((ulong)1) << position); 74 } 75 76 cache[parent] = cacheLine; 77 } 78 79 public bool Contains(TileIndex id) 80 { 81 int level = (int)id.Level; 82 if (level < minLevel || level > maxLevel) 83 return false; 84 85 TileIndex parent = id.GetLowerTile(levelDelta); 86 87 ulong cacheLine; 88 if (cache.TryGetValue(parent, out cacheLine)) 89 { 90 int x = id.X - parent.X * levelSize; 91 int y = id.Y - parent.Y * levelSize; 92 93 int position = x * levelSize + y; 94 95 return (cacheLine & ((ulong)1 << position)) != 0; 96 } 97 return false; 98 } 99 100 public int Count 101 { 102 get { return cache.Count; } 103 } 104 } 105}