diff --git a/libopenage/curve/CMakeLists.txt b/libopenage/curve/CMakeLists.txt index 1aa1efb55b..623a22a25a 100644 --- a/libopenage/curve/CMakeLists.txt +++ b/libopenage/curve/CMakeLists.txt @@ -3,6 +3,7 @@ add_sources(libopenage continuous.cpp discrete.cpp discrete_mod.cpp + element_wrapper.cpp interpolated.cpp iterator.cpp keyframe.cpp diff --git a/libopenage/curve/element_wrapper.cpp b/libopenage/curve/element_wrapper.cpp new file mode 100644 index 0000000000..5d2eaa08af --- /dev/null +++ b/libopenage/curve/element_wrapper.cpp @@ -0,0 +1,9 @@ +// Copyright 2024-2024 the openage authors. See copying.md for legal info. + +#include "element_wrapper.h" + +namespace openage::curve { + +// This file is intended to be empty + +} // namespace openage::curve diff --git a/libopenage/curve/element_wrapper.h b/libopenage/curve/element_wrapper.h new file mode 100644 index 0000000000..764b61d5cd --- /dev/null +++ b/libopenage/curve/element_wrapper.h @@ -0,0 +1,95 @@ +// Copyright 2024-2024 the openage authors. See copying.md for legal info. + +#pragma once + +#include "time/time.h" + +namespace openage::curve { + +/** + * Wrapper for elements in a curve container. + * + * Stores the lifetime of the element (insertion time and erasure time) alongside the value. + */ +template +class element_wrapper { +public: + /** + * Create a new element with insertion time \p time and a given value. + * + * Erasure time is set to time::TIME_MAX, i.e. the element is alive indefinitely. + * + * @param time Insertion time of the element. + * @param value Element value. + */ + element_wrapper(const time::time_t &time, const T &value) : + _alive{time}, + _dead{time::TIME_MAX}, + _value{value} {} + + /** + * Create a new element with insertion time \p alive and erasure time \p dead and a given value. + * + * @param alive Insertion time of the element. + * @param dead Erasure time of the element. + * @param value Element value. + */ + element_wrapper(const time::time_t &alive, const time::time_t &dead, const T &value) : + _alive{alive}, + _dead{dead}, + _value{value} {} + + /** + * Get the insertion time of this element. + * + * @return Time when the element was inserted into the container. + */ + const time::time_t &alive() const { + return _alive; + } + + /** + * Get the erasure time of this element. + * + * @return Time when the element was erased from the container. + */ + const time::time_t &dead() const { + return _dead; + } + + /** + * Set the erasure time of this element. + * + * @param time Time when the element was erased from the container. + */ + void set_dead(const time::time_t &time) { + _dead = time; + } + + /** + * Get the value of this element. + * + * @return Value of the element. + */ + const T &value() const { + return _value; + } + +private: + /** + * Time of insertion of the element into the container + */ + time::time_t _alive; + + /** + * Time of erasure of the element from the container + */ + time::time_t _dead; + + /** + * Element value + */ + T _value; +}; + +} // namespace openage::curve diff --git a/libopenage/curve/map.h b/libopenage/curve/map.h index 9712c89470..5a5e0a4d9b 100644 --- a/libopenage/curve/map.h +++ b/libopenage/curve/map.h @@ -1,4 +1,4 @@ -// Copyright 2017-2023 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #pragma once @@ -8,6 +8,7 @@ #include #include "curve/map_filter_iterator.h" +#include "curve/element_wrapper.h" #include "time/time.h" #include "util/fixed_point.h" @@ -20,26 +21,15 @@ namespace openage::curve { */ template class UnorderedMap { - /** Internal container to access all data and metadata */ - struct map_element { - map_element(const val_t &v, const time::time_t &a, const time::time_t &d) : - value(v), - alive(a), - dead(d) {} - - val_t value; - time::time_t alive; - time::time_t dead; - }; /** * Data holder. Maps keys to map elements. * Map elements themselves store when they are valid. */ - std::unordered_map container; + std::unordered_map> container; public: - using const_iterator = typename std::unordered_map::const_iterator; + using const_iterator = typename std::unordered_map>::const_iterator; std::optional> operator()(const time::time_t &, const key_t &) const; @@ -95,7 +85,7 @@ std::optional>> UnorderedMap::at(const time::time_t &time, const key_t &key) const { auto e = this->container.find(key); - if (e != this->container.end() and e->second.alive <= time and e->second.dead > time) { + if (e != this->container.end() and e->second.alive() <= time and e->second.dead() > time) { return MapFilterIterator>( e, this, @@ -160,7 +150,7 @@ UnorderedMap::insert(const time::time_t &alive, const time::time_t &dead, const key_t &key, const val_t &value) { - map_element e(value, alive, dead); + element_wrapper e{alive, dead, value}; auto it = this->container.insert(std::make_pair(key, e)); return MapFilterIterator>( it.first, diff --git a/libopenage/curve/map_filter_iterator.h b/libopenage/curve/map_filter_iterator.h index 5e71c0789f..3aec2a899e 100644 --- a/libopenage/curve/map_filter_iterator.h +++ b/libopenage/curve/map_filter_iterator.h @@ -1,4 +1,4 @@ -// Copyright 2017-2023 the openage authors. See copying.md for legal info. +// Copyright 2017-2024 the openage authors. See copying.md for legal info. #pragma once @@ -35,8 +35,8 @@ class MapFilterIterator : public CurveIterator { using CurveIterator::operator=; virtual bool valid() const override { - return (this->get_base()->second.alive >= this->from - and this->get_base()->second.dead < this->to); + return (this->get_base()->second.alive() >= this->from + and this->get_base()->second.dead() < this->to); } /** @@ -44,7 +44,7 @@ class MapFilterIterator : public CurveIterator { * Nicer way of accessing it beside operator *. */ val_t const &value() const override { - return this->get_base()->second.value; + return this->get_base()->second.value(); } /** diff --git a/libopenage/curve/queue.h b/libopenage/curve/queue.h index 9604a76308..9314dd3a0e 100644 --- a/libopenage/curve/queue.h +++ b/libopenage/curve/queue.h @@ -13,6 +13,7 @@ #include "curve/iterator.h" #include "curve/queue_filter_iterator.h" +#include "curve/element_wrapper.h" #include "event/evententity.h" #include "time/time.h" #include "util/fixed_point.h" @@ -32,39 +33,11 @@ namespace curve { */ template class Queue : public event::EventEntity { - struct queue_wrapper { - // Insertion time of the element - time::time_t _alive; - // Erase time of the element - // TODO: this has to be mutable because erase() will complain otherwise - mutable time::time_t _dead; - // Element value - T value; - - queue_wrapper(const time::time_t &time, const T &value) : - _alive{time}, - _dead{time::TIME_MAX}, - value{value} {} - - const time::time_t &alive() const { - return _alive; - } - - const time::time_t &dead() const { - return _dead; - } - - // TODO: this has to be const because erase() will complain otherwise - void set_dead(const time::time_t &time) const { - _dead = time; - } - }; - public: /** * The underlaying container type. */ - using container_t = typename std::vector; + using container_t = typename std::vector>; /** * The index type to access elements in the container @@ -304,7 +277,7 @@ const T &Queue::front(const time::time_t &time) const { << ", container size: " << this->container.size() << ")"); - return this->container.at(at).value; + return this->container.at(at).value(); } @@ -330,7 +303,7 @@ const T &Queue::pop_front(const time::time_t &time) { this->changes(time); - return this->container.at(at).value; + return this->container.at(at).value(); } @@ -412,7 +385,7 @@ QueueFilterIterator> Queue::insert(const time::time_t &time, // Get the iterator to the insertion point iterator insertion_point = std::next(this->container.begin(), at); - insertion_point = this->container.insert(insertion_point, queue_wrapper{time, e}); + insertion_point = this->container.insert(insertion_point, element_wrapper{time, e}); // TODO: Inserting before any dead elements shoud reset their death time // since by definition, they cannot be popped before the new element diff --git a/libopenage/curve/queue_filter_iterator.h b/libopenage/curve/queue_filter_iterator.h index 248abb44ad..cf6bc5aa2c 100644 --- a/libopenage/curve/queue_filter_iterator.h +++ b/libopenage/curve/queue_filter_iterator.h @@ -40,7 +40,7 @@ class QueueFilterIterator : public CurveIteratorget_base(); - return a.value; + return a.value(); } };