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.
linearArray.hpp
- Committer:
- UHSLMarcus
- Date:
- 2017-03-01
- Revision:
- 3:8e9f85814809
- Parent:
- 2:92576523c23e
- Child:
- 4:7743528fb9e5
File content as of revision 3:8e9f85814809:
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>
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>
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>
type& LinearArray<type>::peek(int index) {
type item = NULL;
if (index < elem_count) {
item = array[index];
}
return item;
}
template<class type>
int LinearArray<type>::size() {
return array_size;
}
template<class type>
int LinearArray<type>::elements() {
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;
}
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);
}