Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DomNameTable.cs « Dom « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f804251dd2cec0f66927c408ccd372bb9d5e5d7c (plain)
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
//------------------------------------------------------------------------------
// <copyright file="DomNameTable.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Xml.Schema;

namespace System.Xml {

    internal class DomNameTable {
        XmlName[]    entries;
        int          count;
        int          mask;
        XmlDocument  ownerDocument;
        XmlNameTable nameTable;

        const int InitialSize = 64; // must be a power of two

        public DomNameTable( XmlDocument document ) {
            ownerDocument = document;
            nameTable = document.NameTable;
            entries = new XmlName[InitialSize];
            mask = InitialSize - 1;
            Debug.Assert( ( entries.Length & mask ) == 0 );  // entries.Length must be a power of two
        }

        public XmlName GetName(string prefix, string localName, string ns, IXmlSchemaInfo schemaInfo) { 
            if (prefix == null) {
                prefix = string.Empty;
            }
            if (ns == null) {
                ns = string.Empty;
            }

            int hashCode = XmlName.GetHashCode(localName);

            for (XmlName e = entries[hashCode & mask]; e != null; e = e.next) {
                if (e.HashCode == hashCode 
                    && ((object)e.LocalName == (object)localName 
                        || e.LocalName.Equals(localName)) 
                    && ((object)e.Prefix == (object)prefix 
                        || e.Prefix.Equals(prefix)) 
                    && ((object)e.NamespaceURI == (object)ns 
                        || e.NamespaceURI.Equals(ns))
                    && e.Equals(schemaInfo)) {
                    return e;
                }
            }
            return null;
        }

        public XmlName AddName(string prefix, string localName, string ns, IXmlSchemaInfo schemaInfo) { 
            if (prefix == null) {
                prefix = string.Empty;
            }
            if (ns == null) {
                ns = string.Empty;
            }

            int hashCode = XmlName.GetHashCode(localName);

            for (XmlName e = entries[hashCode & mask]; e != null; e = e.next) {
                if (e.HashCode == hashCode 
                    && ((object)e.LocalName == (object)localName 
                        || e.LocalName.Equals(localName)) 
                    && ((object)e.Prefix == (object)prefix 
                        || e.Prefix.Equals(prefix)) 
                    && ((object)e.NamespaceURI == (object)ns 
                        || e.NamespaceURI.Equals(ns))
                    && e.Equals(schemaInfo)) {
                    return e;
                }
            }

            prefix = nameTable.Add(prefix);
            localName = nameTable.Add(localName);
            ns = nameTable.Add(ns);
            int index = hashCode & mask;
            XmlName name = XmlName.Create(prefix, localName, ns, hashCode, ownerDocument, entries[index], schemaInfo);
            entries[index] = name;

            if (count++ == mask) {
                Grow();
            }

            return name;
        }

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

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