C1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.8 kB
5
Indexable
Never
#include <vector>
template<typename T, size_t kMaximumSize>
T& BoundedSizeDynamicArray<T, kMaximumSize>::Front(){
    return elems_[0];
}

template<typename T, size_t kMaximumSize>
const T& BoundedSizeDynamicArray<T, kMaximumSize>::Front() const{
    return elems_[0];
}

template<typename T, size_t kMaximumSize>
T& BoundedSizeDynamicArray<T, kMaximumSize>::Back(){
    return elems_[size_-1];
}

template<typename T, size_t kMaximumSize>
const T& BoundedSizeDynamicArray<T, kMaximumSize>::Back() const{
    return elems_[size_-1];
}


template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::PushFront(const T& val){
    ++size_;
    for(size_t i = size_; i > 0; --i){
        elems_[i] = elems_[i-1];
    }
    elems_[0] = val;
}

template<typename T, size_t kMaximumSIze>
void BoundedSizeDynamicArray<T, kMaximumSIze>::PopFront(){
    for(size_t i = 1; i < size_; ++i){
        elems_[i-1] = elems_[i];
    }
    --size_;
}

template<typename T, size_t kMaximumSize>
bool BoundedSizeDynamicArray<T, kMaximumSize>::Empty() const{
    return size_ == 0;
}

template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::PushMiddle(const T& val){
    if(size_ == 0){
        this->PushFront(val);
    }
    else{
        ++size_;
        for(size_t i = size_; i > int(size_/2); --i){
            elems_[i] = elems_[i-1];
        }
        elems_[size_/2] = val;
    }
}

template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::PopMiddle(){
    for(size_t i = int(size_/2); i < size_; ++i){
        elems_[i] = elems_[i+1];
    }
    --size_;
}

template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::Clear(){
    for(size_t i = 0; i < size_; ++i){
        this->PopFront();
    }
    size_ = 0;
}