Skip to content
Merged
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
26 changes: 0 additions & 26 deletions src/openvic-simulation/types/HasIdentifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <algorithm>
#include <cassert>
#include <string>
#include <string_view>
#include <ostream>

Expand Down Expand Up @@ -104,29 +103,4 @@ namespace OpenVic {

template<typename T>
concept HasGetIdentifierAndGetColour = HasGetIdentifier<T> && HasGetColour<T>;

template<typename TypeTag, std::integral IndexT = size_t>
class HasIndex {
public:
using index_t = IndexT;
private:
const index_t PROPERTY(index);

protected:
HasIndex(index_t new_index) : index { new_index } {}
HasIndex(HasIndex const&) = default;

public:
HasIndex(HasIndex&&) = default;
HasIndex& operator=(HasIndex const&) = delete;
HasIndex& operator=(HasIndex&&) = delete;
constexpr bool operator==(HasIndex const& rhs) const {
return index == rhs.index;
}
};

template<typename T>
concept HasGetIndex = requires(T const& t) {
{ t.get_index() } -> std::integral;
};
}
27 changes: 27 additions & 0 deletions src/openvic-simulation/types/HasIndex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <concepts>

#include "openvic-simulation/utility/Getters.hpp"

namespace OpenVic {
template<typename TypeTag, std::integral IndexT = size_t>
class HasIndex {
public:
using index_t = IndexT;
private:
const index_t PROPERTY(index);

protected:
HasIndex(index_t new_index) : index { new_index } {}
HasIndex(HasIndex const&) = default;

public:
HasIndex(HasIndex&&) = default;
HasIndex& operator=(HasIndex const&) = delete;
HasIndex& operator=(HasIndex&&) = delete;
constexpr bool operator==(HasIndex const& rhs) const {
return index == rhs.index;
}
};
}
12 changes: 8 additions & 4 deletions src/openvic-simulation/types/IndexedMap.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <concepts>
#include <vector>

#include "openvic-simulation/types/fixed_point/FixedPointMap.hpp"
#include "openvic-simulation/types/HasIndex.hpp"
#include "openvic-simulation/utility/ForwardableSpan.hpp"
#include "openvic-simulation/utility/Getters.hpp"
#include "openvic-simulation/utility/Logger.hpp"
Expand Down Expand Up @@ -310,10 +310,14 @@ namespace OpenVic {
}

constexpr size_t get_index_from_item(key_ref_type key) const {
if (has_keys() && keys.data() <= &key && &key <= &keys.back()) {
return std::distance(keys.data(), &key);
if constexpr (utility::is_derived_from_specialization_of<Key, HasIndex>) {
return key.get_index();
} else {
return 0;
if (has_keys() && keys.data() <= &key && &key <= &keys.back()) {
return std::distance(keys.data(), &key);
} else {
return 0;
}
}
}

Expand Down
Loading