|
1 | | -using System.Collections; |
2 | 1 | using System.Collections.Generic; |
3 | | -using System.Diagnostics; |
4 | 2 |
|
5 | 3 | namespace SourceGit.Models |
6 | 4 | { |
7 | | - public class InlineElementCollector : IEnumerable<InlineElement> |
| 5 | + public class InlineElementCollector |
8 | 6 | { |
9 | | - private readonly List<InlineElement> _implementation = []; |
10 | | - |
11 | | - public void Clear() |
12 | | - { |
13 | | - _implementation.Clear(); |
14 | | - |
15 | | - AssertInvariant(); |
16 | | - } |
17 | | - |
18 | 7 | public int Count => _implementation.Count; |
| 8 | + public InlineElement this[int index] => _implementation[index]; |
19 | 9 |
|
20 | | - public void Add(InlineElement element) |
| 10 | + public InlineElement Intersect(int start, int length) |
21 | 11 | { |
22 | | - |
23 | | - var index = FindIndex(element.Start); |
24 | | - if (!IsIntersection(index, element.Start, element.Length)) |
25 | | - _implementation.Insert(index, element); |
26 | | - |
27 | | - AssertInvariant(); |
28 | | - } |
29 | | - |
30 | | - [Conditional("DEBUG")] |
31 | | - private void AssertInvariant() |
32 | | - { |
33 | | - if (_implementation.Count == 0) |
34 | | - return; |
35 | | - |
36 | | - for (var index = 1; index < _implementation.Count; index++) |
| 12 | + foreach (var elem in _implementation) |
37 | 13 | { |
38 | | - var prev = _implementation[index - 1]; |
39 | | - var curr = _implementation[index]; |
40 | | - |
41 | | - Debug.Assert(prev.Start + prev.Length <= curr.Start); |
| 14 | + if (elem.IsIntersecting(start, length)) |
| 15 | + return elem; |
42 | 16 | } |
| 17 | + |
| 18 | + return null; |
43 | 19 | } |
44 | 20 |
|
45 | | - public InlineElement Lookup(int position) |
| 21 | + public void Add(InlineElement element) |
46 | 22 | { |
47 | | - var index = FindIndex(position); |
48 | | - return IsIntersection(index, position, 1) |
49 | | - ? _implementation[index] |
50 | | - : null; |
| 23 | + _implementation.Add(element); |
51 | 24 | } |
52 | 25 |
|
53 | | - private int FindIndex(int start) |
| 26 | + public void Sort() |
54 | 27 | { |
55 | | - var index = 0; |
56 | | - while (index < _implementation.Count && _implementation[index].Start <= start) |
57 | | - index++; |
58 | | - |
59 | | - return index; |
| 28 | + _implementation.Sort((l, r) => l.Start.CompareTo(r.Start)); |
60 | 29 | } |
61 | 30 |
|
62 | | - private bool IsIntersection(int index, int start, int length) |
| 31 | + public void Clear() |
63 | 32 | { |
64 | | - if (index > 0) |
65 | | - { |
66 | | - var predecessor = _implementation[index - 1]; |
67 | | - if (predecessor.Start + predecessor.Length >= start) |
68 | | - return true; |
69 | | - } |
70 | | - |
71 | | - if (index < _implementation.Count) |
72 | | - { |
73 | | - var successor = _implementation[index]; |
74 | | - if (start + length >= successor.Start) |
75 | | - return true; |
76 | | - } |
77 | | - |
78 | | - return false; |
| 33 | + _implementation.Clear(); |
79 | 34 | } |
80 | 35 |
|
81 | | - public IEnumerator<InlineElement> GetEnumerator() => _implementation.GetEnumerator(); |
82 | | - |
83 | | - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 36 | + private readonly List<InlineElement> _implementation = []; |
84 | 37 | } |
85 | 38 | } |
0 commit comments