2.0 - is there a generic for this?

LookitsPuck

Active member
Joined
Jan 23, 2007
Messages
37
Programming Experience
3-5
Alright, I have a queue of objects that I'd like to implement with an ID as the key.

Right now I'm using a Dictionary object, but it's not giving me the functionality I want (FIFO). I can't use a list, because there's no key that I can use there to access the object.

Basically, I have a list of people waiting in line with certain IDs. I have processes that remove them from line based on their ID rather than iterating through, getting a reference to that object, then removing it via that (I could use a Generic list to do that).

Right now, I'm using a Dictionary object (Generic Dictionary) which allows me to use the key to access/add/remove the object, but the objects are not added or listed the way I plan them to be.

Is there an object that I'm overlooking? Something custom I need to create? Or something someone already created?

Basically I'm looking for the FIFO power of a List, with the immediate lookup of a Dictionary which has a key to access the object and the object stored at that key location.

Thanks
-Steve
 
Thanks guys! I think this is more along the lines of what I was looking for:

VB.NET:
Namespace Collections
    Public Class ListQueue(Of ItemType)
        Private _items As List(Of ItemType)
        Private _itemKeys As List(Of Int32)

        Public Sub New()
            _itemKeys = New List(Of Int32)
            _items = New List(Of ItemType)
        End Sub

        Public Sub Enqueue(ByVal key As Int32, ByVal item As ItemType)
            If (Not ContainsKey(key)) Then
                _itemKeys.Add(key)
                _items.Add(item)
            Else
                Throw New ArgumentException("An item with the same key has already been added")
            End If
        End Sub

        Public Function Dequeue() As ItemType
            If _items.Count = 0 Then Return Nothing
            Dim item As ItemType = _items(0)

            'Remove from the start of the list
            _itemKeys.RemoveAt(0)
            _items.RemoveAt(0)

            Return item
        End Function

        Public Overloads Sub Remove(ByVal key As Int32)
            Dim item As ItemType = Find(key)

            If (item IsNot Nothing) Then
                _items.Remove(item)
                _itemKeys.Remove(key)
            End If
        End Sub

        Public Overloads Function Find(ByVal key As Int32) As ItemType
            If ContainsKey(key) Then
                Dim index As Int32 = 0

                For Each keyEntry As Int32 In _itemKeys
                    If (keyEntry = key) Then
                        Exit For
                    End If

                    index += 1
                Next

                Return _items(index)
            Else
                Return Nothing
            End If
        End Function

        Public Overloads Function ContainsKey(ByVal key As Int32) As Boolean
            Return _itemKeys.Contains(key)
        End Function
    End Class
End Namespace

Testing now. Coming along well, I think. :)
 
Back
Top