Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/yeremyacuna/LYNX/llms.txt

Use this file to discover all available pages before exploring further.

Queue<T> implements a First In, First Out (FIFO) structure, defined in include/Queue.h, and built on top of Node<T> nodes from Node.h. In a FIFO structure, the element that has waited the longest is always the next one to be served — new elements join the back and leave from the front. In LYNX, TripManager maintains a Queue<Trip> named waitingQueue. When a passenger requests a ride and no driver is immediately available, their trip is enqueued; when a driver becomes free, the trip at the front of the queue is dequeued and assigned. This guarantees fair, arrival-order dispatch. The class adds three lambda helpers on top of the core FIFO operations: enqueueIf for conditional insertion, forEach for non-destructive traversal, and sumBy for numeric aggregation across the entire queue.

Private Members

MemberTypeRole
frontNode<T>*Points to the oldest element; the next to be dequeued
backNode<T>*Points to the newest element; where new nodes are appended
sizeintCurrent element count; checked by isEmpty() and getSize()

Method Reference

MethodSignatureDescriptionComplexity
enqueuevoid enqueue(T value)Appends a new node after backO(1)
dequeuevoid dequeue()Removes and deletes the front nodeO(1)
getFrontT getFront() constReturns front->data without removing it; returns T{} if emptyO(1)
getBackT getBack() constReturns back->data without removing it; returns T{} if emptyO(1)
isEmptybool isEmpty() constReturns true when size == 0O(1)
getSizeint getSize() constReturns the current element countO(1)
clearvoid clear()Deletes every node and resets front, back, and sizeO(n)
printvoid print()Prints all elements from front to back as a bracketed list using toString()O(n)
enqueueIfbool enqueueIf(T value, function&lt;bool(T)&gt; condicion)Enqueues only if condicion(value) is true; returns whether it was addedO(1)
forEachvoid forEach(function&lt;void(T)&gt; accion) constCalls accion on every element from front to back without dequeuingO(n)
sumByfloat sumBy(function&lt;float(T)&gt; extractor) constSums extractor(element) across every node; returns the total as floatO(n)

Complexity Summary

OperationTime Complexity
enqueueO(1)
dequeueO(1)
getFront / getBackO(1)
isEmpty / getSizeO(1)
enqueueIfO(1)
forEachO(n)
sumByO(n)
clearO(n)

Lambda Methods

enqueueIf — Conditional Insertion

bool enqueueIf(T value, function<bool(T)> condicion)
Evaluates condicion(value) before inserting. If the condition returns true, the element is enqueued and the method returns true. If the condition returns false, nothing is inserted and the method returns false. This removes the need for an explicit if guard at every call site and makes intent clear.
// Only enqueue the trip if the passenger's DNI is registered
bool added = waitingQueue.enqueueIf(newTrip, [&](Trip t) {
    return auth.userExists(t.getPassengerDni());
});

forEach — Non-Destructive Traversal

void forEach(function<void(T)> accion) const
Iterates from front to back, calling accion on each element’s data. The queue is not modified — no elements are dequeued. Used in TripManager::viewWaitingDetailed to display every pending trip’s details to the console without disturbing the queue’s order.
// Print details of every waiting trip
waitingQueue.forEach([](Trip t) {
    cout << t.toString() << "\n";
});

sumBy — Numeric Aggregation

float sumBy(function<float(T)> extractor) const
Accumulates a numeric value extracted from each element and returns the running total as a float. Used in TripManager::calcTotalEnCola to compute the combined fare of all trips currently waiting to be assigned.
// Total fare of all trips waiting in the queue
float total = waitingQueue.sumBy([](Trip t) {
    return t.getPrice();
});

Usage Example

Queue<Trip> waitingQueue;
waitingQueue.enqueue(trip1);
waitingQueue.enqueue(trip2);

// Sum total fare of all waiting trips
float total = waitingQueue.sumBy([](Trip t) {
    return t.getPrice();
});

// Only enqueue if passenger DNI is valid
bool added = waitingQueue.enqueueIf(newTrip, [&](Trip t) {
    return auth.userExists(t.getPassengerDni());
});

// Dequeue to assign to the next available driver
Trip next = waitingQueue.getFront();
waitingQueue.dequeue();
print() calls toString() on each stored element, so the type T must implement a toString() method that returns a std::string (or a type implicitly convertible to one via operator<<). If T does not provide toString(), the code will not compile.

Build docs developers (and LLMs) love