Re: Priority Queues in VBScript
- From: "ekkehard.horner" <ekkehard.horner@xxxxxxxx>
- Date: Wed, 05 Apr 2006 18:30:12 +0200
wieczo.yo@xxxxxxxxxxxxxx wrote:
Hello,This VBScript class implements a simple priority queue:
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
' ============================================================================
''= 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
.
- Follow-Ups:
- Re: Priority Queues in VBScript
- From: wieczo . yo
- Re: Priority Queues in VBScript
- References:
- Priority Queues in VBScript
- From: wieczo . yo
- Priority Queues in VBScript
- Prev by Date: VBScript and HTTPS file download
- Next by Date: Re: Mouseover should change jpg images
- Previous by thread: Priority Queues in VBScript
- Next by thread: Re: Priority Queues in VBScript
- Index(es):
Relevant Pages
|