/// <summary> /// 时间事件 /// </summary> private unsafe void runTime() { DateTime now = AutoCSer.Threading.SecondTimer.SetNow(); Monitor.Enter(timeLock); if (times.Length != 0) { fixed(DateTime *timeFixed = times.GetFixedBuffer()) { if (*timeFixed <= now) { times.Length = 0; minTime = DateTime.MaxValue; } else { DateTime *end = timeFixed + times.Length; while (*--end <= now) { ; } minTime = *end; times.Length = (int)(end - timeFixed) + 1; } } } Monitor.Exit(timeLock); run(); }
public unsafe static DeSerializeResult deSerialize <T>(this T value, ref LeftArray <byte> data, DeSerializeConfig config = null) { if (data.Length == 0) { return new DeSerializeResult { State = DeSerializeState.UnknownData } } ; fixed(byte *dataFixed = data.GetFixedBuffer()) return(BinaryDeSerializer.DeSerialize <T>(data.Array, dataFixed, data.Length, ref value, config)); }
/// <summary> /// 获取第一个匹配值 /// </summary> /// <param name="array">数组数据</param> /// <param name="isValue">数据匹配器</param> /// <param name="index">起始位置</param> /// <returns>第一个匹配值,失败为default(ulong)</returns> public static ulong firstOrDefault(this LeftArray <ulong> array, Func <ulong, bool> isValue, int index) { if ((uint)index < (uint)array.Length) { fixed(ulong *valueFixed = array.GetFixedBuffer()) { ulong *valueIndex = FixedArray.IndexOf(valueFixed + index, array.Length - index, isValue); if (valueIndex != null) { return(*valueIndex); } } } return(default(ulong)); }
/// <summary> /// 获取匹配数据位置 /// </summary> /// <param name="array">数据数组</param> /// <param name="isValue">数据匹配器</param> /// <returns>匹配位置,失败为-1</returns> public static int indexOf(this LeftArray <ulong> array, Func <ulong, bool> isValue) { if (array.Length != 0) { fixed(ulong *valueFixed = array.GetFixedBuffer()) { ulong *index = FixedArray.IndexOf(valueFixed, array.Length, isValue); if (index != null) { return((int)(index - valueFixed)); } } } return(-1); }
/// <summary> /// 替换数据 /// </summary> /// <param name="array">数组数据</param> /// <param name="value">新值</param> /// <param name="isValue">数据匹配器</param> public static LeftArray <ulong> replaceFirst(this LeftArray <ulong> array, ulong value, Func <ulong, bool> isValue) { if (array.Length != 0) { fixed(ulong *valueFixed = array.GetFixedBuffer()) { ulong *valueIndex = FixedArray.IndexOf(valueFixed, array.Length, isValue); if (valueIndex != null) { *valueIndex = value; } } } return(array); }
/// <summary> /// 获取匹配集合 /// </summary> /// <param name="array">数组数据</param> /// <param name="isValue">数据匹配器</param> /// <returns>匹配集合</returns> public static LeftArray <ulong> find(this LeftArray <ulong> array, Func <ulong, bool> isValue) { if (array.Length == 0) { return(array); fixed(ulong *valueFixed = array.GetFixedBuffer()) { ulong *write = valueFixed, start = valueFixed, end = valueFixed + array.Length; do { if (isValue(*start)) { *write++ = *start; } }while (++start != end); return(new LeftArray <ulong> { Array = array.Array, Length = (int)(write - valueFixed) }); } }
/// <summary> /// 获取匹配数量 /// </summary> /// <param name="array">数组数据</param> /// <param name="isValue">数据匹配器</param> /// <returns>匹配数量</returns> public static int count(this LeftArray <ulong> array, Func <ulong, bool> isValue) { if (array.Length == 0) { return(0); } int value = 0; fixed(ulong *valueFixed = array.GetFixedBuffer()) { ulong *start = valueFixed, end = valueFixed + array.Length; do { if (isValue(*start)) { ++value; } }while (++start != end); } return(value); }
/// <summary> /// 状态数据创建器 /// </summary> /// <param name="names">名称集合</param> /// <param name="isStaticUnmanaged">是否固定内存申请</param> internal CharBuilder(KeyValue <string, int>[] names, bool isStaticUnmanaged) { this.names = names; prefixSize = tableCount = stateCount = tableType = 0; state = charsAscii = charStart = charEnd = prefix = table = null; chars = new LeftArray <char>(0); if (names.Length > 1) { Data = new Pointer(); count(0, names.Length, 0); int charCount, asciiCount; System.Array.Sort(chars.Array, 0, chars.Length); fixed(char *charFixed = chars.GetFixedBuffer()) { char *start = charFixed + 1, end = charFixed + chars.Length, write = start; char value = *charFixed; if (*(end - 1) < 128) { while (start != end) { if (*start != value) { *write++ = value = *start; } ++start; } asciiCount = (int)(write - charFixed); charCount = 0; } else { while (value < 128) { while (*start == value) { ++start; } *write++ = value = *start++; } asciiCount = (int)(write - charFixed) - 1; while (start != end) { if (*start != value) { *write++ = value = *start; } ++start; } charCount = (int)(write - charFixed) - asciiCount; } chars.Length = asciiCount + charCount; int size = (1 + (stateCount += tableCount) * 3) * sizeof(int) + (128 + 2 + charCount + prefixSize) * sizeof(ushort); if (stateCount < 256) { size += tableCount * (chars.Length + 1); } else if (stateCount < 65536) { size += tableCount * (chars.Length + 1) * sizeof(ushort); tableType = 1; } else { size += tableCount * (chars.Length + 1) * sizeof(int); tableType = 2; } Data = isStaticUnmanaged ? Unmanaged.GetStaticPointer(size, true) : Unmanaged.GetPointer(size, true); *Data.Int = stateCount; //状态数量[int] state = Data.Byte + sizeof(int); //状态集合[stateCount*(前缀位置[int]+状态位置[int]+名称索引[int])] charsAscii = state + (stateCount * 3) * sizeof(int); //ascii字符查找表[128*ushort] charStart = charsAscii + 128 * sizeof(ushort); *(ushort *)charStart = (ushort)(asciiCount + 1); //特殊字符起始值[ushort] *(ushort *)(charStart + sizeof(ushort)) = (ushort)charCount; //特殊字符数量[ushort] charStart += sizeof(ushort) * 2; ushort charIndex = 0; for (start = charFixed, end = charFixed + asciiCount; start != end; ++start) { *(ushort *)(charsAscii + (*start << 1)) = ++charIndex; } charEnd = charStart; if (charCount != 0) {//特殊字符二分查找表[charCount*char] int charSize = charCount << 1; new Span <byte>(start, charSize).CopyTo(new Span <byte>(charStart, charSize)); charEnd += charSize; } prefix = charStart + charCount * sizeof(ushort); //前缀集合 table = prefix + prefixSize * sizeof(ushort); //状态矩阵[tableCount*(chars.Count+1)*[byte/ushort/int]] } stateCount = 0; create(0, names.Length, 0); } else if (names.Length != 0) { string name = names[0].Key; if (name.Length <= 128) { Data = isStaticUnmanaged ? Unmanaged.GetStaticPointer(sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort), false) : Unmanaged.GetPointer(sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort), false); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3; //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = names[0].Value; //名称索引 prefix = Data.Byte + sizeof(int) * 4; name.AsSpan().CopyTo(new Span <char>(prefix, name.Length)); *(char *)(prefix + (name.Length << 1)) = (char)0; *(int *)(Data.Byte + sizeof(int) * 4 + 128 * sizeof(ushort)) = 0; } else { int dataSize = sizeof(int) + sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort) + name.Length * sizeof(char) + sizeof(char); Data = isStaticUnmanaged ? Unmanaged.GetStaticPointer(dataSize, true) : Unmanaged.GetPointer(dataSize, true); *Data.Int = 1; //状态数量 state = Data.Byte + sizeof(int); *(int *)state = sizeof(int) * 3 + 128 * sizeof(ushort) + 2 * sizeof(ushort); //前缀位置 *(int *)(state + sizeof(int)) = 0; //状态位置 *(int *)(state + sizeof(int) * 2) = names[0].Value; //名称索引 name.AsSpan().CopyTo(new Span <char>(state + *(int *)state, name.Length)); } } else { Data = new Pointer(); } }