Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

6 Commits

Repository files navigation

vba-queue-array

License: MITPlatformArchitectureRubberduck

Generic Queue Class based on a circular encapsulated VB Array

A lightweight, circular array-backed FIFO queue for VBA with:

  • O(1) enqueue at the rear and dequeue from the front via circular buffer
  • Safe enumeration (For Each) via a proper COM enumerator
  • Clear error semantics (Peek / Dequeue on empty queue raise error 5)
  • Zero dependencies (pure VBA)

📁 Files

FileDescription
Queue.clsSource file with Rubberduck annotations ('@Description, '@DefaultMember, '@Enumerator)
Queue_WithAttributes.clsReady-to-import version with VB attributes baked in — no Rubberduck required

Both files are identical in behaviour. Import Queue_WithAttributes.cls if you are not using Rubberduck.


📦 Features

  • Circular buffer avoids data shifting on every Dequeue
  • Dynamic resizing doubles on grow (GrowthFactor = 2); shrinks by half when occupancy drops below ShrinkTrigger (25%)
  • Enumeration: For Each item In Queue (via hidden [_NewEnum])
  • Utility export: Items([base]) returns a 0- or 1-based array copy
  • Pure VBA, no external references, Rubberduck-friendly annotations

⚙️ Public Interface

MemberTypeDescription
Enqueue(Item)SubAdds an item to the rear of the queue.
Dequeue()FunctionReturns and removes the front item. Raises error 5 if empty.
Peek(Default)PropertyReturns the front item without removing it. Raises error 5 if empty.
CountPropertyNumber of items.
IsEmptyPropertyTrue if empty, else False.
ClearSubRemoves all items.
Items([base])FunctionReturns all items as a Variant() array; base default = 0.
For EachEnumeratorIterates front → rear on a snapshot. (Mutation during iteration is safe but iterates the old snapshot.)

Error behavior

  • Empty queue on Peek / Dequeue raises vbErrorInvalidProcedureCall (=5) with source "Queue.Peek" or "Queue.Dequeue".

🚀 Quick Start

DimqAsNewQueueq.Enqueue"alpha"q.Enqueue"beta"Debug.Printq.Peek' -> alpha (front)Debug.Printq.Dequeue' -> alpha (removed)Debug.Printq.Dequeue' -> beta (removed)Debug.Printq.IsEmpty' -> True

⏱️ Performance

Timings (ms) for one Enqueue + one Dequeue, measured on Windows x64:

#Countvba-queue-arrayvba-queue (Collection)
1100.000730.00045
21000.000720.00045
31,0000.000340.00045
410,0000.000350.00045
5100,0000.000400.00045

The array-based version is more memory-efficient and converges to faster performance at larger sizes. The Collection-based version has lower overhead at small counts. The circular buffer avoids data shifting on every Dequeue.


📄 License

MIT © 2025 Vincent van Geerestein

About

Generic Queue Class based on a circular encapsulated VB Array

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages