Common VBA Stack and Queue Services
Queue and stack services appear pretty trivial at first. However, a closer look, and it is worth a comprehensive (code and forget) implementation. This post focuses on Queue services.
Preface
Common VBA Stack and Queue Services README With four lines of code the implementation of a queue (an Enqueue and a Dequeue service) based on a Collection appears - and is - trivial:
Dim MyQueue As New Collection
MyQueue.Add ... ' enqueue
v = MyQueue(1) ' get (non-object) queue item
' Set v = My queue(1) ' get (object) queue item
MyQueue.Remove 1 ' dequeue
Looks like not worth many more effort. However a true comprehensive solution should address some more aspects of the matter and provide all potentially useful services. Considered should be:
- a mixture of items which represent an object and other type of variables all together in one queue
- not only the first but also a specific item should be able to be de-queued (in case not unique in the queue optionally by its position) ¹
Given these and some more aspects a much more comprehensive (what I like to call ‘code and forget’) solution is worth being implemented. The below private procedures are an extract from the StandardModule (mQ.bas for download) and are also used in the Class module (clsQ.cls for download):
The inline documentation in the above code should suffice. For more see the GitHub repository which is just a Workbook (QaS.xlsb for download) in its dedicated folder. Alternatively just see the README which provides a Queue and a Stack, both as Common Modules, plus a (fully traced) Regression-Test.
Comments
Comments of any kind are more than welcome. I apologize for the fact that it requires a GitHub account/login but this is the only appropriate way avoiding spams.
¹ In contrast to a stack where only push and pop matters, a queue should provided a service to de-queue a specific and not only the first item.⏎