-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathElementFactoryCollection.cs
More file actions
49 lines (38 loc) · 1.58 KB
/
ElementFactoryCollection.cs
File metadata and controls
49 lines (38 loc) · 1.58 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
namespace DocumentFormat.OpenXml.Framework.Metadata;
/// <summary>
/// A lookup that identifies properties on an <see cref="OpenXmlElement"/> and caches the schema information
/// from those elements.
/// </summary>
internal class ElementFactoryCollection
{
public static readonly ElementFactoryCollection Empty = new([]);
private readonly List<ElementFactory> _data;
public ElementFactoryCollection(List<ElementFactory> lookup)
{
lookup.Sort(ElementChildNameComparer.Instance);
_data = lookup;
}
public OpenXmlElement? Create(in OpenXmlQualifiedName qname)
{
if (_data.Count == 0)
{
return null;
}
// 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
// list to store them with a binary search improves overall performance.
var idx = _data.BinarySearch(new ElementFactory(new(qname, default), null!), ElementChildNameComparer.Instance);
if (idx < 0)
{
return null;
}
return _data[idx].Create();
}
private sealed class ElementChildNameComparer : IComparer<ElementFactory>
{
public static IComparer<ElementFactory> Instance { get; } = new ElementChildNameComparer();
public int Compare(ElementFactory x, ElementFactory y) => x.Type.Name.CompareTo(y.Type.Name);
}
}