Stxxl  1.4.0
include/stxxl/bits/stream/unique.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/stream/unique.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2003-2005 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #ifndef STXXL_STREAM__UNIQUE_H
00015 #define STXXL_STREAM__UNIQUE_H
00016 
00017 #include <stxxl/bits/namespace.h>
00018 
00019 
00020 __STXXL_BEGIN_NAMESPACE
00021 
00022 //! \brief Stream package subnamespace
00023 namespace stream
00024 {
00025     ////////////////////////////////////////////////////////////////////////
00026     //     UNIQUE                                                         //
00027     ////////////////////////////////////////////////////////////////////////
00028 
00029     struct dummy_cmp_unique_ { };
00030 
00031     //! \brief Equivalent to std::unique algorithms
00032     //!
00033     //! Removes consecutive duplicates from the stream.
00034     //! Uses BinaryPredicate to compare elements of the stream
00035     template <class Input, class BinaryPredicate = dummy_cmp_unique_>
00036     class unique
00037     {
00038         Input & input;
00039         BinaryPredicate binary_pred;
00040         typename Input::value_type current;
00041 
00042     public:
00043         //! \brief Standard stream typedef
00044         typedef typename Input::value_type value_type;
00045 
00046         unique(Input & input_, BinaryPredicate binary_pred_) : input(input_), binary_pred(binary_pred_)
00047         {
00048             if (!input.empty())
00049                 current = *input;
00050         }
00051 
00052         //! \brief Standard stream method
00053         unique & operator ++ ()
00054         {
00055             value_type old_value = current;
00056             ++input;
00057             while (!input.empty() && (binary_pred(current = *input, old_value)))
00058                 ++input;
00059             return *this;
00060         }
00061 
00062         //! \brief Standard stream method
00063         const value_type & operator * () const
00064         {
00065             return current;
00066         }
00067 
00068         //! \brief Standard stream method
00069         const value_type * operator -> () const
00070         {
00071             return &current;
00072         }
00073 
00074         //! \brief Standard stream method
00075         bool empty() const
00076         {
00077             return input.empty();
00078         }
00079     };
00080 
00081     //! \brief Equivalent to std::unique algorithms
00082     //!
00083     //! Removes consecutive duplicates from the stream.
00084     template <class Input>
00085     class unique<Input, dummy_cmp_unique_>
00086     {
00087         Input & input;
00088         typename Input::value_type current;
00089 
00090     public:
00091         //! \brief Standard stream typedef
00092         typedef typename Input::value_type value_type;
00093 
00094         unique(Input & input_) : input(input_)
00095         {
00096             if (!input.empty())
00097                 current = *input;
00098         }
00099 
00100         //! \brief Standard stream method
00101         unique & operator ++ ()
00102         {
00103             value_type old_value = current;
00104             ++input;
00105             while (!input.empty() && ((current = *input) == old_value))
00106                 ++input;
00107             return *this;
00108         }
00109 
00110         //! \brief Standard stream method
00111         const value_type & operator * () const
00112         {
00113             return current;
00114         }
00115 
00116         //! \brief Standard stream method
00117         const value_type * operator -> () const
00118         {
00119             return &current;
00120         }
00121 
00122         //! \brief Standard stream method
00123         bool empty() const
00124         {
00125             return input.empty();
00126         }
00127     };
00128 
00129 //! \}
00130 }
00131 
00132 __STXXL_END_NAMESPACE
00133 
00134 #endif // !STXXL_STREAM__UNIQUE_H
00135 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines