STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unique.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/stream/unique.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2003-2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_STREAM_UNIQUE_HEADER
15 #define STXXL_STREAM_UNIQUE_HEADER
16 
17 #include <stxxl/bits/namespace.h>
18 
20 
21 //! Stream package subnamespace.
22 namespace stream {
23 
24 ////////////////////////////////////////////////////////////////////////
25 // UNIQUE //
26 ////////////////////////////////////////////////////////////////////////
27 
28 struct dummy_cmp_unique { };
29 
30 //! Equivalent to std::unique algorithms.
31 //!
32 //! Removes consecutive duplicates from the stream.
33 //! Uses BinaryPredicate to compare elements of the stream
34 template <class Input, class BinaryPredicate = dummy_cmp_unique>
35 class unique
36 {
37  Input& input;
38  BinaryPredicate binary_pred;
39  typename Input::value_type current;
40 
41 public:
42  //! Standard stream typedef.
43  typedef typename Input::value_type value_type;
44 
45  unique(Input& input_, BinaryPredicate binary_pred_)
46  : input(input_), binary_pred(binary_pred_)
47  {
48  if (!input.empty())
49  current = *input;
50  }
51 
52  //! Standard stream method.
54  {
55  value_type old_value = current;
56  ++input;
57  while (!input.empty() && (binary_pred(current = *input, old_value)))
58  ++input;
59  return *this;
60  }
61 
62  //! Standard stream method.
63  const value_type& operator * () const
64  {
65  return current;
66  }
67 
68  //! Standard stream method.
69  const value_type* operator -> () const
70  {
71  return &current;
72  }
73 
74  //! Standard stream method.
75  bool empty() const
76  {
77  return input.empty();
78  }
79 };
80 
81 //! Equivalent to std::unique algorithms.
82 //!
83 //! Removes consecutive duplicates from the stream.
84 template <class Input>
85 class unique<Input, dummy_cmp_unique>
86 {
87  Input& input;
88  typename Input::value_type current;
89 
90 public:
91  //! Standard stream typedef.
92  typedef typename Input::value_type value_type;
93 
94  unique(Input& input_) : input(input_)
95  {
96  if (!input.empty())
97  current = *input;
98  }
99 
100  //! Standard stream method.
102  {
103  value_type old_value = current;
104  ++input;
105  while (!input.empty() && ((current = *input) == old_value))
106  ++input;
107  return *this;
108  }
109 
110  //! Standard stream method.
111  const value_type& operator * () const
112  {
113  return current;
114  }
115 
116  //! Standard stream method.
117  const value_type* operator -> () const
118  {
119  return &current;
120  }
121 
122  //! Standard stream method.
123  bool empty() const
124  {
125  return input.empty();
126  }
127 };
128 
129 //! \}
130 
131 } // namespace stream
132 
134 
135 #endif // !STXXL_STREAM_UNIQUE_HEADER
136 // vim: et:ts=4:sw=4
bool empty() const
Standard stream method.
Definition: unique.h:75
Input::value_type value_type
Standard stream typedef.
Definition: unique.h:43
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
unique(Input &input_, BinaryPredicate binary_pred_)
Definition: unique.h:45
Equivalent to std::unique algorithms.
Definition: unique.h:35
BinaryPredicate binary_pred
Definition: unique.h:38
Input::value_type current
Definition: unique.h:39
Input::value_type value_type
Standard stream typedef.
Definition: unique.h:92
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
bool empty() const
Standard stream method.
Definition: unique.h:123