aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility/TslHelper.hpp
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-12-25 08:42:11 +0100
committer Spartan322 <Megacake1234@gmail.com>2023-12-31 20:46:26 +0100
commite1496a87178d925277aceed0ebcbab06920e15ee (patch)
treeba081f9f4d74865ab5851a2efd560745900ca81a /src/openvic-simulation/utility/TslHelper.hpp
parent0a425fbe05d6138b753c0e4a7c06f06695bde8af (diff)
Add `https://github.com/Tessil/ordered-map`
Add is_specialization_of to Utility.hpp Add OpenVic::ordered_map and OpenVic::ordered_set Change `std::map` to `ordered_map` Change `std::set to use `ordered_set` Add `set_callback_pointer(tsl::ordered_set<T const*>& set)` Add mutable_iterator to enable mutable value iterator for `tsl::ordered_map` Add std::hash<OpenVic::Date> implementation Enable deps/SCsub to expose dependency includes neccessary for inclusion
Diffstat (limited to 'src/openvic-simulation/utility/TslHelper.hpp')
-rw-r--r--src/openvic-simulation/utility/TslHelper.hpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/openvic-simulation/utility/TslHelper.hpp b/src/openvic-simulation/utility/TslHelper.hpp
new file mode 100644
index 0000000..9d1ae77
--- /dev/null
+++ b/src/openvic-simulation/utility/TslHelper.hpp
@@ -0,0 +1,129 @@
+#pragma once
+
+#include <type_traits>
+
+#include <tsl/ordered_map.h>
+#include <tsl/ordered_set.h>
+
+#include "openvic-simulation/types/OrderedContainers.hpp"
+#include "openvic-simulation/utility/Utility.hpp"
+
+namespace OpenVic {
+ template<IsOrderedMap Map, typename Key = typename Map::key_type, typename Mapped = typename Map::mapped_type>
+ struct _OrderedMapMutable {
+ using map_type = Map;
+ struct ordered_iterator {
+ using key_type = Key;
+ using mapped_type = Mapped;
+ using pair_type = std::pair<key_type const&, mapped_type&>;
+ using value_type = pair_type;
+ using iterator = typename map_type::iterator;
+
+ using iterator_category = std::random_access_iterator_tag;
+ using difference_type = typename map_type::values_container_type::iterator::difference_type;
+
+ pair_type operator*() {
+ return { m_iterator.key(), m_iterator.value() };
+ }
+
+ ordered_iterator& operator++() {
+ ++m_iterator;
+ return *this;
+ }
+ ordered_iterator& operator--() {
+ --m_iterator;
+ return *this;
+ }
+
+ ordered_iterator operator++(int) {
+ ordered_iterator tmp(*this);
+ ++(*this);
+ return tmp;
+ }
+ ordered_iterator operator--(int) {
+ ordered_iterator tmp(*this);
+ --(*this);
+ return tmp;
+ }
+
+ pair_type operator[](difference_type n) {
+ return *(*this + n);
+ }
+
+ ordered_iterator& operator+=(difference_type n) {
+ m_iterator += n;
+ return *this;
+ }
+ ordered_iterator& operator-=(difference_type n) {
+ m_iterator -= n;
+ return *this;
+ }
+
+ ordered_iterator operator+(difference_type n) {
+ ordered_iterator tmp(*this);
+ tmp += n;
+ return tmp;
+ }
+ ordered_iterator operator-(difference_type n) {
+ ordered_iterator tmp(*this);
+ tmp -= n;
+ return tmp;
+ }
+
+ bool operator==(const ordered_iterator& rhs) const {
+ return m_iterator == rhs.m_iterator;
+ }
+
+ bool operator!=(const ordered_iterator& rhs) const {
+ return m_iterator != rhs.m_iterator;
+ }
+
+ bool operator<(const ordered_iterator& rhs) const {
+ return m_iterator < rhs.m_iterator;
+ }
+
+ bool operator>(const ordered_iterator& rhs) const {
+ return m_iterator > rhs.m_iterator;
+ }
+
+ bool operator<=(const ordered_iterator& rhs) const {
+ return m_iterator <= rhs.m_iterator;
+ }
+
+ bool operator>=(const ordered_iterator& rhs) const {
+ return m_iterator >= rhs.m_iterator;
+ }
+
+ friend ordered_iterator operator+(difference_type n, const ordered_iterator& it) {
+ return n + it.m_iterator;
+ }
+
+ ordered_iterator operator+(const ordered_iterator& rhs) const {
+ return m_iterator + rhs.m_iterator;
+ }
+
+ difference_type operator-(const ordered_iterator& rhs) const {
+ return m_iterator - rhs.m_iterator;
+ }
+
+ iterator m_iterator;
+ };
+
+ _OrderedMapMutable(map_type& map) : _map(map) {}
+
+ ordered_iterator begin() {
+ return ordered_iterator { _map.begin() };
+ }
+ ordered_iterator end() {
+ return ordered_iterator { _map.end() };
+ }
+
+ private:
+ map_type& _map;
+ };
+
+ template<IsOrderedMap Map, typename Key = typename Map::key_type, typename Mapped = typename Map::mapped_type>
+ _OrderedMapMutable<Map, Key, Mapped> mutable_iterator(Map& map) {
+ return _OrderedMapMutable<Map, Key, Mapped> { map };
+ }
+}