|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic; |
| 6 | +using System.Linq; |
5 | 7 |
|
6 | | -namespace DocumentFormat.OpenXml.Framework.Metadata; |
7 | | - |
8 | | -/// <summary> |
9 | | -/// A lookup that identifies properties on an <see cref="OpenXmlElement"/> and caches the schema information |
10 | | -/// from those elements. |
11 | | -/// </summary> |
12 | | -internal class ElementFactoryCollection |
| 8 | +namespace DocumentFormat.OpenXml.Framework.Metadata |
13 | 9 | { |
14 | | - public static readonly ElementFactoryCollection Empty = new([]); |
15 | | - |
16 | | - private readonly List<ElementFactory> _data; |
17 | | - |
18 | | - public ElementFactoryCollection(List<ElementFactory> lookup) |
| 10 | + /// <summary> |
| 11 | + /// A lookup that identifies properties on an <see cref="OpenXmlElement"/> and caches the schema information |
| 12 | + /// from those elements. |
| 13 | + /// </summary> |
| 14 | + internal class ElementFactoryCollection |
19 | 15 | { |
20 | | - lookup.Sort(ElementChildNameComparer.Instance); |
21 | | - _data = lookup; |
22 | | - } |
| 16 | + public static readonly ElementFactoryCollection Empty = new(Enumerable.Empty<ElementFactory>()); |
23 | 17 |
|
24 | | - public OpenXmlElement? Create(in OpenXmlQualifiedName qname) |
25 | | - { |
26 | | - if (_data.Count == 0) |
| 18 | + private readonly ElementFactory[] _data; |
| 19 | + |
| 20 | + public ElementFactoryCollection(IEnumerable<ElementFactory> lookup) |
27 | 21 | { |
28 | | - return null; |
| 22 | + var array = lookup.ToArray(); |
| 23 | + |
| 24 | + Array.Sort(array, ElementChildNameComparer.Instance); |
| 25 | + |
| 26 | + _data = array; |
29 | 27 | } |
30 | 28 |
|
31 | | - // This is on a hot-path and using a dictionary adds substantial time to the lookup. Most child lists are small, so using a sorted |
32 | | - // list to store them with a binary search improves overall performance. |
33 | | - var idx = _data.BinarySearch(new ElementFactory(new(qname, default), null!), ElementChildNameComparer.Instance); |
| 29 | + public int Count => _data.Length; |
34 | 30 |
|
35 | | - if (idx < 0) |
| 31 | + public IEnumerable<ElementFactory> Elements => _data; |
| 32 | + |
| 33 | + public OpenXmlElement? Create(in OpenXmlQualifiedName qname) |
36 | 34 | { |
37 | | - return null; |
| 35 | + if (_data.Length == 0) |
| 36 | + { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + // This is on a hot-path and using a dictionary adds substantial time to the lookup. Most child lists are small, so using a sorted |
| 41 | + // list to store them with a binary search improves overall performance. |
| 42 | + var idx = Array.BinarySearch(_data, new ElementFactory(new(qname, default), null!), ElementChildNameComparer.Instance); |
| 43 | + |
| 44 | + if (idx < 0) |
| 45 | + { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + return _data[idx].Create(); |
38 | 50 | } |
39 | 51 |
|
40 | | - return _data[idx].Create(); |
41 | | - } |
| 52 | + private class ElementChildNameComparer : IComparer<ElementFactory> |
| 53 | + { |
| 54 | + public static IComparer<ElementFactory> Instance { get; } = new ElementChildNameComparer(); |
42 | 55 |
|
43 | | - private sealed class ElementChildNameComparer : IComparer<ElementFactory> |
44 | | - { |
45 | | - public static IComparer<ElementFactory> Instance { get; } = new ElementChildNameComparer(); |
| 56 | + private ElementChildNameComparer() |
| 57 | + { |
| 58 | + } |
46 | 59 |
|
47 | | - public int Compare(ElementFactory x, ElementFactory y) => x.Type.Name.CompareTo(y.Type.Name); |
| 60 | + public int Compare(ElementFactory? x, ElementFactory? y) |
| 61 | + { |
| 62 | + if (x is null && y is null) |
| 63 | + { |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + if (x is null) |
| 68 | + { |
| 69 | + return -1; |
| 70 | + } |
| 71 | + |
| 72 | + if (y is null) |
| 73 | + { |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + return x.Type.Name.CompareTo(y.Type.Name); |
| 78 | + } |
| 79 | + } |
48 | 80 | } |
49 | 81 | } |
0 commit comments