PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. namespace Microsoft.Research.DynamicDataDisplay.Charts.Maps
  8. {
  9. [Serializable]
  10. [DebuggerDisplay("Count = {Count}")]
  11. public sealed class ReadonlyTileCache
  12. {
  13. private readonly Dictionary<TileIndex, ulong> cache = new Dictionary<TileIndex, ulong>(new TileIndex.TileIndexEqualityComparer());
  14. public ReadonlyTileCache() { }
  15. public ReadonlyTileCache(Dictionary<TileIndex, ulong> dictionary)
  16. {
  17. this.cache = dictionary;
  18. CalcMinMaxLevels();
  19. }
  20. private const int levelDelta = 3;
  21. private const int levelSize = 8; // 3 << 2
  22. public void CalcMinMaxLevels()
  23. {
  24. minLevel = Int32.MaxValue;
  25. maxLevel = Int32.MinValue;
  26. foreach (var key in cache.Keys)
  27. {
  28. int level = (int)key.Level + levelDelta;
  29. if (minLevel > level)
  30. minLevel = level;
  31. if (maxLevel < level)
  32. maxLevel = level;
  33. }
  34. }
  35. private int minLevel = 0;
  36. public int MinLevel
  37. {
  38. get { return minLevel; }
  39. }
  40. private int maxLevel = Int32.MaxValue;
  41. public int MaxLevel
  42. {
  43. get { return maxLevel; }
  44. }
  45. public void Add(TileIndex id, bool exists)
  46. {
  47. ulong cacheLine = 0;
  48. TileIndex parent = id.GetLowerTile(levelDelta);
  49. cache.TryGetValue(parent, out cacheLine);
  50. int x = id.X - parent.X * levelSize;
  51. int y = id.Y - parent.Y * levelSize;
  52. int position = x * levelSize + y;
  53. if (exists)
  54. {
  55. cacheLine |= ((ulong)1) << position;
  56. }
  57. else
  58. {
  59. cacheLine &= ~(((ulong)1) << position);
  60. }
  61. cache[parent] = cacheLine;
  62. }
  63. public bool Contains(TileIndex id)
  64. {
  65. int level = (int)id.Level;
  66. if (level < minLevel || level > maxLevel)
  67. return false;
  68. TileIndex parent = id.GetLowerTile(levelDelta);
  69. ulong cacheLine;
  70. if (cache.TryGetValue(parent, out cacheLine))
  71. {
  72. int x = id.X - parent.X * levelSize;
  73. int y = id.Y - parent.Y * levelSize;
  74. int position = x * levelSize + y;
  75. return (cacheLine & ((ulong)1 << position)) != 0;
  76. }
  77. return false;
  78. }
  79. public int Count
  80. {
  81. get { return cache.Count; }
  82. }
  83. }
  84. }