STXXL  1.4.0
 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 
19 
21 
22 //! Stream package subnamespace.
23 namespace stream {
24 
25 ////////////////////////////////////////////////////////////////////////
26 // UNIQUE //
27 ////////////////////////////////////////////////////////////////////////
28 
29 struct dummy_cmp_unique_ { };
30 
31 //! Equivalent to std::unique algorithms.
32 //!
33 //! Removes consecutive duplicates from the stream.
34 //! Uses BinaryPredicate to compare elements of the stream
35 template <class Input, class BinaryPredicate = dummy_cmp_unique_>
36 class unique
37 {
38  Input& input;
39  BinaryPredicate binary_pred;
40  typename Input::value_type current;
41 
42 public:
43  //! Standard stream typedef.
44  typedef typename Input::value_type value_type;
45 
46  unique(Input& input_, BinaryPredicate binary_pred_) : 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>
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:44
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Input::value_type value_type
Standard stream typedef.
Definition: unique.h:92
unique(Input &input_, BinaryPredicate binary_pred_)
Definition: unique.h:46
Equivalent to std::unique algorithms.
Definition: unique.h:36
BinaryPredicate binary_pred
Definition: unique.h:39
bool empty() const
Standard stream method.
Definition: unique.h:123
Input::value_type current
Definition: unique.h:40
#define STXXL_END_NAMESPACE
Definition: namespace.h:17