| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Xml; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF |
| | | 8 | | { |
| | | 9 | | internal class ServiceModelDictionary : IXmlDictionary |
| | | 10 | | { |
| | 0 | 11 | | public static readonly ServiceModelDictionary Version1 = new ServiceModelDictionary(new ServiceModelStringsVersi |
| | | 12 | | private readonly ServiceModelStrings _strings; |
| | | 13 | | private readonly int _count; |
| | | 14 | | private XmlDictionaryString[] _dictionaryStrings1; |
| | | 15 | | private XmlDictionaryString[] _dictionaryStrings2; |
| | | 16 | | private Dictionary<string, int> _dictionary; |
| | | 17 | | private XmlDictionaryString[] _versionedDictionaryStrings; |
| | | 18 | | |
| | 0 | 19 | | public ServiceModelDictionary(ServiceModelStrings strings) |
| | | 20 | | { |
| | 0 | 21 | | _strings = strings; |
| | 0 | 22 | | _count = strings.Count; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public static ServiceModelDictionary CurrentVersion |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | 0 | 29 | | return Version1; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public XmlDictionaryString CreateString(string value, int key) |
| | | 34 | | { |
| | 0 | 35 | | return new XmlDictionaryString(this, value, key); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public bool TryLookup(string key, out XmlDictionaryString value) |
| | | 39 | | { |
| | 0 | 40 | | if (key == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | if (_dictionary == null) |
| | | 46 | | { |
| | 0 | 47 | | Dictionary<string, int> dictionary = new Dictionary<string, int>(_count); |
| | 0 | 48 | | for (int i = 0; i < _count; i++) |
| | | 49 | | { |
| | 0 | 50 | | dictionary.Add(_strings[i], i); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | _dictionary = dictionary; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | if (_dictionary.TryGetValue(key, out int id)) |
| | | 57 | | { |
| | 0 | 58 | | return TryLookup(id, out value); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | value = null; |
| | 0 | 62 | | return false; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool TryLookup(int key, out XmlDictionaryString value) |
| | | 66 | | { |
| | | 67 | | const int keyThreshold = 32; |
| | 0 | 68 | | if (key < 0 || key >= _count) |
| | | 69 | | { |
| | 0 | 70 | | value = null; |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | XmlDictionaryString s; |
| | 0 | 75 | | if (key < keyThreshold) |
| | | 76 | | { |
| | 0 | 77 | | if (_dictionaryStrings1 == null) |
| | | 78 | | { |
| | 0 | 79 | | _dictionaryStrings1 = new XmlDictionaryString[keyThreshold]; |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | s = _dictionaryStrings1[key]; |
| | 0 | 83 | | if (s == null) |
| | | 84 | | { |
| | 0 | 85 | | s = CreateString(_strings[key], key); |
| | 0 | 86 | | _dictionaryStrings1[key] = s; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | 0 | 91 | | if (_dictionaryStrings2 == null) |
| | | 92 | | { |
| | 0 | 93 | | _dictionaryStrings2 = new XmlDictionaryString[_count - keyThreshold]; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | s = _dictionaryStrings2[key - keyThreshold]; |
| | 0 | 97 | | if (s == null) |
| | | 98 | | { |
| | 0 | 99 | | s = CreateString(_strings[key], key); |
| | 0 | 100 | | _dictionaryStrings2[key - keyThreshold] = s; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | value = s; |
| | 0 | 105 | | return true; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value) |
| | | 109 | | { |
| | 0 | 110 | | if (key == null) |
| | | 111 | | { |
| | 0 | 112 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | if (key.Dictionary == this) |
| | | 116 | | { |
| | 0 | 117 | | value = key; |
| | 0 | 118 | | return true; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (key.Dictionary == CurrentVersion) |
| | | 122 | | { |
| | 0 | 123 | | if (_versionedDictionaryStrings == null) |
| | | 124 | | { |
| | 0 | 125 | | _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count]; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | XmlDictionaryString s = _versionedDictionaryStrings[key.Key]; |
| | 0 | 129 | | if (s == null) |
| | | 130 | | { |
| | 0 | 131 | | if (!TryLookup(key.Value, out s)) |
| | | 132 | | { |
| | 0 | 133 | | value = null; |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | 0 | 136 | | _versionedDictionaryStrings[key.Key] = s; |
| | | 137 | | } |
| | 0 | 138 | | value = s; |
| | 0 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | value = null; |
| | 0 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |