Marcus Lee / LinearArray

linearArray.hpp

Committer:
UHSLMarcus
Date:
2016-10-04
Revision:
1:49758f1e1317
Child:
2:92576523c23e

File content as of revision 1:49758f1e1317:



template <class type>
LinearArray<type>::LinearArray(int size): elem_count(0) {
    array = new type[size];
}

template <class type>
LinearArray<type>::~LinearArray() {
    delete[] array;
}

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>
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>
int LinearArray<type>::size() {
    return sizeof(array);
}

template <class type>
int LinearArray<type>::elements() {
    return elem_count;
}

template <class type>
bool LinearArray<type>::hasSpace() {
    return elem_count < sizeof(array);
}

template <class type>
type& LinearArray<type>::operator[](int index) {
    if (index < elem_count) {
        return array[index];
    }
    return;
}