From f80de9f4107a01ab3a917eddf15eaaf4a1c998a2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 18 Sep 2012 10:28:51 +0200 Subject: Add building blocks for multi-index containers --- cutl/container/key.hxx | 71 +++++++++++++++++++++++++++++++++++++++++ cutl/container/map-iterator.hxx | 69 +++++++++++++++++++++++++++++++++++++++ cutl/container/multi-index.hxx | 15 +++++++++ 3 files changed, 155 insertions(+) create mode 100644 cutl/container/key.hxx create mode 100644 cutl/container/map-iterator.hxx create mode 100644 cutl/container/multi-index.hxx (limited to 'cutl/container') 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 + struct key; + + template + struct key + { + 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 + struct key + { + 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 + 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 diff --git a/cutl/container/map-iterator.hxx b/cutl/container/map-iterator.hxx new file mode 100644 index 0000000..3e84a4f --- /dev/null +++ b/cutl/container/map-iterator.hxx @@ -0,0 +1,69 @@ +// file : cutl/container/map-iterator.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_CONTAINER_MAP_ITERATOR_HXX +#define CUTL_CONTAINER_MAP_ITERATOR_HXX + +namespace cutl +{ + namespace container + { + // Map iterator adapter 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 + struct map_iterator: M::iterator + { + typedef typename M::iterator base_iterator; + typedef typename M::value_type::second_type value_type; + typedef value_type* pointer; + typedef value_type& reference; + + map_iterator () {} + map_iterator (base_iterator i): base_iterator (i) {} + + reference + operator* () const + { + return base_iterator::operator* ().second; + } + + pointer + operator-> () const + { + return &base_iterator::operator-> ()->second; + } + }; + + template + struct map_const_iterator: M::const_iterator + { + typedef typename M::iterator base_iterator; + typedef typename M::const_iterator base_const_iterator; + typedef const typename M::value_type::second_type value_type; + typedef value_type* pointer; + typedef value_type& reference; + + map_const_iterator () {} + map_const_iterator (base_iterator i): base_const_iterator (i) {} + map_const_iterator (base_const_iterator i): base_const_iterator (i) {} + + reference + operator* () const + { + return base_const_iterator::operator* ().second; + } + + pointer + operator-> () const + { + return &base_const_iterator::operator-> ()->second; + } + }; + } +} + +#endif // CUTL_CONTAINER_MAP_ITERATOR_HXX diff --git a/cutl/container/multi-index.hxx b/cutl/container/multi-index.hxx new file mode 100644 index 0000000..d9a713b --- /dev/null +++ b/cutl/container/multi-index.hxx @@ -0,0 +1,15 @@ +// file : cutl/container/multi-index.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_CONTAINER_MULTI_INDEX_HXX +#define CUTL_CONTAINER_MULTI_INDEX_HXX + +// Multi-index containers support. See the following post for details: +// +// http://www.codesynthesis.com/~boris/blog/2012/09/11/emulating-boost-multi-index-with-std-containers/ +// +#include +#include + +#endif // CUTL_CONTAINER_MULTI_INDEX_HXX -- cgit v1.1