/* Copyright (c) 2024 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #ifndef __Queue_H__ #define __Queue_H__ #include typedef struct Queue { unsigned int itemCount; unsigned int itemAllocatedCount; void ** items; } Queue; typedef bool (* Queue_foreachCallback)(Queue * queue, void * item, void * context); Queue * Queue_create(void); Queue * Queue_copy(Queue * queue); void Queue_dispose(Queue * queue); // Adds item to the end of the queue void Queue_push(Queue * queue, void * item); // Removes the item at the end of the queue and returns it, or NULL if no items are queued void * Queue_pop(Queue * queue); // Adds item to the beginning of the queue void Queue_unshift(Queue * queue, void * item); // Removes the item at the beginning of the queue and returns it, or NULL if no items are queued void * Queue_shift(Queue * queue); // Inserts item at the specified index in the queue. If index is beyond the end of the queue, item is inserted at the end. void Queue_insert(Queue * queue, void * item, unsigned int index); // Removes the item at the specified index from the queue and returns it, or NULL if index is out of range void * Queue_remove(Queue * queue, unsigned int index); // Removes all items from the queue. void Queue_removeAll(Queue * queue); // Appends all items in from to the end of to void Queue_merge(Queue * to, Queue * from); // Invokes callback once for each item in the queue, from beginning to end. If callback returns false, // iteration terminates immediately. Items must not be added or removed during iteration. void Queue_foreach(Queue * queue, Queue_foreachCallback callback, void * callbackContext); // Invokes callback once for each item in the queue, from end to beginning. If callback returns false, // iteration terminates immediately. Items must not be added or removed during iteration. void Queue_foreachReverse(Queue * queue, Queue_foreachCallback callback, void * callbackContext); #endif