diff --git a/Pilz.Collections/Pilz.Collections.vbproj b/Pilz.Collections/Pilz.Collections.vbproj index 2d25955..847fabc 100644 --- a/Pilz.Collections/Pilz.Collections.vbproj +++ b/Pilz.Collections/Pilz.Collections.vbproj @@ -83,4 +83,7 @@ + + + \ No newline at end of file diff --git a/Pilz.Collections/SimpleSorting/ElementSortingPosition.vb b/Pilz.Collections/SimpleSorting/ElementSortingPosition.vb new file mode 100644 index 0000000..420247e --- /dev/null +++ b/Pilz.Collections/SimpleSorting/ElementSortingPosition.vb @@ -0,0 +1,8 @@ +Namespace SimpleSorting + + Public Enum ElementSortingPosition + Before + After + End Enum + +End Namespace diff --git a/Pilz.Collections/SimpleSorting/ISimpleSortingHost.vb b/Pilz.Collections/SimpleSorting/ISimpleSortingHost.vb new file mode 100644 index 0000000..9039be7 --- /dev/null +++ b/Pilz.Collections/SimpleSorting/ISimpleSortingHost.vb @@ -0,0 +1,10 @@ +Namespace SimpleSorting + + Public Interface ISimpleSortingHost + Inherits IList + + Event OnInsert(sender As Object, e As SimpleSortingHostEventArgs) + Event OnRemove(sender As Object, e As SimpleSortingHostEventArgs) + End Interface + +End Namespace diff --git a/Pilz.Collections/SimpleSorting/SimpleSortingEntry.vb b/Pilz.Collections/SimpleSorting/SimpleSortingEntry.vb new file mode 100644 index 0000000..377fd58 --- /dev/null +++ b/Pilz.Collections/SimpleSorting/SimpleSortingEntry.vb @@ -0,0 +1,13 @@ +Imports Pilz.Cryptography + +Namespace SimpleSorting + + Friend Class SimpleSortingEntry + + Public Property Element As UniquieID + Public Property Position As ElementSortingPosition + Public Property ReferenceElement As UniquieID + + End Class + +End Namespace diff --git a/Pilz.Collections/SimpleSorting/SimpleSortingHostEventArgs.vb b/Pilz.Collections/SimpleSorting/SimpleSortingHostEventArgs.vb new file mode 100644 index 0000000..2183df0 --- /dev/null +++ b/Pilz.Collections/SimpleSorting/SimpleSortingHostEventArgs.vb @@ -0,0 +1,18 @@ +Imports Pilz.Cryptography + +Namespace SimpleSorting + + Public Class SimpleSortingHostEventArgs + Inherits EventArgs + + Public ReadOnly Property ElementID As UniquieID + Public ReadOnly Property ElementIndex As Integer + + Public Sub New(elementID As UniquieID, elementIndex As Integer) + Me.ElementID = elementID + Me.ElementIndex = elementIndex + End Sub + + End Class + +End Namespace diff --git a/Pilz.Collections/SimpleSorting/SimpleSortingList.vb b/Pilz.Collections/SimpleSorting/SimpleSortingList.vb new file mode 100644 index 0000000..95e0b03 --- /dev/null +++ b/Pilz.Collections/SimpleSorting/SimpleSortingList.vb @@ -0,0 +1,121 @@ +Imports System.Runtime.InteropServices.ComTypes + +Imports Newtonsoft.Json + +Imports Pilz.Cryptography + +Namespace SimpleSorting + + ''' + ''' Provides some management methods for sorting a list without changing the list itself. + ''' Useful if the host list can not be changed or the sorting is indipendent from the required sorting. + ''' + ''' The content of the host list to sort. + Public Class SimpleSortingList(Of T As IUniquieIDHost) + + Private ReadOnly host As ISimpleSortingHost + + + Private ReadOnly sortingList As New List(Of SimpleSortingEntry) + + Public Sub New(parentList As ISimpleSortingHost) + host = parentList + AddHandler host.OnInsert, AddressOf Host_OnInsert + AddHandler host.OnRemove, AddressOf Host_OnRemove + End Sub + + Protected Overrides Sub Finalize() + RemoveHandler host.OnInsert, AddressOf Host_OnInsert + RemoveHandler host.OnRemove, AddressOf Host_OnRemove + End Sub + + Private Sub Host_OnInsert(sender As Object, e As SimpleSortingHostEventArgs) + '... + End Sub + + Private Sub Host_OnRemove(sender As Object, e As SimpleSortingHostEventArgs) + '... + End Sub + + ''' + ''' Stores positioning infos for the given element. + ''' + ''' + ''' + ''' + Public Sub SetElementPosition(elementID As UniquieID, position As ElementSortingPosition, referenceElementID As UniquieID) + '... + End Sub + + ''' + ''' Removes the positioning infos for the given element if available. + ''' + ''' + Public Sub RemoveElementPosition(elementID As UniquieID) + Dim infoToRemove = FindSortInfoByID(elementID) + + If infoToRemove IsNot Nothing Then + sortingList.Remove(infoToRemove) + + For Each info In sortingList + If info.ReferenceElement = elementID Then + info.ReferenceElement = infoToRemove.ReferenceElement + End If + Next + End If + End Sub + + ''' + ''' Completely removes an element from all positioning infos. If possible, elements will be assigned to nearby reference or removed completely. + ''' + ''' + Public Sub InvalidateElement(elementID As UniquieID) + '... + End Sub + + ''' + ''' Creates a sorted list with all elements from the host list. + ''' + ''' + Public Function GetSortedList() As List(Of T) + Dim list As New List(Of T) + list.AddRange(host) + + For Each info In sortingList + Dim element As T = list.FirstOrDefault(Function(n) n.ID = info.Element) + Dim referenceElement As T = list.FirstOrDefault(Function(n) n.ID = info.ReferenceElement) + + If element IsNot Nothing AndAlso referenceElement IsNot Nothing Then + list.Remove(element) + Dim referenceElementIndex As Integer = list.IndexOf(referenceElement) + + If info.Position = ElementSortingPosition.After Then + referenceElementIndex += 1 + End If + + list.Insert(referenceElementIndex, element) + End If + Next + + Return list + End Function + + ''' + ''' Creates a sorted list with all elements from the host list. + ''' + ''' + Public Function GetSortedArray() As T() + Return GetSortedList.ToArray + End Function + + Private Function FindSortInfoByID(id As UniquieID) As SimpleSortingEntry + Return sortingList.FirstOrDefault(Function(n) n.Element = id) + End Function + + Private Function FindSortInfoByReferenceID(id As UniquieID) As SimpleSortingEntry + Return sortingList.FirstOrDefault(Function(n) n.ReferenceElement = id) + End Function + + End Class + +End Namespace