fome-fw/firmware/controllers/core/fl_stack.h

128 lines
2.5 KiB
C
Raw Normal View History

2014-08-29 07:52:33 -07:00
/**
* @file fl_stack.h
* @brief Fixed-length stack
*
* @date Jul 9, 2014
2015-01-12 15:04:10 -08:00
* @author Andrey Belomutskiy, (c) 2012-2015
2014-08-29 07:52:33 -07:00
*/
#ifndef FL_STACK_H_
#define FL_STACK_H_
2015-02-25 06:04:50 -08:00
#include "main.h"
2014-12-27 13:03:38 -08:00
#include "error_handling.h"
2014-08-29 07:52:33 -07:00
template<typename T, int MAXSIZE>
class FLStack {
public:
FLStack();
void push(T value);
2014-10-03 12:05:03 -07:00
void reset();
2014-08-29 07:52:33 -07:00
T pop();
2014-10-03 15:03:01 -07:00
T get(int index);
2015-04-07 17:11:23 -07:00
bool_t remove(T value);
2015-04-07 18:10:24 -07:00
int size();
bool isEmpty();
2014-08-29 07:52:33 -07:00
private:
2015-04-07 17:11:23 -07:00
int currentSize;
2014-08-29 07:52:33 -07:00
T values[MAXSIZE];
};
template<typename T, int MAXSIZE>
FLStack<T, MAXSIZE>::FLStack() {
2014-10-03 12:05:03 -07:00
reset();
2015-04-07 18:10:24 -07:00
memset(values, 0, sizeof(values));
2014-08-29 07:52:33 -07:00
}
template<typename T, int MAXSIZE>
bool FLStack<T, MAXSIZE>::isEmpty() {
2015-04-07 17:11:23 -07:00
return currentSize == 0;
2014-08-29 07:52:33 -07:00
}
2014-10-03 12:05:03 -07:00
template<typename T, int MAXSIZE>
void FLStack<T, MAXSIZE>::reset() {
2015-04-07 17:11:23 -07:00
currentSize = 0;
}
template<typename T, int MAXSIZE>
bool_t FLStack<T, MAXSIZE>::remove(T value) {
for (int i = 0; i < currentSize; i++) {
if (values[i] == value) {
values[0] = values[currentSize - 1];
currentSize--;
return true;
}
}
return false;
2014-10-03 12:05:03 -07:00
}
2014-08-29 07:52:33 -07:00
template<typename T, int MAXSIZE>
void FLStack<T, MAXSIZE>::push(T value) {
2015-04-07 17:11:23 -07:00
if (currentSize >= MAXSIZE) {
2015-02-25 06:04:50 -08:00
firmwareError("FLstack overflow");
return;
//warning()
}
2015-04-07 17:11:23 -07:00
values[currentSize++] = value;
2014-08-29 07:52:33 -07:00
}
2014-10-03 12:05:03 -07:00
template<typename T, int MAXSIZE>
T FLStack<T, MAXSIZE>::pop() {
2015-04-07 17:11:23 -07:00
if (currentSize == 0) {
2014-10-05 07:03:00 -07:00
firmwareError("FLStack is empty");
}
2015-04-07 17:11:23 -07:00
return values[--currentSize];
2014-10-03 12:05:03 -07:00
}
2015-04-07 17:11:23 -07:00
/**
* @return element at the specified index
*/
2014-10-03 15:03:01 -07:00
template<typename T, int MAXSIZE>
T FLStack<T, MAXSIZE>::get(int index) {
2015-02-25 06:04:50 -08:00
efiAssert(index >= 0 && index < MAXSIZE, "FLget", values[0]);
2014-10-03 15:03:01 -07:00
return values[index];
}
2014-10-03 12:05:03 -07:00
2014-08-29 07:52:33 -07:00
template<typename T, int MAXSIZE>
int FLStack<T, MAXSIZE>::size() {
2015-04-07 17:11:23 -07:00
return currentSize;
2014-08-29 07:52:33 -07:00
}
2015-04-07 17:11:23 -07:00
template<class Type, int Dimention>
2015-01-01 15:04:13 -08:00
class ArrayList {
public:
2015-02-25 14:07:10 -08:00
ArrayList();
2015-01-01 15:04:13 -08:00
int size;
Type elements[Dimention];
void reset(void);
Type *add(void);
2015-04-07 18:10:24 -07:00
void removeAt(int index);
2015-01-01 15:04:13 -08:00
};
2015-04-07 17:11:23 -07:00
template<class Type, int Dimention>
ArrayList<Type, Dimention>::ArrayList(void) {
2015-02-25 14:07:10 -08:00
memset(&elements, 0, sizeof(elements));
reset();
}
2015-04-07 18:10:24 -07:00
template<class Type, int Dimention>
void ArrayList<Type, Dimention>::removeAt(int index) {
efiAssertVoid(index < size, "index greater then size");
memcpy(&elements[index], &elements[size - 1], sizeof(Type));
memset(&elements[size - 1], 0, sizeof(Type));
size--;
}
2015-04-07 17:11:23 -07:00
template<class Type, int Dimention>
void ArrayList<Type, Dimention>::reset(void) {
2015-01-01 15:04:13 -08:00
size = 0;
}
2015-04-07 17:11:23 -07:00
template<class Type, int Dimention>
Type * ArrayList<Type, Dimention>::add(void) {
2015-01-01 15:04:13 -08:00
efiAssert(size < Dimention, "add() too many elements", (Type *)NULL);
return &elements[size++];
}
2014-08-29 07:52:33 -07:00
#endif /* FL_STACK_H_ */