Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curve element wrapper #1723

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions libopenage/curve/element_wrapper.h
heinezen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024-2024 the openage authors. See copying.md for legal info.

#pragma once

#include "time/time.h"

namespace openage::curve {

/**
* wrapper class for elements of a curve, store the insertion time and erase time of the element
* aswell the data of the element.
*/
template <typename T>
struct element_wrapper {
// Insertion time of the element
time::time_t _alive;
// Erase time of the element
time::time_t _dead;
// Element value
T value;

/**
* construct new element, set its start time and value
* its end time will be set to time::TIME_MAX
*/
element_wrapper(const time::time_t &time, const T &value) :
_alive{time},
_dead{time::TIME_MAX},
value{value} {}

// construct new element, set its start time, end time and value
element_wrapper(const T &value, const time::time_t &alive, const time::time_t &dead) :
_alive{alive},
_dead{dead},
value{value} {}


// start time of this element
const time::time_t &alive() const {
return _alive;
}

// end time of this element
const time::time_t &dead() const {
return _dead;
}

// set the end time of this element
void set_dead(const time::time_t &time) {
_dead = time;
}
};

} // namespace openage::curve
22 changes: 6 additions & 16 deletions libopenage/curve/map.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,6 +8,7 @@
#include <utility>

#include "curve/map_filter_iterator.h"
#include "curve/element_wrapper.h"
#include "time/time.h"
#include "util/fixed_point.h"

Expand All @@ -20,26 +21,15 @@ namespace openage::curve {
*/
template <typename key_t, typename val_t>
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<key_t, map_element> container;
std::unordered_map<key_t, element_wrapper<val_t>> container;

public:
using const_iterator = typename std::unordered_map<key_t, map_element>::const_iterator;
using const_iterator = typename std::unordered_map<key_t, element_wrapper<val_t>>::const_iterator;

std::optional<MapFilterIterator<key_t, val_t, UnorderedMap>>
operator()(const time::time_t &, const key_t &) const;
Expand Down Expand Up @@ -95,7 +85,7 @@ std::optional<MapFilterIterator<key_t, val_t, UnorderedMap<key_t, val_t>>>
UnorderedMap<key_t, val_t>::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<key_t, val_t, UnorderedMap<key_t, val_t>>(
e,
this,
Expand Down Expand Up @@ -160,7 +150,7 @@ UnorderedMap<key_t, val_t>::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<val_t> e(value, alive, dead);
auto it = this->container.insert(std::make_pair(key, e));
return MapFilterIterator<key_t, val_t, UnorderedMap<key_t, val_t>>(
it.first,
Expand Down
6 changes: 3 additions & 3 deletions libopenage/curve/map_filter_iterator.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -35,8 +35,8 @@ class MapFilterIterator : public CurveIterator<val_t, container_t> {
using CurveIterator<val_t, container_t>::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);
}

/**
Expand Down
33 changes: 3 additions & 30 deletions libopenage/curve/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,39 +33,11 @@ namespace curve {
*/
template <class T>
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<queue_wrapper>;
using container_t = typename std::vector<element_wrapper<T>>;

/**
* The index type to access elements in the container
Expand Down Expand Up @@ -412,7 +385,7 @@ QueueFilterIterator<T, Queue<T>> Queue<T>::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<T>{time, e});

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