mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-22 03:40:04 +02:00

* added FixedCapacityContainer * added empty to FixedCapacityContainer * removed commented out section * added FixedCapacityContainer to public headers
173 lines
4.9 KiB
C++
173 lines
4.9 KiB
C++
|
|
#pragma once
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace sls {
|
|
template <typename T, size_t Capacity> class FixedCapacityContainer {
|
|
public:
|
|
FixedCapacityContainer() = default;
|
|
explicit FixedCapacityContainer(std::initializer_list<T> l);
|
|
explicit FixedCapacityContainer(const std::vector<T> &v);
|
|
|
|
FixedCapacityContainer &operator=(const std::vector<T> &other);
|
|
|
|
bool operator==(const std::vector<T> &other) const;
|
|
bool operator!=(const std::vector<T> &other) const;
|
|
|
|
template <size_t OtherCapacity>
|
|
bool
|
|
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const;
|
|
|
|
template <size_t OtherCapacity>
|
|
bool
|
|
operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const;
|
|
|
|
T &operator[](size_t i) { return data_[i]; }
|
|
const T &operator[](size_t i) const { return data_[i]; }
|
|
|
|
size_t size() const { return size_; }
|
|
bool empty() const{ return size_ == 0;}
|
|
size_t capacity() const { return Capacity; }
|
|
|
|
void push_back(const T &value);
|
|
void resize(size_t new_size);
|
|
void erase(T *ptr);
|
|
T &front() { return data_.front(); }
|
|
T &back() { return data_[size_ - 1]; }
|
|
const T &front() const { return data_.front(); }
|
|
const T &back() const { return data_[size_ - 1]; }
|
|
|
|
// iterators
|
|
T *begin() { return &data_[0]; }
|
|
T *end() { return &data_[size_]; }
|
|
const T *cbegin() const { return &data_[0]; }
|
|
const T *cend() const { return &data_[size_]; }
|
|
|
|
private:
|
|
size_t size_{0};
|
|
std::array<T, Capacity> data_;
|
|
} __attribute__((packed));
|
|
|
|
/* Member functions */
|
|
template <typename T, size_t Capacity>
|
|
FixedCapacityContainer<T, Capacity>::FixedCapacityContainer(
|
|
std::initializer_list<T> l) {
|
|
size_ = l.size();
|
|
std::copy(l.begin(), l.end(), data_.begin());
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
FixedCapacityContainer<T, Capacity>::FixedCapacityContainer(
|
|
const std::vector<T> &v) {
|
|
size_ = v.size();
|
|
std::copy(v.begin(), v.end(), data_.begin());
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
void FixedCapacityContainer<T, Capacity>::push_back(const T &value) {
|
|
if (size_ == Capacity) {
|
|
throw std::runtime_error("Container is full");
|
|
} else {
|
|
data_[size_] = value;
|
|
++size_;
|
|
}
|
|
}
|
|
template <typename T, size_t Capacity>
|
|
void FixedCapacityContainer<T, Capacity>::resize(size_t new_size) {
|
|
if (new_size > Capacity) {
|
|
throw std::runtime_error("Cannot resize beyond capacity");
|
|
} else {
|
|
size_ = new_size;
|
|
}
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
FixedCapacityContainer<T, Capacity> &FixedCapacityContainer<T, Capacity>::
|
|
operator=(const std::vector<T> &other) {
|
|
std::copy(other.begin(), other.end(), data_.begin());
|
|
size_ = other.size();
|
|
return *this;
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
bool FixedCapacityContainer<T, Capacity>::
|
|
operator==(const std::vector<T> &other) const {
|
|
if (size_ != other.size()) {
|
|
return false;
|
|
} else {
|
|
for (size_t i = 0; i != size_; ++i) {
|
|
if (data_[i] != other[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
bool FixedCapacityContainer<T, Capacity>::
|
|
operator!=(const std::vector<T> &other) const {
|
|
return !(*this == other);
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
template <size_t OtherCapacity>
|
|
bool FixedCapacityContainer<T, Capacity>::
|
|
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const {
|
|
if (size_ != other.size()) {
|
|
return false;
|
|
} else {
|
|
for (size_t i = 0; i != size_; ++i) {
|
|
if (data_[i] != other[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
template <size_t OtherCapacity>
|
|
bool FixedCapacityContainer<T, Capacity>::
|
|
operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const {
|
|
return !(*this == other);
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
void FixedCapacityContainer<T, Capacity>::erase(T *ptr) {
|
|
if (ptr >= begin() && ptr < end()) {
|
|
size_ = static_cast<size_t>(ptr - begin());
|
|
} else {
|
|
throw std::runtime_error("tried to erase with a ptr outside obj");
|
|
}
|
|
}
|
|
|
|
/* Free function concerning FixedCapacityContainer */
|
|
|
|
template <typename T, size_t Capacity>
|
|
T *begin(FixedCapacityContainer<T, Capacity> &container) {
|
|
return container.begin();
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
T *end(FixedCapacityContainer<T, Capacity> &container) {
|
|
return container.end();
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
bool operator==(const std::vector<T> &vec,
|
|
const FixedCapacityContainer<T, Capacity> &fixed_container) {
|
|
return fixed_container == vec;
|
|
}
|
|
|
|
template <typename T, size_t Capacity>
|
|
bool operator!=(const std::vector<T> &vec,
|
|
const FixedCapacityContainer<T, Capacity> &fixed_container) {
|
|
return fixed_container != vec;
|
|
}
|
|
|
|
} // namespace sls
|