Files
Jungfraujoch/common/AutoIncrVector.h
T

56 lines
1.4 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <vector>
#include "JFJochException.h"
template <class T>
class AutoIncrVector {
std::vector<T> v;
T fill_value{};
public:
AutoIncrVector() = default;
AutoIncrVector(T fill_value) : fill_value(fill_value) {}
T& operator[](int64_t pos ) {
if (pos < 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Negative vector pos");
if (pos >= v.size())
v.resize(pos + 1, fill_value);
return v[pos];
}
const T& operator[](int64_t pos) const {
if (pos < 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Negative vector pos");
if (pos >= v.size())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Vector pos out of bounds");
return v[pos];
}
void reserve(int64_t size) {
if (size < 0)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Negative size for std::vector reserve");
v.reserve(size);
}
const std::vector<T> &vec() const {
return v;
}
auto size() const {
return v.size();
}
bool empty() const {
return v.empty();
}
void Clear() {
v.clear();
}
};