40 mapping(_In_ T _from, _In_ T _to) : from(_from), to(_to) {}
49 bool operator==(
const mapping& other)
const {
return from == other.from && to == other.to; }
69 return mapping(from + other.from, to + other.to);
83 template <
class T,
class AX = std::allocator<mapping<T>>>
84 using mapping_vector = std::vector<mapping<T>, AX>;
94 template <
class T,
class AX = std::allocator<mapping<T>>>
100 for (
size_t l = 0, r = mapping.size();;) {
102 auto m = (l + r) / 2;
103 const auto& el = mapping[m];
104 if (to < el.to) r = m;
105 else if (el.to < to) l = m + 1;
109 const auto& el = mapping[l - 1];
110 return el.from + (to - el.to);
113 const auto& el = mapping[0];
114 return std::min<T>(to, el.from);
128 template <
class T,
class AX = std::allocator<mapping<T>>>
129 T dst2src(_In_
const std::vector<
stdex::mapping<T>, AX>& mapping, _In_ T to, _Inout_opt_
size_t& m)
135 const auto& el = mapping[m];
140 else if (el.to < to) {
141 if (mapping.size() - 1 <= m || to < mapping[m + 1].to)
142 return el.from + (to - el.to);
152 const auto& el = mapping[m];
153 if (to < el.to) r = m;
154 else if (el.to < to) l = m + 1;
158 const auto& el = mapping[m = l - 1];
159 return el.from + (to - el.to);
162 const auto& el = mapping[m = 0];
163 return std::min<T>(to, el.from);
176 template <
class T,
class AX = std::allocator<mapping<T>>>
182 for (
size_t l = 0, r = mapping.size();;) {
184 auto m = (l + r) / 2;
185 const auto& el = mapping[m];
186 if (from < el.from) r = m;
187 else if (el.from < from) l = m + 1;
191 const auto& el = mapping[l - 1];
192 return el.to + (from - el.from);
195 const auto& el = mapping[0];
196 return std::min<T>(from, el.to);
210 template <
class T,
class AX = std::allocator<mapping<T>>>
211 T src2dst(_In_
const std::vector<
stdex::mapping<T>, AX>& mapping, _In_ T from, _Inout_opt_
size_t& m)
217 const auto& el = mapping[m];
218 if (from < el.from) {
222 else if (el.from < from) {
223 if (mapping.size() - 1 <= m || from < mapping[m + 1].from)
224 return el.to + (from - el.from);
234 const auto& el = mapping[m];
235 if (from < el.from) r = m;
236 else if (el.from < from) l = m + 1;
240 const auto& el = mapping[m = l - 1];
241 return el.to + (from - el.from);
244 const auto& el = mapping[m = 0];
245 return std::min<T>(from, el.to);
Maps index in source string to index in destination string.
Definition mapping.hpp:18
mapping(T x)
Constructs an id mapping.
Definition mapping.hpp:32
bool operator==(const mapping &other) const
Are mappings identical?
Definition mapping.hpp:49
mapping()
Constructs a zero to zero mapping.
Definition mapping.hpp:25
bool operator!=(const mapping &other) const
Are mappings different?
Definition mapping.hpp:58
mapping operator+(const mapping &other) const
Adds two mappings by components.
Definition mapping.hpp:67
mapping(T _from, T _to)
Constructs a mapping.
Definition mapping.hpp:40
void invert()
Reverses source and destination indexes.
Definition mapping.hpp:75