The ASPSmith Articles, ASPAlliance.com |
Queues in ASP Using Strings |
| http://www.aspalliance.com/stevesmith/articles/string_queues.asp |
|
[Example]
If you've ever taken more than the most rudimentary course in programming, you have probably learned about the basic data types of lists, queues, and stacks. Well, in developing an advertisement rotation system that would avoid repeating the same ads on a page, I decided that a queue would be the best way to solve the problem. Whenever an ad was displayed, it would get placed into the queue. The ad algorithm would then make sure that it didn't display ads if they were already in the queue. Finally, once the queue reached its size limit, the oldest element would automatically be dequeued upon the addition of more elements. By ensuring that the queue's size was always equal to or less than the total number of unique ads I was showing, I could therefore guarantee that I would not repeat ads until all of them had shown once. Now, this is really just a very simple example of queues, and I'm not really going to go into much detail about abstract data types or how one might implement these using pointers and linked lists and the like in C/C++, because I did all of that in school and now I don't even use it because I deal with VB/VBScript most of the time. I may do a followup article that shows how to do this in .NET using the System.Collections.Queue class, but since it does all of the work for you, that would probably not be worthwhile. For now, we're going to keep this very simple. No objects, no classes, just 5 simple functions in an ASP include file that ANYBODY should be able to use. And a working demo, of course. Here's the source of the queue include file: 1 <script language="vbscript" runat="server">2 3 'Return true if the queue is full, false otherwise 4 Function isFull(ByVal strQ, ByVal maxlength) 5 isFull = size(strQ) >= maxlength 6 End Function 7 8 'Return the number of elements in the Queue 9 Function size(strQ) 10 Dim arrQ 11 arrQ = Split(strQ, ",") 12 size = UBound(arrQ) + 1 13 End Function 14 15 'Add an element to the queue 16 'If forceEntry is true, automatically dequeue first element to make room if queue is full 17 'If forceEntry is false and queue is full, do nothing (it is up to calling function to use isFull) 18 Sub Enq(ByRef strQ, ByVal strItem, ByVal maxlength, ByVal forceEntry) 19 If strItem = "" Then Exit Sub 20 If strQ = "" Then 21 strQ = strItem 22 Else 23 If size(strQ) < maxlength Then 24 strQ = strQ & "," & strItem 25 Else 26 If forceEntry Then 27 Call Deq(strQ) 28 strQ = strQ & "," & strItem 29 End If 30 End If 31 End If 32 End Sub 33 34 'Remove the first element from the queue 35 Function Deq(ByRef strQ) 36 Dim arrQ 37 Dim start 38 39 If strQ = "" Then Exit Function 40 arrQ = Split(strQ, ",") 41 If Not IsArray(arrQ) Then Exit Function 42 Deq = arrQ(0) 43 start = Instr(1, strQ, ",")+1 44 strQ = Mid(strQ, start, Len(strQ)-(start-1)) 45 End Function 46 47 'Dump out the Queue and its statistics 48 Function DumpQ(ByVal strQ, maxlength) 49 DumpQ = "Queue: " & strQ & " | Size: " & size(strQ) & " | Full?: " & isFull(strQ, maxlength) & "<br>" 50 End Function 51 52 </script> Now, for the demo, I've simply called each of these functions a few times so that you can get a feel for how these work. Here's the source to the demo: 1 <% OPTION EXPLICIT %>2 <!-- #INCLUDE VIRTUAL="/stevesmith/include/articleformat.asp" --> 3 <!-- #INCLUDE VIRTUAL="/stevesmith/include/queues.asp" --> 4 <% 5 Dim objQ 6 Dim intQMax 7 Dim forceEntry 8 9 intQMax = 5 'Set the maximum length of the Queue 10 forceEntry = True 'Determines whether exceeding length of queue automatically dequeues first item 11 12 Call ArticleHeader("Queues Example","","") 13 14 Response.Write "<p>Creating Queue by adding 'Steve'.<br>" 15 Call Enq(objQ, "Steve", intQMax, forceEntry) 16 Response.Write DumpQ(objQ, intQMax) 17 Response.Write "</p>" 18 19 Response.Write "<p>Adding 'Rachel', 'Amy', 'Mike', 'Bob' to the queue...<br>" 20 Call Enq(objQ, "Rachel", intQMax, forceEntry) 21 Call Enq(objQ, "Amy", intQMax, forceEntry) 22 Call Enq(objQ, "Mike", intQMax, forceEntry) 23 Call Enq(objQ, "Bob", intQMax, forceEntry) 24 Response.Write DumpQ(objQ, intQMax) 25 Response.Write "</p>" 26 27 Response.Write "<p>Now, since forceEntry is set to True, when we add 'Dawn' to the queue, " 28 Response.Write "Steve should be kicked out.<br>" 29 Call Enq(objQ, "Dawn", intQMax, forceEntry) 30 Response.Write DumpQ(objQ, intQMax) 31 Response.Write "</p>" 32 33 Response.Write "<p>That's pretty much it for this example!</p>" 34 Call ArticleFooter() 35 %> |