ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Lua Queue Implementation with Circular Buffer

Lua

Goal -- WPM

Ready
Exercise Algorithm Area
1local CircularBufferQueue = {}
2CircularBufferQueue.__index = CircularBufferQueue
3
4function CircularBufferQueue:new(capacity)
5local queue = setmetatable({}, CircularBufferQueue)
6queue.capacity = capacity
7queue.buffer = {}
8queue.front = 0
9queue.rear = 0
10queue.size = 0
11return queue
12end
13
14function CircularBufferQueue:is_empty()
15return self.size == 0
16end
17
18function CircularBufferQueue:is_full()
19return self.size == self.capacity
20end
21
22function CircularBufferQueue:enqueue(item)
23if self:is_full() then
24error("Queue is full. Cannot enqueue.")
25end
26self.buffer[self.rear] = item
27self.rear = (self.rear + 1) % self.capacity
28self.size = self.size + 1
29end
30
31function CircularBufferQueue:dequeue()
32if self:is_empty() then
33error("Queue is empty. Cannot dequeue.")
34end
35local item = self.buffer[self.front]
36self.buffer[self.front] = nil -- Optional: clear the dequeued slot
37self.front = (self.front + 1) % self.capacity
38self.size = self.size - 1
39return item
40end
41
42function CircularBufferQueue:peek()
43if self:is_empty() then
44error("Queue is empty. Cannot peek.")
45end
46return self.buffer[self.front]
47end
48
49-- Helper function to get the current size
50function CircularBufferQueue:get_size()
51return self.size
52end
53
54-- Helper function to check if a specific index is within the valid buffer range
55-- This is more for internal logic validation and not typically exposed.
56function CircularBufferQueue:is_valid_index(index)
57return index >= 0 and index < self.capacity
58end
Algorithm description viewbox

Lua Queue Implementation with Circular Buffer

Algorithm description:

This Lua code implements a Queue data structure using a circular buffer. This approach is memory-efficient as it reuses space in a fixed-size array. The `enqueue` operation adds an item to the rear, and `dequeue` removes an item from the front, with wrap-around logic handled by the modulo operator. This is a fundamental data structure used in scheduling, breadth-first search, and managing tasks.

Algorithm explanation:

The `CircularBufferQueue` uses a fixed-size array (`buffer`) and two pointers, `front` and `rear`, to manage queue operations. `front` points to the first element, and `rear` points to the next available slot. The `capacity` defines the maximum number of elements. When `rear` reaches the end of the array, it wraps around to the beginning using the modulo operator (`% self.capacity`). Similarly, `front` wraps around. The `size` variable tracks the current number of elements. `enqueue` adds an item at `rear` and increments `rear`. `dequeue` retrieves the item at `front`, clears the slot (optional), and increments `front`. Both operations update `size`. The time complexity for `enqueue`, `dequeue`, `peek`, `is_empty`, `is_full`, and `get_size` is O(1) because they involve a constant number of operations regardless of the queue size. The space complexity is O(capacity) because the buffer size is fixed. The invariant is that `size` accurately reflects the number of elements between `front` (inclusive) and `rear` (exclusive), considering wrap-around.

Pseudocode:

define CircularBufferQueue with capacity, buffer, front, rear, size

function CircularBufferQueue.new(capacity):
  initialize queue with capacity, empty buffer, front=0, rear=0, size=0
  return queue

function CircularBufferQueue.is_empty():
  return size == 0

function CircularBufferQueue.is_full():
  return size == capacity

function CircularBufferQueue.enqueue(item):
  if is_full():
    raise error "Queue full"
  buffer[rear] = item
  rear = (rear + 1) % capacity
  size = size + 1

function CircularBufferQueue.dequeue():
  if is_empty():
    raise error "Queue empty"
  item = buffer[front]
  buffer[front] = nil -- optional
  front = (front + 1) % capacity
  size = size - 1
  return item

function CircularBufferQueue.peek():
  if is_empty():
    raise error "Queue empty"
  return buffer[front]

function CircularBufferQueue.get_size():
  return size