示例#1
0
        /// <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;
            }
        }
示例#2
0
            /// <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;
            }
示例#3
0
        /// <summary>
        /// 将字符串转换成正则代达式
        /// </summary>
        /// <param name="value">待转换成与正则表达式的字符串</param>
        /// <returns>转换后的正则表达式</returns>
        public unsafe static string toRegex(this string value)
        {
            if (value == null || value.Length == 0)
            {
                fixed(char *valueFixed = value)
                {
                    char *end   = valueFixed + value.Length;
                    int   count = unsafer.String.AsciiCount(valueFixed, end, regexEscapeMap.Map);

                    if (count != 0)
                    {
                        fixedMap map = new fixedMap(regexEscapeMap.Map);
                        value = fastCSharp.String.FastAllocateString(count += value.Length);
                        fixed(char *writeFixed = value)
                        {
                            for (char *start = valueFixed, write = writeFixed; start != end; *write++ = *start++)
                            {
                                if ((*start & 0xff80) == 0 && map.Get(*start))
                                {
                                    *write++ = '\\';
                                }
                            }
                        }
                    }
                }
            }
            return(value);
        }
示例#4
0
        /// <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));
                }
            }
        }
示例#5
0
 /// <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);
 }
示例#6
0
        /// <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);
        }
示例#7
0
 /// <summary>
 /// 将字符串转换成正则代达式
 /// </summary>
 /// <param name="value">待转换成与正则表达式的字符串</param>
 /// <returns>转换后的正则表达式</returns>
 public unsafe static string toRegex(this string value)
 {
     if (value == null || value.Length == 0)
     {
         fixed (char* valueFixed = value)
         {
             char* end = valueFixed + value.Length;
             int count = unsafer.String.asciiCount(valueFixed, end, regexEscapeMap.Byte, RegexEscape[0]);
             if (count != 0)
             {
                 fixedMap map = new fixedMap(regexEscapeMap);
                 value = new string((char)0, count += value.Length);
                 fixed (char* writeFixed = value)
                 {
                     for (char* start = valueFixed, write = writeFixed; start != end; *write++ = *start++)
                     {
                         if ((*start & 0xff80) == 0 && map.Get(*start)) *write++ = '\\';
                     }
                 }
             }
         }
     }
     return value;
 }