Skip to content

Commit

Permalink
Fixed merge filter and related extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Mar 19, 2020
1 parent da843e9 commit 17223c2
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 11 deletions.
2 changes: 1 addition & 1 deletion SharedAssemblyVersion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;

[assembly: AssemblyVersion("7.0.0")]
[assembly: AssemblyInformationalVersion("7.0.0-pre008")]
[assembly: AssemblyInformationalVersion("7.0.0-pre009")]
33 changes: 28 additions & 5 deletions src/OsmSharp/Streams/Filters/OsmStreamFilterMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ public override bool CanReset
{
get
{
return _source1.CanReset && _source2.CanReset;
if (_source1 != null && _source2 != null)
{
return _source1.CanReset && _source2.CanReset;
}
else if (_source1 != null)
{
return _source1.CanReset;
}
else if(_source2 != null)
{
return _source2.CanReset;
}

return true; // a stream can be reset when there is no data.
}
}

Expand All @@ -65,8 +78,8 @@ public override bool CanReset
/// </summary>
public override void Reset()
{
_source1.Reset();
_source2.Reset();
_source1?.Reset();
_source2?.Reset();

_source1Status = null;
_source2Status = null;
Expand Down Expand Up @@ -115,16 +128,26 @@ public override void RegisterSource(OsmStreamSource source)
private OsmGeo _current;
private bool? _source1Status = null; // false when finished, true when there is data, null when unintialized.
private bool? _source2Status = null; // false when finished, true when there is data, null when unintialized.
private ConflictResolutionType _resolutionType = ConflictResolutionType.None;
private readonly ConflictResolutionType _resolutionType;

/// <summary>
/// Move to the next object.
/// </summary>
public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
{
if (_source1 != null && _source2 == null)
{
return _source1.MoveNext(ignoreNodes, ignoreWays, ignoreRelations);
}

if (_source1 == null && _source2 != null)
{
return _source2.MoveNext(ignoreNodes, ignoreWays, ignoreRelations);
}

OsmGeo source1Current = null;
OsmGeo source2Current = null;

// get currents or move to first.
if (_source1Status == null)
{
Expand Down
107 changes: 102 additions & 5 deletions src/OsmSharp/Streams/OsmStreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,122 @@ public static OsmStreamSource Progress(this OsmStreamSource source)
return progress;
}

/// <summary>
/// Merges a stream with the given other stream.
/// </summary>
public static OsmStreamSource Merge(this IEnumerable<OsmGeo> source, ConflictResolutionType resolutionType, OsmStreamSource other)
{
var merged = new OsmStreamFilterMerge(resolutionType);
merged.RegisterSource(source);
merged.RegisterSource(other);
return merged;
}

/// <summary>
/// Merges the given sources into this source.
/// </summary>
public static OsmStreamSource Merge(this IEnumerable<OsmGeo> source, params OsmStreamSource[] sources)
{
return source.Merge(ConflictResolutionType.FirstStream, new OsmEnumerableStreamSource(source));
return source.Merge(ConflictResolutionType.FirstStream, sources);
}

/// <summary>
/// Merges the given sources into this source.
/// </summary>
public static OsmStreamSource Merge(this IEnumerable<OsmGeo> source, ConflictResolutionType resolutionType, params OsmStreamSource[] sources)
{
if (sources.Length == 0)
{
return new OsmEnumerableStreamSource(source);
}

var merged = new OsmStreamFilterMerge(resolutionType);
merged.RegisterSource(source);
merged.RegisterSource(sources[0]);
for (var s = 1; s < sources.Length; s++)
{
var next = new OsmStreamFilterMerge(resolutionType);
next.RegisterSource(merged);
next.RegisterSource(sources[s]);

merged = next;
}

return merged;
}

/// <summary>
/// Merges all the given sources.
/// </summary>
/// <param name="sources">The sources.</param>
/// <param name="resolutionType">The resolution type.</param>
/// <returns>A merged stream.</returns>
public static OsmStreamSource Merge(this IEnumerable<IEnumerable<OsmGeo>> sources,
ConflictResolutionType resolutionType = ConflictResolutionType.FirstStream)
{
OsmStreamSource previous = null;
foreach (var source in sources)
{
var osmSource = new OsmEnumerableStreamSource(source);

if (previous == null)
{
previous = osmSource;
}
else
{
var next = new OsmStreamFilterMerge(resolutionType);
next.RegisterSource(previous);
next.RegisterSource(osmSource);

previous = next;
}
}

return previous;
}

/// <summary>
/// Merges all the given sources.
/// </summary>
/// <param name="sources">The sources.</param>
/// <param name="resolutionType">The resolution type.</param>
/// <returns>A merged stream.</returns>
public static OsmStreamSource Merge(this IEnumerable<OsmStreamSource> sources,
ConflictResolutionType resolutionType = ConflictResolutionType.FirstStream)
{
OsmStreamSource previous = null;
foreach (var osmSource in sources)
{
if (previous == null)
{
previous = osmSource;
}
else
{
var next = new OsmStreamFilterMerge(resolutionType);
next.RegisterSource(previous);
next.RegisterSource(osmSource);

previous = next;
}
}

return previous;
}

/// <summary>
/// Merges the given sources into this source.
/// </summary>
public static OsmStreamSource Merge(this IEnumerable<OsmGeo> source, ConflictResolutionType resolutionType, params IEnumerable<OsmGeo>[] sources)
{
var merge = new OsmStreamFilterMerge(resolutionType);
for(var i = 0; i < sources.Length; i++)
var osmSources = new OsmStreamSource[sources.Length];
for (var s = 0; s < sources.Length; s++)
{
merge.RegisterSource(sources[i]);
osmSources[s] = new OsmEnumerableStreamSource(sources[s]);
}
return merge;

return source.Merge(resolutionType, osmSources);
}

/// <summary>
Expand Down

0 comments on commit 17223c2

Please sign in to comment.