Stxxl  1.3.2
unique.h
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_H
15 #define STXXL_STREAM__UNIQUE_H
16 
17 #include <stxxl/bits/namespace.h>
18 
19 
20 __STXXL_BEGIN_NAMESPACE
21 
23 namespace stream
24 {
26  // UNIQUE //
28 
29  struct dummy_cmp_unique_ { };
30 
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:
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 
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 
63  const value_type & operator * () const
64  {
65  return current;
66  }
67 
69  const value_type * operator -> () const
70  {
71  return &current;
72  }
73 
75  bool empty() const
76  {
77  return input.empty();
78  }
79  };
80 
84  template <class Input>
85  class unique<Input, dummy_cmp_unique_>
86  {
87  Input & input;
88  typename Input::value_type current;
89 
90  public:
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 
102  {
103  value_type old_value = current;
104  ++input;
105  while (!input.empty() && ((current = *input) == old_value))
106  ++input;
107  return *this;
108  }
109 
111  const value_type & operator * () const
112  {
113  return current;
114  }
115 
117  const value_type * operator -> () const
118  {
119  return &current;
120  }
121 
123  bool empty() const
124  {
125  return input.empty();
126  }
127  };
128 
130 }
131 
132 __STXXL_END_NAMESPACE
133 
134 #endif // !STXXL_STREAM__UNIQUE_H
135 // vim: et:ts=4:sw=4
Input::value_type value_type
Standard stream typedef.
Definition: unique.h:44
bool empty() const
Standard stream method.
Definition: unique.h:123
Equivalent to std::unique algorithms.
Definition: unique.h:36
bool empty() const
Standard stream method.
Definition: unique.h:75
Input::value_type value_type
Standard stream typedef.
Definition: unique.h:92
unique & operator++()
Standard stream method.
Definition: unique.h:53
const value_type * operator->() const
Standard stream method.
Definition: unique.h:69
const value_type & operator*() const
Standard stream method.
Definition: unique.h:63