ValueMap.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Lextm.SharpSnmpLib.Mib
  4. {
  5. public class ValueMap : Dictionary<Int64, string>
  6. {
  7. public ValueMap()
  8. {
  9. }
  10. /// <summary>
  11. /// Returns the values of the map as continuous range. At best as one range.
  12. /// </summary>
  13. /// <returns></returns>
  14. public ValueRanges GetContinousRanges()
  15. {
  16. ValueRanges result = new ValueRanges();
  17. if (this.Count > 0)
  18. {
  19. List<Int64> values = new List<long>(this.Keys);
  20. values.Sort();
  21. Int64 last = values[0];
  22. Int64 offset = values[0];
  23. for (int i=1; i<values.Count; i++)
  24. {
  25. if (values[i] != last + 1)
  26. {
  27. if (last == offset)
  28. {
  29. result.Add(new ValueRange(offset, null));
  30. }
  31. else
  32. {
  33. result.Add(new ValueRange(offset, last));
  34. }
  35. offset = values[i];
  36. }
  37. last = values[i];
  38. }
  39. if (last == offset)
  40. {
  41. result.Add(new ValueRange(offset, null));
  42. }
  43. else
  44. {
  45. result.Add(new ValueRange(offset, last));
  46. }
  47. }
  48. return result;
  49. }
  50. /// <summary>
  51. /// Gets the highest value contained in this value map.
  52. /// </summary>
  53. /// <returns></returns>
  54. public Int64 GetHighestValue()
  55. {
  56. Int64 result = 0;
  57. foreach (Int64 value in this.Keys)
  58. {
  59. if (value > result)
  60. {
  61. result = value;
  62. }
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// Interprets the single values as bit positions and creates a mask of it.
  68. /// </summary>
  69. /// <returns></returns>
  70. public UInt32 GetBitMask()
  71. {
  72. UInt32 result = 0;
  73. foreach (Int64 key in this.Keys)
  74. {
  75. if (key < 0)
  76. {
  77. throw new NotSupportedException("Negative numbers are not allowed for Bits!");
  78. }
  79. if (key > 31)
  80. {
  81. throw new NotSupportedException("Bits with more than 32 bits are not supported!");
  82. }
  83. result |= (UInt32)(1 << (int)key);
  84. }
  85. return result;
  86. }
  87. }
  88. }