diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-18 10:28:51 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-18 10:28:51 +0200 |
commit | f80de9f4107a01ab3a917eddf15eaaf4a1c998a2 (patch) | |
tree | 42e58fe8e05ce618c5297ce1975dbf04bdeb3250 /cutl/container/key.hxx | |
parent | 4044d9c9ba0622b28a49a1b36b582614a2135a23 (diff) |
Add building blocks for multi-index containers
Diffstat (limited to 'cutl/container/key.hxx')
-rw-r--r-- | cutl/container/key.hxx | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/cutl/container/key.hxx b/cutl/container/key.hxx new file mode 100644 index 0000000..6afef96 --- /dev/null +++ b/cutl/container/key.hxx @@ -0,0 +1,71 @@ +// file : cutl/container/key.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : MIT; see accompkeying LICENSE file + +#ifndef CUTL_CONTAINER_KEY_HXX +#define CUTL_CONTAINER_KEY_HXX + +namespace cutl +{ + namespace container + { + // A modifiable map key wrapper that can be used to implement multi- + // index containers, as discussed in the following post: + // + // http://www.codesynthesis.com/~boris/blog/2012/09/11/emulating-boost-multi-index-with-std-containers/ + // + template <class T1, class T2 = void, class T3 = void> + struct key; + + template <class T1> + struct key<T1, void, void> + { + mutable const T1* p1; + + key (): p1 (0) {} + key (const T1& r1): p1 (&r1) {} + void assign (const T1& r1) const {p1 = &r1;} + + bool operator< (const key& x) const {return *p1 < *x.p1;} + }; + + template <class T1, class T2> + struct key<T1, T2, void> + { + mutable const T1* p1; + mutable const T2* p2; + + key (): p1 (0), p2 (0) {} + key (const T1& r1, const T2& r2): p1 (&r1), p2 (&r2) {} + void assign (const T1& r1, const T2& r2) const {p1 = &r1; p2 = &r2;} + + bool operator< (const key& x) const + { + return *p1 < *x.p1 || (!(*x.p1 < *p1) && *p2 < *x.p2); + } + }; + + template <class T1, class T2, class T3> + struct key + { + mutable const T1* p1; + mutable const T2* p2; + mutable const T3* p3; + + key (): p1 (0), p2 (0), p3 (0) {} + key (const T1& r1, const T2& r2, const T3& r3) + : p1 (&r1), p2 (&r2) , p3 (&r3) {} + void assign (const T1& r1, const T2& r2, const T3& r3) const + {p1 = &r1; p2 = &r2; p3 = &r3;} + + bool operator< (const key& x) const + { + return (*p1 < *x.p1 || + (!(*x.p1 < *p1) && (*p2 < *x.p2 || + (!(*x.p2 < *p2) && *p3 < *x.p3)))); + } + }; + } +} + +#endif // CUTL_CONTAINER_KEY_HXX |