File: QueryUtil.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 (865 lines) | stat: -rw-r--r-- 22,891 bytes parent folder | download
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime;

#if NO
    internal interface IQueryBufferPool
    {
        // Clear all pools
        void Reset();
        // Trim pools
        void Trim();
    } 
#endif

    //
    // Generic struct representing ranges within buffers
    //
    internal struct QueryRange
    {
        internal int end;       // INCLUSIVE - the end of the range
        internal int start;     // INCLUSIVE - the start of the range
#if NO
        internal QueryRange(int offset, QueryRange range)
        {
            this.start = range.start + offset;
            this.end = range.end + offset;
        }
#endif
        internal QueryRange(int start, int end)
        {
            this.start = start;
            this.end = end;
        }

        internal int Count
        {
            get
            {
                return this.end - this.start + 1;
            }
        }
#if NO        
        internal int this[int offset]
        {
            get
            {
                return this.start + offset;
            }
        }
                
        internal bool IsNotEmpty
        {
            get
            {
                return (this.end >= this.start);
            }
        }

        internal void Clear()
        {
            this.end = this.start - 1;
        }
                        
        internal void Grow(int offset)
        {
            this.end += offset;
        }
#endif
        internal bool IsInRange(int point)
        {
            return (this.start <= point && point <= this.end);
        }
#if NO
        internal void Set(int start, int end)
        {
            this.start = start;
            this.end = end;
        }
#endif
        internal void Shift(int offset)
        {
            this.start += offset;
            this.end += offset;
        }
    }

    /// <summary>
    /// Our own buffer management
    /// There are a few reasons why we don't reuse something in System.Collections.Generic
    ///  1. We want Clear() to NOT reallocate the internal array. We want it to simply set the Count = 0
    ///     This allows us to reuse buffers with impunity.
    ///  2. We want to be able to replace the internal buffer in a collection with a different one. Again,
    ///     this is to help with pooling
    ///  3. We want to be able to control how fast buffers grow. 
    ///  4. Does absolutely no bounds or null checking. As fast as we can make it. All checking should be done
    ///  by whoever wraps this. Checking is unnecessary for many internal uses where we need optimal perf.
    ///  5. Does more precise trimming
    ///  6. AND this is a struct
    ///
    /// </summary>        
    internal struct QueryBuffer<T>
    {
        internal T[] buffer;    // buffer of T. Frequently larger than count
        internal int count;     // Actual # of items
        internal static T[] EmptyBuffer = new T[0];

        /// <summary>
        /// Construct a new buffer
        /// </summary>
        /// <param name="capacity"></param>
        internal QueryBuffer(int capacity)
        {
            if (0 == capacity)
            {
                this.buffer = QueryBuffer<T>.EmptyBuffer;
            }
            else
            {
                this.buffer = new T[capacity];
            }
            this.count = 0;
        }
#if NO
		internal QueryBuffer(QueryBuffer<T> buffer)
		{
		    this.buffer = (T[]) buffer.buffer.Clone();
		    this.count = buffer.count;
		}
		
		internal QueryBuffer(T[] buffer)
		{
      Fx.Assert(null != buffer, "");
            this.buffer = buffer;
            this.count = 0;
        }
	
		/// <summary>
		/// Get and set the internal buffer
		/// If you set the buffer, the count will automatically be set to 0
		/// </summary>
		internal T[] Buffer
		{
		    get
		    {
		        return this.buffer;
		    }
		    set
		    {
          Fx.Assert(null != value, "");
		        this.buffer = value;
		        this.count = 0;
		    }
		}
#endif
        /// <summary>
        /// # of items
        /// </summary>
        internal int Count
        {
            get
            {
                return this.count;
            }
#if NO
		    set
		    {
          Fx.Assert(value >= 0 && value <= this.buffer.Length, "");
		        this.count = value;
		    }
#endif
        }

#if NO
		/// <summary>
		/// How much can it hold
		/// </summary>
		internal int Capacity
		{
		    get
		    {
		        return this.buffer.Length;
		    }
		    set
		    {
          Fx.Assert(value >= this.count, "");
		        if (value > this.buffer.Length)
		        {
		            Array.Resize<T>(ref this.buffer, value);
		        }
		    }
		}
#endif

        internal T this[int index]
        {
            get
            {
                return this.buffer[index];
            }
            set
            {
                this.buffer[index] = value;
            }
        }

#if NO
		internal void Add()
		{
            if (this.count == this.buffer.Length)
            {
                Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
            }
            this.count++;
        }
#endif

        /// <summary>
        /// Add an element to the buffer
        /// </summary>
        internal void Add(T t)
        {
            if (this.count == this.buffer.Length)
            {
                Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
            }
            this.buffer[this.count++] = t;
        }

#if NO
        /// <summary>
        /// Useful when this is a buffer of structs
        /// </summary>
        internal void AddReference(ref T t)
        {
		    if (this.count == this.buffer.Length)
		    {
                Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
            }
            this.buffer[this.count++] = t;
        }
#endif

        /// <summary>
        /// Add all the elements in the given buffer to this one
        /// We can do this very efficiently using an Array Copy
        /// </summary>
        internal void Add(ref QueryBuffer<T> addBuffer)
        {
            if (1 == addBuffer.count)
            {
                this.Add(addBuffer.buffer[0]);
                return;
            }

            int newCount = this.count + addBuffer.count;
            if (newCount >= this.buffer.Length)
            {
                this.Grow(newCount);
            }
            // Copy all the new elements in
            Array.Copy(addBuffer.buffer, 0, this.buffer, this.count, addBuffer.count);
            this.count = newCount;
        }

#if NO 
        internal void Add(T[] addBuffer, int startAt, int addCount)
        {
            int newCount = this.count + addCount;
            if (newCount >= this.buffer.Length)
            {
                this.Grow(newCount);
            }
            // Copy all the new elements in
            Array.Copy(addBuffer, startAt, this.buffer, this.count, addCount);
            this.count = newCount;
        }
        
        /// <summary>
		/// Add without attempting to grow the buffer. Faster, but must be used with care.
		/// Caller must ensure that the buffer is large enough.
		/// </summary>
        internal void AddOnly(T t)
		{
		    this.buffer[this.count++] = t;
		}
#endif

        /// <summary>
        /// Set the count to zero but do NOT get rid of the actual buffer
        /// </summary>
        internal void Clear()
        {
            this.count = 0;
        }

#if NO        
        //
        // Copy from one location in the buffer to another
        //
        internal void Copy(int from, int to)
        {
            this.buffer[to] = this.buffer[from];    
        }
        
        internal void Copy(int from, int to, int count)
        {
            Array.Copy(this.buffer, from, this.buffer, to, count);
        }
#endif

        internal void CopyFrom(ref QueryBuffer<T> addBuffer)
        {
            int addCount = addBuffer.count;
            switch (addCount)
            {
                default:
                    if (addCount > this.buffer.Length)
                    {
                        this.buffer = new T[addCount];
                    }
                    // Copy all the new elements in
                    Array.Copy(addBuffer.buffer, 0, this.buffer, 0, addCount);
                    this.count = addCount;
                    break;

                case 0:
                    this.count = 0;
                    break;

                case 1:
                    if (this.buffer.Length == 0)
                    {
                        this.buffer = new T[1];
                    }
                    this.buffer[0] = addBuffer.buffer[0];
                    this.count = 1;
                    break;
            }
        }

        internal void CopyTo(T[] dest)
        {
            Array.Copy(this.buffer, dest, this.count);
        }

#if NO
        /// <summary>
		/// Ensure that the internal buffer has adequate capacity
		/// </summary>
		internal void EnsureCapacity(int capacity)
		{
		    if (capacity > this.buffer.Length)
		    {
		        this.Grow(capacity);
            }
		}		

  
        internal void Erase()
        {
            Array.Clear(this.buffer, 0, this.count);
            this.count = 0;
        }
#endif
        void Grow(int capacity)
        {
            int newCapacity = this.buffer.Length * 2;
            Array.Resize<T>(ref this.buffer, capacity > newCapacity ? capacity : newCapacity);
        }

        internal int IndexOf(T t)
        {
            for (int i = 0; i < this.count; ++i)
            {
                if (t.Equals(this.buffer[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        internal int IndexOf(T t, int startAt)
        {
            for (int i = startAt; i < this.count; ++i)
            {
                if (t.Equals(this.buffer[i]))
                {
                    return i;
                }
            }
            return -1;
        }
#if NO 
        internal void InsertAt(T t, int at)
        {
            this.ReserveAt(at, 1);
            this.buffer[at] = t;
        }
#endif
        internal bool IsValidIndex(int index)
        {
            return (index >= 0 && index < this.count);
        }
#if NO        
        internal T Pop()
        {
            Fx.Assert(this.count > 0, "");
            return this.buffer[--this.count];
        }
        
        internal void Push(T t)
        {
            this.Add(t);
        }
#endif

        /// <summary>
        /// Reserve enough space for count elements
        /// </summary>
        internal void Reserve(int reserveCount)
        {
            int newCount = this.count + reserveCount;
            if (newCount >= this.buffer.Length)
            {
                this.Grow(newCount);
            }
            this.count = newCount;
        }

        internal void ReserveAt(int index, int reserveCount)
        {
            if (index == this.count)
            {
                this.Reserve(reserveCount);
                return;
            }

            int newCount;
            if (index > this.count)
            {
                // We want to reserve starting at a location past what is current committed. 
                // No shifting needed
                newCount = index + reserveCount + 1;
                if (newCount >= this.buffer.Length)
                {
                    this.Grow(newCount);
                }
            }
            else
            {
                // reserving space within an already allocated portion of the buffer
                // we'll ensure that the buffer can fit 'newCount' items, then shift by reserveCount starting at index
                newCount = this.count + reserveCount;
                if (newCount >= this.buffer.Length)
                {
                    this.Grow(newCount);
                }
                // Move to make room
                Array.Copy(this.buffer, index, this.buffer, index + reserveCount, this.count - index);
            }
            this.count = newCount;
        }

        internal void Remove(T t)
        {
            int index = this.IndexOf(t);
            if (index >= 0)
            {
                this.RemoveAt(index);
            }
        }

        internal void RemoveAt(int index)
        {
            if (index < this.count - 1)
            {
                Array.Copy(this.buffer, index + 1, this.buffer, index, this.count - index - 1);
            }
            this.count--;
        }

        internal void Sort(IComparer<T> comparer)
        {
            Array.Sort<T>(this.buffer, 0, this.count, comparer);
        }
#if NO

        /// <summary>
        /// Reduce the buffer capacity so that it is no greater than twice the element count
        /// </summary>
        internal void Trim()
        {
            int maxSize = this.count * 2;
            if (maxSize < this.buffer.Length / 2)
            {
                if (0 == maxSize)
                {
                    this.buffer = QueryBuffer<T>.EmptyBuffer;
                }
                else
                {
                    T[] newBuffer = new T[maxSize];
                    Array.Copy(this.buffer, newBuffer, maxSize);
                }
            }
        }
#endif

        /// <summary>
        /// Reduce the buffer capacity so that its size is exactly == to the element count
        /// </summary>
        internal void TrimToCount()
        {
            if (this.count < this.buffer.Length)
            {
                if (0 == this.count)
                {
                    this.buffer = QueryBuffer<T>.EmptyBuffer;
                }
                else
                {
                    T[] newBuffer = new T[this.count];
                    Array.Copy(this.buffer, newBuffer, this.count);
                }
            }
        }
    }

    internal struct SortedBuffer<T, C>
            where C : IComparer<T>
    {
        int size;
        T[] buffer;
        static DefaultComparer Comparer;

        internal SortedBuffer(C comparerInstance)
        {
            this.size = 0;
            this.buffer = null;

            if (Comparer == null)
            {
                Comparer = new DefaultComparer(comparerInstance);
            }
            else
            {
                Fx.Assert(object.ReferenceEquals(DefaultComparer.Comparer, comparerInstance), "The SortedBuffer type has already been initialized with a different comparer instance.");
            }
        }

        internal T this[int index]
        {
            get
            {
                return GetAt(index);
            }
        }

        internal int Capacity
        {
#if NO        
            get
            {
                return this.buffer == null ? 0 : this.buffer.Length;
            }
#endif
            set
            {
                if (this.buffer != null)
                {
                    if (value != this.buffer.Length)
                    {
                        Fx.Assert(value >= this.size, "New capacity must be >= size");
                        if (value > 0)
                        {
                            Array.Resize(ref this.buffer, value);
                        }
                        else
                        {
                            this.buffer = null;
                        }
                    }
                }
                else
                {
                    this.buffer = new T[value];
                }
            }
        }

        internal int Count
        {
            get
            {
                return this.size;
            }
        }

        internal int Add(T item)
        {
            int i = Search(item);

            if (i < 0)
            {
                i = ~i;
                InsertAt(i, item);
            }

            return i;
        }

#if NO
        internal void CopyTo(T[] array)
        {
            CopyTo(array, 0, this.size);
        }

        internal void CopyTo(T[] array, int start, int length)
        {
            Fx.Assert(array != null, "");
            Fx.Assert(start >= 0, "");
            Fx.Assert(length >= 0, "");
            Fx.Assert(start + length < this.size, "");
            Array.Copy(this.buffer, 0, array, start, length);
        }
#endif
        internal void Clear()
        {
            this.size = 0;
        }
#if NO
        internal bool Contains(T item)
        {
            return IndexOf(item) >= 0;
        }
#endif
        internal void Exchange(T old, T replace)
        {
            if (Comparer.Compare(old, replace) == 0)
            {
                int i = IndexOf(old);
                if (i >= 0)
                {
                    this.buffer[i] = replace;
                }
                else
                {
                    Insert(replace);
                }
            }
            else
            {
                // PERF, [....], can this be made more efficient?  Does it need to be?
                Remove(old);
                Insert(replace);
            }
        }

        internal T GetAt(int index)
        {
            Fx.Assert(index < this.size, "Index is greater than size");
            return this.buffer[index];
        }

        internal int IndexOf(T item)
        {
            return Search(item);
        }

        internal int IndexOfKey<K>(K key, IItemComparer<K, T> itemComp)
        {
            return Search(key, itemComp);
        }

        internal int Insert(T item)
        {
            int i = Search(item);

            if (i >= 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new ArgumentException(SR.GetString(SR.QueryItemAlreadyExists)));
            }

            // If an item is not found, Search returns the bitwise negation of
            // the index an item should inserted at;
            InsertAt(~i, item);
            return ~i;
        }

        void InsertAt(int index, T item)
        {
            Fx.Assert(index >= 0 && index <= this.size, "");

            if (this.buffer == null)
            {
                this.buffer = new T[1];
            }
            else if (this.buffer.Length == this.size)
            {
                // PERF, [....], how should we choose a new size?
                T[] tmp = new T[this.size + 1];

                if (index == 0)
                {
                    Array.Copy(this.buffer, 0, tmp, 1, this.size);
                }
                else if (index == this.size)
                {
                    Array.Copy(this.buffer, 0, tmp, 0, this.size);
                }
                else
                {
                    Array.Copy(this.buffer, 0, tmp, 0, index);
                    Array.Copy(this.buffer, index, tmp, index + 1, this.size - index);
                }

                this.buffer = tmp;
            }
            else
            {
                Array.Copy(this.buffer, index, this.buffer, index + 1, this.size - index);
            }

            this.buffer[index] = item;
            ++this.size;
        }

        internal bool Remove(T item)
        {
            int i = IndexOf(item);

            if (i >= 0)
            {
                RemoveAt(i);
                return true;
            }

            return false;
        }

        internal void RemoveAt(int index)
        {
            Fx.Assert(index >= 0 && index < this.size, "");

            if (index < this.size - 1)
            {
                Array.Copy(this.buffer, index + 1, this.buffer, index, this.size - index - 1);
            }

            this.buffer[--this.size] = default(T);
        }

        int Search(T item)
        {
            if (size == 0)
                return ~0;
            return Search(item, Comparer);
        }

        int Search<K>(K key, IItemComparer<K, T> comparer)
        {
            if (this.size <= 8)
            {
                return LinearSearch<K>(key, comparer, 0, this.size);
            }
            else
            {
                return BinarySearch(key, comparer);
            }
        }

        int BinarySearch<K>(K key, IItemComparer<K, T> comparer)
        {
            // [low, high)
            int low = 0;
            int high = this.size;
            int mid, result;

            // Binary search is implemented here so we could look for a type that is different from the
            // buffer type.  Also, the search switches to linear for 8 or fewer elements.
            while (high - low > 8)
            {
                mid = (high + low) / 2;
                result = comparer.Compare(key, this.buffer[mid]);
                if (result < 0)
                {
                    high = mid;
                }
                else if (result > 0)
                {
                    low = mid + 1;
                }
                else
                {
                    return mid;
                }
            }

            return LinearSearch<K>(key, comparer, low, high);
        }

        // [start, bound)
        int LinearSearch<K>(K key, IItemComparer<K, T> comparer, int start, int bound)
        {
            int result;

            for (int i = start; i < bound; ++i)
            {
                result = comparer.Compare(key, this.buffer[i]);
                if (result == 0)
                {
                    return i;
                }

                if (result < 0)
                {
                    // Return the bitwise negation of the insertion index
                    return ~i;
                }
            }

            // Return the bitwise negation of the insertion index
            return ~bound;
        }
#if NO
        internal T[] ToArray()
        {
            T[] tmp = new T[this.size];
            Array.Copy(this.buffer, 0, tmp, 0, this.size);
            return tmp;
        }
#endif
        internal void Trim()
        {
            this.Capacity = this.size;
        }

        internal class DefaultComparer : IItemComparer<T, T>
        {
            public static IComparer<T> Comparer;

            public DefaultComparer(C comparer)
            {
                Comparer = comparer;
            }

            public int Compare(T item1, T item2)
            {
                return Comparer.Compare(item1, item2);
            }
        }
    }

    internal interface IItemComparer<K, V>
    {
        int Compare(K key, V value);
    }
}