From 4532afffb4806b2b1901a5a31fcad49e21cfadce Mon Sep 17 00:00:00 2001 From: ChUrl Date: Thu, 8 Dec 2022 13:30:37 +0100 Subject: [PATCH] Fix the Array initializer list constructor (repeat last value) --- src/lib/container/Array.h | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/lib/container/Array.h b/src/lib/container/Array.h index ae7481f..dd905ec 100644 --- a/src/lib/container/Array.h +++ b/src/lib/container/Array.h @@ -7,10 +7,10 @@ namespace Container { /** - * This class implements a stack allocated array with bounds checking + * This class implements a stack allocated array with runtime bounds checking * and iterator support. * - * @tparam T The type of the objects + * @tparam T The type of the held objects * @tparam N The number of elements the array can store */ template @@ -24,14 +24,26 @@ private: public: Array() = default; // If i write default something like Container::Array arr; is not initialized... - // TODO: This doesn't account for initializer lists of the wrong length, last value should be repeated - // Only increment iterator when it < list.end() - 1? - // Construct like this: bse::array {1, 2, 3, 4, 5}; + // Construct like this: bse::array arr = {1, 2, 3, 4, 5}; + /** + * Initialize the Array with initial values provided by an initializer list. + * If the initializer list has a smaller size than the Array the last value + * will be repeated. + * + * @param list The initial values + */ Array(std::initializer_list list) { + // TODO: Exception on wrong size? Can't static_assert on list.size() typename std::initializer_list::iterator it = list.begin(); + typename std::initializer_list::iterator end = list.end(); + for (unsigned int i = 0; i < N; ++i) { buf[i] = *it; - ++it; + if (it < end - 1) { + // Repeat the last initializer_list element + // if the array has more slots than initial values are provided + ++it; + } } } @@ -49,6 +61,11 @@ public: constexpr const T &operator[](std::size_t i) const { return buf[i]; } + /** + * Get a pointer to the stack allocated memory. + * + * @return The pointer to the stack allocated memory + */ T *data() { return &buf[0]; } const T *data() const { return &buf[0]; }