InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
KeyValuePair.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Mat Marcus (mmarcus)
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //
23 // Purpose: clone of std::K2Pair based on STLPort 4.0
24 //
25 //========================================================================================
26 
27 #ifndef __KEYVALUEPAIR__
28 #define __KEYVALUEPAIR__
29 
30 #include <adobe/move.hpp>
31 #include <adobe/typeinfo.hpp>
32 
37 template <class _T1, class _T2>
38 struct KeyValuePair {
39  typedef object_type data_type;
40 
41  typedef _T1 key_type;
42  typedef _T2 value_type;
43 
44  KeyValuePair() : fKey(), fValue() {}
45 
48  KeyValuePair(adobe::move_from<KeyValuePair> other)
49  : fKey(adobe::move(other.source.fKey)), fValue(adobe::move(other.source.fValue)) {}
50 
51  KeyValuePair(_T1 __a, _T2 __b)
52  : fKey(adobe::move(__a)), fValue(adobe::move(__b)) {}
53 
54  KeyValuePair(const KeyValuePair &other) = default;
55  KeyValuePair(KeyValuePair &&other) = default;
56  KeyValuePair& operator=(const KeyValuePair &other) = default;
57  KeyValuePair& operator=(KeyValuePair &&other) = default;
58 
59  friend inline void swap(KeyValuePair<_T1, _T2>& a, KeyValuePair<_T1, _T2>& b)
60  {
61  using std::swap;
62  swap(a.fKey, b.fKey);
63  swap(a.fValue, b.fValue);
64  }
65 
66  _T1 fKey;
67  _T2 fValue;
68 
72  _T1& Key() { return fKey; }
73 
77  _T2& Value() { return fValue; }
78 
82  const _T1& Key() const { return fKey; }
83 
87  const _T2& Value() const { return fValue; }
88 
92  void SetValue(const _T2& val) { fValue = val; }
93 };
94 
95 // specialize adobe::type_info<> to avoid typeid problems across DLL boundaries
96 ADOBE_NAME_TYPE_2("keyvaluepair:indesign:adobe",KeyValuePair<T0,T1>)
97 
98 
103 template <class _T1, class _T2>
104 inline bool operator==(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y)
105 {
106  return __x.Key() == __y.Key() && __x.Value() == __y.Value();
107 }
108 
118 template <class _T1, class _T2>
119 inline bool operator==(const KeyValuePair<_T1, _T2>& __x, const _T1& __y)
120 {
121  return __x.Key() == __y;
122 }
123 
133 template <class _T1, class _T2>
134 inline bool operator==(const _T1& __y, const KeyValuePair<_T1, _T2>& __x)
135 {
136  return __x.Key() == __y;
137 }
138 
145 template <class _T1, class _T2>
146 inline bool operator<(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y)
147 {
148  return __x.Key() < __y.Key();
149 }
150 
156 template <class _T1, class _T2>
157 inline bool operator<(const KeyValuePair<_T1, _T2>& __x, const _T1& __y)
158 {
159  return __x.Key() < __y;
160 }
161 
167 template <class _T1, class _T2>
168 inline bool operator<(const _T1& __y, const KeyValuePair<_T1, _T2>& __x)
169 {
170  return __y < __x.Key();
171 }
172 
178 template <class _T1, class _T2>
179 inline bool operator!=(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y) {
180  return !(__x == __y);
181 }
182 
188 template <class _T1, class _T2>
189 inline bool operator>(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y) {
190  return __y < __x;
191 }
192 
198 template <class _T1, class _T2>
199 inline bool operator<=(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y) {
200  return !(__y < __x);
201 }
202 
208 template <class _T1, class _T2>
209 inline bool operator>=(const KeyValuePair<_T1, _T2>& __x, const KeyValuePair<_T1, _T2>& __y) {
210  return !(__x < __y);
211 }
212 
218 template <class _T1, class _T2>
219 inline KeyValuePair<_T1, _T2> K2make_keyvalue_pair(const _T1& __x, const _T2& __y)
220 {
221  return KeyValuePair<_T1, _T2>(__x, __y);
222 }
223 
224 // For sorted KeyValue vectors
225 template <class T>
226 struct KeyValueLess {
227  bool operator()(const T& lhs, const T& rhs)
228 {
229  return lhs.fKey < rhs.fKey;
230 }
231 };
232 
235 template <class Key, class Value>
241  KeyMatchPredicate(const Key& key) : fKey(key) {}
242 
247  bool operator()(const KeyValuePair<Key,Value>& x) { return x.Key() == fKey; }
248  Key fKey;
249 };
250 
251 
257 template <class Key, class Container>
258 int32 FindLocation(const Container& cont, const Key& key)
259 {
260  typedef typename Container::value_type KVP;
261  typedef typename KVP::value_type Value;
262 
263  typename Container::const_iterator p = std::find_if(cont.begin(), cont.end(), KeyMatchPredicate<Key, Value>(key));
264  return cont.end() == p ? -1 : p - cont.begin();
265 }
266 
267 
271 template <class Key, class Value>
273  ValueMatchPredicate(const Value& value) : fValue(value) {}
274  bool operator()(const KeyValuePair<Key,Value>& x) { return x.Value() == fValue; }
275  Value fValue;
276 };
277 
278 
284 template <class Value, class Container>
285 int32 FindLocationByValue(const Container& cont, const Value& value)
286 {
287  typedef typename Container::value_type KVP;
288  typedef typename KVP::key_type Key;
289 
290  typename Container::const_iterator p = std::find_if(cont.begin(), cont.end(), ValueMatchPredicate<Key, Value>(value));
291  return cont.end() == p ? -1 : p - cont.begin();
292 }
293 
294 
301 template <class Key, class Value, class Container>
302 void InsertKeyValue(Container& cont, const Key& key, const Value& val)
303 {
304  cont.push_back(KeyValuePair<Key, Value>(key, val));
305 }
306 
307 
308 
315 template <class Key, class Value, class Container>
316 void InsertOrReplaceKeyValue(Container& cont, const Key& key, const Value& val)
317 {
318  typename Container::iterator p = K2find(cont.begin(), cont.end(), key);
319  if (p == cont.end())
320  cont.push_back(KeyValuePair<Key, Value>(key, val));
321  else
322  p->SetValue(val);
323 }
324 
325 
331 template <class Value, class Container>
332 int32 InsertInSorted(Container& c, const Value& t)
333 {
334  typename Container::iterator i = std::lower_bound(c.begin(), c.end(), t);
335  i = c.insert(i, t);
336  return i - c.begin();
337 }
338 
345 template <class Key, class Value, class Container>
346 int32 InsertKeyValueInSorted(Container& c,const Key& k,const Value& v)
347 {
348  const KeyValuePair<Key, Value> el(k,v);
349 
350  typename Container::iterator i = std::lower_bound(c.begin(), c.end(), el);
351  ASSERT(i == c.end() || *i != el);
352  i = c.insert(i, el);
353  return i - c.begin();
354 }
355 
362 template <class Key, class Value, class Container>
363 int32 InsertOrReplaceKeyValueInSorted(Container& c, const Key& k, const Value& v)
364 {
365  typename Container::iterator i = std::lower_bound(c.begin(), c.end(), k);
366  if (i == c.end() || i->Key() != k)
367  i = c.insert( i, KeyValuePair<Key, Value>(k, v));
368  else
369  i->SetValue(v);
370  return i - c.begin();
371 }
372 
378 template <class Key, class Container>
379 int32 FindLocationInSorted(const Container& c, const Key& k)
380 {
381  typename Container::const_iterator i = std::lower_bound(c.begin(), c.end(), k);
382  return (i != c.end() && *i == k) ? (i - c.begin()) : -1;
383 }
384 
385 #endif /* __KEYVALUEPAIR__ */