Circular Lists
The Class
''' <summary>
''' a list with a limit
''' </summary>
''' <typeparam name="T">the type of data</typeparam>
''' <remarks></remarks>
Public Class ListLimit(Of T)
Private _max As Integer = 0
Private _List As New List(Of T)
Private _pos As Integer = 0
Private _NewLast As Boolean = True
''' <summary>
''' list with max entires
''' </summary>
''' <param name="MaxEntries">maximum entries in list</param>
''' <param name="NewLast">True if new items added to end, False if new entries added to beginning</param>
''' <remarks></remarks>
Public Sub New(MaxEntries As Integer,
Optional NewLast As Boolean = True)
Me._max = MaxEntries
Me._NewLast = NewLast
If NewLast Then
Dim foo(MaxEntries - 1) As T
Me._List.AddRange(foo)
End If
End Sub
Public Sub Add(item As T)
If Me._NewLast Then
Me._List(Me._pos) = item
Me._pos += 1
If Me._pos = Me._max Then
Me._pos = 0
End If
Else
Me._List.Insert(0, item)
If Me._List.Count = Me._max + 1 Then
Me._List.RemoveAt(Me._max)
End If
End If
End Sub
Public Function TheList() As List(Of T)
Return Me._List
End Function
End Class
Test Code
Debug.WriteLine("---------- Circular List ----------------")
Debug.WriteLine("-------TEST1 step by step - keep 4 newest")
Dim TEST1 As New ListLimit(Of String)(4, False)
Dim N As DateTime = DateTime.Now
Dim EndDT As DateTime = N.AddSeconds(11)
Do
TEST1.Add(N.ToString("HH:mm:ss"))
Debug.WriteLine("Add " & N.ToString("HH:mm:ss") & " -------")
For Each li As String In TEST1.TheList
If li IsNot Nothing Then Debug.WriteLine(li)
Next
Threading.Thread.Sleep(999)
N = DateTime.Now
Loop While N < EndDT
Debug.WriteLine("")
Debug.WriteLine("-------TEST2 step by step - keep 4 oldest")
Dim TEST2 As New ListLimit(Of String)(4)
N = DateTime.Now
EndDT = N.AddSeconds(11)
Do
TEST2.Add(N.ToString("HH:mm:ss"))
Debug.WriteLine("Add " & N.ToString("HH:mm:ss") & " -------")
For Each li As String In TEST2.TheList
If li IsNot Nothing Then Debug.WriteLine(li)
Next
Threading.Thread.Sleep(999)
N = DateTime.Now
Loop While N < EndDT
Results
**---------- Circular List ----------------
-------TEST1 step by step - keep 4 newest
Add 14:06:02 -------
14:06:02
Add 14:06:03 -------
14:06:03
14:06:02
Add 14:06:04 -------
14:06:04
14:06:03
14:06:02
Add 14:06:05 -------
14:06:05
14:06:04
14:06:03
14:06:02
Add 14:06:06 -------
14:06:06
14:06:05
14:06:04
14:06:03
Add 14:06:07 -------
14:06:07
14:06:06
14:06:05
14:06:04
Add 14:06:08 -------
14:06:08
14:06:07
14:06:06
14:06:05
Add 14:06:09 -------
14:06:09
14:06:08
14:06:07
14:06:06
Add 14:06:10 -------
14:06:10
14:06:09
14:06:08
14:06:07
Add 14:06:11 -------
14:06:11
14:06:10
14:06:09
14:06:08
Add 14:06:12 -------
14:06:12
14:06:11
14:06:10
14:06:09
-------TEST2 step by step - keep 4 oldest
Add 14:06:13 -------
14:06:13
Add 14:06:14 -------
14:06:13
14:06:14
Add 14:06:15 -------
14:06:13
14:06:14
14:06:15
Add 14:06:16 -------
14:06:13
14:06:14
14:06:15
14:06:16
Add 14:06:17 -------
14:06:17
14:06:14
14:06:15
14:06:16
Add 14:06:18 -------
14:06:17
14:06:18
14:06:15
14:06:16
Add 14:06:19 -------
14:06:17
14:06:18
14:06:19
14:06:16
Add 14:06:20 -------
14:06:17
14:06:18
14:06:19
14:06:20
Add 14:06:21 -------
14:06:21
14:06:18
14:06:19
14:06:20
Add 14:06:22 -------
14:06:21
14:06:22
14:06:19
14:06:20
Add 14:06:23 -------
14:06:21
14:06:22
14:06:23
14:06:20**