Merge pull request #433 from LudwigOrtmann/lifo_debug

lifo debug and reset
This commit is contained in:
Ludwig Ortmann 2013-12-20 06:29:44 -08:00
commit b0d51d326a
2 changed files with 70 additions and 10 deletions

View File

@ -11,17 +11,52 @@
* @{ * @{
* *
* @file lifo.h * @file lifo.h
* @brief LIFO buffer API * @brief LIFO buffer API, read long description carefully
* @author probably Kaspar Schleiser
* *
* @author unknwon * @long This LIFO implementation very efficiently handles
* integer values. 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.
*/ */
#ifndef __LIFO_H #ifndef __LIFO_H
#define __LIFO_H #define __LIFO_H
/**
* @brief: check if the given lifo is empty
* @return: true if empty, false otherwise
*/
int lifo_empty(int *array); int lifo_empty(int *array);
/**
* @brief: initialize a lifo array
*
* @param array: an array of int of size n+1
* @param n: maximum integer value the lifo is able to store
*/
void lifo_init(int *array, int n); void lifo_init(int *array, int n);
/**
* @brief: insert an element into the lifo
*
* @param array: an integer array of least i+1 size that does not
* already contain i
* @param i: the integer value to store, between 0 and the size
* of the array -1, must not be stored already
*
*/
void lifo_insert(int *array, int i); void lifo_insert(int *array, int i);
/**
* @brief: extract the least recently inserted element from the lifo
*
* @param array: an integer array
*
* @return: -1 if the lifo is empty, the least recently
* inserted element otherwise
*/
int lifo_get(int *array); int lifo_get(int *array);
/** @} */ /** @} */

View File

@ -9,18 +9,17 @@
/** /**
* @ingroup core_util * @ingroup core_util
* @{ * @{
*
* @file lifo.c * @file lifo.c
* @brief LIFO buffer implementation * @brief LIFO buffer implementation
* * @author probably Kaspar Schleiser
* @file lifo.c
* @brief LIFO buffer implementation
* @author unknown
* @} * @}
*/ */
#include "lifo.h" #include "lifo.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
int lifo_empty(int *array) int lifo_empty(int *array)
{ {
return array[0] == -1; return array[0] == -1;
@ -28,6 +27,7 @@ int lifo_empty(int *array)
void lifo_init(int *array, int n) void lifo_init(int *array, int n)
{ {
DEBUG("lifo_init(%i)\n", n);
for (int i = 0; i <= n; i++) { for (int i = 0; i <= n; i++) {
array[i] = -1; array[i] = -1;
} }
@ -35,19 +35,36 @@ void lifo_init(int *array, int n)
void lifo_insert(int *array, int i) void lifo_insert(int *array, int i)
{ {
DEBUG("lifo_insert(%i)\n", i);
int index = i + 1; int index = i + 1;
#if DEVELHELP
if ((array[index] != -1) && (array[0] != -1)) {
printf("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[index] = array[0];
array[0] = i; array[0] = i;
} }
int lifo_get(int *array) int lifo_get(int *array)
{ {
DEBUG("lifo_get\n");
int head = array[0]; int head = array[0];
if (head != -1) { if (head != -1) {
array[0] = array[head + 1]; array[0] = array[head + 1];
} }
#if 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; return head;
} }
@ -60,12 +77,20 @@ int main()
lifo_init(array, 4); lifo_init(array, 4);
lifo_insert(array, 0);
lifo_insert(array, 1);
lifo_insert(array, 2); lifo_insert(array, 2);
lifo_insert(array, 1);
lifo_insert(array, 3);
lifo_insert(array, 0);
lifo_insert(array, 3); lifo_insert(array, 3);
printf("get: %i\n", lifo_get(array)); printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
printf("get: %i\n", lifo_get(array));
return 0; return 0;
} }