diff --git a/firmware/controllers/core/fl_stack.h b/firmware/controllers/core/fl_stack.h index 8f25dd9c4d..5ebc436a87 100644 --- a/firmware/controllers/core/fl_stack.h +++ b/firmware/controllers/core/fl_stack.h @@ -20,10 +20,10 @@ public: void reset(); T pop(); T get(int index); - int size(); - bool isEmpty(); + bool_t remove(T value); + int size();bool isEmpty(); private: - int index; + int currentSize; T values[MAXSIZE]; }; @@ -34,32 +34,47 @@ FLStack::FLStack() { template bool FLStack::isEmpty() { - return index == 0; + return currentSize == 0; } template void FLStack::reset() { - index = 0; + currentSize = 0; +} + +template +bool_t FLStack::remove(T value) { + for (int i = 0; i < currentSize; i++) { + if (values[i] == value) { + values[0] = values[currentSize - 1]; + currentSize--; + return true; + } + } + return false; } template void FLStack::push(T value) { - if(index >= MAXSIZE) { + if (currentSize >= MAXSIZE) { firmwareError("FLstack overflow"); return; //warning() } - values[index++] = value; + values[currentSize++] = value; } template T FLStack::pop() { - if (index == 0) { + if (currentSize == 0) { firmwareError("FLStack is empty"); } - return values[--index]; + return values[--currentSize]; } +/** + * @return element at the specified index + */ template T FLStack::get(int index) { efiAssert(index >= 0 && index < MAXSIZE, "FLget", values[0]); @@ -68,10 +83,10 @@ T FLStack::get(int index) { template int FLStack::size() { - return index; + return currentSize; } -template +template class ArrayList { public: ArrayList(); @@ -81,19 +96,19 @@ public: Type *add(void); }; -template -ArrayList< Type, Dimention>::ArrayList(void) { +template +ArrayList::ArrayList(void) { memset(&elements, 0, sizeof(elements)); reset(); } -template -void ArrayList< Type, Dimention>::reset(void) { +template +void ArrayList::reset(void) { size = 0; } -template -Type * ArrayList< Type, Dimention>::add(void) { +template +Type * ArrayList::add(void) { efiAssert(size < Dimention, "add() too many elements", (Type *)NULL); return &elements[size++]; } diff --git a/unit_tests/main.cpp b/unit_tests/main.cpp index 5a722e42b4..d89de8eee6 100644 --- a/unit_tests/main.cpp +++ b/unit_tests/main.cpp @@ -150,7 +150,7 @@ int main(void) { testTriggerDecoder(); // resizeMap(); - printf("Success 20150213\r\n"); + printf("Success 20150407\r\n"); return EXIT_SUCCESS; } diff --git a/unit_tests/test_data_structures/test_event_registry.cpp b/unit_tests/test_data_structures/test_event_registry.cpp index 477b2d1115..f236ef9e13 100644 --- a/unit_tests/test_data_structures/test_event_registry.cpp +++ b/unit_tests/test_data_structures/test_event_registry.cpp @@ -9,10 +9,5 @@ #include "test_event_registry.h" #include "main.h" -static ActuatorEventList eventList; -static ActuatorEventList result; - -extern int outputSignalCount; - void testEventRegistry(void) { } diff --git a/unit_tests/test_util.cpp b/unit_tests/test_util.cpp index d50f0b6f90..50f41a0db5 100644 --- a/unit_tests/test_util.cpp +++ b/unit_tests/test_util.cpp @@ -362,6 +362,15 @@ void testFLStack(void) { assertEquals(123, v); assertEquals(0, stack.size()); + stack.push(123); + stack.push(234); + stack.push(345); + stack.push(456); + assertEquals(4, stack.size()); + + stack.remove(123); + assertEquals(456, stack.get(0)); + assertEquals(3, stack.size()); } static char buff[32];