|
| 1 | +using EdgeDB.DataTypes; |
| 2 | +using System.Collections; |
| 3 | + |
| 4 | +namespace EdgeDB.Models.DataTypes; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Represents the <c>multirange</c> type in EdgeDB. |
| 8 | +/// </summary> |
| 9 | +/// <typeparam name="T">The inner type of the multirange.</typeparam> |
| 10 | +public readonly struct MultiRange<T> : IEnumerable<Range<T>> |
| 11 | + where T : struct |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Gets the length of this multirange. |
| 15 | + /// </summary> |
| 16 | + public int Length |
| 17 | + => _ranges.Length; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Gets a <see cref="Range{T}"/> element within this multirange. |
| 21 | + /// </summary> |
| 22 | + /// <param name="i"></param> |
| 23 | + public readonly ref Range<T> this[int i] |
| 24 | + => ref _ranges[i]; |
| 25 | + |
| 26 | + private readonly Range<T>[] _ranges; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Constructs a new <see cref="MultiRange{T}"/>. |
| 30 | + /// </summary> |
| 31 | + /// <param name="set">A set of ranges to put within this multirange.</param> |
| 32 | + public MultiRange(HashSet<Range<T>> set) |
| 33 | + { |
| 34 | + _ranges = set.ToArray(); |
| 35 | + } |
| 36 | + |
| 37 | + internal MultiRange(Range<T>[] ranges) |
| 38 | + { |
| 39 | + _ranges = ranges; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Returns a hashset that represents this multirange. |
| 44 | + /// </summary> |
| 45 | + /// <returns>A hashset, derived from the contents of this multirange.</returns> |
| 46 | + public HashSet<Range<T>> ToSet() => new(_ranges); |
| 47 | + |
| 48 | + #region Enumerator |
| 49 | + private sealed class MultiRangeEnumerator : IEnumerator<Range<T>> |
| 50 | + { |
| 51 | + private readonly MultiRange<T> _multiRange; |
| 52 | + private int _index; |
| 53 | + |
| 54 | + internal MultiRangeEnumerator(in MultiRange<T> multiRange) |
| 55 | + { |
| 56 | + _multiRange = multiRange; |
| 57 | + _index = -1; |
| 58 | + } |
| 59 | + |
| 60 | + public bool MoveNext() |
| 61 | + { |
| 62 | + var index = _index + 1; |
| 63 | + if (index >= _multiRange.Length) |
| 64 | + { |
| 65 | + _index = _multiRange.Length; |
| 66 | + return false; |
| 67 | + } |
| 68 | + _index = index; |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + public void Reset() => _index = -1; |
| 73 | + |
| 74 | + public Range<T> Current |
| 75 | + { |
| 76 | + get |
| 77 | + { |
| 78 | + var index = _index; |
| 79 | + |
| 80 | + if (index >= _multiRange.Length) |
| 81 | + { |
| 82 | + if (index < 0) |
| 83 | + { |
| 84 | + throw new InvalidOperationException("Enumeration hasn't started"); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + throw new InvalidOperationException("The enumeration has finished"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return _multiRange[index]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public void Dispose() |
| 97 | + { } |
| 98 | + |
| 99 | + object IEnumerator.Current => Current; |
| 100 | + } |
| 101 | + #endregion |
| 102 | + |
| 103 | + /// <inheritdoc /> |
| 104 | + public IEnumerator<Range<T>> GetEnumerator() => new MultiRangeEnumerator(in this); |
| 105 | + |
| 106 | + /// <inheritdoc /> |
| 107 | + IEnumerator IEnumerable.GetEnumerator() => _ranges.GetEnumerator(); |
| 108 | +} |
0 commit comments