Any FIFO, Queue, Stack helper classes?

Hi,

Would like to use a FIFO without building one :D

Have searched forums without success but thought I'd ask. Needed to buffer multiple sensor readings then compute a trend line. (Put "Stack" in subject 'cause these are all Data Structures 101 exercises).

Thanks!!

PM me with your email address. I have a queue class that I recently wrote that I think will do what you need. It is specifically tailored to store structures but works with data items of any size.

Note that some familiarity with pointers is necessary to use it. For example the prototype of the method to get an item from the queue is:

uint8_t get(void * destination_ptr);

Which puts the item in location destination_ptr and returns 1 if there was something to get and 0 if not.

If this makes sense to you then I'd be happy to zip up the files and email them to you.

Prototypes for all class methods:

            queue(void * ptr_to_storage, int itemsize, int max_entries);
            uint8_t size(void); // returns the maximum number of entries the queue can hold (_max_items)
            uint8_t unused(void); // returns the number of entries that can be added without overwriting oldest
            uint8_t available(void); // gets the number of entries currently in the queue      
            void flush(void); // empties the queue
            uint8_t add(void *new_item_ptr); // adds 1 item to queue. If queue is full the oldest entry will be overwritten
            uint8_t get(void * destination_ptr); // Gets oldest item in queue, returns 0 if nothing to get
            uint8_t getLeave(void * destination_ptr); // retrieves the oldest item but doesn't remove it from the queue // returns 0 if nothing to get
            void dropOldest(void); // Drops the oldest entry in the queue. If the queue is empty does nothing.

Stack.h -

/**
 * Stack.h - an adjustable fixed sized stack template
 * 
 *  Example: to create a stack with max of 10 chars just do this:
 *  
 *  Stack<char, 10> myStack;
 *
 *  myStack.push('a');
 *  myStack.push('b');
 *  myStack.push('c');
 *  while(myStack.empty()) {
 *     char foo = myStack.pop();
 *  }
 * 
 */

#ifndef _STACK_H_
#define _STACK_H_

/**
 * Fixed Size Stack Template
 */
template<typename T, int MAX_ITEMS>
class Stack {
private:
  T stackData[MAX_ITEMS]; // max number of items in this stack
  T* stackPtr;
  int top;
public:
  Stack();
  ~Stack() { }

  int empty() const { return top == -1; }
  int full() const { return top == MAX_ITEMS - 1; }
  T pop();
  void push(const T& item);
  int size() const { return top+1; }
};

/**
 * Fixed Size Stack Constructor
 */
template<typename T, int N>
Stack<T, N>::Stack():top(-1) {
  stackPtr = &stackData[0]; // initialize stackPt
}

/**
 * push a value on the stack
 */
template<typename T, int N>
void Stack<T, N>::push(const T& item) {
  if (!full()) {
      stackPtr[++top] = item;
  }
  // push unsuccessful
}

/**
 * pop a value off the stack
 */
template<typename T, int N>
T Stack<T, N>::pop() {
  if (!empty()) {
    return stackPtr[top--]; // pop successful
  }
  else {
    stackPtr[0]; // pop unsuccessful .. need to return something
  }
}
#endif

Stack of 10 Ints -

// example using Fixed Size Stack

#include "Stack.h"

// create a stack of 10 ints
typedef Stack<int,10> IntStack;

static IntStack myStack;

void setup() {
  Serial.begin(9600);

  // push some data into the stack
  for(int i=0; i < 10; ++i ) {
    myStack.push(i);
  }
}

void loop() {

  if ( !myStack.empty() ) {
    Serial.print("Stack size=");
    Serial.println(myStack.size());

    Serial.print('<');
    while( !myStack.empty() ) {
      Serial.print(myStack.pop());
      if ( !myStack.empty() ) {
        Serial.print(", ");
      }
    }
    Serial.println('>');
  }
  else {
    Serial.println("stack empty");
  }
  delay(1000);
}

Output looks like this:

Stack size=10
<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>
stack empty

Hi,

Would like to use a FIFO without building one

Would like to use a FIFO without building one

You might look at this:

http://code.google.com/p/alexanderbrevig/source/browse/trunk#trunk/Arduino/DataStorage/SimpleFIFO