Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jere8184 committed Dec 9, 2024
1 parent faec07d commit fb2a888
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 3 deletions.
1 change: 1 addition & 0 deletions libopenage/curve/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_sources(libopenage
queue.cpp
queue_filter_iterator.cpp
segmented.cpp
array.cpp
)

add_subdirectory("tests")
10 changes: 10 additions & 0 deletions libopenage/curve/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2017-2024 the openage authors. See copying.md for legal info.


#include "array.h"

namespace openage::curve {

// This file is intended to be empty

} // openage::curve
161 changes: 161 additions & 0 deletions libopenage/curve/array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2017-2024 the openage authors. See copying.md for legal info.


#pragma once

#include <array>

#include "curve/base_curve.h"
#include "curve/keyframe_container.h"


// remember to update docs
namespace openage {
namespace curve {

template <typename T, size_t Size>
class Array {
public:
using this_type = Array<T, Size>;
using container_t = std::array<KeyframeContainer<T>, Size>;


// Array() = default;

// prevent accidental copy of queue
// Array(const Array &) = delete;


T get(const time::time_t &t, const size_t index) const;
// T get(const time::time_t& t, const size_t index, const size_t hint) const;


std::array<T, Size> get_all(const time::time_t &t) const;
// std::array<Keyframe<T>, size> get_all(const time::time_t& t, const size_t hint) const;


size_t size() const;

const Keyframe<T> &frame(const time::time_t &t, const size_t index) const;


const Keyframe<T> &next_frame(const time::time_t &t, const size_t index) const;

void set_insert(const time::time_t &t, const size_t index, T value);

void set_last(const time::time_t &t, const size_t index, T value);

void set_replace(const time::time_t &t, const size_t index, T value);

void sync(const Array<T, Size> &other, const time::time_t &t);


class Iterator {
public:
const T *ptr;
size_t offset;
const Array<T, Size> *const curve;
time::time_t time;

Iterator(const Array<T, Size> *curve, const time::time_t &time = time::TIME_MAX, size_t offset = 0) :
curve(curve), time(time), offset(offset) {
if (this->offset != Size) {
std::cout << this->offset << Size;
ptr = &this->curve->frame(this->time, this->offset).value;
}
};

const T &operator*() {
ptr = &curve->frame(this->time, this->offset).value;
return *this->ptr;
}

void operator++() {
this->offset++;
}

bool operator!=(const Array<T, Size>::Iterator &rhs) const {
return this->offset != rhs.offset;
}
};


Iterator begin(const time::time_t &time = time::TIME_MAX) const;

Iterator end(const time::time_t &time = time::TIME_MAX) const;


private:
container_t container;
mutable size_t last_hit_index = 0;
};


template <typename T, size_t Size>
const Keyframe<T> &Array<T, Size>::frame(const time::time_t &t, const size_t index) const {
this->last_hit_index = container[index].last(t, this->last_hit_index);
return container[index].get(this->last_hit_index);
}

template <typename T, size_t Size>
const Keyframe<T> &Array<T, Size>::next_frame(const time::time_t &t, const size_t index) const {
this->last_hit_index = container[index].last(t, this->last_hit_index);
return container[index].get(this->last_hit_index + 1);
}

template <typename T, size_t Size>
T Array<T, Size>::get(const time::time_t &t, const size_t index) const {
return this->frame(t, index).value;
}

template <typename T, size_t Size>
std::array<T, Size> Array<T, Size>::get_all(const time::time_t &t) const {
return [&]<auto... I>(std::index_sequence<I...>) {
return std::array<T, Size>{this->get(t, I)...};
}(std::make_index_sequence<Size>{});
}

template <typename T, size_t Size>
size_t Array<T, Size>::size() const {
return Size;
}


template <typename T, size_t Size>
void Array<T, Size>::set_insert(const time::time_t &t, const size_t index, T value) {
this->last_hit_index = this->container[index].insert_after(Keyframe(t, value), this->last_hit_index);
}


template <typename T, size_t Size>
void Array<T, Size>::set_last(const time::time_t &t, const size_t index, T value) {
this->last_hit_index = this->container[index].insert_after(Keyframe(t, value), this->last_hit_index);
this->container[index].erase_after(this->last_hit_index);
}


template <typename T, size_t Size>
void Array<T, Size>::set_replace(const time::time_t &t, const size_t index, T value) {
this->container[index].insert_overwrite(Keyframe(t, value), this->last_hit_index);
}

template <typename T, size_t Size>
void Array<T, Size>::sync(const Array<T, Size> &other, const time::time_t &start) {
for (int i = 0; i < Size; i++) {
this->container[i].sync(other, start);
}
}

template <typename T, size_t Size>
typename Array<T, Size>::Iterator Array<T, Size>::begin(const time::time_t &time) const {
return Array<T, Size>::Iterator(this, time);
}


template <typename T, size_t Size>
typename Array<T, Size>::Iterator Array<T, Size>::end(const time::time_t &time) const {
return Array<T, Size>::Iterator(this, time, this->container.size());
}

} // namespace curve
} // namespace openage
20 changes: 17 additions & 3 deletions libopenage/curve/tests/curve_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <string>

#include "curve/array.h"
#include "curve/continuous.h"
#include "curve/discrete.h"
#include "curve/discrete_mod.h"
Expand Down Expand Up @@ -232,7 +233,7 @@ void curve_types() {
TESTEQUALS(c.get(8), 4);
}

//Check the discrete type
// Check the discrete type
{
auto f = std::make_shared<event::EventLoop>();
Discrete<int> c(f, 0);
Expand All @@ -257,7 +258,7 @@ void curve_types() {
TESTEQUALS(complex.get(10), "Test 10");
}

//Check the discrete mod type
// Check the discrete mod type
{
auto f = std::make_shared<event::EventLoop>();
DiscreteMod<int> c(f, 0);
Expand Down Expand Up @@ -290,7 +291,7 @@ void curve_types() {
TESTEQUALS(c.get_mod(15, 0), 0);
}

//check set_last
// check set_last
{
auto f = std::make_shared<event::EventLoop>();
Discrete<int> c(f, 0);
Expand Down Expand Up @@ -386,6 +387,19 @@ void curve_types() {
TESTEQUALS(c.get(1), 0);
TESTEQUALS(c.get(5), 0);
}

{ // array
Array<int, 2> a;
a.set_insert(time::time_t(1), 0, 0);
a.set_insert(time::time_t(1), 1, 1);
const auto res = a.get_all(1);
TESTEQUALS(res[0], 0);
TESTEQUALS(res[1], 1);

for (auto r : a) {
std::cout << r << std::endl;
}
}
}

} // namespace openage::curve::tests

0 comments on commit fb2a888

Please sign in to comment.