start implementation fo SimpleSorting

This commit is contained in:
2023-08-09 09:25:06 +02:00
parent e142513083
commit 3ae6e827a5
6 changed files with 173 additions and 0 deletions

View File

@@ -83,4 +83,7 @@
<ItemGroup>
<Compile Remove="SimpleHistory\Enums.vb" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pilz.Cryptography\Pilz.Cryptography.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
Namespace SimpleSorting
Public Enum ElementSortingPosition
Before
After
End Enum
End Namespace

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,121 @@
Imports System.Runtime.InteropServices.ComTypes
Imports Newtonsoft.Json
Imports Pilz.Cryptography
Namespace SimpleSorting
''' <summary>
''' 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.
''' </summary>
''' <typeparam name="T">The content of the host list to sort.</typeparam>
Public Class SimpleSortingList(Of T As IUniquieIDHost)
Private ReadOnly host As ISimpleSortingHost
<JsonProperty("SortingInfo")>
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
''' <summary>
''' Stores positioning infos for the given element.
''' </summary>
''' <param name="elementID"></param>
''' <param name="position"></param>
''' <param name="referenceElementID"></param>
Public Sub SetElementPosition(elementID As UniquieID, position As ElementSortingPosition, referenceElementID As UniquieID)
'...
End Sub
''' <summary>
''' Removes the positioning infos for the given element if available.
''' </summary>
''' <param name="elementID"></param>
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
''' <summary>
''' Completely removes an element from all positioning infos. If possible, elements will be assigned to nearby reference or removed completely.
''' </summary>
''' <param name="elementID"></param>
Public Sub InvalidateElement(elementID As UniquieID)
'...
End Sub
''' <summary>
''' Creates a sorted list with all elements from the host list.
''' </summary>
''' <returns></returns>
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
''' <summary>
''' Creates a sorted list with all elements from the host list.
''' </summary>
''' <returns></returns>
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