-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathJoinedTripleEnumerator.cs
More file actions
100 lines (85 loc) · 2.43 KB
/
JoinedTripleEnumerator.cs
File metadata and controls
100 lines (85 loc) · 2.43 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
using System.Collections;
using System.Collections.Generic;
namespace Waher.Content.Semantic
{
/// <summary>
/// Enumerator of triples from a collection of semantic data sources.
/// The enumerator can remove duplicates available in different sources.
/// </summary>
public class JoinedTripleEnumerator : IEnumerator<ISemanticTriple>
{
private readonly Dictionary<ISemanticTriple, bool> reported;
private readonly IEnumerable<IEnumerator<ISemanticTriple>> enumerators;
private IEnumerator<IEnumerator<ISemanticTriple>> e;
private IEnumerator<ISemanticTriple> current;
private readonly bool removeDuplicates;
/// <summary>
/// Enumerator of triples from a collection of semantic data sources.
/// The enumerator can remove duplicates available in different sources.
/// </summary>
/// <param name="Enumerators">Set of enumerators</param>
/// <param name="RemoveDuplicates">If duplicates should be removed.</param>
public JoinedTripleEnumerator(IEnumerable<IEnumerator<ISemanticTriple>> Enumerators,
bool RemoveDuplicates)
{
this.enumerators = Enumerators;
this.removeDuplicates = RemoveDuplicates;
if (this.removeDuplicates)
this.reported = new Dictionary<ISemanticTriple, bool>();
}
/// <summary>
/// Current element
/// </summary>
public ISemanticTriple Current => this.current?.Current;
/// <summary>
/// Current element
/// </summary>
object IEnumerator.Current => this.current?.Current;
/// <summary>
/// Disposes of the enumerator and sub-enumerators.
/// </summary>
public void Dispose()
{
foreach (IEnumerator<ISemanticTriple> Enumerator in this.enumerators)
Enumerator.Dispose();
}
/// <summary>
/// Moves to next elements.
/// </summary>
/// <returns>If a new element is found.</returns>
public bool MoveNext()
{
if (this.e is null)
{
this.e = this.enumerators.GetEnumerator();
if (!this.e.MoveNext())
return false;
this.current = this.e.Current;
}
while (true)
{
if (this.current.MoveNext())
{
if (this.removeDuplicates)
{
if (this.reported.ContainsKey(this.current.Current))
continue;
this.reported[this.current.Current] = true;
}
return true;
}
if (!this.e.MoveNext())
return false;
this.current = this.e.Current;
}
}
/// <summary>
/// Resets the enumerator.
/// </summary>
public void Reset()
{
this.current = null;
this.e = null;
}
}
}