2021-04-15 00:12:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace ET
|
|
|
|
|
{
|
|
|
|
|
public static class IntFlagHelper
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取标志
|
|
|
|
|
/// example : GetFlag(010203,1,2) == 2
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">标志值</param>
|
|
|
|
|
/// <param name="index">第几个标志 (0,1,2,3)</param>
|
|
|
|
|
/// <param name="flagValue">每个标志的大小,10进制移动次数</param>
|
|
|
|
|
/// <returns></returns>
|
2021-04-16 00:06:30 +08:00
|
|
|
|
public static int GetFlag(int value, int index, int flagValue, int flagCount = 0)
|
2021-04-15 00:12:07 +08:00
|
|
|
|
{
|
|
|
|
|
int perFlag = 1;
|
|
|
|
|
while (flagValue-- > 0)
|
|
|
|
|
{
|
|
|
|
|
perFlag *= BIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ListComponent<int> listComponent = ListComponent<int>.Create();
|
|
|
|
|
int temp = value;
|
|
|
|
|
while (temp != 0)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(temp % perFlag);
|
|
|
|
|
temp /= perFlag;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 00:06:30 +08:00
|
|
|
|
if (flagCount != 0)
|
|
|
|
|
{
|
|
|
|
|
if (flagCount > listComponent.List.Count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = listComponent.List.Count; i < flagCount; i++)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index >= listComponent.List.Count)
|
|
|
|
|
throw new IndexOutOfRangeException($"index is invalid when index = {index} and list.Count = {listComponent.List.Count}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listComponent.List[^(index+1)];
|
2021-04-15 00:12:07 +08:00
|
|
|
|
}
|
2021-04-16 00:06:30 +08:00
|
|
|
|
|
|
|
|
|
public static long GetFlag(long value, int index, int flagValue, int flagCount = 0)
|
2021-04-15 00:12:07 +08:00
|
|
|
|
{
|
|
|
|
|
int perFlag = 1;
|
|
|
|
|
while (flagValue-- > 0)
|
|
|
|
|
{
|
|
|
|
|
perFlag *= BIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ListComponent<long> listComponent = ListComponent<long>.Create();
|
|
|
|
|
long temp = value;
|
|
|
|
|
while (temp != 0)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(temp % perFlag);
|
|
|
|
|
temp /= perFlag;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 00:06:30 +08:00
|
|
|
|
if (flagCount != 0)
|
|
|
|
|
{
|
|
|
|
|
if (flagCount > listComponent.List.Count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = listComponent.List.Count; i < flagCount; i++)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index >= listComponent.List.Count)
|
|
|
|
|
throw new IndexOutOfRangeException($"index is invalid when index = {index} and list.Count = {listComponent.List.Count}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listComponent.List[^(index+1)];
|
2021-04-15 00:12:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const int BIT = 10;
|
|
|
|
|
|
2021-04-16 00:06:30 +08:00
|
|
|
|
public static void SetFlagValue(ref int oldValue, int index, int setValue, int flagValue, int flagCount = 0)
|
2021-04-15 00:12:07 +08:00
|
|
|
|
{
|
|
|
|
|
int perFlag = 1;
|
|
|
|
|
while (flagValue-- > 0)
|
|
|
|
|
{
|
|
|
|
|
perFlag *= BIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ListComponent<int> listComponent = ListComponent<int>.Create();
|
|
|
|
|
int temp = oldValue;
|
|
|
|
|
while (temp != 0)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(temp % perFlag);
|
|
|
|
|
temp /= perFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Math.Pow(perFlag, listComponent.List.Count) > int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
throw new OverflowException($"the value is too big : {oldValue} > {Math.Pow(perFlag, listComponent.List.Count)}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 00:06:30 +08:00
|
|
|
|
if (flagCount != 0)
|
|
|
|
|
{
|
|
|
|
|
if (flagCount > listComponent.List.Count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = listComponent.List.Count; i < flagCount; i++)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index >= listComponent.List.Count)
|
|
|
|
|
throw new IndexOutOfRangeException($"index is invalid when index = {index} and list.Count = {listComponent.List.Count}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 00:12:07 +08:00
|
|
|
|
listComponent.List[^(index + 1)] = setValue;
|
|
|
|
|
oldValue = 0;
|
|
|
|
|
checked
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < listComponent.List.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
int value = listComponent.List[i];
|
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
|
{
|
|
|
|
|
value *= perFlag;
|
|
|
|
|
}
|
2021-04-16 00:06:30 +08:00
|
|
|
|
|
2021-04-15 00:12:07 +08:00
|
|
|
|
oldValue += value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-16 00:06:30 +08:00
|
|
|
|
|
|
|
|
|
public static void SetFlagValue(ref long oldValue, int index, long setValue, int flagValue, int flagCount = 0)
|
2021-04-15 00:12:07 +08:00
|
|
|
|
{
|
|
|
|
|
int perFlag = 1;
|
|
|
|
|
while (flagValue-- > 0)
|
|
|
|
|
{
|
|
|
|
|
perFlag *= BIT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ListComponent<long> listComponent = ListComponent<long>.Create();
|
|
|
|
|
long temp = oldValue;
|
|
|
|
|
while (temp != 0)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(temp % perFlag);
|
|
|
|
|
temp /= perFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Math.Pow(perFlag, listComponent.List.Count) > long.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
throw new OverflowException($"the value is too big : {oldValue} > {Math.Pow(perFlag, listComponent.List.Count)}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 00:06:30 +08:00
|
|
|
|
if (flagCount != 0)
|
|
|
|
|
{
|
|
|
|
|
if (flagCount > listComponent.List.Count)
|
|
|
|
|
{
|
|
|
|
|
for (int i = listComponent.List.Count; i < flagCount; i++)
|
|
|
|
|
{
|
|
|
|
|
listComponent.List.Add(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index >= listComponent.List.Count)
|
|
|
|
|
throw new IndexOutOfRangeException($"index is invalid when index = {index} and list.Count = {listComponent.List.Count}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 00:12:07 +08:00
|
|
|
|
listComponent.List[^(index + 1)] = setValue;
|
|
|
|
|
oldValue = 0;
|
|
|
|
|
checked
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < listComponent.List.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
long value = listComponent.List[i];
|
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
|
{
|
|
|
|
|
value *= perFlag;
|
|
|
|
|
}
|
2021-04-16 00:06:30 +08:00
|
|
|
|
|
2021-04-15 00:12:07 +08:00
|
|
|
|
oldValue += value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|