CFx SDK Documentation 2026 SP0
Loading...
Searching...
No Matches
TrVisBimap.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2024, Open Design Alliance (the "Alliance").
3// All rights reserved.
4//
5// This software and its documentation and related materials are owned by
6// the Alliance. The software may only be incorporated into application
7// programs owned by members of the Alliance, subject to a signed
8// Membership Agreement and Supplemental Software License Agreement with the
9// Alliance. The structure and organization of this software are the valuable
10// trade secrets of the Alliance and its suppliers. The software is also
11// protected by copyright law and international treaty provisions. Application
12// programs incorporating this software must include the following statement
13// with their copyright notices:
14//
15// This application incorporates Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2024 by Open Design Alliance.
18// All rights reserved.
19//
20// By use of this software, its documentation or related materials, you
21// acknowledge and accept the above terms.
23// Bidirectional maps.
24
25#ifndef ODTRVISBIMAP
26#define ODTRVISBIMAP
27
28#include "TD_PackPush.h"
29
30#include <unordered_set>
31#include <set>
32
33// Original source code: https://github.com/LarsHagemann/bimap
34
35template <class _KeyType, class _ValueType,
36 class _Key_Container = std::set<_KeyType>,
37 class _Mapped_Container = std::set<_ValueType>> class OdTrVisBimap
38{
39 public:
40 class const_iterator : public std::iterator<std::bidirectional_iterator_tag, std::pair<const _KeyType&, const _ValueType&>>
41 {
42 public:
44 typedef std::pair<typename _Key_Container::iterator, typename _Mapped_Container::iterator> value_type;
45 typedef int difference_type;
46 explicit const_iterator(const value_type& value) : m_data(std::move(value)) { }
47 const self_type& operator++() { ++m_data.first; ++m_data.second; return *this; }
48 const value_type& operator*() { return m_data; }
49 bool operator==(const self_type& rhs) { return m_data.first == rhs.m_data.first; }
50 bool operator!=(const self_type& rhs) { return m_data.first != rhs.m_data.first; }
51 private:
52 value_type m_data;
53 };
54 using key_type = _KeyType;
55 using value_type = _ValueType;
56 using rkey_type = _ValueType;
57 using rvalue_type = _KeyType;
58 private:
59 using _Tree = _Key_Container;
60 using _RTree = _Mapped_Container;
61 using _Tree_Node = std::pair<key_type, value_type>;
62 using _RTree_Node = std::pair<rkey_type, rvalue_type>;
63 private:
64 // Test for the container types
65#if 0 // C++17
66 static_assert(std::is_same_v<key_type, typename _Tree::key_type>,
67 "OdTrVisBimap::key_type has to match _Container_Type::key_type!");
68 static_assert(std::is_same_v<rkey_type, typename _RTree::key_type>,
69 "OdTrVisBimap::rkey_type has to match _Reverse_Container_Type::key_type!");
70#else
71 static_assert(std::is_same<key_type, typename _Tree::key_type>::value,
72 "OdTrVisBimap::key_type has to match _Container_Type::key_type!");
73 static_assert(std::is_same<rkey_type, typename _RTree::key_type>::value,
74 "OdTrVisBimap::rkey_type has to match _Reverse_Container_Type::key_type!");
75#endif
76 private:
77 _Tree m_key_tree;
78 _RTree m_value_tree;
79 private:
80 /* Inner helper functions */
81 inline size_t index_of_key(const key_type& key) const
82 {
83 const auto k_itr = m_key_tree.find(key);
84 const auto offset = static_cast<unsigned>(std::distance(m_key_tree.begin(), k_itr));
85 return (offset < m_key_tree.size() ? offset : 0);
86 }
87 inline size_t index_of_value(const value_type& value) const
88 {
89 const auto v_itr = m_value_tree.find(value);
90 const auto offset = static_cast<unsigned>(std::distance(m_value_tree.begin(), v_itr));
91 return (offset < m_value_tree.size() ? offset : 0);
92 }
93 inline value_type value_at_index(size_t index) const
94 {
95 auto v_itr = m_value_tree.begin();
96 std::advance(v_itr, index);
97 return *v_itr;
98 }
99 inline key_type key_at_index(size_t index) const
100 {
101 auto k_itr = m_key_tree.begin();
102 std::advance(k_itr, index);
103 return *k_itr;
104 }
105 public:
106 /* Constructors */
107 OdTrVisBimap() = default;
108 OdTrVisBimap(std::initializer_list<_Tree_Node> init)
109 {
110 for (const auto& elem : init)
111 insert(elem);
112 }
113 public:
114 /* Iterator functions */
115 const_iterator begin() const { return const_iterator({ m_key_tree.begin(), m_value_tree.begin() }); }
116 const_iterator end() const { return const_iterator({ m_key_tree.end(), m_value_tree.end() }); }
117 public:
118 /* Modification functions */
119 void insert(_Tree_Node&& node)
120 {
121 m_key_tree.emplace_hint(m_key_tree.begin(), std::move(node.first));
122 m_value_tree.emplace_hint(m_value_tree.begin(), std::move(node.second));
123 }
124 void insert(const _Tree_Node& node)
125 {
126 m_key_tree.emplace_hint(m_key_tree.begin(), std::move(node.first));
127 m_value_tree.emplace_hint(m_value_tree.begin(), std::move(node.second));
128 }
129 void insert(const key_type& key, const value_type& value)
130 {
131 m_key_tree.emplace_hint(m_key_tree.begin(), std::move(key));
132 m_value_tree.emplace_hint(m_value_tree.begin(), std::move(value));
133 }
134 void erase_key(const key_type& key)
135 {
136 const auto k_itr = m_key_tree.find(key);
137 const auto offset = std::distance(m_key_tree.begin(), k_itr);
138 auto v_itr = m_value_tree.begin();
139 std::advance(v_itr, offset);
140 m_key_tree.erase(k_itr);
141 m_value_tree.erase(v_itr);
142 }
144 {
145 const auto v_itr = m_value_tree.find(value);
146 const auto offset = std::distance(m_value_tree.begin(), v_itr);
147 auto k_itr = m_key_tree.begin();
148 std::advance(k_itr, offset);
149 m_key_tree.erase(k_itr);
150 m_value_tree.erase(v_itr);
151 }
152 public:
153 /* Helper functions */
154 bool has_key(const key_type& key) const
155 {
156 return m_key_tree.find(key) != m_key_tree.end();
157 }
158 bool has_value(const value_type& value) const
159 {
160 return m_value_tree.find(value) != m_value_tree.end();
161 }
162 value_type get_value(const key_type& key) const
163 {
164 const auto offset = index_of_key(key);
165 auto v_itr = m_value_tree.begin();
166 std::advance(v_itr, offset);
167 return *v_itr;
168 }
170 {
171 const auto offset = index_of_value(value);
172 auto k_itr = m_key_tree.begin();
173 std::advance(k_itr, offset);
174 return *k_itr;
175 }
176 size_t size() const
177 {
178 return m_key_tree.size();
179 }
180};
181
182template<class _KeyType, class _ValueType>
184
185#include "TD_PackPop.h"
186
187#endif // ODTRVISBIMAP
OdTrVisBimap< _KeyType, _ValueType, std::unordered_set< _KeyType >, std::unordered_set< _ValueType > > OdTrVisUnorderedBimap
Definition TrVisBimap.h:183
bool operator!=(const self_type &rhs)
Definition TrVisBimap.h:50
const self_type & operator++()
Definition TrVisBimap.h:47
bool operator==(const self_type &rhs)
Definition TrVisBimap.h:49
std::pair< typename _Key_Container::iterator, typename _Mapped_Container::iterator > value_type
Definition TrVisBimap.h:44
const_iterator(const value_type &value)
Definition TrVisBimap.h:46
const value_type & operator*()
Definition TrVisBimap.h:48
OdTrVisBimap()=default
_ValueType rkey_type
Definition TrVisBimap.h:56
const_iterator end() const
Definition TrVisBimap.h:116
_KeyType rvalue_type
Definition TrVisBimap.h:57
bool has_key(const key_type &key) const
Definition TrVisBimap.h:154
_ValueType value_type
Definition TrVisBimap.h:55
void insert(_Tree_Node &&node)
Definition TrVisBimap.h:119
void erase_key(const key_type &key)
Definition TrVisBimap.h:134
_KeyType key_type
Definition TrVisBimap.h:54
void insert(const _Tree_Node &node)
Definition TrVisBimap.h:124
bool has_value(const value_type &value) const
Definition TrVisBimap.h:158
OdTrVisBimap(std::initializer_list< _Tree_Node > init)
Definition TrVisBimap.h:108
value_type get_value(const key_type &key) const
Definition TrVisBimap.h:162
key_type get_key(const value_type &value) const
Definition TrVisBimap.h:169
void insert(const key_type &key, const value_type &value)
Definition TrVisBimap.h:129
const_iterator begin() const
Definition TrVisBimap.h:115
size_t size() const
Definition TrVisBimap.h:176
void erase_value(const value_type &value)
Definition TrVisBimap.h:143
GLuint index
Definition gles2_ext.h:265
GLintptr offset
Definition gles2_ext.h:183
GLsizei const GLfloat * value
Definition gles2_ext.h:302