• Welcome to Valhalla Legends Archive.
 

Queue Sorting

Started by Don Cullen, July 27, 2006, 12:48 AM

Previous topic - Next topic

Don Cullen

This is what I have so far:

Private Type Queue
    DataToSend As String
    Priority As Integer
End Type

Dim BotQueue(200) As Queue

Private Sub Command1_Click()
    Dim HighPriorityQueue() As Queue
    Dim MediumPriorityQueue() As Queue
    Dim LowPriorityQueue() As Queue
   
    tmpArray = Split(Text1, vbCrLf)
    For i = 0 To UBound(tmpArray) Step 2
        BotQueue(i).DataToSend = tmpArray(i)
        BotQueue(i).Priority = tmpArray(i + 1)
    Next
   
    HighPriorityQueue = GetPriorityQueue(BotQueue, 2)
    MediumPriorityQueue = GetPriorityQueue(BotQueue, 1)
    LowPriorityQueue = GetPriorityQueue(BotQueue, 0)
   
    Text2 = Text2 & "Processing High Priority Queue... " & vbCrLf
    For i = 0 To UBound(HighPriorityQueue)
        Text2 = Text2 & vbCrLf & HighPriorityQueue(i).DataToSend
    Next
   
    Text2 = Text2 & "Processing Medium Priority Queue... " & vbCrLf
    For i = 0 To UBound(MediumPriorityQueue)
        Text2 = Text2 & vbCrLf & MediumPriorityQueue(i).DataToSend
    Next
   
    Text2 = Text2 & "Processing Low Priority Queue... " & vbCrLf
    For i = 0 To UBound(LowPriorityQueue)
        Text2 = Text2 & vbCrLf & LowPriorityQueue(i).DataToSend
    Next
   
    Text2 = Text2 & vbCrLf & "Done processing queues."
End Sub

Private Function GetPriorityQueue(tmpArray() As Queue, PriorityLevel As Integer)
    Dim tmpPriorityQueue() As Queue
    Dim l As Integer
    l = 0
    For i = 0 To UBound(tmpArray)
        If tmpArray(i).Priority = PriorityLevel Then
            tmpPriorityQueue(l).DataToSend = tmpArray(i).DataToSend
            tmpPriorityQueue(l).Priority = tmpArray(i).Priority
            l = l + 1
        End If
    Next
    GetPriorityQueue = tmpPriorityQueue
End Function


The form had two textboxes (Text1 and Text2), and a command button. In Text1, I had the following text:

hey whats up
0
nothing much
0
/ban somebody
3
,dsl
2
,rank
2
heh yeah
0
/kick somebody
3


I ran the program to test it, inputted the above data, clicked the command button. I got this error:

Compile Error:

Only user-defined types defined in public object modules can be coerced to
or from a variant or passed to late-bound functions


So I switched the user-defined Queue type to a module, made it public, and tried again. Same error.

It was at this point my mind pretty much fizzled. Basically I'm trying to sort the BotQueue array which is of a custom type into three separate queues which correspond to their level of priorities...

Any idea what I'm doing wrong?
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Stealth

I'd suggest couple easy options, both of which require creating a basic queue-item class object and then storing a bunch of those objects in a Collection. You could:

- Add high-priority items at the front of the queue (or closer to the front of the queue, anyways) than lower-priority items. You can accomplish this with a Collection, as rearranging the elements of a dynamic array is a pain in the ass. With this method, you would simply always send the front item of the queue because you arranged things the way you wanted them to come out with each addition to the queue;

- Look through your queue ahead of time, storing the index of the highest-priority item, something like this:

(pseudo)
the highest-priority-item so far's index is 1 (send the item at the front of the queue by default)
the highest priority seen so far is 0 (no higher-priority items exist so far)

for each queue item
    if its priority exceeds (> comparison) the highest priority seen so far
        the highest priority seen so far is this item's priority
        the highest-priority-item so far's index is this index


The result here is that the work is done each time your queue "ticks". With this method, you remove and dispatch the message you determine to have the highest-priority on the fly.

Good luck - HTH, and people with more knowledge of these things and I -- feel free to comment! I'm always interested in better ways to do this sort of thing as well.
- Stealth
Author of StealthBot

Don Cullen

Are there any collections tutorials? I tried googling them, but the results were mostly irrelevant...
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Hero

http://people.revoledu.com/kardi/tutorial/VB/lesson06/Collection.htm

^Thats one of many from googling:
visual basic collection tutorials

Don Cullen

I was using vb6 collections as the keywords. that's probably why i had bad results.
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Arta