start implementation fo SimpleSorting
This commit is contained in:
121
Pilz.Collections/SimpleSorting/SimpleSortingList.vb
Normal file
121
Pilz.Collections/SimpleSorting/SimpleSortingList.vb
Normal 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
|
||||
Reference in New Issue
Block a user