zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/Helper/IntFlagHelper.cs

187 lines
6.0 KiB
C#
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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>
public static int GetFlag(int value, int index, int flagValue, int flagCount = 0)
{
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;
}
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<long> listComponent = ListComponent<long>.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<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)}");
}
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<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)}");
}
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;
}
}
}
}
}