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:
- 3:8e9f85814809
- Parent:
- 2:92576523c23e
- Child:
- 4:7743528fb9e5
--- a/linearArray.hpp Wed Oct 05 09:35:55 2016 +0000
+++ b/linearArray.hpp Wed Mar 01 10:35:15 2017 +0000
@@ -1,59 +1,86 @@
-
+template<class type>
+LinearArray<type>::LinearArray(int size) :
+ elem_count(0), array_size(size) {
+ array = new 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;
+ if (hasSpace()) {
+ array[elem_count] = item;
+ ret = elem_count;
+ elem_increase();
+ }
+
+ 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(int index) {
+
+ type item = NULL;
+
+ if (index < elem_count) {
+ for (int i = index; i < elem_count - 1; i++) {
+ item = array[index];
+ array[i] = array[i + 1];
+ }
+
+ elem_decrease();
+ }
+
+ 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>
+type& LinearArray<type>::peek(int index) {
+
+ type item = NULL;
+
+ if (index < elem_count) {
+ item = array[index];
+ }
+
+ return item;
+
}
-template <class type>
+template<class type>
int LinearArray<type>::size() {
- return array_size;
+ return array_size;
}
-template <class type>
+template<class type>
int LinearArray<type>::elements() {
- return elem_count;
+ return elem_count;
+}
+
+template<class type>
+bool LinearArray<type>::hasSpace() {
+ return elem_count < array_size;
}
-template <class type>
-bool LinearArray<type>::hasSpace() {
- return elem_count < array_size;
+template<class type>
+bool LinearArray<type>::empty() {
+ return elem_count == 0;
}
-template <class type>
-type& LinearArray<type>::operator[](int index) {
- if (index < elem_count) {
- return array[index];
- }
- return;
+
+template<class type>
+int LinearArray<type>::elem_decrease() {
+ return (elem_count = (elem_count - 1) < 0 ? 0 : elem_count - 1);
}
+
+template<class type>
+int LinearArray<type>::elem_increase() {
+
+ return (elem_count = (elem_count + 1) > array_size ? array_size : elem_count + 1);
+}