2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2002-10-12 11:37:38 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
2011-02-27 20:37:56 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2014-08-07 14:42:47 +10:00
|
|
|
*
|
|
|
|
* \brief A generic structure queue
|
|
|
|
* (a queue for fixed length generally small) structures.
|
|
|
|
*
|
|
|
|
* \note Only use this if you need (first-in-first-out),
|
2015-07-31 22:59:02 +10:00
|
|
|
* otherwise #BLI_Stack is more efficient (first-in-last-out).
|
2011-02-27 20:37:56 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
2013-03-09 05:35:49 +00:00
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BLI_gsqueue.h"
|
2013-09-01 03:43:10 +00:00
|
|
|
#include "BLI_strict_flags.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
typedef struct _GSQueueElem GSQueueElem;
|
|
|
|
struct _GSQueueElem {
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *next;
|
|
|
|
char data[0];
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GSQueue {
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *head;
|
|
|
|
GSQueueElem *tail;
|
|
|
|
size_t elem_size;
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
2013-03-24 01:51:54 +00:00
|
|
|
/**
|
|
|
|
* Create a new GSQueue.
|
|
|
|
*
|
2018-12-12 12:50:58 +11:00
|
|
|
* \param elem_size: The size of the structures in the queue.
|
2013-03-24 01:51:54 +00:00
|
|
|
* \retval The new queue
|
|
|
|
*/
|
2014-06-15 03:48:22 +10:00
|
|
|
GSQueue *BLI_gsqueue_new(size_t elem_size)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueue *gq = MEM_mallocN(sizeof(*gq), "gqueue_new");
|
|
|
|
gq->head = gq->tail = NULL;
|
|
|
|
gq->elem_size = elem_size;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
return gq;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:51:54 +00:00
|
|
|
/**
|
|
|
|
* Query if the queue is empty
|
|
|
|
*/
|
2013-03-09 05:35:49 +00:00
|
|
|
bool BLI_gsqueue_is_empty(GSQueue *gq)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return (gq->head == NULL);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:51:54 +00:00
|
|
|
/**
|
|
|
|
* Query number elements in the queue
|
|
|
|
*/
|
2018-02-15 23:36:11 +11:00
|
|
|
int BLI_gsqueue_len(GSQueue *gq)
|
2018-06-17 16:32:54 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *elem;
|
|
|
|
int size = 0;
|
2010-01-22 11:10:24 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
for (elem = gq->head; elem; elem = elem->next) {
|
|
|
|
size++;
|
|
|
|
}
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
return size;
|
2010-01-22 11:10:24 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:51:54 +00:00
|
|
|
/**
|
|
|
|
* Access the item at the head of the queue
|
|
|
|
* without removing it.
|
|
|
|
*
|
2015-05-20 12:54:45 +10:00
|
|
|
* \param r_item: A pointer to an appropriately
|
|
|
|
* sized structure (the size passed to #BLI_gsqueue_new)
|
2013-03-24 01:51:54 +00:00
|
|
|
*/
|
2014-06-15 04:30:32 +10:00
|
|
|
void BLI_gsqueue_peek(GSQueue *gq, void *r_item)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
memcpy(r_item, &gq->head->data, gq->elem_size);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2013-03-24 01:51:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the item at the head of the queue
|
|
|
|
* and remove it.
|
|
|
|
*
|
2015-05-20 12:54:45 +10:00
|
|
|
* \param r_item: A pointer to an appropriately
|
|
|
|
* sized structure (the size passed to #BLI_gsqueue_new).
|
2013-03-24 01:51:54 +00:00
|
|
|
* Can be NULL if desired.
|
|
|
|
*/
|
2014-06-15 04:30:32 +10:00
|
|
|
void BLI_gsqueue_pop(GSQueue *gq, void *r_item)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *elem = gq->head;
|
|
|
|
if (elem == gq->tail) {
|
|
|
|
gq->head = gq->tail = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gq->head = gq->head->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_item) {
|
|
|
|
memcpy(r_item, elem->data, gq->elem_size);
|
|
|
|
}
|
|
|
|
MEM_freeN(elem);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2013-03-24 01:51:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Push an element onto the tail of the queue.
|
|
|
|
*
|
2018-12-12 12:50:58 +11:00
|
|
|
* \param item: A pointer to an appropriately
|
2013-03-24 01:51:54 +00:00
|
|
|
* sized structure (the size passed to BLI_gsqueue_new).
|
|
|
|
*/
|
2014-06-15 04:30:32 +10:00
|
|
|
void BLI_gsqueue_push(GSQueue *gq, const void *item)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *elem;
|
|
|
|
|
|
|
|
/* compare: prevent events added double in row */
|
|
|
|
if (!BLI_gsqueue_is_empty(gq)) {
|
|
|
|
if (0 == memcmp(item, gq->head->data, gq->elem_size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push");
|
|
|
|
memcpy(elem->data, item, gq->elem_size);
|
|
|
|
elem->next = NULL;
|
|
|
|
|
|
|
|
if (BLI_gsqueue_is_empty(gq)) {
|
|
|
|
gq->tail = gq->head = elem;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gq->tail = gq->tail->next = elem;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2013-03-24 01:51:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Push an element back onto the head of the queue (so
|
|
|
|
* it would be returned from the next call to BLI_gsqueue_pop).
|
|
|
|
*
|
2018-12-12 12:50:58 +11:00
|
|
|
* \param item: A pointer to an appropriately
|
2013-03-24 01:51:54 +00:00
|
|
|
* sized structure (the size passed to BLI_gsqueue_new).
|
|
|
|
*/
|
2018-02-15 23:36:11 +11:00
|
|
|
void BLI_gsqueue_push_back(GSQueue *gq, const void *item)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
GSQueueElem *elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push");
|
|
|
|
memcpy(elem->data, item, gq->elem_size);
|
|
|
|
elem->next = gq->head;
|
|
|
|
|
|
|
|
if (BLI_gsqueue_is_empty(gq)) {
|
|
|
|
gq->head = gq->tail = elem;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gq->head = elem;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-24 01:51:54 +00:00
|
|
|
/**
|
|
|
|
* Free the queue
|
|
|
|
*/
|
2002-10-12 11:37:38 +00:00
|
|
|
void BLI_gsqueue_free(GSQueue *gq)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
while (gq->head) {
|
|
|
|
BLI_gsqueue_pop(gq, NULL);
|
|
|
|
}
|
|
|
|
MEM_freeN(gq);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|