Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: linearArray.hpp
- Revision:
- 9:68d882e457c5
- Parent:
- 2:92576523c23e
- Child:
- 10:864b79e79ca8
--- a/linearArray.hpp Wed Oct 05 09:35:55 2016 +0000
+++ b/linearArray.hpp Tue Mar 14 10:05:47 2017 +0000
@@ -1,59 +1,93 @@
-
+template<class type>
+LinearArray<type>::LinearArray(int size, bool forced) :
+ _elem_count(0), _array_size(size), _front(0), _rear(-1), _forced(forced) {
+ _array = new (std::nothrow) type[size];
+}
-template <class type>
-LinearArray<type>::LinearArray(int size): elem_count(0), array_size(size) {
- array = new type[size];
+template<class type>
+LinearArray<type>::~LinearArray() {
+ delete[] _array;
}
-template <class type>
-LinearArray<type>::~LinearArray() {
- delete[] array;
+template<class type>
+int LinearArray<type>::push(type item) {
+
+ int ret = -1;
+ bool room = _elem_count < _array_size;
+ if (!room && _forced) {
+ if (++_front == _array_size) _front = 0;
+ _elem_count--;
+ room = true;
+ }
+ if (room) {
+ if (_rear == _array_size - 1) _rear = -1;
+ _array[++_rear] = item;
+ ret = _elem_count++;
+ }
+
+ return ret;
}
-template <class type>
-int LinearArray<type>::add(type item) {
-
- int ret = -1;
- if (hasSpace()) {
- array[elem_count] = item;
- ret = elem_count++;
- }
-
- return ret;
+template<class type>
+type& LinearArray<type>::pop() {
+
+ static type defaultType;
+ type item = defaultType;
+
+ if (_elem_count > 0) {
+ item = _array[_front++];
+ if (_front == _array_size) _front = 0;
+ _elem_count--;
+ }
+
+ return item;
+
}
-template <class type>
-void LinearArray<type>::remove(int index) {
-
- if (index < elem_count) {
- for (int i = index; i < elem_count-1; i++) {
- array[i] = array[i+1];
- }
-
- elem_count--;
- }
-
-}
+template<class type>
+bool LinearArray<type>::try_pop(type& item) {
+ bool success = false;
-template <class type>
-int LinearArray<type>::size() {
- return array_size;
+ if (_elem_count > 0) {
+ item = _array[_front++];
+ if (_front == _array_size) _front = 0;
+ _elem_count--;
+ success = true;
+ }
+
+ return success;
}
-template <class type>
-int LinearArray<type>::elements() {
- return elem_count;
+template<class type>
+type& LinearArray<type>::peek(int idx) {
+
+ static type defaultType;
+ type item = defaultType;
+
+ if (idx <= _elem_count) {
+ int real_loc = _front + idx;
+ if (real_loc >= _array_size) {
+ real_loc = real_loc - _array_size;
+ }
+
+ item = _array[real_loc];
+ }
+
+ return item;
+
}
-template <class type>
-bool LinearArray<type>::hasSpace() {
- return elem_count < array_size;
+template<class type>
+int LinearArray<type>::size() {
+ return _array_size;
}
-template <class type>
-type& LinearArray<type>::operator[](int index) {
- if (index < elem_count) {
- return array[index];
- }
- return;
+template<class type>
+int LinearArray<type>::count() {
+ return _elem_count;
}
+
+template<class type>
+bool LinearArray<type>::full() {
+ return _elem_count == _array_size;
+}