mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-11 20:37:15 +02:00
WIP
This commit is contained in:
@ -4,176 +4,133 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include "TypeTraits.h"
|
||||
|
||||
namespace sls {
|
||||
template <typename T, size_t Capacity> class FixedCapacityContainer {
|
||||
|
||||
public:
|
||||
using size_type = typename std::array<T, Capacity>::size_type;
|
||||
using value_type = typename std::array<T, Capacity>::value_type;
|
||||
using iterator = typename std::array<T, Capacity>::iterator;
|
||||
using const_iterator = typename std::array<T, Capacity>::const_iterator;
|
||||
|
||||
FixedCapacityContainer() = default;
|
||||
explicit FixedCapacityContainer(std::initializer_list<T> l);
|
||||
explicit FixedCapacityContainer(const std::vector<T> &v);
|
||||
explicit FixedCapacityContainer(std::initializer_list<T> l) {
|
||||
current_size = l.size();
|
||||
std::copy(l.begin(), l.end(), data_.begin());
|
||||
}
|
||||
|
||||
template <typename V,
|
||||
typename = typename std::enable_if<
|
||||
is_light_container<V>::value &&
|
||||
std::is_same<T, typename V::value_type>::value>::type>
|
||||
explicit FixedCapacityContainer(const V &v) {
|
||||
if (v.size() > Capacity) {
|
||||
throw std::runtime_error(
|
||||
"Capacity needs to be same size or larger than vector");
|
||||
}
|
||||
current_size = v.size();
|
||||
std::copy(v.begin(), v.end(), data_.begin());
|
||||
}
|
||||
|
||||
template <size_t OtherCapacity>
|
||||
explicit FixedCapacityContainer(
|
||||
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept;
|
||||
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept {
|
||||
static_assert(Capacity >= OtherCapacity,
|
||||
"Container needs to be same size or larger");
|
||||
current_size = other.size();
|
||||
std::copy(other.cbegin(), other.cend(), data_.begin());
|
||||
}
|
||||
|
||||
FixedCapacityContainer &operator=(const std::vector<T> &other);
|
||||
FixedCapacityContainer &operator=(const std::vector<T> &other) {
|
||||
std::copy(other.begin(), other.end(), data_.begin());
|
||||
current_size = other.size();
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const std::vector<T> &other) const noexcept;
|
||||
bool operator!=(const std::vector<T> &other) const noexcept;
|
||||
/** Compare FixedCapacityContainer with any other container*/
|
||||
template <typename V>
|
||||
typename std::enable_if<is_container<V>::value, bool>::type
|
||||
operator==(const V &other) const noexcept {
|
||||
if (current_size != other.size()) {
|
||||
return false;
|
||||
} else {
|
||||
for (size_t i = 0; i != current_size; ++i) {
|
||||
if (data_[i] != other[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
operator std::vector<T>(){return std::vector<T>(begin(), end());}
|
||||
template <typename V>
|
||||
typename std::enable_if<is_container<V>::value, bool>::type
|
||||
operator!=(const V &other) const noexcept {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
template <size_t OtherCapacity>
|
||||
bool operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const
|
||||
noexcept;
|
||||
|
||||
template <size_t OtherCapacity>
|
||||
bool operator!=(const FixedCapacityContainer<T, OtherCapacity> &other) const
|
||||
noexcept;
|
||||
operator std::vector<T>() { return std::vector<T>(begin(), end()); }
|
||||
|
||||
T &operator[](size_t i) { return data_[i]; }
|
||||
const T &operator[](size_t i) const { return data_[i]; }
|
||||
|
||||
constexpr size_t size() const noexcept { return size_; }
|
||||
bool empty() const noexcept { return size_ == 0; }
|
||||
constexpr size_type size() const noexcept { return current_size; }
|
||||
bool empty() const noexcept { return current_size == 0; }
|
||||
constexpr size_t capacity() const noexcept { return Capacity; }
|
||||
|
||||
void push_back(const T &value);
|
||||
void resize(size_t new_size);
|
||||
void erase(T *ptr);
|
||||
void push_back(const T &value) {
|
||||
if (current_size == Capacity) {
|
||||
throw std::runtime_error("Container is full");
|
||||
} else {
|
||||
data_[current_size] = value;
|
||||
++current_size;
|
||||
}
|
||||
}
|
||||
|
||||
void resize(size_t new_size) {
|
||||
if (new_size > Capacity) {
|
||||
throw std::runtime_error("Cannot resize beyond capacity");
|
||||
} else {
|
||||
current_size = new_size;
|
||||
}
|
||||
}
|
||||
|
||||
void erase(T *ptr) {
|
||||
if (ptr >= begin() && ptr < end()) {
|
||||
current_size = static_cast<size_t>(ptr - begin());
|
||||
} else {
|
||||
throw std::runtime_error("tried to erase with a ptr outside obj");
|
||||
}
|
||||
}
|
||||
T &front() noexcept { return data_.front(); }
|
||||
T &back() noexcept { return data_[size_ - 1]; }
|
||||
T &back() noexcept { return data_[current_size - 1]; }
|
||||
constexpr const T &front() const noexcept { return data_.front(); }
|
||||
constexpr const T &back() const noexcept { return data_[size_ - 1]; }
|
||||
constexpr const T &back() const noexcept { return data_[current_size - 1]; }
|
||||
|
||||
// iterators
|
||||
T *begin() noexcept { return &data_[0]; }
|
||||
T *end() noexcept { return &data_[size_]; }
|
||||
const T *cbegin() const noexcept { return &data_[0]; }
|
||||
const T *cend() const noexcept { return &data_[size_]; }
|
||||
iterator begin() noexcept { return data_.begin(); }
|
||||
const_iterator begin() const noexcept { return data_.begin(); }
|
||||
iterator end() noexcept { return &data_[current_size]; }
|
||||
const_iterator end() const noexcept { return &data_[current_size]; }
|
||||
const_iterator cbegin() const noexcept { return data_.cbegin(); }
|
||||
const_iterator cend() const noexcept { return &data_[current_size]; }
|
||||
|
||||
private:
|
||||
size_t size_{0};
|
||||
size_type current_size{};
|
||||
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) {
|
||||
if (v.size() > Capacity) {
|
||||
throw std::runtime_error(
|
||||
"Capacity needs to be same size or larger than vector");
|
||||
}
|
||||
size_ = v.size();
|
||||
std::copy(v.begin(), v.end(), data_.begin());
|
||||
}
|
||||
|
||||
template <typename T, size_t Capacity>
|
||||
template <size_t OtherCapacity>
|
||||
FixedCapacityContainer<T, Capacity>::FixedCapacityContainer(
|
||||
const FixedCapacityContainer<T, OtherCapacity> &other) noexcept {
|
||||
static_assert(Capacity >= OtherCapacity,
|
||||
"Container needs to be same size or larger");
|
||||
size_ = other.size();
|
||||
std::copy(other.cbegin(), other.cend(), 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 noexcept {
|
||||
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 noexcept {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
template <typename T, size_t Capacity>
|
||||
template <size_t OtherCapacity>
|
||||
bool FixedCapacityContainer<T, Capacity>::
|
||||
operator==(const FixedCapacityContainer<T, OtherCapacity> &other) const
|
||||
noexcept {
|
||||
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
|
||||
noexcept {
|
||||
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>
|
||||
constexpr T *begin(FixedCapacityContainer<T, Capacity> &container) noexcept {
|
||||
typename FixedCapacityContainer<T, Capacity>::iterator
|
||||
begin(FixedCapacityContainer<T, Capacity> &container) noexcept {
|
||||
return container.begin();
|
||||
}
|
||||
|
||||
template <typename T, size_t Capacity>
|
||||
constexpr T *end(FixedCapacityContainer<T, Capacity> &container) noexcept {
|
||||
typename FixedCapacityContainer<T, Capacity>::iterator
|
||||
end(FixedCapacityContainer<T, Capacity> &container) noexcept {
|
||||
return container.end();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user