File: MTNameTable.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (731 lines) | stat: -rw-r--r-- 22,961 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------------------------
// <copyright file="MTNameTable.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">helenak</owner>
//------------------------------------------------------------------------------

#if MTNAMETABLE
using System;
using System.IO;
using System.Collections;
using System.Threading;

namespace System.Xml {

#if !SPLAY_MTNAMETABLE

    // MTNameTable is a modified version of our normal NameTable
    // that is thread-safe on read & write.  The design is kept
    // simple by using the Entry[] as the atomic update pivot point.
    public class MTNameTable : XmlNameTable {
        //
        // Private types
        //
        class Entry {
            internal string str;
            internal int    hashCode;
            internal Entry  next;

            internal Entry( string str, int hashCode, Entry next ) {
                this.str = str;
                this.hashCode = hashCode;
                this.next = next;
            }
        }

        //
        // Fields
        //
        Entry[] entries;
        int     count;
        int     hashCodeRandomizer;

        //
        // Constructor
        //
        public MTNameTable() {
            entries = new Entry[32];
            hashCodeRandomizer = Environment.TickCount;
        }

        //
        // XmlNameTable public methods
        //
        public override string Add( string key ) {
            if ( key == null ) {
                throw new ArgumentNullException( "key" );
            }
            int len = key.Length;            
            if ( len == 0 ) {
                return string.Empty;
            }
            int hashCode = len + hashCodeRandomizer;
            // use key.Length to eliminate the rangecheck
            for ( int i = 0; i < key.Length; i++ ) {
                hashCode += ( hashCode << 7 ) ^ key[i];
            }
            // mix it a bit more
            hashCode -= hashCode >> 17; 
            hashCode -= hashCode >> 11; 
            hashCode -= hashCode >> 5;

            Entry[] entries = this.entries;
            for ( Entry e = entries[hashCode & (entries.Length-1)]; 
                  e != null; 
                  e = e.next ) {
                if ( e.hashCode == hashCode && e.str.Equals( key ) ) {
                    return e.str;
                }
            }
            return AddEntry( key, hashCode );
        }

        public override string Add( char[] key, int start, int len ) {
            if ( len == 0 ) {
                return string.Empty;
            }

            int hashCode = len + hashCodeRandomizer;
            hashCode += ( hashCode << 7 ) ^ key[start];   // this will throw IndexOutOfRangeException in case the start index is invalid
            int end = start+len;
            for ( int i = start + 1; i < end; i++) {
                hashCode += ( hashCode << 7 ) ^ key[i];
            }
            // mix it a bit more
            hashCode -= hashCode >> 17; 
            hashCode -= hashCode >> 11; 
            hashCode -= hashCode >> 5;

            Entry[] entries = this.entries;
            for ( Entry e = entries[hashCode & (entries.Length-1)]; 
                  e != null; 
                  e = e.next ) {
                if ( e.hashCode == hashCode && TextEquals( e.str, key, start ) ) {
                    return e.str;
                }
            }
            return AddEntry( new string( key, start, len ), hashCode );
        }

        public override string Get( string value ) {
            if ( value == null ) {
                throw new ArgumentNullException("value");
            }
            if ( value.Length == 0 ) {
                return string.Empty;
            }

            int len = value.Length + hashCodeRandomizer;
            int hashCode = len;
            // use value.Length to eliminate the rangecheck
            for ( int i = 0; i < value.Length; i++ ) {
                hashCode += ( hashCode << 7 ) ^ value[i];
            }
            // mix it a bit more
            hashCode -= hashCode >> 17; 
            hashCode -= hashCode >> 11; 
            hashCode -= hashCode >> 5;
            
            Entry[] entries = this.entries;
            for ( Entry e = entries[hashCode & (entries.Length-1)]; 
                  e != null; 
                  e = e.next ) {
                if ( e.hashCode == hashCode && e.str.Equals( value ) ) {
                    return e.str;
                }
            }
            return null;
        }

        public override string Get( char[] key, int start, int len ) {
            if ( len == 0 ) {
                return string.Empty;
            }

            int hashCode = len + hashCodeRandomizer;
            hashCode += ( hashCode << 7 ) ^ key[start]; // this will throw IndexOutOfRangeException in case the start index is invalid
            int end = start+len;
            for ( int i = start + 1; i < end; i++) {
                hashCode += ( hashCode << 7 ) ^ key[i];
            }
            // mix it a bit more
            hashCode -= hashCode >> 17; 
            hashCode -= hashCode >> 11; 
            hashCode -= hashCode >> 5;
            
            Entry[] entries = this.entries;
            for ( Entry e = entries[hashCode & (entries.Length-1)]; 
                  e != null; 
                  e = e.next ) {
                if ( e.hashCode == hashCode && TextEquals( e.str, key, start ) ) {
                    return e.str;
                }
            }
            return null;
        }

        //
        // Private methods
        //

        private string AddEntry( string str, int hashCode ) {
            Entry e;
            lock (this) {
                Entry[] entries = this.entries;
                int index = hashCode & entries.Length-1;
                for ( e = entries[index]; e != null; e = e.next ) {
                    if ( e.hashCode == hashCode && e.str.Equals( str ) ) {
                        return e.str;
                    }
                }
                e = new Entry( str, hashCode, entries[index] );
                entries[index] = e;
                if ( count++ == mask ) {
                    Grow();
                }
            }
            return e.str;
        }

        private void Grow() {
            int newMask = mask * 2 + 1;
            Entry[] oldEntries = entries;
            Entry[] newEntries = new Entry[newMask+1];

            // use oldEntries.Length to eliminate the rangecheck            
            for ( int i = 0; i < oldEntries.Length; i++ ) {
                Entry e = oldEntries[i];
                while ( e != null ) {
                    int newIndex = e.hashCode & newMask;
                    Entry tmp = e.next;
                    e.next = newEntries[newIndex];
                    newEntries[newIndex] = e;
                    e = tmp;
                }
            }
            
            entries = newEntries;
            mask = newMask;
        }

        private static bool TextEquals( string array, char[] text, int start ) {
            // use array.Length to eliminate the rangecheck
            for ( int i = 0; i < array.Length; i++ ) {
                if ( array[i] != text[start+i] ) {
                    return false;
                }
            }
            return true;
        }
    }

#else 

    // XmlNameTable implemented as a multi-threaded splay tree.
    [Obsolete("This class is going away")]
    public class MTNameTable : XmlNameTable {
        internal MTNameTableNode rootNode;
        internal ReaderWriterLock rwLock;
        internal int timeout;

        public MTNameTable( bool isThreadSafe, int timeout ) {
            if (isThreadSafe) {
                this.rwLock = new ReaderWriterLock();
                this.timeout = timeout;
            }
        }

        public MTNameTable( bool isThreadSafe ): this( isThreadSafe, Timeout.Infinite ) {
        }

        public MTNameTable(): this( false ) {
        }    



        public IEnumerator GetEnumerator() {
            return new MTNameTableEnumerator( this );
        }


        public override String Get( String value ) {
            if (value == null) {
                throw new ArgumentNullException("value");
            }

            MTNameTableName name = new MTNameTableName(value);
            return Get( ref name );
        }

        public override String Get( char[] key, int start, int len ) {
            if (key == null) {
                throw new ArgumentNullException("key");
            }
            else {
                if ((start < 0) || (len < 0) || (start > key.Length - len))
                    throw new ArgumentOutOfRangeException();
            }

            MTNameTableName name = new MTNameTableName(key, start, len);
            return Get( ref name );
        }

        private String Get( ref MTNameTableName nn ) {
            String name = null;

            if (rootNode != null) {
                if (rwLock != null)
                    rwLock.AcquireReaderLock(timeout);

                MTNameTableNode currentNode = rootNode;

                while (true) {
                    Int64 d = currentNode.Compare(ref nn);

                    if (d == 0) {
                        Promote( currentNode );
                        name = currentNode.value;
                        break;
                    }
                    else if (d < 0) {
                        if (currentNode.leftNode == null)
                            break;

                        currentNode = currentNode.leftNode;
                    }
                    else {
                        if (currentNode.rightNode == null)
                            break;

                        currentNode = currentNode.rightNode;
                    }
                }

                if (rwLock != null)
                    rwLock.ReleaseReaderLock();
            }

            return name;
        }



        // Find the matching string atom given a string, or
        // insert a new one.
        public override String Add(String value) {
            if (value == null) {
                throw new ArgumentNullException("value");
            }

            MTNameTableName name = new MTNameTableName( value );
            return Add( ref name, rwLock != null ).value;
        }

        public override String Add(char[] key, int start, int len) {
            if (key == null) {
                throw new ArgumentNullException("key");
            }
            else {
                if ((start < 0) || (len < 0) || (start > key.Length - len))
                    throw new ArgumentOutOfRangeException();
            }

            MTNameTableName name = new MTNameTableName( key, start, len );
            return Add( ref name, rwLock != null ).value;
        }

        private MTNameTableNode Add( ref MTNameTableName name, bool fLock) {
            if (fLock)
                rwLock.AcquireReaderLock(timeout);

            MTNameTableNode currentNode = rootNode;

            while (true) {
                if (currentNode == null) {
                    currentNode = AddRoot( ref name, fLock );
                    break;
                }
                else {
                    Int64 d = currentNode.Compare(ref name);

                    if (d == 0) {
                        Promote( currentNode );
                        break;
                    }
                    else if (d < 0) {
                        if (currentNode.leftNode == null) {
                            currentNode = AddLeft( currentNode, ref name, fLock );
                            break;
                        }
                        else {
                            currentNode = currentNode.leftNode;
                        }
                    }
                    else {
                        if (currentNode.rightNode == null) {
                            currentNode = AddRight( currentNode, ref name, fLock );
                            break;
                        }
                        else {
                            currentNode = currentNode.rightNode;
                        }
                    }
                }
            }

            if (fLock)
                rwLock.ReleaseReaderLock();

            return currentNode;
        }

        // Sets the root node given a string 
        private MTNameTableNode AddRoot( ref MTNameTableName name, bool fLock ) {
            MTNameTableNode newNode = null;

            if (fLock) {
                LockCookie lc = rwLock.UpgradeToWriterLock(timeout);

                // recheck for failsafe against race-condition
                if (rootNode == null) {
                    rootNode = newNode = new MTNameTableNode( ref name );
                }
                else {
                    // try again, with write-lock active
                    newNode = Add( ref name, false );
                }

                rwLock.DowngradeFromWriterLock(ref lc);
            }
            else {
                rootNode = newNode = new MTNameTableNode( ref name );
            }

            return newNode;
        }


        // Adds the a node to the left of the specified node given a string
        private MTNameTableNode AddLeft( MTNameTableNode node, ref MTNameTableName name, bool fLock ) {
            MTNameTableNode newNode = null;

            if (fLock) {
                LockCookie lc = rwLock.UpgradeToWriterLock(timeout);

                // recheck for failsafe against race-condition
                if (node.leftNode == null) {
                    newNode = new MTNameTableNode( ref name );
                    node.leftNode = newNode;
                    newNode.parentNode = node;
                }
                else {
                    // try again, with write-lock active
                    newNode = Add( ref name, false );
                }

                rwLock.DowngradeFromWriterLock(ref lc);
            }
            else {
                newNode = new MTNameTableNode( ref name );
                node.leftNode = newNode;
                newNode.parentNode = node;
            }

            return newNode;
        }


        // Adds the a node to the right of the specified node, given a string.
        private MTNameTableNode AddRight( MTNameTableNode node, ref MTNameTableName name, bool fLock ) {
            MTNameTableNode newNode = null;

            if (fLock) {
                LockCookie lc = rwLock.UpgradeToWriterLock(timeout);

                // recheck for failsafe against race-condition
                if (node.rightNode == null) {
                    newNode = new MTNameTableNode( ref name );
                    node.rightNode = newNode;
                    newNode.parentNode = node;
                }
                else {
                    // try again, with write-lock active
                    newNode = Add( ref name, false );
                }

                rwLock.DowngradeFromWriterLock(ref lc);
            }
            else {
                newNode = new MTNameTableNode( ref name );
                node.rightNode = newNode;
                newNode.parentNode = node;
            }

            return newNode;
        }


        private const int threshhold = 20;

        // Promote the node into the parent's position (1 ply closer to the rootNode)
        private void Promote( MTNameTableNode node ) {
            // count number of times promotion requested
            node.counter++;

            if (node != rootNode &&
                node.counter > threshhold &&
                node.counter > node.parentNode.counter * 2) {
                if (rwLock != null) {
                    LockCookie lc = rwLock.UpgradeToWriterLock(timeout);

                    // recheck for failsafe against race-condition
                    if (node != rootNode && 
                        node.counter > threshhold &&
                        node.counter > node.parentNode.counter * 2) {
                        InternalPromote( node );
                    }

                    rwLock.DowngradeFromWriterLock(ref lc);
                }
                else {
                    InternalPromote( node );
                }
            }
        }

        private void InternalPromote( MTNameTableNode node ) {
            MTNameTableNode parent = node.parentNode;

            if (parent != null) {
                MTNameTableNode grandParent = parent.parentNode;

                if (parent.leftNode == node) {
                    parent.leftNode = node.rightNode;
                    node.rightNode = parent;

                    // update lineage
                    if (parent.leftNode != null)
                        parent.leftNode.parentNode = parent;

                    node.parentNode = grandParent;
                    parent.parentNode = node;
                }
                else {
                    parent.rightNode = node.leftNode;
                    node.leftNode = parent;

                    // update lineage
                    if (parent.rightNode != null)
                        parent.rightNode.parentNode = parent;

                    node.parentNode = grandParent;
                    parent.parentNode = node;
                }

                // fixup pointer to promoted node in grand parent
                if (grandParent == null) {
                    rootNode = node;
                }
                else {
                    if (grandParent.leftNode == parent) {
                        grandParent.leftNode = node;
                    }
                    else {
                        grandParent.rightNode = node;
                    }
                }
            }
        }
    }


    internal struct MTNameTableName {
        internal String str;
        internal char[] array;
        internal int start;
        internal int len;
        internal Int64 hash;

        public MTNameTableName( string str ) {
            this.str = str;
            this.hash = Hash(str);
            this.array = null;
            this.start = 0;
            this.len = 0;
        }

        public MTNameTableName( char[] array, int start, int len ) {
            this.array = array;
            this.start = start;
            this.len = len;
            this.str = null;
            this.hash = Hash(array, start, len);
        }

        static private Int64 Hash(String value) {
            Int64 hash = 0;
            int len = value.Length;

            if (len > 0)
                hash = (((Int64)value[0]) & 0xFF) << 48;

            if (len > 1)
                hash = hash | ((((Int64)value[1]) & 0xFF) << 32);

            if (len > 2)
                hash = hash | ((((Int64)value[2]) & 0xFF) << 16);

            if (len > 3)
                hash = hash | (((Int64)value[3]) & 0xFF);

            return hash;
        }    

        static private Int64 Hash(char[] key, int start, int len) {
            Int64 hash = 0;

            if (len > 0)
                hash = (((Int64)key[start]) & 0xFF) << 48;

            if (len > 1)
                hash = hash | ((((Int64)key[start+1]) & 0xFF) << 32);

            if (len > 2)
                hash = hash | ((((Int64)key[start+2]) & 0xFF) << 16);

            if (len > 3)
                hash = hash | (((Int64)key[start+3]) & 0xFF);

            return hash;
        }
    }


    // A MTNameTable node.
    internal class MTNameTableNode {
        internal String value;
        internal Int64 hash;
        internal Int64 counter;
        internal MTNameTableNode leftNode;
        internal MTNameTableNode rightNode;
        internal MTNameTableNode parentNode;

        internal MTNameTableNode(ref MTNameTableName name ) {
            if (name.str != null) {
                value = name.str;
            }
            else {
                value = new String(name.array, name.start, name.len);
            }

            this.hash = name.hash;
        }

        internal Int64 Compare( ref MTNameTableName name ) {
            if (name.array != null) {
                return Compare( name.hash, name.array, name.start, name.len );
            }
            else {
                return Compare( name.hash, name.str );
            }
        }

        private Int64 Compare(Int64 hash, string value) {
            Int64 d = hash - this.hash;

            if (d == 0) {
                int valueLength = this.value.Length;
                d = value.Length - valueLength;

                // if length is not equal, break
                if (d != 0)
                    return(Int64)d;

                for (int ii = 4; ii < valueLength; ii++) {
                    int dd = value[ii] - this.value[ii];
                    if (dd != 0) {
                        d = dd;
                        break;
                    }
                }
            }

            return(Int64)d;
        }

        private Int64 Compare(Int64 hash, char[] key, int start, int len) {
            Int64 d = hash - this.hash;

            if (d == 0) {
                int valueLength = this.value.Length;
                d = len - valueLength;

                // if length is not equal, break
                if (d != 0)
                    return(Int64)d;

                for (int ii = 4; ii < valueLength; ii++) {
                    int dd = key[start + ii] - this.value[ii];
                    if (dd != 0) {
                        d = dd;
                        break;
                    }
                }
            }

            return(Int64)d;
        }
    }


    // Enumerates all the names (strings) of a MTNameTable
    internal class MTNameTableEnumerator: IEnumerator {
        private ArrayList names;
        private int iName;

        internal MTNameTableEnumerator( MTNameTable nt ) {
            if (nt.rwLock != null)
                nt.rwLock.AcquireReaderLock(nt.timeout);

            names = new ArrayList();
            Walk( nt.rootNode );
            iName = -1;

            if (nt.rwLock != null)
                nt.rwLock.ReleaseReaderLock();
        }

        internal void Walk( MTNameTableNode node ) {
            if (node != null) {
                if (node.leftNode != null)
                    Walk( node.leftNode );

                names.Add( node.value );

                if (node.rightNode != null)
                    Walk( node.rightNode );
            }
        }

        public void Reset() {
            iName = -1;
        }

        public bool MoveNext() {
            iName++;
            return iName < names.Count;
        }

        public object Current {
            get {
                if (iName < names.Count)
                    return names[iName];
                return null;
            }
        }
    }
#endif
}

#endif