Skip to content

Instantly share code, notes, and snippets.

@vbjay
Created May 12, 2015 02:45
Show Gist options
  • Save vbjay/262e404eb3d262826ec3 to your computer and use it in GitHub Desktop.
Save vbjay/262e404eb3d262826ec3 to your computer and use it in GitHub Desktop.
Custom Merging
Imports System.Runtime.CompilerServices
Public Interface IMerge(Of T)
Function Matched() As IEnumerable(Of IMergeMatched(Of T))
Function Matched(predicate As Func(Of T, T, Boolean)) As IEnumerable(Of IMergeMatched(Of T))
Function NotMatchedBySource() As IEnumerable(Of T)
Function NotMatchedBySource(predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
Function NotMatchedByTarget() As IEnumerable(Of T)
Function NotMatchedByTarget(predicate As Func(Of T, Boolean)) As IEnumerable(Of T)
Function Merged() As IEnumerable(Of T)
End Interface
Public Interface IMergeMatched(Of T)
ReadOnly Property Source() As T
ReadOnly Property Target() As T
End Interface
Namespace Extensions
Public Module Enumerable
<Extension> _
Public Function Merge(Of TSource)(source As IEnumerable(Of TSource), target As IEnumerable(Of TSource), predicate As Func(Of TSource, TSource, Boolean)) As IMerge(Of TSource)
Return New Merge(Of TSource)(source, target, predicate)
End Function
End Module
End Namespace
Public Class Merge(Of T)
Implements IMerge(Of T)
Private ReadOnly _predicate As Func(Of T, T, Boolean)
Private ReadOnly _source As IEnumerable(Of T)
Private ReadOnly _target As IEnumerable(Of T)
Private _matcheds As IEnumerable(Of IMergeMatched(Of T))
Private _notMatchedBySource As IEnumerable(Of T)
Private _notMatchedByTarget As IEnumerable(Of T)
Private _merged As IEnumerable(Of T)
Public Sub New(source As IEnumerable(Of T), taget As IEnumerable(Of T), predicate As Func(Of T, T, Boolean))
_source = source
_target = taget
_predicate = predicate
End Sub
Public Function Matched() As IEnumerable(Of IMergeMatched(Of T)) Implements IMerge(Of T).Matched
If _matcheds Is Nothing Then
Analyze()
End If
Return _matcheds
End Function
Public Function Matched(predicate As Func(Of T, T, Boolean)) As IEnumerable(Of IMergeMatched(Of T)) Implements IMerge(Of T).Matched
Return Matched().Where(Function(t) predicate.Invoke(t.Source, t.Target)).ToArray()
End Function
Public Function NotMatchedBySource() As IEnumerable(Of T) Implements IMerge(Of T).NotMatchedBySource
If _notMatchedBySource Is Nothing Then
Analyze()
End If
Return _notMatchedBySource
End Function
Public Function NotMatchedBySource(predicate As Func(Of T, Boolean)) As IEnumerable(Of T) Implements IMerge(Of T).NotMatchedBySource
Return NotMatchedBySource().Where(predicate).ToArray()
End Function
Public Function NotMatchedByTarget() As IEnumerable(Of T) Implements IMerge(Of T).NotMatchedByTarget
If _notMatchedByTarget Is Nothing Then
Analyze()
End If
Return _notMatchedByTarget
End Function
Public Function NotMatchedByTarget(predicate As Func(Of T, Boolean)) As IEnumerable(Of T) Implements IMerge(Of T).NotMatchedByTarget
Return NotMatchedByTarget().Where(predicate).ToArray()
End Function
Public Function Merged() As IEnumerable(Of T) Implements IMerge(Of T).Merged
If _merged Is Nothing Then
Analyze()
End If
Return _merged
End Function
Private Sub Analyze()
Dim macheds = New List(Of MergeMached(Of T))()
Dim notMachedBySource = New List(Of T)(_source)
Dim notMachedByTarget = New List(Of T)(_target)
For Each source In _source
For Each target In _target
Dim macth = _predicate.Invoke(source, target)
If Not macth Then
Continue For
End If
macheds.Add(New MergeMached(Of T)(source, target))
notMachedBySource.Remove(source)
notMachedByTarget.Remove(target)
Next
Next
_matcheds = macheds.ToArray()
_notMatchedBySource = notMachedBySource.ToArray()
_notMatchedByTarget = notMachedByTarget.ToArray()
_merged = (From m In _matcheds Select m.Source).Union(_notMatchedBySource).Union(_notMatchedByTarget).ToArray
End Sub
End Class
Public Class MergeMached(Of T)
Implements IMergeMatched(Of T)
Public Sub New(Source As T, Target As T)
m_Source = Source
m_Target = Target
End Sub
Public ReadOnly Property Source() As T Implements IMergeMatched(Of T).Source
Get
Return m_Source
End Get
End Property
Private m_Source As T
Public ReadOnly Property Target() As T Implements IMergeMatched(Of T).Target
Get
Return m_Target
End Get
End Property
Private m_Target As T
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment