aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cutl/container/key.hxx71
-rw-r--r--cutl/container/map-iterator.hxx69
-rw-r--r--cutl/container/multi-index.hxx15
-rw-r--r--tests/container/makefile17
-rw-r--r--tests/container/multi-index/driver.cxx292
-rw-r--r--tests/container/multi-index/makefile69
-rw-r--r--tests/makefile2
7 files changed, 534 insertions, 1 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
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 <typename M>
+ 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 <typename M>
+ 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 <cutl/container/key.hxx>
+#include <cutl/container/map-iterator.hxx>
+
+#endif // CUTL_CONTAINER_MULTI_INDEX_HXX
diff --git a/tests/container/makefile b/tests/container/makefile
new file mode 100644
index 0000000..5f537db
--- /dev/null
+++ b/tests/container/makefile
@@ -0,0 +1,17 @@
+# file : tests/container/makefile
+# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make
+
+tests := multi-index
+
+default := $(out_base)/
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+$(default): $(addprefix $(out_base)/,$(addsuffix /,$(tests)))
+$(test): $(addprefix $(out_base)/,$(addsuffix /.test,$(tests)))
+$(clean): $(addprefix $(out_base)/,$(addsuffix /.clean,$(tests)))
+
+$(foreach t,$(tests),$(call import,$(src_base)/$t/makefile))
diff --git a/tests/container/multi-index/driver.cxx b/tests/container/multi-index/driver.cxx
new file mode 100644
index 0000000..9ddf73d
--- /dev/null
+++ b/tests/container/multi-index/driver.cxx
@@ -0,0 +1,292 @@
+// file : tests/container/multi-index/driver.cxx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+#include <map>
+#include <list>
+#include <string>
+#include <cassert>
+#include <iostream>
+
+#include <cutl/container/multi-index.hxx>
+
+using namespace std;
+using namespace cutl::container;
+
+struct person
+{
+ person (const string& e, const string& f, const string& l, unsigned short a)
+ : email (e), first (f), last (l), age (a) {}
+
+ const string email;
+ const string first;
+ const string last;
+ unsigned short age;
+};
+
+struct person_email_set
+{
+ typedef map<key<string>, person> email_map;
+ typedef map_const_iterator<email_map> const_iterator;
+
+ pair<const_iterator, bool>
+ insert (const person& v)
+ {
+ pair<email_map::iterator, bool> r (
+ email_map_.insert (email_map::value_type (v.email, v)));
+
+ const_iterator i (r.first);
+
+ if (r.second)
+ r.first->first.assign (i->email);
+
+ return make_pair (i, r.second);
+ }
+
+ const_iterator
+ find (const string& email) const
+ {
+ return email_map_.find (email);
+ }
+
+ const_iterator begin () const {return email_map_.begin ();}
+ const_iterator end () const {return email_map_.end ();}
+
+private:
+ email_map email_map_;
+};
+
+struct person_name_set
+{
+ typedef key<string, string> name_key;
+ typedef map<name_key, person> name_map;
+ typedef map_const_iterator<name_map> const_iterator;
+
+ pair<const_iterator, bool>
+ insert (const person& v)
+ {
+ pair<name_map::iterator, bool> r (
+ name_map_.insert (
+ name_map::value_type (name_key (v.first, v.last), v)));
+
+ const_iterator i (r.first);
+
+ if (r.second)
+ r.first->first.assign (i->first, i->last);
+
+ return make_pair (i, r.second);
+ }
+
+ const_iterator
+ find (const string& first, const string& last) const
+ {
+ return name_map_.find (name_key (first, last));
+ }
+
+ const_iterator begin () const {return name_map_.begin ();}
+ const_iterator end () const {return name_map_.end ();}
+
+private:
+ name_map name_map_;
+};
+
+struct person_email_name_set
+{
+ typedef key<string, string> name_key;
+ typedef map<name_key, person> name_map;
+ typedef map_iterator<name_map> iterator;
+ typedef map_const_iterator<name_map> const_iterator;
+
+ typedef map<key<string>, iterator> email_map;
+
+ pair<iterator, bool>
+ insert (const person& v)
+ {
+ // First check that we don't have any collisions in the secondary
+ // indexes.
+ //
+ {
+ email_map::iterator i (email_map_.find (v.email));
+
+ if (i != email_map_.end ())
+ return make_pair (i->second, false);
+ }
+
+ pair<name_map::iterator, bool> r (
+ name_map_.insert (
+ name_map::value_type (name_key (v.first, v.last), v)));
+
+ iterator i (r.first);
+
+ if (r.second)
+ {
+ r.first->first.assign (i->first, i->last);
+ email_map_.insert (email_map::value_type (i->email, i));
+ }
+
+ return make_pair (i, r.second);
+ }
+
+ iterator
+ find (const string& first, const string& last)
+ {
+ return name_map_.find (name_key (first, last));
+ }
+
+ const_iterator
+ find (const string& first, const string& last) const
+ {
+ return name_map_.find (name_key (first, last));
+ }
+
+ iterator
+ find (const string& email)
+ {
+ email_map::iterator i (email_map_.find (email));
+ return i != email_map_.end () ? i->second : end ();
+ }
+
+ const_iterator
+ find (const string& email) const
+ {
+ email_map::const_iterator i (email_map_.find (email));
+ return i != email_map_.end () ? i->second : end ();
+ }
+
+ void
+ erase (iterator i )
+ {
+ email_map_.erase (i->email);
+ name_map_.erase (i);
+ }
+
+ iterator begin () {return name_map_.begin ();}
+ const_iterator begin () const {return name_map_.begin ();}
+
+ iterator end () {return name_map_.end ();}
+ const_iterator end () const {return name_map_.end ();}
+
+private:
+ name_map name_map_;
+ email_map email_map_;
+};
+
+struct person_list_email_set
+{
+ typedef list<person> person_list;
+ typedef person_list::iterator iterator;
+ typedef person_list::const_iterator const_iterator;
+
+ typedef map<key<string>, iterator> email_map;
+
+ pair<iterator, bool>
+ insert (const person& v)
+ {
+ // First check that we don't have any collisions in the secondary
+ // indexes.
+ //
+ {
+ email_map::iterator i (email_map_.find (v.email));
+
+ if (i != email_map_.end ())
+ return make_pair (i->second, false);
+ }
+
+ iterator i (person_list_.insert (end (), v));
+ email_map_.insert (email_map::value_type (i->email, i));
+ return make_pair (i, true);
+ }
+
+ iterator
+ find (const string& email)
+ {
+ email_map::iterator i (email_map_.find (email));
+ return i != email_map_.end () ? i->second : end ();
+ }
+
+ const_iterator
+ find (const string& email) const
+ {
+ email_map::const_iterator i (email_map_.find (email));
+ return i != email_map_.end () ? i->second : end ();
+ }
+
+ iterator begin () {return person_list_.begin ();}
+ const_iterator begin () const {return person_list_.begin ();}
+
+ iterator end () {return person_list_.end ();}
+ const_iterator end () const {return person_list_.end ();}
+
+private:
+ person_list person_list_;
+ email_map email_map_;
+};
+
+int
+main ()
+{
+ {
+ person_email_set s;
+
+ assert (s.insert (person ("john@doe.com", "John", "Doe", 20)).second);
+ assert (s.insert (person ("jane@doe.com", "Jane", "Doe", 21)).second);
+ assert (!s.insert (person ("john@doe.com", "Johnny", "Doe", 22)).second);
+
+ assert (s.find ("john@doe.com") != s.end ());
+ assert (s.find ("jane@doe.com") != s.end ());
+ assert (s.find ("john@doe.org") == s.end ());
+ }
+
+ {
+ person_name_set s;
+
+ assert (s.insert (person ("john@doe.com", "John", "Doe", 20)).second);
+ assert (s.insert (person ("jane@doe.com", "Jane", "Doe", 21)).second);
+ assert (!s.insert (person ("john@doe.org", "John", "Doe", 22)).second);
+
+ assert (s.find ("John", "Doe") != s.end ());
+ assert (s.find ("Jane", "Doe") != s.end ());
+ assert (s.find ("Johnny", "Doe") == s.end ());
+ }
+
+ {
+ person_email_name_set s;
+ person_email_name_set const& cs (s);
+
+ assert (s.insert (person ("john@doe.com", "John", "Doe", 20)).second);
+ assert (s.insert (person ("jane@doe.com", "Jane", "Doe", 21)).second);
+ assert (!s.insert (person ("john@doe.org", "John", "Doe", 22)).second);
+ assert (!s.insert (person ("john@doe.com", "Johnny", "Doe", 23)).second);
+
+ assert (s.find ("John", "Doe") != s.end ());
+ assert (cs.find ("Jane", "Doe") != cs.end ());
+ assert (s.find ("john@doe.com") != s.end ());
+ assert (cs.find ("jane@doe.com") != s.end ());
+ assert (s.find ("Johnny", "Doe") == s.end ());
+ assert (cs.find ("john@doe.org") == s.end ());
+
+ person_email_name_set::iterator i (s.find ("John", "Doe"));
+ i->age++;
+
+ s.erase (i);
+ assert (s.find ("John", "Doe") == s.end ());
+ assert (s.find ("john@doe.com") == s.end ());
+ }
+
+ {
+ person_list_email_set s;
+
+ assert (s.insert (person ("john@doe.com", "John", "Doe", 20)).second);
+ assert (s.insert (person ("jane@doe.com", "Jane", "Doe", 21)).second);
+ assert (!s.insert (person ("john@doe.com", "Johnny", "Doe", 22)).second);
+
+ assert (s.find ("john@doe.com") != s.end ());
+ assert (s.find ("jane@doe.com") != s.end ());
+ assert (s.find ("jane@doe.org") == s.end ());
+
+ person_list_email_set::iterator i (s.begin ());
+ assert (i != s.end () && i->email == "john@doe.com");
+ assert (++i != s.end () && i->email == "jane@doe.com");
+ assert (++i == s.end ());
+ }
+}
diff --git a/tests/container/multi-index/makefile b/tests/container/multi-index/makefile
new file mode 100644
index 0000000..fd70e3f
--- /dev/null
+++ b/tests/container/multi-index/makefile
@@ -0,0 +1,69 @@
+# file : tests/container/multi-index/makefile
+# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+# license : MIT; see accompanying LICENSE file
+
+include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make
+
+cxx_tun := driver.cxx
+
+#
+#
+cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o))
+cxx_od := $(cxx_obj:.o=.o.d)
+
+cutl.l := $(out_root)/cutl/cutl.l
+cutl.l.cpp-options := $(out_root)/cutl/cutl.l.cpp-options
+
+driver := $(out_base)/driver
+test := $(out_base)/.test
+clean := $(out_base)/.clean
+
+# Build.
+#
+$(driver): $(cxx_obj) $(cutl.l)
+$(cxx_obj) $(cxx_od): $(cutl.l.cpp-options)
+
+
+$(call include-dep,$(cxx_od))
+
+
+# Alias for default target.
+#
+$(out_base)/: $(driver)
+
+
+# Test.
+#
+$(test): $(driver)
+ $(call message,test $<,$<)
+
+
+# Clean.
+#
+$(clean): \
+ $(driver).o.clean \
+ $(addsuffix .cxx.clean,$(cxx_obj)) \
+ $(addsuffix .cxx.clean,$(cxx_od))
+
+
+# Generated .gitignore.
+#
+ifeq ($(out_base),$(src_base))
+$(driver): | $(out_base)/.gitignore
+
+$(out_base)/.gitignore: files := driver
+$(clean): $(out_base)/.gitignore.clean
+
+$(call include,$(bld_root)/git/gitignore.make)
+endif
+
+
+# How to.
+#
+$(call include,$(bld_root)/cxx/o-e.make)
+$(call include,$(bld_root)/cxx/cxx-o.make)
+$(call include,$(bld_root)/cxx/cxx-d.make)
+
+# Dependencies.
+#
+$(call import,$(src_root)/cutl/makefile)
diff --git a/tests/makefile b/tests/makefile
index b09b947..8d16183 100644
--- a/tests/makefile
+++ b/tests/makefile
@@ -4,7 +4,7 @@
include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make
-tests := compiler fs re shared-ptr
+tests := compiler container fs re shared-ptr
default := $(out_base)/
test := $(out_base)/.test