File: ValueMap.cs

package info (click to toggle)
lwip 2.2.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,008 kB
  • sloc: ansic: 109,524; cs: 6,714; sh: 115; makefile: 112; perl: 81
file content (103 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;

namespace Lextm.SharpSnmpLib.Mib
{
	public class ValueMap : Dictionary<Int64, string>
	{
		public ValueMap()
		{
		}

		/// <summary>
		/// Returns the values of the map as continuous range. At best as one range.
		/// </summary>
		/// <returns></returns>
		public ValueRanges GetContinousRanges()
		{
			ValueRanges result = new ValueRanges();

			if (this.Count > 0)
			{
				List<Int64> values = new List<long>(this.Keys);
				values.Sort();

				Int64 last   = values[0];
				Int64 offset = values[0];
				for (int i=1; i<values.Count; i++)
				{
					if (values[i] != last + 1)
					{
						if (last == offset)
						{
							result.Add(new ValueRange(offset, null));
						}
						else
						{
							result.Add(new ValueRange(offset, last));
						}

						offset = values[i];
					}

					last = values[i];
				}

				if (last == offset)
				{
					result.Add(new ValueRange(offset, null));
				}
				else
				{
					result.Add(new ValueRange(offset, last));
				}
			}

			return result;
		}

		/// <summary>
		/// Gets the highest value contained in this value map.
		/// </summary>
		/// <returns></returns>
		public Int64 GetHighestValue()
		{
			Int64 result = 0;

			foreach (Int64 value in this.Keys)
			{
				if (value > result)
				{
					result = value;
				}
			}

			return result;
		}

		/// <summary>
		/// Interprets the single values as bit positions and creates a mask of it.
		/// </summary>
		/// <returns></returns>
		public UInt32 GetBitMask()
		{
			UInt32 result = 0;

			foreach (Int64 key in this.Keys)
			{
				if (key < 0)
				{
					throw new NotSupportedException("Negative numbers are not allowed for Bits!");
				}
				if (key > 31)
				{
					throw new NotSupportedException("Bits with more than 32 bits are not supported!");
				}

				result |= (UInt32)(1 << (int)key);
			}

			return result;
		}
	}
}