Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | /** | |||
2 | * @file fl_stack.h | |||
3 | * @brief Fixed-length stack | |||
4 | * | |||
5 | * @date Jul 9, 2014 | |||
6 | * @author Andrey Belomutskiy, (c) 2012-2020 | |||
7 | */ | |||
8 | ||||
9 | #pragma once | |||
10 | ||||
11 | #include "error_handling.h" | |||
12 | ||||
13 | template<typename T, int MAXSIZE> | |||
14 | class FLStack { | |||
15 | public: | |||
16 | FLStack(); | |||
17 | void push(T value); | |||
18 | void reset(); | |||
19 | T pop(); | |||
20 | T get(int index); | |||
21 | bool remove(T value); | |||
22 | int size(); | |||
23 | bool isEmpty(); | |||
24 | private: | |||
25 | int currentSize; | |||
26 | T values[MAXSIZE]; | |||
27 | }; | |||
28 | ||||
29 | template<typename T, int MAXSIZE> | |||
30 | 1 | FLStack<T, MAXSIZE>::FLStack() { | ||
31 | 1 | reset(); | ||
32 | 1 | memset(values, 0, sizeof(values)); | ||
33 | 1 | } | ||
34 | ||||
35 | template<typename T, int MAXSIZE> | |||
36 | bool FLStack<T, MAXSIZE>::isEmpty() { | |||
37 | return currentSize == 0; | |||
38 | } | |||
39 | ||||
40 | template<typename T, int MAXSIZE> | |||
41 | 1 | void FLStack<T, MAXSIZE>::reset() { | ||
42 | 1 | currentSize = 0; | ||
43 | 1 | } | ||
44 | ||||
45 | template<typename T, int MAXSIZE> | |||
46 | 1 | bool FLStack<T, MAXSIZE>::remove(T value) { | ||
47 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1 time.
✗ Decision 'false' not taken.
|
1 | for (int i = 0; i < currentSize; i++) { |
48 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1 time.
✗ Decision 'false' not taken.
|
1 | if (values[i] == value) { |
49 | 1 | values[0] = values[currentSize - 1]; | ||
50 | 1 | currentSize--; | ||
51 | 1 | return true; | ||
52 | } | |||
53 | } | |||
54 | ✗ | return false; | ||
55 | } | |||
56 | ||||
57 | template<typename T, int MAXSIZE> | |||
58 | 6 | void FLStack<T, MAXSIZE>::push(T value) { | ||
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 6 times.
|
6 | if (currentSize >= MAXSIZE) { |
60 | ✗ | firmwareError(ObdCode::ERROR_FL_STACK_OVERFLOW, "FLstack overflow"); | ||
61 | ✗ | return; | ||
62 | //warning() | |||
63 | } | |||
64 | 6 | values[currentSize++] = value; | ||
65 | } | |||
66 | ||||
67 | template<typename T, int MAXSIZE> | |||
68 | 2 | T FLStack<T, MAXSIZE>::pop() { | ||
69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 2 times.
|
2 | if (currentSize == 0) { |
70 | ✗ | firmwareError(ObdCode::CUSTOM_FLSTACK, "FLStack is empty"); | ||
71 | } | |||
72 | 2 | return values[--currentSize]; | ||
73 | } | |||
74 | ||||
75 | /** | |||
76 | * @return element at the specified index | |||
77 | */ | |||
78 | template<typename T, int MAXSIZE> | |||
79 | 2 | T FLStack<T, MAXSIZE>::get(int index) { | ||
80 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | efiAssert(ObdCode::CUSTOM_ERR_ASSERT, index >= 0 && index < MAXSIZE, "FLget", values[0]); | |
81 | 2 | return values[index]; | ||
82 | } | |||
83 | ||||
84 | template<typename T, int MAXSIZE> | |||
85 | 6 | int FLStack<T, MAXSIZE>::size() { | ||
86 | 6 | return currentSize; | ||
87 | } | |||
88 | ||||
89 | template<class Type, int Dimention> | |||
90 | class ArrayList { | |||
91 | public: | |||
92 | ArrayList(); | |||
93 | int size; | |||
94 | Type elements[Dimention]; | |||
95 | void reset(void); | |||
96 | Type *add(void); | |||
97 | void removeAt(int index); | |||
98 | }; | |||
99 | ||||
100 | template<class Type, int Dimention> | |||
101 | ArrayList<Type, Dimention>::ArrayList(void) { | |||
102 | memset(&elements, 0, sizeof(elements)); | |||
103 | reset(); | |||
104 | } | |||
105 | ||||
106 | template<class Type, int Dimention> | |||
107 | void ArrayList<Type, Dimention>::removeAt(int index) { | |||
108 | efiAssertVoid(ObdCode::CUSTOM_ERR_ARRAY_REMOVE, index >= 0 && index < size, "invalid index"); | |||
109 | memcpy(&elements[index], &elements[size - 1], sizeof(Type)); | |||
110 | memset(&elements[size - 1], 0, sizeof(Type)); | |||
111 | size--; | |||
112 | } | |||
113 | ||||
114 | template<class Type, int Dimention> | |||
115 | void ArrayList<Type, Dimention>::reset(void) { | |||
116 | size = 0; | |||
117 | } | |||
118 | ||||
119 | template<class Type, int Dimention> | |||
120 | Type * ArrayList<Type, Dimention>::add(void) { | |||
121 | efiAssert(ObdCode::CUSTOM_ERR_ASSERT, size < Dimention, "add() too many elements", (Type *)NULL); | |||
122 | return &elements[size++]; | |||
123 | } | |||
124 |