forked from dotnet/Open-XML-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticlePath.cs
More file actions
169 lines (141 loc) · 5.15 KB
/
ParticlePath.cs
File metadata and controls
169 lines (141 loc) · 5.15 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Validation.Schema;
using System;
using System.Collections.Generic;
namespace DocumentFormat.OpenXml.Framework.Schema
{
internal class ParticlePath : IComparable<ParticlePath>, IEquatable<ParticlePath>
{
private readonly ParticlePathItem[] _values;
public static ParticlePath Create(params ParticlePathItem[] values)
=> new ParticlePath(values);
private ParticlePath(ParticlePathItem[] values)
{
_values = values;
}
public ParticleType Type
{
get
{
if (_values.Length == 0)
{
return ParticleType.Invalid;
}
else
{
return _values[_values.Length - 1].Type;
}
}
}
public int CompareTo(ParticlePath? other)
=> CompareTo(other, true);
public override string ToString()
=> string.Join(", ", (IEnumerable<ParticlePathItem>)_values);
private int CompareTo(ParticlePath? other, bool isCompare)
{
if (other is null)
{
// See https://docs.microsoft.com/en-us/dotnet/api/system.string.compareto
return 1;
}
var en = new Enumerator(this);
var on = new Enumerator(other);
while (en.MoveNext() && on.MoveNext())
{
var (currentParticleType, currentIndex) = en.Current;
var (otherParticleType, otherIndex) = on.Current;
if (currentParticleType == ParticleType.Element && otherParticleType == ParticleType.Element)
{
return currentIndex.CompareTo(otherIndex);
}
else if (currentParticleType == ParticleType.Sequence && otherParticleType == ParticleType.Sequence)
{
var compared = currentIndex.CompareTo(otherIndex);
if (compared != 0)
{
return compared;
}
}
else if (currentParticleType == ParticleType.Choice && otherParticleType == ParticleType.Choice)
{
// If the two elements are at the same index within a choice, we want to continue down the path. If they are
// different, we can end and say that we're at an equivalent point.
if (currentIndex != otherIndex)
{
return 0;
}
}
else if (currentParticleType == ParticleType.All && otherParticleType == ParticleType.All)
{
var compared = currentIndex.CompareTo(otherIndex);
if (compared != 0)
{
// When checking for compare, anything within an All is equivalent. For equality, it needs to be the same
return isCompare ? 0 : -1;
}
}
else
{
return 0;
}
}
return 0;
}
public bool IsSibling(ParticlePath? other)
=> Equals(other, _values.Length - 1);
public override bool Equals(object? obj)
{
if (obj is ParticlePath path)
{
return Equals(path);
}
return false;
}
/// <summary>
/// Checks if two <see cref="ParticlePath"/> instances are the same. They may not have the exact same sequence, but if they are replaceable, then they are equal.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(ParticlePath? other)
=> CompareTo(other, false) == 0;
public override int GetHashCode()
=> HashCode.Combine(Type, _values.Length);
private bool Equals(ParticlePath? other, int length)
{
if (other is null)
{
return false;
}
length = System.Math.Min(other._values.Length, length);
for (int i = 0; i < length; i++)
{
if (!Equals(_values[i], other._values[i]))
{
return false;
}
}
return true;
}
private struct Enumerator
{
private readonly ParticlePathItem[] _path;
private int _idx;
public Enumerator(ParticlePath path)
{
_path = path._values;
_idx = -1;
}
public ParticlePathItem Current => _path[_idx];
public bool MoveNext()
{
if (_idx + 1 < _path.Length)
{
_idx++;
return true;
}
return false;
}
}
}
}