From 390af1f09da622822492c5f55cfee69efe0d6845 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Mon, 14 Aug 2006 12:09:51 +0000 Subject: [PATCH] - Add support for input sequences via template; this should simplify a little iterating over containers git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@857 3ecf2c5c-341e-0410-92b4-d18e462d057c --- Makefile.am | 1 + src/templates/sequences.tcc | 92 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/templates/sequences.tcc diff --git a/Makefile.am b/Makefile.am index 89e3d1e..225721d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -326,6 +326,7 @@ glade_DATA = \ pkginclude_HEADERS += \ src/templates/deletor.tcc \ src/templates/parameter.tcc \ + src/templates/sequences.tcc \ src/templates/singleton.hh \ src/templates/singleton.tcc \ src/templates/smartp.hh \ diff --git a/src/templates/sequences.tcc b/src/templates/sequences.tcc new file mode 100644 index 0000000..e221a38 --- /dev/null +++ b/src/templates/sequences.tcc @@ -0,0 +1,92 @@ +// src/templates/sequences.tcc - Copyright 2005, 2006, University +// of Padova, dept. of Pure and Applied +// Mathematics +// +// This file is part of SGPEMv2. +// +// This is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// SGPEMv2 is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with SGPEMv2; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +/* Helper templates to make easier to work on an entire sequence over a container */ + +#include +#include + +// Class defining an input sequence + +template +class Iseq : protected std::pair +{ +public: + typedef typename In::pointer pointer; + typedef typename In::reference reference; + + explicit Iseq(In a, In b) + : std::pair(a, b) + {} + + operator bool() + { return Iseq::first != Iseq::second; } + + Iseq& operator++() + { ++Iseq::first; return *this; } + + Iseq operator++(int) + { Iseq t = *this; ++Iseq::first; return t; } + + const reference operator*() const + { return *Iseq::first; } + + const In& operator->() const + { return Iseq::first; } +}; + + + +// Adaptor functions for containers: + +template +Iseq +iseq(const Container& c) +{ + return Iseq(c.begin(), c.end()); +} + + +template +Iseq +iseq(Container& c) +{ + return Iseq(c.begin(), c.end()); +} + + +// Adaptor functions for containers that support reverse iteration + +template +Iseq +r_iseq(const Container& c) +{ + return Iseq(c.rbegin(), c.rend()); +} + + +template +Iseq +r_iseq(Container& c) +{ + return Iseq(c.rbegin(), c.rend()); +} +