From eec4ec6701092b33cdfc26e41a7be32d691bfb90 Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Tue, 9 Sep 2003 18:22:32 +0000 Subject: [PATCH] Renamed at() to get(), some fixes for map type, added assign() [SVN r1508] --- .../python/suite/indexing/suite_utils.hpp | 96 +++++++++++++++++-- 1 file changed, 86 insertions(+), 10 deletions(-) diff --git a/include/boost/python/suite/indexing/suite_utils.hpp b/include/boost/python/suite/indexing/suite_utils.hpp index c9e21afc..4b05b9da 100755 --- a/include/boost/python/suite/indexing/suite_utils.hpp +++ b/include/boost/python/suite/indexing/suite_utils.hpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include namespace indexing { template @@ -43,10 +45,13 @@ namespace indexing { { typedef typename ContainerTraits::container container; typedef typename ContainerTraits::iterator iterator; + typedef typename ContainerTraits::reference reference; typedef typename ContainerTraits::key_type key_type; typedef typename ContainerTraits::size_type size_type; typedef typename ContainerTraits::index_type index_type; + typedef typename ContainerTraits::value_type value_type; + typedef typename boost::call_traits::param_type value_param; typedef typename boost::call_traits::param_type key_param; typedef typename boost::call_traits::param_type index_param; @@ -54,9 +59,14 @@ namespace indexing { static iterator find (container &, key_param); static size_type count (container &, key_param); static void reverse (container &); - static iterator at (container&, index_param); + static reference get (container &, index_param); + static void assign (container &, index_param, value_param); + static void push_back (container &, value_param); static void sort (container &); // static void sort (container &, PyObject *); + + static iterator begin (container &c) { return c.begin(); } + static iterator end (container &c) { return c.end(); } }; ///////////////////////////////////////////////////////////////////////// @@ -79,7 +89,7 @@ namespace indexing { container_algorithms::find (container &c , key_param key) { - return std::find (c.begin(), c.end(), key); + return std::find (begin(c), end(c), key); } ///////////////////////////////////////////////////////////////////////// @@ -91,18 +101,43 @@ namespace indexing { container_algorithms::count (container &c , key_param key) { - return std::count (c.begin(), c.end(), key); + return std::count (begin(c), end(c), key); } ///////////////////////////////////////////////////////////////////////// - // Index into a container (iterator version) + // Index into a container (generic version) ///////////////////////////////////////////////////////////////////////// template - typename container_algorithms::iterator - container_algorithms::at (container &c, index_param ix) + typename container_algorithms::reference + container_algorithms::get (container &c, index_param ix) { - return c.begin() + ix; + return c.at (ix); + } + + ///////////////////////////////////////////////////////////////////////// + // Assign a value at a particular index (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + container_algorithms::assign (container &c + , index_param ix + , value_param val) + { + c.at(ix) = val; + } + + ///////////////////////////////////////////////////////////////////////// + // Insert at end of a container (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + container_algorithms::push_back (container &c + , value_param v) + { + c.push_back (v); } ///////////////////////////////////////////////////////////////////////// @@ -112,7 +147,7 @@ namespace indexing { template void container_algorithms::reverse (container &c) { - std::reverse (c.begin(), c.end()); + std::reverse (begin(c), end(c)); } ///////////////////////////////////////////////////////////////////////// @@ -122,7 +157,7 @@ namespace indexing { template void container_algorithms::sort (container &c) { - std::sort (c.begin(), c.end()); + std::sort (begin(c), end(c)); } ///////////////////////////////////////////////////////////////////////// @@ -178,13 +213,54 @@ namespace indexing { typedef typename Parent::iterator iterator; typedef typename Parent::size_type size_type; typedef typename Parent::container container; + typedef typename Parent::reference reference; typedef typename Parent::key_param key_param; + typedef typename Parent::index_param index_param; + typedef typename Parent::value_param value_param; + + static reference get (container &, index_param); + static void assign (container &, index_param, value_param); // Use member functions for the following (hiding base class versions) static iterator find (container &, key_param); static size_type count (container &, key_param); }; + ///////////////////////////////////////////////////////////////////////// + // Index into a container (associative version) + ///////////////////////////////////////////////////////////////////////// + + template + typename assoc_algorithms::reference + assoc_algorithms::get (container &c, index_param ix) + { + iterator iter = find (c, ix); + + if (iter == end(c)) + { + throw std::domain_error + (std::string ("associative container: key not found")); + } + + else + { + return iter->second; + } + } + + ///////////////////////////////////////////////////////////////////////// + // Assign a value at a particular index (associative version) + ///////////////////////////////////////////////////////////////////////// + + template + void + assoc_algorithms::assign (container &c + , index_param ix + , value_param val) + { + c[ix] = val; + } + ///////////////////////////////////////////////////////////////////////// // Find an element in an associative container ///////////////////////////////////////////////////////////////////////// @@ -197,7 +273,7 @@ namespace indexing { } ///////////////////////////////////////////////////////////////////////// - // Count occurances of an element in a container (std algorithm version) + // Count occurances of an element in a container (associative version) ///////////////////////////////////////////////////////////////////////// template