2013-11-27 16:28:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
|
|
*
|
2014-08-23 15:43:13 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-01-07 15:36:14 +01:00
|
|
|
* @ingroup core_util
|
2013-11-27 16:28:31 +01:00
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2014-04-01 15:17:36 +02:00
|
|
|
* @brief LIFO buffer API, read long description carefully
|
|
|
|
* @author Heiko Will <hwill@inf.fu-berlin.de>
|
|
|
|
*
|
2014-10-19 23:05:36 +02:00
|
|
|
* @details This LIFO implementation very efficiently handles integer values.
|
2014-04-01 15:17:36 +02:00
|
|
|
* The caveat is that it **can only handle values between 0 and its own
|
|
|
|
* size - 1**. Also it can only handle up to one element of each value.
|
|
|
|
* If you insert a value twice the LIFO will break.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
2017-01-18 13:00:05 +01:00
|
|
|
#ifndef LIFO_H
|
|
|
|
#define LIFO_H
|
2010-12-10 16:49:29 +01:00
|
|
|
|
2021-02-26 12:27:08 +01:00
|
|
|
#include "log.h"
|
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
2020-03-30 17:02:08 +02:00
|
|
|
extern "C" {
|
2014-10-09 01:18:16 +02:00
|
|
|
#endif
|
|
|
|
|
2013-12-20 00:00:29 +01:00
|
|
|
/**
|
2014-04-01 15:17:36 +02:00
|
|
|
* @brief Check if the given lifo is empty.
|
|
|
|
*
|
|
|
|
* @param[in] array The lifo array to check.
|
|
|
|
*
|
|
|
|
* @return 1, if empty
|
|
|
|
* @return 0, otherwise.
|
2013-12-20 00:00:29 +01:00
|
|
|
*/
|
2021-02-26 12:27:08 +01:00
|
|
|
static inline int lifo_empty(int *array)
|
|
|
|
{
|
|
|
|
return array[0] == -1;
|
|
|
|
}
|
2013-12-20 00:00:29 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-01 15:17:36 +02:00
|
|
|
* @brief Initialize a lifo array.
|
2013-12-20 00:00:29 +01:00
|
|
|
*
|
2017-12-19 09:00:09 +01:00
|
|
|
* @pre `array != NULL`
|
|
|
|
*
|
|
|
|
* @param[in,out] array An array of size *n* + 1. Must not be `NULL`.
|
2014-04-01 15:17:36 +02:00
|
|
|
* @param[in] n Maximum integer value the lifo is able to store.
|
2013-12-20 00:00:29 +01:00
|
|
|
*/
|
2021-02-26 12:27:08 +01:00
|
|
|
static inline void lifo_init(int *array, int n)
|
|
|
|
{
|
|
|
|
LOG_DEBUG("lifo_init(%i)\n", n);
|
|
|
|
for (int i = 0; i <= n; i++) {
|
|
|
|
array[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 00:00:29 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-01 15:17:36 +02:00
|
|
|
* @brief Insert an element into the lifo
|
2013-12-20 00:00:29 +01:00
|
|
|
*
|
2017-12-19 09:00:09 +01:00
|
|
|
* @pre `array != NULL`
|
|
|
|
*
|
2014-04-01 15:17:36 +02:00
|
|
|
* @param[in,out] array An integer array of least *i* + 1 size that **does not
|
2017-12-19 09:00:09 +01:00
|
|
|
* already contain *i***. Must not be `NULL`.
|
2014-04-01 15:17:36 +02:00
|
|
|
* @param[in] i The integer value to store, between 0 and the size of
|
|
|
|
* the array - 1, must not be stored already.
|
2013-12-20 00:00:29 +01:00
|
|
|
*
|
|
|
|
*/
|
2021-02-26 12:27:08 +01:00
|
|
|
static inline void lifo_insert(int *array, int i)
|
|
|
|
{
|
|
|
|
LOG_DEBUG("lifo_insert(%i)\n", i);
|
|
|
|
|
|
|
|
int index = i + 1;
|
|
|
|
|
|
|
|
#ifdef DEVELHELP
|
|
|
|
if ((array[index] != -1) && (array[0] != -1)) {
|
|
|
|
LOG_WARNING(
|
|
|
|
"lifo_insert: overwriting array[%i] == %i with %i\n\n\n"
|
|
|
|
"\t\tThe lifo is broken now.\n\n\n", index,
|
|
|
|
array[index], array[0]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
array[index] = array[0];
|
|
|
|
array[0] = i;
|
|
|
|
}
|
2013-12-20 00:00:29 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-01 15:17:36 +02:00
|
|
|
* @brief Extract the least recently inserted element from the lifo.
|
2014-02-11 18:15:43 +01:00
|
|
|
*
|
2017-12-19 09:00:09 +01:00
|
|
|
* @pre `array != NULL`
|
|
|
|
*
|
|
|
|
* @param[in] array An integer array. Must not be NULL.
|
2013-12-20 00:00:29 +01:00
|
|
|
*
|
2014-04-01 15:17:36 +02:00
|
|
|
* @return -1, if the lifo is empty.
|
|
|
|
* @return the least recently inserted element, otherwise.
|
2013-12-20 00:00:29 +01:00
|
|
|
*/
|
2021-02-26 12:27:08 +01:00
|
|
|
static inline int lifo_get(int *array)
|
|
|
|
{
|
|
|
|
DEBUG("lifo_get\n");
|
|
|
|
int head = array[0];
|
|
|
|
|
|
|
|
if (head != -1) {
|
|
|
|
array[0] = array[head + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEVELHELP
|
|
|
|
/* make sure a double insert does not result in an infinite
|
|
|
|
* resource of values */
|
|
|
|
array[head + 1] = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUG("lifo_get: returning %i\n", head);
|
|
|
|
return head;
|
|
|
|
}
|
2010-12-10 16:49:29 +01:00
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-18 13:00:05 +01:00
|
|
|
#endif /* LIFO_H */
|
2013-11-27 16:28:31 +01:00
|
|
|
/** @} */
|