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:
- 4:7743528fb9e5
- Parent:
- 3:8e9f85814809
- Child:
- 5:0f65cdadb1a4
--- a/linearArray.hpp Wed Mar 01 10:35:15 2017 +0000
+++ b/linearArray.hpp Thu Mar 02 13:10:56 2017 +0000
@@ -1,6 +1,6 @@
template<class type>
LinearArray<type>::LinearArray(int size) :
- elem_count(0), array_size(size) {
+ elem_count(0), array_size(size), front(0), rear(-1) {
array = new type[size];
}
@@ -13,27 +13,24 @@
int LinearArray<type>::push(type item) {
int ret = -1;
- if (hasSpace()) {
- array[elem_count] = item;
- ret = elem_count;
- elem_increase();
+ if (elem_count < array_size) {
+ if (rear == array_size - 1) rear = -1;
+ array[++rear] = item;
+ ret = elem_count++;
}
return ret;
}
template<class type>
-type LinearArray<type>::pop(int index) {
+type LinearArray<type>::pop() {
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();
+ if (elem_count > 0) {
+ item = array[front++];
+ if (front == array_size) front = 0;
+ elem_count--;
}
return item;
@@ -41,12 +38,12 @@
}
template<class type>
-type& LinearArray<type>::peek(int index) {
+type& LinearArray<type>::peek() {
type item = NULL;
- if (index < elem_count) {
- item = array[index];
+ if (front <= rear) {
+ item = array[front];
}
return item;
@@ -59,28 +56,11 @@
}
template<class type>
-int LinearArray<type>::elements() {
+int LinearArray<type>::count() {
return elem_count;
}
template<class type>
-bool LinearArray<type>::hasSpace() {
- return elem_count < array_size;
-}
-
-template<class type>
-bool LinearArray<type>::empty() {
- return elem_count == 0;
+bool LinearArray<type>::full() {
+ return elem_count == array_size;
}
-
-
-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);
-}