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