using System;
using System.Collections.Generic;
using System.Linq;
namespace CommonUtil
{
///
/// 公共简易算法
///
public static class UtilAlgorithm
{
#region 生成编号
//生成编号显示字符,原始
static readonly char[] NO_patternOld = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'X', 'Y' };
//生成编号显示字符,乱序
static readonly char[] NO_pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'X', 'Y' };
static readonly List<char> NO_patternList = NO_pattern.ToList();
///
/// 生成编号
///
/// 编号位数
/// 编号数值
///
public static string NO_CreateNumberString(int len, out int[] v)
{
//生成len个小于_pattern.Length的整数
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
v = new int[len];
string nu = "";
for (int i = 0; i < len; i++)
{
v[i]= random.Next(0, NO_pattern.Length);
nu += NO_pattern[v[i]];
}
return nu;
}
///
/// 下一个编号
///
///
public static string NO_NumberStringNext(string nu)
{
var v = NO_NumberStringToInts(nu);
v = NO_IntsNext(v);
return NO_IntsToNumberString(v);
}
///
/// int数组转编号字符
///
///
///
public static string NO_IntsToNumberString(int[] v)
{
string nu = "";
for (int i = 0; i < v.Length; i++)
{
nu += NO_pattern[v[i]];
}
return nu;
}
///
/// 编号字符转int数组
///
///
///
public static int[] NO_NumberStringToInts(string nu)
{
int[] v = new int[nu.Length];
for (int i = 0; i < nu.Length; i++)
{
v[i] = NO_patternList.IndexOf(nu[i]);
}
return v;
}
///
/// int数组下一个编号
///
///
///
public static int[] NO_IntsNext(int[] v,int? index=null)
{
int i = index ?? v.Length - 1;
if (i < 0)
{
//无法增加
throw new Exception("CreateReportNumberLast 超出界限");
return null;
}
if (v[i] < NO_pattern.Length - 1)
{
v[i]++;
return v;
}
else
{
v[i] = 0;
return NO_IntsNext(v, i - 1);
}
}
#endregion
}
}