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