File: QueryPrefixOp.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (884 lines) | stat: -rw-r--r-- 25,549 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
    using System.Collections.Generic;
    using System.Runtime;
    using System.Text;

    internal class TrieSegmentComparer : IComparer<TrieSegment>
    {
        public int Compare(TrieSegment t1, TrieSegment t2)
        {
            return ((int)t1.FirstChar) - ((int)t2.FirstChar);
        }

        public bool Equals(TrieSegment t1, TrieSegment t2)
        {
            return t1.FirstChar == t2.FirstChar;
        }

        public int GetHashCode(TrieSegment t)
        {
            return t.GetHashCode();
        }
    }

    internal class TrieSegmentKeyComparer : IItemComparer<char, TrieSegment>
    {
        public int Compare(char c, TrieSegment t)
        {
            return ((int)c) - ((int)t.FirstChar);
        }
    }

    internal class TrieSegment
    {
        static readonly TrieSegmentKeyComparer SegKeyComparer = new TrieSegmentKeyComparer();
        static readonly TrieSegmentComparer SegComparer = new TrieSegmentComparer();

        SortedBuffer<TrieSegment, TrieSegmentComparer> children;
        QueryBranch data;
        TrieSegment parent; // segment's parent
        char segmentFirstChar; // this segment's first character
        string segmentTail; // this segment's tail
        int segmentLength;

        internal TrieSegment()
            : this(char.MinValue)
        {
        }

        internal TrieSegment(char firstChar)
            : this(firstChar, string.Empty)
        {
        }

        internal TrieSegment(char firstChar, string segmentTail)
        {
            Fx.Assert(null != segmentTail, "");
            this.SetSegment(firstChar, segmentTail);
            this.children = new SortedBuffer<TrieSegment, TrieSegmentComparer>(SegComparer);
        }

        internal TrieSegment(string sourceSegment, int offset, int length)
        {
            Fx.Assert(null != sourceSegment && length > 0, "");
            this.SetSegmentString(sourceSegment, offset, length);
            this.children = new SortedBuffer<TrieSegment, TrieSegmentComparer>(SegComparer);
        }

        internal bool CanMerge
        {
            get
            {
                return (null == this.data && 1 == this.children.Count);
            }
        }

        internal bool CanPrune
        {
            get
            {
                return (null == this.data && 0 == this.children.Count);
            }
        }
#if NO
        internal int ChildCount
        {
            get
            {
                return this.children.Count;
            }
        }
#endif
        internal void CollectXPathFilters(ICollection<MessageFilter> filters)
        {
            if (this.data != null)
            {
                (this.data).Branch.CollectXPathFilters(filters);
            }

            for (int i = 0; i < this.children.Count; ++i)
            {
                this.children[i].CollectXPathFilters(filters);
            }
        }

        internal QueryBranch Data
        {
            get
            {
                return this.data;
            }
            set
            {
                this.data = value;
            }
        }

        internal char FirstChar
        {
            get
            {
                return this.segmentFirstChar;
            }
        }

        internal bool HasChildren
        {
            get
            {
                return (this.children.Count > 0);
            }
        }

        internal int Length
        {
            get
            {
                //return (this.segmentFirstChar == char.MinValue) ? 0 : this.segmentTail.Length + 1;
                return this.segmentLength;
            }
        }
#if NO
        internal string Tail
        {
            get
            {
                return this.segmentTail;
            }
        }
#endif
        internal TrieSegment AddChild(TrieSegment segment)
        {
            Fx.Assert(null != segment, "");

            this.children.Insert(segment);
            segment.parent = this;

            return segment;
        }
#if NO
        internal TrieSegment[] CopyChildren()
        {
            return this.children.ToArray();
        }
#endif
        internal int FindDivergence(string compareString, int offset, int length)
        {
            Fx.Assert(null != compareString && length > 0, "");

            if (compareString[offset] != this.segmentFirstChar)
            {
                return 0;
            }

            --length;
            ++offset;

            int charCount = (length <= this.segmentTail.Length) ? length : this.segmentTail.Length;
            for (int iSegment = 0, iCompare = offset; iSegment < charCount; ++iSegment, ++iCompare)
            {
                if (compareString[iCompare] != this.segmentTail[iSegment])
                {
                    return iSegment + 1;
                }
            }

            if (length < this.segmentTail.Length)
            {
                return length + 1;
            }

            return -1;
        }

        internal TrieSegment GetChild(int index)
        {
            Fx.Assert(this.HasChildren, "");
            return this.children[index];
        }

        /// <summary>
        /// Return the index of the child such that matchString == the string segment stored at that child
        /// i.e. matchString[0] == child.segmentFirstChar and matchString[1 -> length] == child.segmentTail[0 -> length]
        /// </summary>
        internal int GetChildPosition(string matchString, int offset, int length)
        {
            Fx.Assert(null != matchString, "");

            if (this.HasChildren)
            {
                char matchChar = matchString[offset];
                int tailLength = length - 1;
                int tailOffset = offset + 1;
                int index = this.children.IndexOfKey(matchChar, SegKeyComparer);
                if (index >= 0)
                {
                    TrieSegment child = this.children[index];
                    if (tailLength >= child.segmentTail.Length && (0 == child.segmentTail.Length || 0 == string.CompareOrdinal(matchString, tailOffset, child.segmentTail, 0, child.segmentTail.Length)))
                    {
                        return index;
                    }
                }
            }
            return -1;
        }

        /// <summary>
        /// Return the index of the child such that child.segmentFirstChar == ch
        /// </summary>
        internal int GetChildPosition(char ch)
        {
            return this.children.IndexOfKey(ch, SegKeyComparer);
        }

        internal int IndexOf(TrieSegment segment)
        {
            return this.children.IndexOf(segment);
        }

        internal void MergeChild(TrieSegment segment)
        {
            int childIndex = this.IndexOf(segment);
            if (childIndex > -1)
            {
                this.MergeChild(childIndex);
            }
        }

        /// <summary>
        /// If the child node has no data associated with it and but one child of its own, it can be
        /// merged with the child, reducing the path by 1
        /// </summary>
        internal void MergeChild(int childIndex)
        {
            Fx.Assert(this.HasChildren, "");

            TrieSegment child = this.children[childIndex];
            if (child.CanMerge)
            {
                TrieSegment grandchild = child.children[0];

                StringBuilder newTail = new StringBuilder();
                newTail.Append(child.segmentTail);
                newTail.Append(grandchild.segmentFirstChar);
                newTail.Append(grandchild.segmentTail);

                grandchild.SetSegment(child.segmentFirstChar, newTail.ToString());
                grandchild.parent = this;

                this.children.Exchange(child, grandchild);
                child.parent = null;
            }
        }

        internal void Remove()
        {
            if (null != this.parent)
            {
                this.parent.RemoveChild(this);
            }
        }

        /// <summary>
        /// Remove the child == segment, and prune the tree
        /// </summary>
        void RemoveChild(TrieSegment segment)
        {
            Fx.Assert(null != segment, "");
            int childIndex = this.IndexOf(segment);
            if (childIndex >= 0)
            {
                this.RemoveChild(childIndex, true);
            }
        }

        /// <summary>
        /// Remove the child at index childIndex.
        /// If fixupTree is true, traverse back up the tree, removing prunable nodes or merging mergable nodes
        /// as appropriate
        /// </summary>
        internal void RemoveChild(int childIndex, bool fixupTree)
        {
            Fx.Assert(this.HasChildren && childIndex >= 0, "");

            TrieSegment child = this.children[childIndex];

            child.parent = null;
            this.children.RemoveAt(childIndex);

            if (0 == this.children.Count)
            {
                if (fixupTree && this.CanPrune)
                {
                    this.Remove();
                }
            }
            else
            {
                if (fixupTree && this.CanMerge && null != this.parent)
                {
                    this.parent.MergeChild(this);
                }
            }
        }

        void SetSegment(char firstChar, string segmentTail)
        {
            this.segmentFirstChar = firstChar;
            this.segmentTail = segmentTail;
            this.segmentLength = firstChar == char.MinValue ? 0 : 1 + segmentTail.Length;
        }

        void SetSegmentString(string segmentString, int offset, int length)
        {
            this.segmentFirstChar = segmentString[offset];
            if (length > 1)
            {
                this.segmentTail = segmentString.Substring(offset + 1, length - 1);
            }
            else
            {
                this.segmentTail = string.Empty;
            }
            this.segmentLength = length;
        }

        TrieSegment SplitAt(int charIndex)
        {
            Fx.Assert(charIndex > 0, "");

            TrieSegment newSegment;
            if (1 == charIndex)
            {
                newSegment = new TrieSegment(this.segmentFirstChar);
            }
            else
            {
                Fx.Assert(this.segmentTail.Length > 0, "");
                newSegment = new TrieSegment(this.segmentFirstChar, this.segmentTail.Substring(0, charIndex - 1));
            }
            --charIndex;
            this.SetSegmentString(this.segmentTail, charIndex, this.segmentTail.Length - charIndex);
            newSegment.AddChild(this);
            return newSegment;
        }

        internal TrieSegment SplitChild(int childIndex, int charIndex)
        {
            Fx.Assert(this.HasChildren, "");

            TrieSegment child = this.children[childIndex];
            this.children.Remove(child);
            TrieSegment newChild = child.SplitAt(charIndex);
            this.children.Insert(newChild);
            newChild.parent = this;
            return newChild;
        }

        internal void Trim()
        {
            this.children.Trim();
            for (int i = 0; i < this.children.Count; ++i)
            {
                this.children[i].Trim();
            }
        }
    }

    internal struct TrieTraverser
    {
        int length;
        int offset;
        string prefix;
        TrieSegment rootSegment;
        TrieSegment segment;
        int segmentIndex;

        internal TrieTraverser(TrieSegment root, string prefix)
        {
            Fx.Assert(null != root && null != prefix, "");
            this.prefix = prefix;
            this.rootSegment = root;
            this.segment = null;
            this.segmentIndex = -1;
            this.offset = 0;
            this.length = prefix.Length;
        }

        /// <summary>
        /// What length of segment that matched
        /// </summary>
        internal int Length
        {
            get
            {
                return this.length;
            }
        }

        /// <summary>
        /// The offset within the original segment where the matching part of the source string began
        /// </summary>
        internal int Offset
        {
            get
            {
                return this.offset;
            }
        }

        /// <summary>
        /// The traverser is currently at this segment node
        /// </summary>
        internal TrieSegment Segment
        {
            get
            {
                return this.segment;
            }
            set
            {
                Fx.Assert(null != value, "");
                this.segment = value;
            }
        }

        internal int SegmentIndex
        {
            get
            {
                return this.segmentIndex;
            }
        }

        /// <summary>
        /// Traverse the prefix tree
        /// </summary>
        internal bool MoveNext()
        {
            if (null != this.segment)
            {
                int segmentLength = this.segment.Length;
                this.offset += segmentLength;
                this.length -= segmentLength;

                if (this.length > 0)
                {
                    this.segmentIndex = this.segment.GetChildPosition(this.prefix, this.offset, this.length);
                    if (this.segmentIndex > -1)
                    {
                        this.segment = this.segment.GetChild(this.segmentIndex);
                        return true;
                    }
                }
                else
                {
                    this.segmentIndex = -1;
                }
                this.segment = null;
            }
            else if (null != this.rootSegment)
            {
                this.segment = this.rootSegment;
                this.rootSegment = null;
                return true;
            }
            return false;
        }

        /// <summary>
        /// Traverse the prefix tree.. using only the first character of each segment to jump from
        /// segment to segment
        /// </summary>
        internal bool MoveNextByFirstChar()
        {
            if (null != this.segment)
            {
                int segmentLength = this.segment.Length;
                this.offset += segmentLength;
                this.length -= segmentLength;

                if (this.length > 0)
                {
                    this.segmentIndex = this.segment.GetChildPosition(this.prefix[this.offset]);
                    if (this.segmentIndex > -1)
                    {
                        this.segment = this.segment.GetChild(this.segmentIndex);
                        return true;
                    }
                }
                else
                {
                    this.segmentIndex = -1;
                }
                this.segment = null;
            }
            else if (null != this.rootSegment)
            {
                this.segment = this.rootSegment;
                this.rootSegment = null;
                return true;
            }

            return false;
        }
    }


    internal class Trie
    {
        TrieSegment root; // prefix tree root
        bool hasDescendants;

        internal Trie()
        {
            this.hasDescendants = false;
        }

        bool HasDescendants
        {
            get
            {
                //return (null != this.root && this.root.ChildCount > 0);
                return this.hasDescendants;
            }
        }
#if NO
        internal bool IsEmpty
        {
            get
            {
                return (null == this.root || (null == this.root.Data && 0 == this.root.ChildCount));
            }
        }
#endif
        internal TrieSegment this[string prefix]
        {
            get
            {
                return this.Find(prefix);
            }
        }

        /// <summary>
        /// Tree root segment
        /// </summary>
        internal TrieSegment Root
        {
            get
            {
                this.EnsureRoot();
                return this.root;
            }
        }

        internal TrieSegment Add(string newPrefix)
        {
            if (newPrefix.Length <= 0)
            {
                return this.Root;
            }

            this.EnsureRoot();
            TrieTraverser traverser = new TrieTraverser(this.root, newPrefix); // struct
            TrieSegment parent;
            int indexDivergence;
            while (true)
            {
                parent = traverser.Segment;
                if (traverser.MoveNextByFirstChar())
                {
                    // There is a child segment that starts with the same character as the remainder of newPrefix
                    // We have a shared segment
                    // How much does newPrefix share with the current segment? Find the point at which they diverge
                    if (null != parent && -1 != (indexDivergence = traverser.Segment.FindDivergence(newPrefix, traverser.Offset, traverser.Length)))
                    {
                        // Segments diverge at character # 'indexDivergence'. newPrefix will share the segment upto
                        // that character. Beyond that character, we will now have 2 child segments:
                        // - one for the portion of the current segment that diverged
                        // - one for the portion of the new segment that diverged

                        // Split the current segment into a shared part and a child containing the remainder of the segment..
                        traverser.Segment = parent.SplitChild(traverser.SegmentIndex, indexDivergence);
                    }
                }
                else
                {
                    if (traverser.Length <= 0)
                    {
                        // The entire new prefix has been added to the tree
                        break;
                    }
                    // No existing segment to share. Add a new one
                    traverser.Segment = parent.AddChild(new TrieSegment(newPrefix, traverser.Offset, traverser.Length));
                }
            }

            this.hasDescendants = true;

            return parent;
        }

        void EnsureRoot()
        {
            if (null == this.root)
            {
                this.root = new TrieSegment();
            }
        }

        TrieSegment Find(string prefix)
        {
            Fx.Assert(null != prefix, "");

            if (0 == prefix.Length)
            {
                return this.Root;
            }

            if (!this.HasDescendants)
            {
                return null;
            }

            TrieTraverser traverser = new TrieTraverser(this.root, prefix); // struct
            TrieSegment foundSegment = null;
            while (traverser.MoveNext())
            {
                foundSegment = traverser.Segment;
            }
            if (traverser.Length > 0)
            {
                // We haven't used up the entire prefix in this search. Clearly, we didn't find the matching node
                return null;
            }
            return foundSegment;
        }

        void PruneRoot()
        {
            if (null != this.root && this.root.CanPrune)
            {
                this.root = null;
            }
        }

        internal void Remove(string segment)
        {
            Fx.Assert(null != segment, "");

            TrieSegment trieSegment = this[segment];
            if (null == trieSegment)
            {
                return;
            }

            if (trieSegment.HasChildren)
            {
                trieSegment.Data = null;
                return;
            }

            if (trieSegment == this.root)
            {
                // Remove the entire tree!
                this.root = null;
                this.hasDescendants = false;
                return;
            }

            trieSegment.Remove();
            this.PruneRoot();
        }

        internal void Trim()
        {
            this.root.Trim();
        }
    }

    internal class StringPrefixOpcode : LiteralRelationOpcode
    {
        string literal;

        internal StringPrefixOpcode(string literal)
            : base(OpcodeID.StringPrefix)
        {
            Fx.Assert(null != literal, "");
            this.literal = literal;
        }
#if NO
        internal override ValueDataType DataType
        {
            get
            {
                return ValueDataType.String;
            }
        }
#endif
        internal override object Literal
        {
            get
            {
                return this.literal;
            }
        }

        internal override void Add(Opcode op)
        {
            StringPrefixOpcode prefixOp = op as StringPrefixOpcode;
            if (null == prefixOp)
            {
                base.Add(op);
                return;
            }

            Fx.Assert(null != this.prev, "");

            StringPrefixBranchOpcode branch = new StringPrefixBranchOpcode();
            this.prev.Replace(this, branch);
            branch.Add(this);
            branch.Add(prefixOp);
        }

        internal override bool Equals(Opcode op)
        {
            if (base.Equals(op))
            {
                StringPrefixOpcode prefixOp = (StringPrefixOpcode)op;
                return (prefixOp.literal == this.literal);
            }

            return false;
        }

        internal override Opcode Eval(ProcessingContext context)
        {
            StackFrame arg = context.TopArg;
            if (1 == arg.Count)
            {
                Fx.Assert(context.Values[arg.basePtr].IsType(ValueDataType.String), "");

                string target = context.Values[arg.basePtr].String;
                context.Values[arg.basePtr].Boolean = target.StartsWith(this.literal, StringComparison.Ordinal);
            }
            else
            {
                for (int i = arg.basePtr; i <= arg.endPtr; ++i)
                {
                    Fx.Assert(context.Values[i].IsType(ValueDataType.String), "");
                    string target = context.Values[i].String;
                    context.Values[i].Boolean = target.StartsWith(this.literal, StringComparison.Ordinal);
                }
            }

            return this.next;
        }
    }

    internal class TrieBranchIndex : QueryBranchIndex
    {
        int count;
        Trie trie;

        internal TrieBranchIndex()
        {
            this.count = 0;
            this.trie = new Trie();
        }

        internal override int Count
        {
            get
            {
                return this.count;
            }
        }

        internal override QueryBranch this[object key]
        {
            get
            {
                TrieSegment segment = this.trie[(string)key];
                if (null != segment)
                {
                    return segment.Data;
                }
                return null;
            }
            set
            {
                Fx.Assert(null != key, "");
                TrieSegment segment = this.trie.Add((string)key);
                Fx.Assert(null != segment, "");
                this.count++;
                segment.Data = value;
            }
        }

        internal override void CollectXPathFilters(ICollection<MessageFilter> filters)
        {
            this.trie.Root.CollectXPathFilters(filters);
        }

#if NO
        internal override IEnumerator GetEnumerator()
        {
            //return new TrieBreadthFirstEnum(this.trie);
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException("TODO"));
        }
#endif

        void Match(int valIndex, string segment, QueryBranchResultSet results)
        {
            TrieTraverser traverser = new TrieTraverser(this.trie.Root, segment);
            while (traverser.MoveNext())
            {
                object segmentData = traverser.Segment.Data;
                if (null != segmentData)
                {
                    results.Add((QueryBranch)segmentData, valIndex);
                }
            }
        }

        internal override void Match(int valIndex, ref Value val, QueryBranchResultSet results)
        {
            if (ValueDataType.Sequence == val.Type)
            {
                NodeSequence sequence = val.Sequence;
                for (int i = 0; i < sequence.Count; ++i)
                {
                    this.Match(valIndex, sequence.Items[i].StringValue(), results);
                }
            }
            else
            {
                this.Match(valIndex, val.String, results);
            }
        }

        internal override void Remove(object key)
        {
            this.trie.Remove((string)key);
            this.count--;
        }

        internal override void Trim()
        {
            this.trie.Trim();
        }
    }

    internal class StringPrefixBranchOpcode : QueryConditionalBranchOpcode
    {
        internal StringPrefixBranchOpcode()
            : base(OpcodeID.StringPrefixBranch, new TrieBranchIndex())
        {
        }
    }
}