Skip to content

Commit beb8945

Browse files
authored
Merge pull request #1723 from jere8184/curve_element_wrapper
Add curve element wrapper
2 parents ae0fe46 + d6bf5b9 commit beb8945

7 files changed

Lines changed: 121 additions & 53 deletions

File tree

libopenage/curve/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_sources(libopenage
33
continuous.cpp
44
discrete.cpp
55
discrete_mod.cpp
6+
element_wrapper.cpp
67
interpolated.cpp
78
iterator.cpp
89
keyframe.cpp
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#include "element_wrapper.h"
4+
5+
namespace openage::curve {
6+
7+
// This file is intended to be empty
8+
9+
} // namespace openage::curve

libopenage/curve/element_wrapper.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#pragma once
4+
5+
#include "time/time.h"
6+
7+
namespace openage::curve {
8+
9+
/**
10+
* Wrapper for elements in a curve container.
11+
*
12+
* Stores the lifetime of the element (insertion time and erasure time) alongside the value.
13+
*/
14+
template <typename T>
15+
class element_wrapper {
16+
public:
17+
/**
18+
* Create a new element with insertion time \p time and a given value.
19+
*
20+
* Erasure time is set to time::TIME_MAX, i.e. the element is alive indefinitely.
21+
*
22+
* @param time Insertion time of the element.
23+
* @param value Element value.
24+
*/
25+
element_wrapper(const time::time_t &time, const T &value) :
26+
_alive{time},
27+
_dead{time::TIME_MAX},
28+
_value{value} {}
29+
30+
/**
31+
* Create a new element with insertion time \p alive and erasure time \p dead and a given value.
32+
*
33+
* @param alive Insertion time of the element.
34+
* @param dead Erasure time of the element.
35+
* @param value Element value.
36+
*/
37+
element_wrapper(const time::time_t &alive, const time::time_t &dead, const T &value) :
38+
_alive{alive},
39+
_dead{dead},
40+
_value{value} {}
41+
42+
/**
43+
* Get the insertion time of this element.
44+
*
45+
* @return Time when the element was inserted into the container.
46+
*/
47+
const time::time_t &alive() const {
48+
return _alive;
49+
}
50+
51+
/**
52+
* Get the erasure time of this element.
53+
*
54+
* @return Time when the element was erased from the container.
55+
*/
56+
const time::time_t &dead() const {
57+
return _dead;
58+
}
59+
60+
/**
61+
* Set the erasure time of this element.
62+
*
63+
* @param time Time when the element was erased from the container.
64+
*/
65+
void set_dead(const time::time_t &time) {
66+
_dead = time;
67+
}
68+
69+
/**
70+
* Get the value of this element.
71+
*
72+
* @return Value of the element.
73+
*/
74+
const T &value() const {
75+
return _value;
76+
}
77+
78+
private:
79+
/**
80+
* Time of insertion of the element into the container
81+
*/
82+
time::time_t _alive;
83+
84+
/**
85+
* Time of erasure of the element from the container
86+
*/
87+
time::time_t _dead;
88+
89+
/**
90+
* Element value
91+
*/
92+
T _value;
93+
};
94+
95+
} // namespace openage::curve

libopenage/curve/map.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
22

33
#pragma once
44

@@ -8,6 +8,7 @@
88
#include <utility>
99

1010
#include "curve/map_filter_iterator.h"
11+
#include "curve/element_wrapper.h"
1112
#include "time/time.h"
1213
#include "util/fixed_point.h"
1314

@@ -20,26 +21,15 @@ namespace openage::curve {
2021
*/
2122
template <typename key_t, typename val_t>
2223
class UnorderedMap {
23-
/** Internal container to access all data and metadata */
24-
struct map_element {
25-
map_element(const val_t &v, const time::time_t &a, const time::time_t &d) :
26-
value(v),
27-
alive(a),
28-
dead(d) {}
29-
30-
val_t value;
31-
time::time_t alive;
32-
time::time_t dead;
33-
};
3424

3525
/**
3626
* Data holder. Maps keys to map elements.
3727
* Map elements themselves store when they are valid.
3828
*/
39-
std::unordered_map<key_t, map_element> container;
29+
std::unordered_map<key_t, element_wrapper<val_t>> container;
4030

4131
public:
42-
using const_iterator = typename std::unordered_map<key_t, map_element>::const_iterator;
32+
using const_iterator = typename std::unordered_map<key_t, element_wrapper<val_t>>::const_iterator;
4333

4434
std::optional<MapFilterIterator<key_t, val_t, UnorderedMap>>
4535
operator()(const time::time_t &, const key_t &) const;
@@ -95,7 +85,7 @@ std::optional<MapFilterIterator<key_t, val_t, UnorderedMap<key_t, val_t>>>
9585
UnorderedMap<key_t, val_t>::at(const time::time_t &time,
9686
const key_t &key) const {
9787
auto e = this->container.find(key);
98-
if (e != this->container.end() and e->second.alive <= time and e->second.dead > time) {
88+
if (e != this->container.end() and e->second.alive() <= time and e->second.dead() > time) {
9989
return MapFilterIterator<key_t, val_t, UnorderedMap<key_t, val_t>>(
10090
e,
10191
this,
@@ -160,7 +150,7 @@ UnorderedMap<key_t, val_t>::insert(const time::time_t &alive,
160150
const time::time_t &dead,
161151
const key_t &key,
162152
const val_t &value) {
163-
map_element e(value, alive, dead);
153+
element_wrapper<val_t> e{alive, dead, value};
164154
auto it = this->container.insert(std::make_pair(key, e));
165155
return MapFilterIterator<key_t, val_t, UnorderedMap<key_t, val_t>>(
166156
it.first,

libopenage/curve/map_filter_iterator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2017-2024 the openage authors. See copying.md for legal info.
22

33
#pragma once
44

@@ -35,16 +35,16 @@ class MapFilterIterator : public CurveIterator<val_t, container_t> {
3535
using CurveIterator<val_t, container_t>::operator=;
3636

3737
virtual bool valid() const override {
38-
return (this->get_base()->second.alive >= this->from
39-
and this->get_base()->second.dead < this->to);
38+
return (this->get_base()->second.alive() >= this->from
39+
and this->get_base()->second.dead() < this->to);
4040
}
4141

4242
/**
4343
* Get the value behind the iterator.
4444
* Nicer way of accessing it beside operator *.
4545
*/
4646
val_t const &value() const override {
47-
return this->get_base()->second.value;
47+
return this->get_base()->second.value();
4848
}
4949

5050
/**

libopenage/curve/queue.h

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "curve/iterator.h"
1515
#include "curve/queue_filter_iterator.h"
16+
#include "curve/element_wrapper.h"
1617
#include "event/evententity.h"
1718
#include "time/time.h"
1819
#include "util/fixed_point.h"
@@ -32,39 +33,11 @@ namespace curve {
3233
*/
3334
template <class T>
3435
class Queue : public event::EventEntity {
35-
struct queue_wrapper {
36-
// Insertion time of the element
37-
time::time_t _alive;
38-
// Erase time of the element
39-
// TODO: this has to be mutable because erase() will complain otherwise
40-
mutable time::time_t _dead;
41-
// Element value
42-
T value;
43-
44-
queue_wrapper(const time::time_t &time, const T &value) :
45-
_alive{time},
46-
_dead{time::TIME_MAX},
47-
value{value} {}
48-
49-
const time::time_t &alive() const {
50-
return _alive;
51-
}
52-
53-
const time::time_t &dead() const {
54-
return _dead;
55-
}
56-
57-
// TODO: this has to be const because erase() will complain otherwise
58-
void set_dead(const time::time_t &time) const {
59-
_dead = time;
60-
}
61-
};
62-
6336
public:
6437
/**
6538
* The underlaying container type.
6639
*/
67-
using container_t = typename std::vector<queue_wrapper>;
40+
using container_t = typename std::vector<element_wrapper<T>>;
6841

6942
/**
7043
* The index type to access elements in the container
@@ -304,7 +277,7 @@ const T &Queue<T>::front(const time::time_t &time) const {
304277
<< ", container size: " << this->container.size()
305278
<< ")");
306279

307-
return this->container.at(at).value;
280+
return this->container.at(at).value();
308281
}
309282

310283

@@ -330,7 +303,7 @@ const T &Queue<T>::pop_front(const time::time_t &time) {
330303

331304
this->changes(time);
332305

333-
return this->container.at(at).value;
306+
return this->container.at(at).value();
334307
}
335308

336309

@@ -412,7 +385,7 @@ QueueFilterIterator<T, Queue<T>> Queue<T>::insert(const time::time_t &time,
412385

413386
// Get the iterator to the insertion point
414387
iterator insertion_point = std::next(this->container.begin(), at);
415-
insertion_point = this->container.insert(insertion_point, queue_wrapper{time, e});
388+
insertion_point = this->container.insert(insertion_point, element_wrapper<T>{time, e});
416389

417390
// TODO: Inserting before any dead elements shoud reset their death time
418391
// since by definition, they cannot be popped before the new element

libopenage/curve/queue_filter_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueFilterIterator : public CurveIterator<val_t, container_t, typename co
4040

4141
const val_t &value() const override {
4242
const auto &a = *this->get_base();
43-
return a.value;
43+
return a.value();
4444
}
4545
};
4646

0 commit comments

Comments
 (0)