Esempio n. 1
0
 /// <summary>
 /// 清楚数据
 /// </summary>
 public void Clear()
 {
     ParameterIndexs.Empty();
     ParameterIndex = 0;
 }
Esempio n. 2
0
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="bitmap">位图</param>
        /// <param name="leftOffset">X方向偏移量</param>
        /// <param name="topOffset">Y方向偏移量</param>
        /// <param name="width">图象宽度</param>
        /// <param name="height">图象高度</param>
        /// <param name="bitmapLeftOffset">位图剪切X方向偏移量</param>
        /// <param name="bitmapTopOffset">位图剪切Y方向偏移量</param>
        /// <param name="isInterlace">图象数据是否连续方式排列,否则使用顺序排列</param>
        /// <param name="maxPixel">最大色彩深度</param>
        /// <returns>图片是否添加成功</returns>
        internal unsafe bool addImage(BitmapData bitmap, int bitmapLeftOffset, int bitmapTopOffset
                                      , int leftOffset, int topOffset, int width, int height, bool isInterlace, byte maxPixel)
        {
            if (fileBuffer == null)
            {
                return(false);

                fixed(Color *colorFixed = colors)
                fixed(int *colorCountFixed = colorCounts)
                {
                    byte * bitmapFixed = (byte *)bitmap.Scan0, currentBitmap = bitmapFixed + bitmap.Stride * (bitmapTopOffset - 1) + (bitmapLeftOffset + width) * 3;
                    Color *currentColor = colorFixed;
                    int    bitMapSpace  = bitmap.Stride - (width << 1) - width;

                    colorIndexs.Empty();
                    for (int colorIndex, row = height; row != 0; --row)
                    {
                        currentBitmap += bitMapSpace;
                        for (int col = width; col != 0; --col)
                        {
                            Color color = new Color {
                                Green = *currentBitmap++, Blue = *currentBitmap++, Red = *currentBitmap++
                            };
                            if (colorIndexs.TryGetValue(color, out colorIndex))
                            {
                                ++colorCountFixed[colorIndex];
                            }
                            else
                            {
                                colorIndexs.Set(color, colorIndex = (int)(currentColor - colorFixed));
                                colorCountFixed[colorIndex]       = 1;
                            }
                            *currentColor++ = color;
                        }
                    }
                    int pixel = ((uint)colorIndexs.Count).bits() - 1;

                    if ((1 << pixel) != colorIndexs.Count)
                    {
                        ++pixel;
                    }
                    if (pixel > maxPixel)
                    {
                        pixel = maxPixel;
                    }
                    else if (pixel < 2)
                    {
                        pixel = 2;
                    }
                    int maxColorCount = 1 << pixel;

                    fixed(byte *bufferFixed = fileBuffer)
                    {
                        byte *currentBuffer = bufferFixed + bufferIndex;

                        *currentBuffer = 0x2c;
                        *(short *)(currentBuffer + 1) = (short)leftOffset;
                        *(short *)(currentBuffer + 3) = (short)topOffset;
                        *(short *)(currentBuffer + 5) = (short)width;
                        *(short *)(currentBuffer + 7) = (short)height;
                        *(currentBuffer + 9)          = (byte)(0x80 + (isInterlace ? 0x40 : 0) + (pixel - 1));
                        if (!checkBuffer(bufferFixed, 10))
                        {
                            return(false);
                        }
                    }

                    if (colorIndexs.Count <= maxColorCount)
                    {
                        fixed(byte *bufferFixed = fileBuffer)
                        {
                            int *currentColorCount = colorCountFixed;

                            foreach (Color colorKey in colorIndexs.Keys)
                            {
                                *currentColorCount++ = colorKey.Value;
                            }
                            Color color             = new Color();
                            int   currentColorIndex = 0;
                            byte *currentBuffer     = bufferFixed + bufferIndex;

                            while (currentColorCount != colorCountFixed)
                            {
                                color.Value = *--currentColorCount;
                                *currentBuffer++ = color.Red;
                                *currentBuffer++ = color.Blue;
                                *currentBuffer++ = color.Green;
                                colorIndexs.Set(color, currentColorIndex++);
                            }
                            *(bufferFixed + bufferIndex + (maxColorCount << 1) + maxColorCount) = (byte)pixel;
                            if (!checkBuffer(bufferFixed, (maxColorCount << 1) + maxColorCount + 1))
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        int           indexCount = colorIndexs.Count;
                        UnmanagedPool pool       = UnmanagedPool.GetDefaultPool(indexCount * sizeof(IntSortIndex));
                        Pointer.Size  sizeBuffer = pool.GetSize(indexCount * (sizeof(IntSortIndex) + sizeof(int)));
                        int *         buffer     = sizeBuffer.Int;
                        try
                        {
                            IntSortIndex *indexFixed = (IntSortIndex *)(buffer + indexCount), currentSortIndex = indexFixed;
                            foreach (KeyValue <Color, int> colorIndex in colorIndexs.KeyValues)
                            {
                                int color0 = colorIndex.Key.Value;
                                int color3 = ((color0 >> 3) & 0x111111) * 0x1020400;
                                int color2 = ((color0 >> 2) & 0x111111) * 0x1020400;
                                int color1 = ((color0 >> 1) & 0x111111) * 0x1020400;
                                color0 = (color0 & 0x111111) * 0x1020400;
                                (*currentSortIndex++).Set((color3 & 0x70000000) | ((color2 >> 4) & 0x7000000)
                                                          | ((color1 >> 8) & 0x700000) | ((color0 >> 12) & 0x70000) | ((color3 >> 12) & 0x7000)
                                                          | ((color2 >> 16) & 0x700) | ((color1 >> 20) & 0x70) | ((color0 >> 24) & 7), colorIndex.Value);
                            }
                            AutoCSer.Algorithm.FixedArrayQuickSort.sort(indexFixed, indexFixed + indexCount - 1);
                            int *currentSortArray;
                            if (maxColorCount != 2)
                            {
                                currentSortArray = buffer;
                                for (int currentColorCode, lastColorCode = (*--currentSortIndex).Value; currentSortIndex != indexFixed; lastColorCode = currentColorCode)
                                {
                                    currentColorCode = (*--currentSortIndex).Value;
                                    *currentSortArray++ = lastColorCode - currentColorCode;
                                }
                                currentSortArray = buffer + (maxColorCount >> 1) - 2;
                                new AutoCSer.Algorithm.FixedArrayQuickRangeSort.IntRangeSorterDesc {
                                    SkipCount = currentSortArray, GetEndIndex = currentSortArray
                                }.Sort(buffer, buffer + indexCount - 2);
                                int minColorDifference = *currentSortArray, minColorDifferenceCount = 1;
                                while (currentSortArray != buffer)
                                {
                                    if (*--currentSortArray == minColorDifference)
                                    {
                                        ++minColorDifferenceCount;
                                    }
                                }
                                currentSortIndex = indexFixed + indexCount;
                                int maxCountIndex = (*--currentSortIndex).Index, maxCount = *(colorCountFixed + maxCountIndex);
                                for (int currentColorCode, lastColorCode = (*currentSortIndex).Value; currentSortIndex != indexFixed; lastColorCode = currentColorCode)
                                {
                                    currentColorCode = (*--currentSortIndex).Value;
                                    int colorDifference = lastColorCode - currentColorCode;
                                    if (colorDifference >= minColorDifference)
                                    {
                                        if (colorDifference == minColorDifference && --minColorDifferenceCount == 0)
                                        {
                                            ++minColorDifference;
                                        }
                                        *(colorCountFixed + maxCountIndex) = int.MaxValue;
                                        maxCount = *(colorCountFixed + (maxCountIndex = (*currentSortIndex).Index));
                                    }
                                    else
                                    {
                                        int countIndex = (*currentSortIndex).Index, count = *(colorCountFixed + countIndex);
                                        if (count > maxCount)
                                        {
                                            maxCountIndex = countIndex;
                                            maxCount      = count;
                                        }
                                    }
                                }
                                *(colorCountFixed + maxCountIndex) = int.MaxValue;
                            }
                            for (currentSortArray = buffer + indexCount, currentSortIndex = indexFixed; currentSortArray != buffer; *(--currentSortArray) = *(colorCountFixed + (*currentSortIndex++).Index))
                            {
                                ;
                            }
                            currentSortArray = buffer + maxColorCount - 1;
                            new AutoCSer.Algorithm.FixedArrayQuickRangeSort.IntRangeSorterDesc {
                                SkipCount = currentSortArray, GetEndIndex = currentSortArray
                            }.Sort(buffer, buffer + indexCount - 1);
                            int minColorCount = *currentSortArray, minColorCounts = 1;
                            while (currentSortArray != buffer)
                            {
                                if (*--currentSortArray == minColorCount)
                                {
                                    ++minColorCounts;
                                }
                            }
                            fixed(byte *fileBufferFixed = fileBuffer)
                            {
                                byte *        currentBuffer = fileBufferFixed + bufferIndex;
                                IntSortIndex *lastSortIndex = indexFixed, endSortIndex = indexFixed + indexCount;

                                while (*(colorCountFixed + (*lastSortIndex).Index) < minColorCount)
                                {
                                    colorIndexs.Set(*(colorFixed + (*lastSortIndex++).Index), 0);
                                }
                                if (*(colorCountFixed + (*lastSortIndex).Index) == minColorCount && --minColorCounts == 0)
                                {
                                    ++minColorCount;
                                }
                                Color outputColor = *(colorFixed + (*lastSortIndex).Index);

                                *currentBuffer++ = outputColor.Red;
                                *currentBuffer++ = outputColor.Blue;
                                *currentBuffer++ = outputColor.Green;
                                colorIndexs.Set(outputColor, 0);
                                for (--maxColorCount; *(colorCountFixed + (*--endSortIndex).Index) < minColorCount; colorIndexs.Set(*(colorFixed + (*endSortIndex).Index), maxColorCount))
                                {
                                    ;
                                }
                                if (*(colorCountFixed + (*endSortIndex).Index) == minColorCount && --minColorCounts == 0)
                                {
                                    ++minColorCount;
                                }
                                colorIndexs.Set(*(colorFixed + (*endSortIndex).Index), maxColorCount++);
                                int currentColorIndex = 0;

                                for (int *lastColorCount = colorCountFixed + (*endSortIndex).Index; lastSortIndex != endSortIndex;)
                                {
                                    for (*lastColorCount = 0; *(colorCountFixed + (*++lastSortIndex).Index) >= minColorCount; colorIndexs.Set(outputColor, ++currentColorIndex))
                                    {
                                        if (*(colorCountFixed + (*lastSortIndex).Index) == minColorCount && --minColorCounts == 0)
                                        {
                                            ++minColorCount;
                                        }
                                        outputColor = *(colorFixed + (*lastSortIndex).Index);
                                        *currentBuffer++ = outputColor.Red;
                                        *currentBuffer++ = outputColor.Blue;
                                        *currentBuffer++ = outputColor.Green;
                                    }
                                    if (lastSortIndex == endSortIndex)
                                    {
                                        break;
                                    }
                                    *lastColorCount = int.MaxValue;
                                    IntSortIndex *nextSortIndex = lastSortIndex;
                                    while (*(colorCountFixed + (*++nextSortIndex).Index) < minColorCount)
                                    {
                                        ;
                                    }
                                    for (int lastColorCode = (*(lastSortIndex - 1)).Value, nextColorCode = (*nextSortIndex).Value; lastSortIndex != nextSortIndex; ++lastSortIndex)
                                    {
                                        colorIndexs.Set(*(colorFixed + (*lastSortIndex).Index), (*lastSortIndex).Value - lastColorCode <= nextColorCode - (*lastSortIndex).Value ? currentColorIndex : (currentColorIndex + 1));
                                    }
                                    if (lastSortIndex != endSortIndex)
                                    {
                                        if (*(colorCountFixed + (*lastSortIndex).Index) == minColorCount && --minColorCounts == 0)
                                        {
                                            ++minColorCount;
                                        }
                                        outputColor = *(colorFixed + (*lastSortIndex).Index);
                                        *currentBuffer++ = outputColor.Red;
                                        *currentBuffer++ = outputColor.Blue;
                                        *currentBuffer++ = outputColor.Green;
                                        colorIndexs.Set(outputColor, ++currentColorIndex);
                                    }
                                }
                                outputColor = *(colorFixed + (*lastSortIndex).Index);
                                *currentBuffer++ = outputColor.Red;
                                *currentBuffer++ = outputColor.Blue;
                                *currentBuffer++ = outputColor.Green;
                                *currentBuffer   = (byte)pixel;
                                if (!checkBuffer(fileBufferFixed, (maxColorCount << 1) + maxColorCount + 1))
                                {
                                    return(false);
                                }
                            }
                        }
                        finally { pool.Push(ref sizeBuffer); }
                    }
                    byte *colorIndexFixed = (byte *)colorCountFixed;

                    if (isInterlace)
                    {
                        Color *colorEnd   = colorFixed + width * height;
                        int    inputSpace = (width << 3) - width;
                        for (Color *inputColor = colorFixed; inputColor < colorEnd; inputColor += inputSpace)
                        {
                            for (Color *inputEnd = inputColor + width; inputColor != inputEnd; *colorIndexFixed++ = (byte)colorIndexs[*inputColor++])
                            {
                                ;
                            }
                        }
                        for (Color *inputColor = colorFixed + (width << 2); inputColor < colorEnd; inputColor += inputSpace)
                        {
                            for (Color *inputEnd = inputColor + width; inputColor != inputEnd; *colorIndexFixed++ = (byte)colorIndexs[*inputColor++])
                            {
                                ;
                            }
                        }
                        inputSpace -= width << 2;
                        for (Color *inputColor = colorFixed + (width << 1); inputColor < colorEnd; inputColor += inputSpace)
                        {
                            for (Color *inputEnd = inputColor + width; inputColor != inputEnd; *colorIndexFixed++ = (byte)colorIndexs[*inputColor++])
                            {
                                ;
                            }
                        }
                        for (Color *inputColor = colorFixed + width; inputColor < colorEnd; inputColor += width)
                        {
                            for (Color *inputEnd = inputColor + width; inputColor != inputEnd; *colorIndexFixed++ = (byte)colorIndexs[*inputColor++])
                            {
                                ;
                            }
                        }
                    }
                    else
                    {
                        for (Color *inputColor = colorFixed, inputEnd = colorFixed + width * height; inputColor != inputEnd; *colorIndexFixed++ = (byte)colorIndexs[*inputColor++])
                        {
                            ;
                        }
                    }
                    return(lzwEncode((byte *)colorCountFixed, colorIndexFixed, pixel));
                }
        }
Esempio n. 3
0
            /// <summary>
            /// 安装下一个类型
            /// </summary>
            protected override void nextCreate()
            {
                if ((Attribute.IsAbstract || Type.Type.IsSealed || !Type.Type.IsAbstract) && !Type.Type.IsInterface)
                {
                    defaultServerName = Attribute.ServerName;
                    defaultServer     = null;
                    defaultType       = null;
                    if (defaultServerName != null)
                    {
                        HashString nameKey = defaultServerName;
                        if (!servers.TryGetValue(nameKey, out defaultServer))
                        {
                            servers.Add(nameKey, defaultServer = new Server());
                        }
                        defaultServer.Attribute.Name        = defaultServerName;
                        defaultServer.Types.Add(defaultType = new ServerType {
                            Type = Type, Attribute = Attribute
                        });
                        if (Attribute.IsServer)
                        {
                            defaultServer.AttributeType = Type;
                            defaultServer.Attribute.CopyFrom(Attribute);
                        }
                    }
                    RemoteLinkType remoteLinkType = null;
                    if (Attribute.ServerName != null)
                    {
                        if (Attribute.IsRemoteLinkType)
                        {
                            remoteLinkType = new RemoteLinkType {
                                Type = Type, ParameterType = Type
                            }
                        }
                        ;
                        else if (Attribute.IsRemoteLink)
                        {
                            remoteLinkType = GetRemoteLinkType(Type);
                        }
                    }
                    LeftArray <RemoteMethod> remoteMethods = new LeftArray <RemoteMethod>(0);
                    foreach (MethodIndex method in MethodIndex.GetMethods <AutoCSer.Net.TcpStaticServer.MethodAttribute>(Type, Attribute.GetMemberFilters, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute))
                    {
                        next(new TcpMethod {
                            Method = method, MethodType = Type
                        });
                        if (remoteLinkType != null && !method.Method.IsGenericMethodDefinition)
                        {
                            AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute remoteAttribute = method.GetSetupAttribute <AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute>(false);
                            if (remoteAttribute != null && remoteAttribute.IsClientRemoteMember)
                            {
                                MethodParameter[] parameters = method.Parameters;
                                if (parameters.Length >= 1 && parameters[0].ParameterType.Type == remoteLinkType.ParameterType)
                                {
                                    remoteMethods.Add(new RemoteMethod {
                                        Type = Type, Method = method, MethodParameters = parameters, Attribute = remoteAttribute
                                    });
                                }
                            }
                        }
                    }
                    if (remoteLinkType != null)
                    {
                        remoteLinkType.RemoteMethods = remoteMethods.ToArray();

                        LeftArray <RemoteMember> remoteMembers = new LeftArray <RemoteMember>(0);
                        foreach (MemberIndex member in MemberIndex.GetMembers <AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute>(Type, AutoCSer.Metadata.MemberFilters.NonPublicInstanceProperty, true, false))
                        {
                            if (member.CanGet)
                            {
                                AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute remoteAttribute = member.GetSetupAttribute <AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute>(false);
                                if (remoteAttribute != null)
                                {
                                    if (member.IsField || ((PropertyInfo)member.Member).GetIndexParameters().Length == 0)
                                    {
                                        remoteMembers.Add(new RemoteMember {
                                            Member = member, Attribute = remoteAttribute
                                        });
                                    }
                                }
                            }
                        }
                        foreach (MethodIndex member in MethodIndex.GetMethods <AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute>(Type, AutoCSer.Metadata.MemberFilters.NonPublicInstance, false, true, false))
                        {
                            if (!member.Method.IsGenericMethodDefinition)
                            {
                                AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute remoteAttribute = member.GetSetupAttribute <AutoCSer.Net.TcpStaticServer.RemoteMemberAttribute>(false);
                                if (remoteAttribute != null)
                                {
                                    remoteMembers.Add(new RemoteMember {
                                        Method = member, Attribute = remoteAttribute
                                    });
                                }
                            }
                        }
                        remoteLinkType.RemoteMembers = remoteMembers.ToArray();

                        RemoteLinkBuilder cacheBuilder = new RemoteLinkBuilder();
                        foreach (MemberIndex member in MemberIndex.GetMembers <AutoCSer.Net.TcpStaticServer.RemoteLinkAttribute>(Type, AutoCSer.Metadata.MemberFilters.NonPublicInstance, true, false))
                        {
                            cacheBuilder.Push(member);
                        }
                        remoteLinkType.RemoteLinks = cacheBuilder.Caches.ToArray();

                        if (remoteLinkType.IsRemoteLink)
                        {
                            defaultServer.RemoteLinkTypes.Add(remoteLinkType);
                        }
                    }
                    if (!Type.Type.IsGenericType)
                    {
                        foreach (MemberIndexInfo member in StaticMemberIndexGroup.Get <AutoCSer.Net.TcpStaticServer.MethodAttribute>(Type, Attribute.GetMemberFilters, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute))
                        {
                            if (member.IsField)
                            {
                                FieldInfo field     = (FieldInfo)member.Member;
                                TcpMethod getMethod = new TcpMethod
                                {
                                    Method      = new Metadata.MethodIndex(field, true),
                                    MemberIndex = member,
                                    MethodType  = Type
                                };
                                if (!getMethod.Attribute.IsOnlyGetMember)
                                {
                                    getMethod.SetMethod = new TcpMethod {
                                        Method = new Metadata.MethodIndex(field, false), MemberIndex = member, MethodType = Type
                                    };
                                }
                                next(getMethod);
                                if (getMethod.SetMethod != null)
                                {
                                    next(getMethod.SetMethod);
                                }
                            }
                            else if (member.CanGet)
                            {
                                PropertyInfo property  = (PropertyInfo)member.Member;
                                TcpMethod    getMethod = new TcpMethod
                                {
                                    Method      = new Metadata.MethodIndex(property, true),
                                    MemberIndex = member,
                                    MethodType  = Type
                                };
                                if (member.CanSet && !getMethod.Attribute.IsOnlyGetMember)
                                {
                                    getMethod.SetMethod = new TcpMethod {
                                        Method = new Metadata.MethodIndex(property, false), MemberIndex = member, MethodType = Type
                                    };
                                }
                                next(getMethod);
                                if (getMethod.SetMethod != null)
                                {
                                    next(getMethod.SetMethod);
                                }
                            }
                        }
                    }
                    serverTypes.Empty();
                }
            }
 /// <summary>
 /// 安装下一个类型
 /// </summary>
 protected override void nextCreate()
 {
     if ((Attribute.IsAbstract || Type.Type.IsSealed || !Type.Type.IsAbstract) && !Type.Type.IsInterface)
     {
         defaultServerName = Attribute.ServerName;
         defaultServer     = null;
         defaultType       = null;
         if (defaultServerName != null)
         {
             HashString nameKey = defaultServerName;
             if (!servers.TryGetValue(nameKey, out defaultServer))
             {
                 servers.Add(nameKey, defaultServer = new Server());
             }
             defaultServer.Attribute.Name        = defaultServerName;
             defaultServer.Types.Add(defaultType = new ServerType {
                 Type = Type, Attribute = Attribute
             });
             if (Attribute.IsServer)
             {
                 defaultServer.AttributeType = Type;
                 defaultServer.Attribute.CopyFrom(Attribute);
             }
         }
         foreach (MethodIndex method in MethodIndex.GetMethods <AutoCSer.Net.TcpStaticSimpleServer.MethodAttribute>(Type, Attribute.GetMemberFilters, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute))
         {
             next(new TcpMethod {
                 Method = method, MethodType = Type
             });
         }
         if (!Type.Type.IsGenericType)
         {
             foreach (MemberIndexInfo member in StaticMemberIndexGroup.Get <AutoCSer.Net.TcpStaticSimpleServer.MethodAttribute>(Type, Attribute.GetMemberFilters, false, Attribute.IsAttribute, Attribute.IsBaseTypeAttribute))
             {
                 if (member.IsField)
                 {
                     FieldInfo field     = (FieldInfo)member.Member;
                     TcpMethod getMethod = new TcpMethod
                     {
                         Method      = new Metadata.MethodIndex(field, true),
                         MemberIndex = member,
                         MethodType  = Type
                     };
                     if (!getMethod.Attribute.IsOnlyGetMember)
                     {
                         getMethod.SetMethod = new TcpMethod {
                             Method = new Metadata.MethodIndex(field, false), MemberIndex = member, MethodType = Type
                         };
                     }
                     next(getMethod);
                     if (getMethod.SetMethod != null)
                     {
                         next(getMethod.SetMethod);
                     }
                 }
                 else if (member.CanGet)
                 {
                     PropertyInfo property  = (PropertyInfo)member.Member;
                     TcpMethod    getMethod = new TcpMethod
                     {
                         Method      = new Metadata.MethodIndex(property, true),
                         MemberIndex = member,
                         MethodType  = Type
                     };
                     if (member.CanSet && !getMethod.Attribute.IsOnlyGetMember)
                     {
                         getMethod.SetMethod = new TcpMethod {
                             Method = new Metadata.MethodIndex(property, false), MemberIndex = member, MethodType = Type
                         };
                     }
                     next(getMethod);
                     if (getMethod.SetMethod != null)
                     {
                         next(getMethod.SetMethod);
                     }
                 }
             }
         }
         serverTypes.Empty();
     }
 }
Esempio n. 5
0
 public void GetWeights(Func <keyType, bool> isKey = null, IWeight getWeight = null)
 {
     weights.Empty();
     searcher.GetWeights(ref queryResult, weights, isKey, getWeight);
 }
Esempio n. 6
0
            /// <summary>
            /// 获取文本分词结果
            /// </summary>
            /// <param name="text"></param>
            protected void getResult(string text)
            {
                result.Empty();
                formatLength = text.Length;
                formatText   = AutoCSer.Extension.StringExtension.FastAllocateString(formatLength + 1);
                fixed(char *textFixed = formatText)
                {
                    Simplified.FormatNotEmpty(text, textFixed, formatLength);
                    words.Length = matchs.Length = 0;
                    char *start = textFixed, end = textFixed + formatLength;
                    byte  type, nextType, wordType;
                    bool  isMatchMap = false;

                    if (charTypeData != StringTrieGraph.DefaultCharTypeData.Byte)
                    {
                        StaticStringTrieGraph trieGraph = searcher.trieGraph;
                        int  count, index, startIndex;
                        char trieGraphHeadChar = trieGraph.AnyHeadChar;
                        do
                        {
                            if (((type = charTypeData[*start]) & StringTrieGraph.TrieGraphHeadFlag) == 0)
                            {
                                *end = trieGraphHeadChar;
                                do
                                {
                                    if ((type & ((byte)WordType.Chinese | (byte)WordType.TrieGraph)) == ((byte)WordType.Chinese | (byte)WordType.TrieGraph))
                                    {
                                        addWord((int)(start - textFixed), 1, WordType.Chinese);
                                    }
                                    if (((nextType = charTypeData[*++start]) & StringTrieGraph.TrieGraphHeadFlag) != 0)
                                    {
                                        if (start == end)
                                        {
                                            goto TRIEGRAPHEND;
                                        }
                                        if ((nextType & (byte)WordType.Chinese) != 0 ||
                                            (type & nextType & ((byte)WordType.OtherLetter | (byte)WordType.Letter | (byte)WordType.Number | (byte)WordType.Keep)) == 0)
                                        {
                                            goto TRIEGRAPH;
                                        }
                                    }
                                    type = nextType;
                                }while (true);
                            }
TRIEGRAPH:
                            *end = ' ';
                            char *segment = start, segmentEnd = (type & StringTrieGraph.TrieGraphEndFlag) == 0 ? start++ : ++start;
                            while (((type = charTypeData[*start]) & (byte)WordType.TrieGraph) != 0)
                            {
                                ++start;
                                if ((type & StringTrieGraph.TrieGraphEndFlag) != 0)
                                {
                                    segmentEnd = start;
                                }
                            }
                            if ((int)(start - segment) == 1)
                            {
                                if ((type & (byte)WordType.Chinese) != 0)
                                {
                                    addWord((int)(segment - textFixed), 1, (type & (byte)WordType.TrieGraph) != 0 ? WordType.TrieGraph : WordType.Chinese);
                                }
                            }
                            else
                            {
                                if (segment != segmentEnd)
                                {
                                    matchs.Length = 0;
                                    trieGraph.LeftRightMatchs(segment, segmentEnd, ref matchs);
                                    if ((count = matchs.Length) == 0)
                                    {
                                        segmentEnd = segment;
                                        goto CHINESE;
                                    }
                                    if (!isMatchMap)
                                    {
                                        checkMatchMap();
                                        isMatchMap = true;
                                    }
                                    startIndex = (int)(segment - textFixed);
                                    foreach (KeyValue <int, int> value in matchs.Array)
                                    {
                                        addWord(index = value.Key + startIndex, value.Value, WordType.TrieGraph);
                                        matchMap.Set(index, value.Value);
                                        if (--count == 0)
                                        {
                                            break;
                                        }
                                    }
                                    index = (int)(segmentEnd - textFixed);
                                    do
                                    {
                                        if (matchMap.Get(startIndex) == 0 && (charTypeData[textFixed[startIndex]] & (byte)WordType.Chinese) != 0)
                                        {
                                            addWord(startIndex, 1, WordType.Chinese);
                                        }
                                    }while (++startIndex != index);
                                }
CHINESE:
                                while (segmentEnd != start)
                                {
                                    if ((charTypeData[*segmentEnd] & (byte)WordType.Chinese) != 0)
                                    {
                                        addWord((int)(segmentEnd - textFixed), 1, WordType.Chinese);
                                    }
                                    ++segmentEnd;
                                }
                            }
                        }while (start != end);
TRIEGRAPHEND:
                        start = textFixed;
                    }
                    do
                    {
                        type = charTypeData[*start];
                        if ((type &= ((byte)WordType.Chinese | (byte)WordType.OtherLetter | (byte)WordType.Letter | (byte)WordType.Number | (byte)WordType.Keep)) == 0)
                        {
                            *end = '0';
                            do
                            {
                                type = charTypeData[*++start];
                                if ((type &= ((byte)WordType.Chinese | (byte)WordType.OtherLetter | (byte)WordType.Letter | (byte)WordType.Number | (byte)WordType.Keep)) != 0)
                                {
                                    if (start == end)
                                    {
                                        goto END;
                                    }
                                    goto OTHER;
                                }
                            }while (true);
                        }
OTHER:
                        *end = ' ';
                        if ((type & (byte)WordType.Chinese) != 0)
                        {
                            do
                            {
                                if ((type & (byte)WordType.TrieGraph) == 0)
                                {
                                    addWord((int)(start - textFixed), 1, WordType.Chinese);
                                }
                            }while (((type = charTypeData[*++start]) & (byte)WordType.Chinese) != 0);
                        }
                        else
                        {
                            char *segment = start;
                            if ((type & (byte)WordType.OtherLetter) == 0)
                            {
                                char *word = start;
                                wordType = type;
                                for (nextType = charTypeData[*++start]; (nextType &= ((byte)WordType.Letter | (byte)WordType.Number | (byte)WordType.Keep)) != 0; nextType = charTypeData[*++start])
                                {
                                    if (type != nextType)
                                    {
                                        if (type != (byte)WordType.Keep)
                                        {
                                            addWord((int)(word - textFixed), (int)(start - word), (WordType)type);
                                        }
                                        wordType |= nextType;
                                        type      = nextType;
                                        word      = start;
                                    }
                                }
                                if (word != segment && type != (byte)WordType.Keep)
                                {
                                    addWord((int)(word - textFixed), (int)(start - word), (WordType)type);
                                }
                                addWord((int)(segment - textFixed), (int)(start - segment), (WordType)wordType);
                            }
                            else
                            {
                                while ((charTypeData[*++start] & (byte)WordType.OtherLetter) != 0)
                                {
                                    ;
                                }
                                addWord((int)(segment - textFixed), (int)(start - segment), WordType.OtherLetter);
                            }
                        }
                    }while (start != end);
END:
                    if (words.Length != 0)
                    {
                        int count = words.Length, textLength = text.Length;
                        if ((searcher.flags & SearchFlags.ResultIndexs) == 0)
                        {
                            foreach (KeyValue <SubString, WordType> word in words.Array)
                            {
                                result.Set(word.Key, new ResultIndexLeftArray {
                                    WordType = word.Value, TextLength = textLength
                                });
                                if (--count == 0)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            ResultIndexLeftArray indexs;
                            foreach (KeyValue <SubString, WordType> word in words.Array)
                            {
                                HashString wordKey = word.Key;
                                if (result.TryGetValue(ref wordKey, out indexs))
                                {
                                    indexs.Indexs.Add(word.Key.Start);
                                    result.Set(ref wordKey, indexs);
                                }
                                else
                                {
                                    indexs.Set(textLength, word.Value);
                                    if (indexArrays.Length != 0)
                                    {
                                        indexs.Indexs.Set(indexArrays.UnsafePopOnly(), 0);
                                    }
                                    indexs.Indexs.Add(word.Key.Start);
                                    result.Set(ref wordKey, indexs);
                                }
                                if (--count == 0)
                                {
                                    break;
                                }
                            }
                            foreach (ResultIndexLeftArray indexArray in result.Values)
                            {
                                indexArray.Indexs.Sort();
                            }
                        }
                    }
                }
            }
Esempio n. 7
0
 /// <summary>
 /// 释放结果
 /// </summary>
 public void Dispose()
 {
     counterPool.Free(results.Values, results.Count);
     results.Empty();
 }
Esempio n. 8
0
            /// <summary>
            /// 获取搜索数据标识集合(匹配所有分词结果)
            /// </summary>
            /// <param name="text">搜索关键字</param>
            /// <param name="maxSize">关键字最大字符长度</param>
            /// <param name="isKey">数据标识过滤</param>
            /// <returns>数据标识集合</returns>
            public LeftArray <keyType> SearchAll(string text, int maxSize, Func <keyType, bool> isKey = null)
            {
                resultArray.Length = 0;
                Simplified.Set(text, maxSize);
                Search(true);
                switch (queryResult.Count)
                {
                case 0: break;

                case 1:
                    foreach (KeyValuePair <keyType, ResultIndexArray> result in queryResult[0].Value.Dictionary)
                    {
                        if (isKey(result.Key))
                        {
                            resultArray.Add(result.Key);
                        }
                    }
                    break;

                default:
                    Dictionary <keyType, ResultIndexArray> resultDictionary = null;
                    foreach (KeyValue <HashString, QueryResult> result in queryResult)
                    {
                        if (resultDictionary == null || result.Value.Dictionary.Count < resultDictionary.Count)
                        {
                            resultDictionary = result.Value.Dictionary;
                        }
                    }
                    resultCountDictionary.Empty();
                    foreach (keyType key in resultDictionary.Keys)
                    {
                        if (isKey(key))
                        {
                            resultCountDictionary.Set(key, 0);
                        }
                    }
                    if (resultCountDictionary.Count != 0)
                    {
                        int count = 0, keyCount = resultCountDictionary.Count;
                        foreach (KeyValue <HashString, QueryResult> result in queryResult)
                        {
                            if (!object.ReferenceEquals(result.Value.Dictionary, resultDictionary))
                            {
                                int nextCount = count + 1, resultCount;
                                keyCount = 0;
                                foreach (keyType key in result.Value.Dictionary.Keys)
                                {
                                    if (resultCountDictionary.TryGetValue(key, out resultCount) && resultCount == count)
                                    {
                                        resultCountDictionary.Set(key, nextCount);
                                        ++keyCount;
                                    }
                                }
                                if (keyCount == 0)
                                {
                                    resultCountDictionary.Empty();
                                    break;
                                }
                                count = nextCount;
                            }
                        }
                        if (keyCount > 0)
                        {
                            resultArray.PrepLength(keyCount);
                            foreach (KeyValue <keyType, int> result in resultCountDictionary.KeyValues)
                            {
                                if (result.Value == count)
                                {
                                    resultArray.UnsafeAdd(result.Key);
                                }
                            }
                        }
                    }
                    break;
                }
                return(resultArray);
            }