Re: Priority Queues in VBScript



wieczo.yo@xxxxxxxxxxxxxx wrote:
Hello,
i am trying to make a priority queue in VBScript. I want to use an
array and some function for that.
I have it defined in VBA like that:

--->VBA Code
Type Item
Name As String
Priority As Integer
End Type

Dim Queue(100) As Item
function AddToQueue() As Boolean
function DeleteFromQueue() As Boolean
<---VBA Code

I want to use it in the MUSHClient. It is a telnet-Client for MUD
games.
I wonder how I could do the same definitions in VBScript because I read
that I cannot use data types. Do I have to declare two arrays? e.g.
Priorities(100) and Names(100)

Thank you,

Thomas

This VBScript class implements a simple priority queue:

' ============================================================================
''= implements a simple priority queue via a disconnected ado recordset
' A priority queue is an ADT (abstract data type) supporting the following three operations:
' * add an element to the queue with an associated priority
' * remove the element from the queue that has the highest priority, and return it
' * (optionally) peek at the element with highest priority without removing it
' The simplest way to implement a priority queue data type is to keep an
' associative array mapping each priority to a list of elements with
' that priority.
' [http://en.wikipedia.org/wiki/Priority_queue]
' ============================================================================

Class cPriQ

Private m_darQ ''< disconnected ado recordset to store priority queue

' ----------------------------------------------------------------------------
''- *structors: create/destroy disconnected ado recordset
''- 2 Fields: Priority, Name
''- Sort : "iPri DESC"
' ----------------------------------------------------------------------------

Private Sub Class_Initialize()
Const adVarWChar = 202 ' 000000CA
Const adInteger = 3 ' 00000003
Set m_darQ = CreateObject( "ADODB.Recordset" )
m_darQ.Fields.Append "iPri" , adInteger
m_darQ.Fields.Append "sName", adVarWChar, 250
m_darQ.Open
m_darQ.Sort = "iPri DESC"
End Sub

Private Sub Class_Terminate()
Set m_darQ = Nothing
End Sub

' ----------------------------------------------------------------------------
''- add priority, name to queue
' ----------------------------------------------------------------------------

Public Sub addPriQ( nPri, sName )
m_darQ.AddNew
m_darQ.Fields( "iPri" ) = nPri
m_darQ.Fields( "sName" ) = sName
m_darQ.Update
End Sub

' ----------------------------------------------------------------------------
''- show table (= recordset)
' ----------------------------------------------------------------------------

Public Sub showPriQ()
Const adClipString = 2 ' 00000002
Dim sTmp
m_darQ.MoveFirst
If m_darQ.EOF Then
sTmp = "--- PriQ is empty ---"
Else
sTmp = "--- PriQ ---" + vbCrLf _
+ m_darQ.GetString( adClipString, , vbTab, vbCrLf, "NULL" ) _
+ "-------------"
End If
WScript.Echo sTmp
End Sub

' ----------------------------------------------------------------------------
''- remove = peek + delete first
' ----------------------------------------------------------------------------

Public Function removePriQ()
Dim sRVal : sRVal = peekPriQ()
If "--- PriQ is empty ---" <> sRVal Then
m_darQ.MoveFirst
m_darQ.Delete
End If
removePriQ = sRVal
End Function

' ----------------------------------------------------------------------------
''- peek - return sName from top row
' ----------------------------------------------------------------------------

Public Function peekPriQ()
m_darQ.MoveFirst
If m_darQ.EOF Then
peekPriQ = "--- PriQ is empty ---"
Else
peekPriQ = m_darQ.Fields( "sName" )
End If
End Function

End Class

and this main code may be used to test the class:

Dim oPriQ : Set oPriQ = New cPriQ
Dim aActs : aActs = Array( Array( "add" , 5, "5 1" ) _
, Array( "add" , 7, "7 1" ) _
, Array( "add" , 3, "3 1" ) _
, Array( "show" ) _
, Array( "peek" , "7 1" ) _
, Array( "remove", "7 1" ) _
, Array( "peek" , "5 1" ) _
, Array( "show" ) _
, Array( "add" , 7, "7 1" ) _
, Array( "add" , 7, "7 3" ) _
, Array( "add" , 7, "7 2" ) _
, Array( "show" ) _
, Array( "peek" , "7 3" ) _
)
Dim aAct, sName
For Each aAct In aActs
Select Case aAct( 0 )
Case "add"
oPriQ.addPriQ aAct( 1 ), aAct( 2 )
Case "show"
oPriQ.showPriQ
Case "remove"
sName = oPriQ.removePriQ()
If sName <> aAct( 1 ) Then
WScript.Echo "Error remove: " + sName + " <> " + aAct( 1 )
End If
Case "peek"
sName = oPriQ.peekPriQ()
If sName <> aAct( 1 ) Then
WScript.Echo "Error peek: " + sName + " <> " + aAct( 1 )
End If
End Select
Next

.



Relevant Pages

  • Re: [QUIZ] Word Chains (#44)
    ... // Ruby extension implementing a priority queue ... // Except for using a value except of a void* the priority queue c-code is ruby ... unsigned int degree; ...
    (comp.lang.ruby)
  • Re: QoS to limit bandwith to a particular app?
    ... Create a class map such as: ... With CBWFQ you should not reserve more than 75% of the bandwidth. ... Priority 880 ... line speed for priority queue will most likely leave other traffic no chance ...
    (comp.dcom.sys.cisco)
  • [RFC PATCH 4/5] cfq-iosched: enable idling for last queue on priority class
    ... priority queue has idling disabled, lower priority queues may steal ... The patch solves this issue by always performing idle on the last ...
    (Linux-Kernel)
  • Re: Common data structures (sets, queues, maps etc
    ... The thing that I do is to insert than then sort the whole list. ... determines the priority of the item. ... priority queue instead, but then you need to write your own insertion ... vector operations) for making a matlab code run fast. ...
    (comp.soft-sys.matlab)