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