Solved

collections.Queue with a definite capacity where old objects expire

Posted on 2006-06-22
9
246 Views
Last Modified: 2010-04-24
Hello,

This is in VB.Net although C# may be acceptable too if it can be translated.

I'd like to use something FIFO like System.Collections.Generic.Queue but I'd also like it to have a maximum capacity so that if there are too many items in the queue, the oldest items will fall out of the queue (or expire) as new items are added. I don't want this queue to grow indefinitely.

Is there any way to do this, or is there another object like Queue that has this functionality?

In case you're still confused, this is what I need..

Queue (with a maximum capacity of 3):
(Empty)

This Queue (though I'm not sure if it can be done) should have a maximum size of 3.

When I add in A,B,C, in that order, A will be in 0, B will be in 1, and C will be in 2, like this:

0) A
1) B
2) C

But if I add a fourth item D, A will expire, B will be in 0, C will be in 1, and D will be in 2, like this:

Queue:
0) B
1) C
2) D

I also need to pull items off the stack in a FIFO manner (like Queue's .dequeue), like this (from the B,C,D queue):

Dequeue (pop B off the stack), Dequeue (pop C off the stack) then the stack will look like this:

0) D

Thanks in advance!

Jake
0
Comment
Question by:jakeryan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16962278
I'm pretty sure you'll have to either subclass or wrap the Queue class and implement the required functionality yourself.

Maybe you'd be better of with a simple array.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16963012
It would be easy enough to wrap an ArrayList in a Class to give you this functionality...
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 16963202
Hi jakeryan;

Here is a Queue Collections Class modified to do what you want.

Public Class SmallQueue1
    Inherits System.Collections.Queue

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal col As ICollection)
        MyBase.New(col)
    End Sub

    Public Sub New(ByVal capacity As Integer)
        ' As you only want a queue of three set the queue to be three
        ' Do not need to set the grow factor
        MyBase.New(3)
    End Sub

    Public Sub New(ByVal capacity As Integer, ByVal growFactor As Single)
        ' As you only want a queue of three set the queue to be three
        MyBase.New(3)
    End Sub

    Public Overrides Sub Enqueue(ByVal obj As Object)

        If Me.Count > 2 Then
            MyBase.Dequeue()
        End If
        MyBase.Enqueue(obj)

    End Sub

End Class

Now in the Form you can do this.

        Dim myQueue As New SmallQueue1
        myQueue.Enqueue("A")
        myQueue.Enqueue("B")
        myQueue.Enqueue("C")
        myQueue.Enqueue("D")
        myQueue.Enqueue("E")
        myQueue.Enqueue("F")
        myQueue.Enqueue("G")

        Console.WriteLine(myQueue.Dequeue())    ' Prints out E
        Console.WriteLine(myQueue.Dequeue())    ' Prints out F
        Console.WriteLine(myQueue.Dequeue())    ' Prints out G


You can modify the SmallQueue1 Calss as needed.

Fernando
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:jakeryan
ID: 17053283
FernandoSoto,

Based on the example you've given, that looks like it would do exactly what I need! Unfortunately, due to a more pressing bug I've had to roll back my project to a few weeks ago. I can't wait to try it out though. I just wanted to give you a heads up I didn't abandon this.

Jake
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17053352
No problem. Let me know if you need any other help with this.

Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17208044
Hi Bob;

I would like to see a resolution to the problem seeming I gave jakeryan a solution that works and his statement was, "Based on the example you've given, that looks like it would do exactly what I need!"."

Yhanks;

Fernando
0
 

Author Comment

by:jakeryan
ID: 17214054
Not abandoned, I needed more time. Thank you FernandoSoto! I'm sorry I couldn't test this earlier, but due to a very nasty bug in my program popping up, I had to resort to an older version and work my way back up to present. Your queue class worked for me!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17215222
No problem. Glad I was able to help. ;=)
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question