| | | 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 | | { |
| | 13 | 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 | | |
| | 13 | 19 | | public ServiceModelDictionary(ServiceModelStrings strings) |
| | | 20 | | { |
| | 13 | 21 | | _strings = strings; |
| | 13 | 22 | | _count = strings.Count; |
| | 13 | 23 | | } |
| | | 24 | | |
| | | 25 | | public static ServiceModelDictionary CurrentVersion |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | 57451 | 29 | | return Version1; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public XmlDictionaryString CreateString(string value, int key) |
| | | 34 | | { |
| | 35774 | 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 | | } |
| | 0 | 55 | | if (_dictionary.TryGetValue(key, out int id)) |
| | | 56 | | { |
| | 0 | 57 | | return TryLookup(id, out value); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | value = null; |
| | 0 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool TryLookup(int key, out XmlDictionaryString value) |
| | | 65 | | { |
| | | 66 | | const int keyThreshold = 32; |
| | 10117 | 67 | | if (key < 0 || key >= _count) |
| | | 68 | | { |
| | 0 | 69 | | value = null; |
| | 0 | 70 | | return false; |
| | | 71 | | } |
| | | 72 | | XmlDictionaryString s; |
| | 10117 | 73 | | if (key < keyThreshold) |
| | | 74 | | { |
| | 8654 | 75 | | if (_dictionaryStrings1 == null) |
| | | 76 | | { |
| | 5 | 77 | | _dictionaryStrings1 = new XmlDictionaryString[keyThreshold]; |
| | | 78 | | } |
| | | 79 | | |
| | 8654 | 80 | | s = _dictionaryStrings1[key]; |
| | 8654 | 81 | | if (s == null) |
| | | 82 | | { |
| | 54 | 83 | | s = CreateString(_strings[key], key); |
| | 54 | 84 | | _dictionaryStrings1[key] = s; |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | else |
| | | 88 | | { |
| | 1463 | 89 | | if (_dictionaryStrings2 == null) |
| | | 90 | | { |
| | 3 | 91 | | _dictionaryStrings2 = new XmlDictionaryString[_count - keyThreshold]; |
| | | 92 | | } |
| | | 93 | | |
| | 1463 | 94 | | s = _dictionaryStrings2[key - keyThreshold]; |
| | 1463 | 95 | | if (s == null) |
| | | 96 | | { |
| | 62 | 97 | | s = CreateString(_strings[key], key); |
| | 62 | 98 | | _dictionaryStrings2[key - keyThreshold] = s; |
| | | 99 | | } |
| | | 100 | | } |
| | 10117 | 101 | | value = s; |
| | 10117 | 102 | | return true; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value) |
| | | 106 | | { |
| | 607 | 107 | | if (key == null) |
| | | 108 | | { |
| | 0 | 109 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key)); |
| | | 110 | | } |
| | | 111 | | |
| | 607 | 112 | | if (key.Dictionary == this) |
| | | 113 | | { |
| | 0 | 114 | | value = key; |
| | 0 | 115 | | return true; |
| | | 116 | | } |
| | 607 | 117 | | if (key.Dictionary == CurrentVersion) |
| | | 118 | | { |
| | 0 | 119 | | if (_versionedDictionaryStrings == null) |
| | | 120 | | { |
| | 0 | 121 | | _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count]; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | XmlDictionaryString s = _versionedDictionaryStrings[key.Key]; |
| | 0 | 125 | | if (s == null) |
| | | 126 | | { |
| | 0 | 127 | | if (!TryLookup(key.Value, out s)) |
| | | 128 | | { |
| | 0 | 129 | | value = null; |
| | 0 | 130 | | return false; |
| | | 131 | | } |
| | 0 | 132 | | _versionedDictionaryStrings[key.Key] = s; |
| | | 133 | | } |
| | 0 | 134 | | value = s; |
| | 0 | 135 | | return true; |
| | | 136 | | } |
| | 607 | 137 | | value = null; |
| | 607 | 138 | | return false; |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |