示例#1
0
        public void Insert(string search, Item item, ref MergeResult result, int pos = 0)
        {
            TraversalOption nextStep = new TraversalOption(Label, search, pos);

            switch (nextStep.option)
            {
            case TraversalOptions.Found:
                if (!Items.Any(item1 => item1.ComparePath(item)))
                {
                    Items.Add(item);
                    Results.Add(item);
                    result.ToBeMergedInto = Results;
                    result.NeedsMerge     = true;
                }
                break;

            case TraversalOptions.MoveNext:
                if (NextNode == null)
                {
                    NextNode = new SearchNode {
                        Label = search.Substring(pos)
                    };
                    NextNode.Items.Add(item);
                    NextNode.Results.Add(item);
                    result.ToBeMergedInto = NextNode.Results;
                    result.NeedsMerge     = true;
                }
                else
                {
                    NextNode.Insert(search, item, ref result, pos);
                }
                break;

            case TraversalOptions.MoveDown:
                if (ChildNode == null)
                {
                    ChildNode = new SearchNode()
                    {
                        Label = search.Substring(pos + nextStep.commonPrefixLength),
                        Items = new List <Item>(),
                    };
                    ChildNode.Items.Add(item);
                    ChildNode.Results.Add(item);
                    result.ToBeMergedInto = ChildNode.Results;
                    result.NeedsMerge     = true;
                    if (result.NeedsMerge)
                    {
                        result.NeedsMerge     = Results.MergeIntoList(result.ToBeMergedInto) > 0;
                        result.ToBeMergedInto = Results;
                    }
                }
                else
                {
                    ChildNode.Insert(search, item, ref result, pos + nextStep.commonPrefixLength);
                    if (result.NeedsMerge)
                    {
                        result.NeedsMerge     = Results.MergeIntoList(result.ToBeMergedInto) > 0;
                        result.ToBeMergedInto = Results;
                    }
                }
                break;

            case TraversalOptions.Split:
                SearchNode newNode = new SearchNode
                {
                    Label     = Label.Substring(nextStep.commonPrefixLength),
                    Items     = Items,
                    Results   = Results,
                    ChildNode = ChildNode
                };

                Items     = new List <Item>();
                Results   = new MergedResultList(newNode.ResultItems);
                Label     = Label.Substring(0, nextStep.commonPrefixLength);
                ChildNode = newNode;
                if (search.Length == nextStep.commonPrefixLength + pos)
                {
                    Items.Add(item);
                    Results.Add(item);
                }
                else
                {
                    SearchNode toInsert = new SearchNode
                    {
                        Label = search.Substring(nextStep.commonPrefixLength + pos),
                        Items = new List <Item> {
                            item
                        },
                        Results = new MergedResultList(new List <Item> {
                            item
                        })
                    };
                    Results.MergeIntoList(toInsert.Results);
                    newNode.NextNode = toInsert;
                }

                result.ToBeMergedInto = Results;
                result.NeedsMerge     = true;
                break;
            }
        }