Esempio n. 1
0
        /// <summary>
        /// 从左到右最大匹配
        /// </summary>
        /// <param name="text">匹配文本</param>
        /// <param name="matchs">匹配结果集合</param>
        private unsafe void leftRightMatchs(ref subString text, list <keyValue <int, int> > matchs)
        {
            node[] pool = NodePool;
            Dictionary <char, int> bootNode = pool[boot].Nodes;
            string value;
            int    node = boot, nextNode = 0, index = text.StartIndex - 1, matchCount;

            fixed(char *valueFixed = text.Value)
            {
                for (char *start = valueFixed + text.StartIndex, end = start + text.Length; start != end; ++start)
                {
                    char letter = *start;
                    if (pool[node].GetLinkWhereNull(letter, ref nextNode, ref node) != 0)
                    {
                        while (node != 0)
                        {
                            int isGetValue = pool[node].GetNodeOrLink(letter, ref nextNode, ref node, out value);
                            if (value != null)
                            {
                                matchCount = matchs.Count;
                                matchs.AddLength();
                                matchs.UnsafeArray[matchCount].Set(index - value.Length + 1, value.Length);
                            }
                            if (isGetValue == 0)
                            {
                                break;
                            }
                        }
                        if (node == 0 && !bootNode.TryGetValue(letter, out nextNode))
                        {
                            nextNode = boot;
                        }
                    }
                    ++index;
                    if ((value = pool[node = nextNode].Value) != null)
                    {
                        matchCount = matchs.Count;
                        matchs.AddLength();
                        matchs.UnsafeArray[matchCount].Set(index - value.Length + 1, value.Length);
                    }
                }
            }

            node = pool[node].Link;
            while (node != 0)
            {
                if ((value = pool[node].GetLink(ref node)) != null)
                {
                    matchCount = matchs.Count;
                    matchs.AddLength();
                    matchs.UnsafeArray[matchCount].Set(index - value.Length + 1, value.Length);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 拓扑排序
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="edges">边集合</param>
        /// <param name="points">无边点集合</param>
        /// <param name="isDesc">是否反向排序</param>
        /// <returns>排序结果</returns>
        public static valueType[] Sort <valueType>(ICollection <keyValue <valueType, valueType> > edges, HashSet <valueType> points
                                                   , bool isDesc = false)
        {
            Dictionary <valueType, list <valueType> > graph = new Dictionary <valueType, list <valueType> >();

            list <valueType> .unsafer edgePoints = default(list <valueType> .unsafer);
            if (edges != null)
            {
                edgePoints = new list <valueType>(edges.Count).Unsafer;
                list <valueType> values;
                foreach (keyValue <valueType, valueType> edge in edges)
                {
                    if (!graph.TryGetValue(edge.Key, out values))
                    {
                        graph.Add(edge.Key, values = new list <valueType>());
                    }
                    values.Add(edge.Value);
                    edgePoints.Add(edge.Value);
                }
                valueType[] pointArray = edgePoints.Array;
                int         count      = 0;
                if (points.count() != 0)
                {
                    while (count != pointArray.Length)
                    {
                        valueType point = pointArray[count];
                        if (graph.ContainsKey(point) || points.Contains(point))
                        {
                            break;
                        }
                        ++count;
                    }
                    if (count != pointArray.Length)
                    {
                        for (int index = count; ++index != pointArray.Length;)
                        {
                            valueType point = pointArray[index];
                            if (!graph.ContainsKey(point) && !points.Contains(point))
                            {
                                pointArray[count++] = point;
                            }
                        }
                    }
                }
                else
                {
                    while (count != pointArray.Length && !graph.ContainsKey(pointArray[count]))
                    {
                        ++count;
                    }
                    if (count != pointArray.Length)
                    {
                        for (int index = count; ++index != pointArray.Length;)
                        {
                            valueType point = pointArray[index];
                            if (!graph.ContainsKey(point))
                            {
                                pointArray[count++] = point;
                            }
                        }
                    }
                }
                edgePoints.AddLength(count - pointArray.Length);
            }
            list <valueType> pointList = edgePoints.List;

            if (points.count() != 0)
            {
                if (pointList == null)
                {
                    pointList = points.getList();
                }
                else
                {
                    foreach (valueType point in points)
                    {
                        if (!graph.ContainsKey(point))
                        {
                            pointList.Add(point);
                        }
                    }
                }
            }
            return(new sorter <valueType>(graph, pointList, isDesc).Sort());
        }