Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
counter.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2010, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory
4 // LLNL-CODE-433662
5 // All rights reserved.
6 //
7 // This file is part of Muster. For details, see http://github.com/tgamblin/muster.
8 // Please also read the LICENSE file for further information.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // * Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the disclaimer below.
15 // * Redistributions in binary form must reproduce the above copyright notice, this list of
16 // conditions and the disclaimer (as noted below) in the documentation and/or other materials
17 // provided with the distribution.
18 // * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse
19 // or promote products derived from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
22 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 ///
33 /// @file counter.h
34 /// @author Todd Gamblin tgamblin@llnl.gov
35 /// @brief Dummy output iterator that counts how many times it was assigned to.
36 /// without actually storing anything.
37 ///
38 #ifndef COUNTER_ITERATOR_H
39 #define COUNTER_ITERATOR_H
40 
41 #include <cstdlib>
42 #include <iterator>
43 
44 namespace cluster {
45 
46  ///
47  /// Counting output iterator that records how many times an output iterator
48  /// was assigned to, but ignores the value stored.
49  ///
50  /// This is useful if you just want to know the size of something that an STL
51  /// algorithm would output, without actually allocating space for it.
52  ///
53  /// @note
54  /// Don't use this directly; Instantiate this using the counter() template function
55  /// so that you don't have to supply a type (see sample usage in file docs).
56  ///
57  template <class T>
59  typedef T value_type;
60  typedef T* pointer;
61  typedef T& reference;
62  typedef size_t difference_type;
63  typedef std::output_iterator_tag iterator_category;
64 
65  /// struct representation of a no-op. Makes assignment to target do nothing.
66  struct target {
67  void operator=(T t) { }
68  };
69 
71  counter_iterator(value_type& c) : count(&c) { *count = 0; }
72  counter_iterator(const counter_iterator& other) : count(other.count) { }
73  counter_iterator& operator=(const counter_iterator& other) { count = other.count; return *this; }
74  target operator*() { return target(); }
75  counter_iterator& operator++() { (*count)++; return *this; }
76  counter_iterator operator++(int) { (*count)++; return *this; }
77  };
78 
79  ///
80  /// Adaptor for creating type-inferred counters.
81  ///
82  /// <b>Example Usage:</b>
83  /// @code
84  /// #include <algorithm>
85  ///
86  /// // construct two sets
87  /// set<int> s1, s2;
88  ///
89  /// // insert some things so that their intersection has 2 ints.
90  /// s1.insert(1); s1.insert(2); s1.insert(3);
91  /// s2.insert(2); s2.insert(3); s2.insert(4);
92  ///
93  /// // Compute intersection, but throw away the values and just
94  /// // store the size in the count variable.
95  /// size_t count;
96  /// set_intersection(s1.begin(), s1.end(),
97  /// s2.begin(), s2.end(), counter(count));
98  ///
99  /// // now count == 2, since 2 items were inserted
100  /// // by <code>set_intersection.
101  ///
102  /// @endcode
103  ///
104  template <class T>
106  return counter_iterator<T>(ref);
107  }
108 
109 } // namespace cluster
110 
111 #endif // COUNTER_ITERATOR_H
counter_iterator operator++(int)
Definition: counter.h:76
std::output_iterator_tag iterator_category
Definition: counter.h:63
Counting output iterator that records how many times an output iterator was assigned to...
Definition: counter.h:58
counter_iterator(value_type &c)
Definition: counter.h:71
struct representation of a no-op. Makes assignment to target do nothing.
Definition: counter.h:66
counter_iterator & operator=(const counter_iterator &other)
Definition: counter.h:73
counter_iterator & operator++()
Definition: counter.h:75
counter_iterator< T > counter(T &ref)
Adaptor for creating type-inferred counters.
Definition: counter.h:105
counter_iterator(const counter_iterator &other)
Definition: counter.h:72
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5