C2

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
1
Indexable
Never
#include <vector>
template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::Fill(const T& val){
    for(size_t i = 0; i < size_; ++i){
        elems_[i] = val;
    }
}

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

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

//TODO: 
template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::Remove(const T& val){
    std::vector<size_t> positions;
    size_t i;
    for(i = 0; i < size_; ++i){
        if(elems_[i] == val){
            positions.push_back(i);
        }
    }
    for(i = 0; i < positions.size(); ++i){
        this->Erase(positions[i] - i);
    }

}

// TODO: MLE 
template<typename T, size_t kMaximumSize>
void BoundedSizeDynamicArray<T, kMaximumSize>::Swap(BoundedSizeDynamicArray<T, kMaximumSize>& other){
    std::swap(this->elems_, other.elems_);
    
    size_t tempSize = other.size_;
    other.size_ = this->size_;
    this->size_ = tempSize;
}