From 415be6e71a9afda489b5c8bccceb3412bf640810 Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Thu, 11 Sep 2003 22:32:54 +0000 Subject: [PATCH] Uniform interface layer for various containers [SVN r1532] --- .../python/suite/indexing/algorithms.hpp | 311 ++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100755 include/boost/python/suite/indexing/algorithms.hpp diff --git a/include/boost/python/suite/indexing/algorithms.hpp b/include/boost/python/suite/indexing/algorithms.hpp new file mode 100755 index 00000000..7c44582c --- /dev/null +++ b/include/boost/python/suite/indexing/algorithms.hpp @@ -0,0 +1,311 @@ +// -*- mode:c++ -*- +// +// Header file algorithms.hpp +// +// Uniform interface layer for all containers. +// +// Copyright (c) 2003 Raoul M. Gough +// +// This material is provided "as is", with absolutely no warranty expressed +// or implied. Any use is at your own risk. +// +// Permission to use or copy this material for any purpose is hereby +// granted without fee, provided the above notices are retained on all +// copies. Permission to modify the material and to distribute modified +// versions is granted, provided the above notices are retained, and a +// notice that the material was modified is included with the above +// copyright notice. +// +// History +// ======= +// 2003/ 9/11 rmg File creation from suite_utils.hpp +// +// $Id$ +// + +#ifndef algorithms_rmg_20030823_included +#define algorithms_rmg_20030823_included + +#include "suite_utils.hpp" +#include +#include +#include +#include +#include + +namespace indexing { + template + struct default_algorithms + { + typedef ContainerTraits container_traits; + + 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; + + static size_type size (container &); + static iterator find (container &, key_param); + static size_type count (container &, key_param); + static void reverse (container &); + static reference get (container &, index_param); + static void assign (container &, index_param, value_param); + static void insert (container &, index_param, value_param); + static void erase (container &, index_param, index_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(); } + }; + + ///////////////////////////////////////////////////////////////////////// + // Get the size of a container + ///////////////////////////////////////////////////////////////////////// + + template + typename default_algorithms::size_type + default_algorithms::size (container &c) + { + return c.size(); + } + + ///////////////////////////////////////////////////////////////////////// + // Find an element in a container (std algorithm version) + ///////////////////////////////////////////////////////////////////////// + + template + typename default_algorithms::iterator + default_algorithms::find (container &c + , key_param key) + { + return std::find (begin(c), end(c), key); + } + + ///////////////////////////////////////////////////////////////////////// + // Count occurances of an element in a container (std algorithm version) + ///////////////////////////////////////////////////////////////////////// + + template + typename default_algorithms::size_type + default_algorithms::count (container &c + , key_param key) + { + return std::count (begin(c), end(c), key); + } + + ///////////////////////////////////////////////////////////////////////// + // Index into a container (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + typename default_algorithms::reference + default_algorithms::get (container &c, index_param ix) + { + return c.at (ix); + } + + ///////////////////////////////////////////////////////////////////////// + // Assign a value at a particular index (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + default_algorithms::assign (container &c + , index_param ix + , value_param val) + { + c.at(ix) = val; + } + + ///////////////////////////////////////////////////////////////////////// + // Insert at end of a container (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + default_algorithms::push_back (container &c + , value_param v) + { + c.push_back (v); + } + + ///////////////////////////////////////////////////////////////////////// + // Insert at an index in the container (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + default_algorithms::insert (container &c + , index_param i + , value_param v) + { + c.insert (c.begin() + i, v); + } + + ///////////////////////////////////////////////////////////////////////// + // Erase between given indexes in the container (generic version) + ///////////////////////////////////////////////////////////////////////// + + template + void + default_algorithms::erase (container &c + , index_param from + , index_param to) + { + c.erase (c.begin() + from, c.begin() + to); + } + + ///////////////////////////////////////////////////////////////////////// + // Reverse the contents of a container (std algorithm version) + ///////////////////////////////////////////////////////////////////////// + + template + void default_algorithms::reverse (container &c) + { + std::reverse (begin(c), end(c)); + } + + ///////////////////////////////////////////////////////////////////////// + // Sort the contents of a container (std algorithm version) + ///////////////////////////////////////////////////////////////////////// + + template + void default_algorithms::sort (container &c) + { + std::sort (begin(c), end(c)); + } + + ///////////////////////////////////////////////////////////////////////// + // Special cases for std::list + ///////////////////////////////////////////////////////////////////////// + + template + struct list_algorithms : public default_algorithms + { + private: + typedef default_algorithms Parent; + + public: + typedef typename Parent::container container; + + // Use member functions for the following (hiding base class versions) + static void reverse (container &); + static void sort (container &); + // static void sort (container &, PyObject *); + }; + + ///////////////////////////////////////////////////////////////////////// + // Reverse the contents of a list (member function version) + ///////////////////////////////////////////////////////////////////////// + + template + void list_algorithms::reverse (container &c) + { + c.reverse(); + } + + ///////////////////////////////////////////////////////////////////////// + // Sort the contents of a container (std algorithm version) + ///////////////////////////////////////////////////////////////////////// + + template + void list_algorithms::sort (container &c) + { + c.sort(); + } + + ///////////////////////////////////////////////////////////////////////// + // Special cases for associative containers + ///////////////////////////////////////////////////////////////////////// + + template + struct assoc_algorithms : public default_algorithms + { + private: + typedef default_algorithms Parent; + + public: + 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 + ///////////////////////////////////////////////////////////////////////// + + template + typename assoc_algorithms::iterator + assoc_algorithms::find (container &c, key_param key) + { + return c.find (key); + } + + ///////////////////////////////////////////////////////////////////////// + // Count occurances of an element in a container (associative version) + ///////////////////////////////////////////////////////////////////////// + + template + typename assoc_algorithms::size_type + assoc_algorithms::count (container &c, key_param key) + { + return c.count (key); + } +} + +#endif // algorithms_rmg_20030823_included