/// <summary> /// 获取匹配值集合 /// </summary> /// <param name="isValue">数据匹配器</param> /// <param name="map">匹配结果位图</param> /// <returns>匹配值集合</returns> private valueType[] getFindArray(Func <valueType, bool> isValue, fixedMap map) { if (startIndex == 0) { int count = 0, index = 0; foreach (valueType value in array) { if (isValue(value)) { ++count; map.Set(index); } if (++index == length) { break; } } if (count != 0) { valueType[] values = new valueType[count]; for (index = length; count != 0; values[--count] = array[index]) { while (!map.Get(--index)) { ; } } return(values); } } else { int count = 0, index = startIndex, endIndex = startIndex + length; do { if (isValue(array[index])) { ++count; map.Set(index - startIndex); } }while (++index != endIndex); if (count != 0) { valueType[] values = new valueType[count]; for (index = length; count != 0; values[--count] = array[startIndex + index]) { while (!map.Get(--index)) { ; } } return(values); } } return(nullValue <valueType> .Array); }
/// <summary> /// 唯一静态哈希 /// </summary> /// <param name="values">数据集合</param> /// <param name="size">哈希容器尺寸</param> public unsafe uniqueHashSet(valueType[] values, int size) { if (values.length() > size || size <= 0) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } array = new valueType[size]; int length = ((size + 31) >> 5) << 2; byte * isValue = stackalloc byte[length]; fixedMap map = new fixedMap(isValue, length, 0); foreach (valueType value in values) { int index = value.GetHashCode(); if ((uint)index >= size) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } if (map.Get(index)) { log.Error.Throw(log.exceptionType.ErrorOperation); } map.Set(index); array[index] = value; } }
/// <summary> /// 唯一静态哈希字典 /// </summary> /// <param name="keys">键值数据集合</param> /// <param name="getValue">数据获取器</param> /// <param name="size">哈希容器尺寸</param> public unsafe uniqueDictionary(keyType[] keys, Func <keyType, valueType> getValue, int size) { int count = keys.length(); if (count > size || size <= 0) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } if (getValue == null) { log.Error.Throw(log.exceptionType.Null); } array = new keyValue <keyType, valueType> [size]; if (count != 0) { int length = ((size + 31) >> 5) << 2; byte * isValue = stackalloc byte[length]; fixedMap map = new fixedMap(isValue, length, 0); foreach (keyType key in keys) { int index = key.GetHashCode(); if ((uint)index >= size) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } if (map.Get(index)) { log.Error.Throw(log.exceptionType.ErrorOperation); } map.Set(index); array[index] = new keyValue <keyType, valueType>(key, getValue(key)); } } }
/// <summary> /// 预增数据流长度并写入长度与数据(4字节对齐) /// </summary> /// <param name="data">数据,不能为null</param> public unsafe void PrepSerialize(bool?[] array) { int mapLength = ((array.Length + 31) >> 5) << 2; int prepLength = sizeof(int) + mapLength + mapLength; Stream.PrepLength(prepLength); fixed(byte *dataFixed = Stream.array) { byte *write = dataFixed + Stream.Length; *(int *)write = array.Length; fixedMap nullMap = new fixedMap(write += sizeof(int)); fixedMap valueMap = new fixedMap(write += mapLength); mapLength = 0; foreach (bool?value in array) { if (value == null) { nullMap.Set(mapLength); } else if ((bool)value) { valueMap.Set(mapLength); } ++mapLength; } } Stream.Length += prepLength; }
/// <summary> /// 唯一静态哈希字典 /// </summary> /// <param name="values">数据集合</param> /// <param name="count">数据数量</param> /// <param name="size">哈希容器尺寸</param> private unsafe void fromArray(keyValue <keyType, valueType>[] values, int count, int size) { int length = ((size + 31) >> 5) << 2; byte * isValue = stackalloc byte[length]; fixedMap map = new fixedMap(isValue, length, 0); do { keyValue <keyType, valueType> value = values[--count]; int index = value.Key.GetHashCode(); if ((uint)index >= size) { log.Error.Throw(log.exceptionType.IndexOutOfRange); } if (map.Get(index)) { log.Error.Throw(log.exceptionType.ErrorOperation); } map.Set(index); array[index] = value; }while (count != 0); }