using System;
namespace ET
{
public static class IntFlagHelper
{
///
/// 获取标志
/// example : GetFlag(010203,1,2) == 2
///
/// 标志值
/// 第几个标志 (0,1,2,3)
/// 每个标志的大小,10进制移动次数
///
public static int GetFlag(int value, int index, int flagValue, int flagCount = 0)
{
int perFlag = 1;
while (flagValue-- > 0)
{
perFlag *= BIT;
}
using ListComponent listComponent = ListComponent.Create();
int temp = value;
while (temp != 0)
{
listComponent.List.Add(temp % perFlag);
temp /= perFlag;
}
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)];
}
public static long GetFlag(long value, int index, int flagValue, int flagCount = 0)
{
int perFlag = 1;
while (flagValue-- > 0)
{
perFlag *= BIT;
}
using ListComponent listComponent = ListComponent.Create();
long temp = value;
while (temp != 0)
{
listComponent.List.Add(temp % perFlag);
temp /= perFlag;
}
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)];
}
private const int BIT = 10;
public static void SetFlagValue(ref int oldValue, int index, int setValue, int flagValue, int flagCount = 0)
{
int perFlag = 1;
while (flagValue-- > 0)
{
perFlag *= BIT;
}
using ListComponent listComponent = ListComponent.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)}");
}
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}");
}
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;
}
oldValue += value;
}
}
}
public static void SetFlagValue(ref long oldValue, int index, long setValue, int flagValue, int flagCount = 0)
{
int perFlag = 1;
while (flagValue-- > 0)
{
perFlag *= BIT;
}
using ListComponent listComponent = ListComponent.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)}");
}
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}");
}
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;
}
oldValue += value;
}
}
}
}
}