-
Notifications
You must be signed in to change notification settings - Fork 2
/
BitVector32Extensions.cs
114 lines (107 loc) · 4.06 KB
/
BitVector32Extensions.cs
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
104
105
106
107
108
109
110
111
112
113
114
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace System.Collections.Specialized
{
[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class BitSectionAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly short _len;
// This is a positional argument
public BitSectionAttribute(short len)
{
_len = len;
}
public short Len
{
get { return _len; }
}
public int Index { get; set; }
internal BitVector32.Section Section { get; set; }
}
public static class BitVector32Extensions
{
/// <summary>
/// 取高位值
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int BitHeightValue(this int value)
{
BitVector32 vector32 = new BitVector32(value);
BitVector32.Section _58_height = BitVector32.CreateSection(0xf, BitVector32.CreateSection(0xf));
return vector32[_58_height];
}
/// <summary>
/// 去低位值
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int BitLowValue(this int value)
{
BitVector32 vector32 = new BitVector32(value);
BitVector32.Section _04_low = BitVector32.CreateSection(0xf);
return vector32[_04_low];
}
public static Dictionary<string,int> ToDictionary(this BitVector32 vector32,Dictionary<string,short> secs)
{
Dictionary<string, int > warn = new Dictionary<string, int> ();
List<(string pi, BitSectionAttribute bsa)> pairs = new List<(string pi, BitSectionAttribute bsa)> ();
secs.Keys.ToList().ForEach(p =>
{
var bs = new BitSectionAttribute(secs[p]);
if (bs != null)
{
pairs.Add((p, bs));
}
});
var lst = pairs.ToList();
for (int i = 0; i < lst.Count; i++)
{
var bsa = lst[i].bsa;
var pi = lst[i].pi;
if (i == 0)
{
bsa.Section = BitVector32.CreateSection((short)(Math.Pow(2, bsa.Len)- bsa.Len));
}
else
{
bsa.Section = BitVector32.CreateSection((short)(Math.Pow(2, bsa.Len )- bsa.Len), lst[i - 1].bsa.Section);
}
warn.Add(pi, vector32[bsa.Section]);
}
return warn;
}
public static T To<T>(this BitVector32 vector32) where T : class, new()
{
T warn = default;
List<(PropertyInfo pi, BitSectionAttribute bsa)> pairs = new List<(PropertyInfo pi, BitSectionAttribute bsa)>();
warn?.GetType().GetProperties().ToList().ForEach(p =>
{
var bs = p.GetCustomAttributes(typeof(BitSectionAttribute), true).ToList().FirstOrDefault() as BitSectionAttribute;
if (bs != null)
{
pairs.Add((p, bs));
}
});
var lst = pairs.OrderBy(k => k.bsa.Index).ToList();
for (int i = 0; i < lst.Count; i++)
{
var bsa = lst[i].bsa;
var pi = lst[i].pi;
if (i == 0)
{
bsa.Section = BitVector32.CreateSection((short)(2 ^ bsa.Len - 1));
}
else
{
bsa.Section = BitVector32.CreateSection((short)(2 ^ bsa.Len - 1), lst[i - 1].bsa.Section);
}
pi.SetValue(warn, vector32[bsa.Section]);
}
return warn;
}
}
}