| 1 |
// file : cutl/container/key.hxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompkeying LICENSE file |
| 4 |
|
| 5 |
#ifndef CUTL_CONTAINER_KEY_HXX |
| 6 |
#define CUTL_CONTAINER_KEY_HXX |
| 7 |
|
| 8 |
namespace cutl |
| 9 |
{ |
| 10 |
namespace container |
| 11 |
{ |
| 12 |
// A modifiable map key wrapper that can be used to implement multi- |
| 13 |
// index containers, as discussed in the following post: |
| 14 |
// |
| 15 |
// http://www.codesynthesis.com/~boris/blog/2012/09/11/emulating-boost-multi-index-with-std-containers/ |
| 16 |
// |
| 17 |
template <class T1, class T2 = void, class T3 = void> |
| 18 |
struct key; |
| 19 |
|
| 20 |
template <class T1> |
| 21 |
struct key<T1, void, void> |
| 22 |
{ |
| 23 |
mutable const T1* p1; |
| 24 |
|
| 25 |
key (): p1 (0) {} |
| 26 |
key (const T1& r1): p1 (&r1) {} |
| 27 |
void assign (const T1& r1) const {p1 = &r1;} |
| 28 |
|
| 29 |
bool operator< (const key& x) const {return *p1 < *x.p1;} |
| 30 |
}; |
| 31 |
|
| 32 |
template <class T1, class T2> |
| 33 |
struct key<T1, T2, void> |
| 34 |
{ |
| 35 |
mutable const T1* p1; |
| 36 |
mutable const T2* p2; |
| 37 |
|
| 38 |
key (): p1 (0), p2 (0) {} |
| 39 |
key (const T1& r1, const T2& r2): p1 (&r1), p2 (&r2) {} |
| 40 |
void assign (const T1& r1, const T2& r2) const {p1 = &r1; p2 = &r2;} |
| 41 |
|
| 42 |
bool operator< (const key& x) const |
| 43 |
{ |
| 44 |
return *p1 < *x.p1 || (!(*x.p1 < *p1) && *p2 < *x.p2); |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
template <class T1, class T2, class T3> |
| 49 |
struct key |
| 50 |
{ |
| 51 |
mutable const T1* p1; |
| 52 |
mutable const T2* p2; |
| 53 |
mutable const T3* p3; |
| 54 |
|
| 55 |
key (): p1 (0), p2 (0), p3 (0) {} |
| 56 |
key (const T1& r1, const T2& r2, const T3& r3) |
| 57 |
: p1 (&r1), p2 (&r2) , p3 (&r3) {} |
| 58 |
void assign (const T1& r1, const T2& r2, const T3& r3) const |
| 59 |
{p1 = &r1; p2 = &r2; p3 = &r3;} |
| 60 |
|
| 61 |
bool operator< (const key& x) const |
| 62 |
{ |
| 63 |
return (*p1 < *x.p1 || |
| 64 |
(!(*x.p1 < *p1) && (*p2 < *x.p2 || |
| 65 |
(!(*x.p2 < *p2) && *p3 < *x.p3)))); |
| 66 |
} |
| 67 |
}; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
#endif // CUTL_CONTAINER_KEY_HXX |