NEST main@caf0ae8
 
Loading...
Searching...
No Matches
iterator_pair.h
Go to the documentation of this file.
1/*
2 * iterator_pair.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef ITERATOR_PAIR_H_
24#define ITERATOR_PAIR_H_
25
26#include <boost/iterator/iterator_adaptor.hpp>
27#include <boost/tuple/tuple.hpp>
28
29#include "source.h"
30
31namespace boost
32{
33namespace tuples
34{
40template < typename T, typename U >
41inline void
42swap( boost::tuple< T&, U& > a, boost::tuple< T&, U& > b ) noexcept
43{
44 using std::swap;
45 swap( boost::get< 0 >( a ), boost::get< 0 >( b ) );
46 swap( boost::get< 1 >( a ), boost::get< 1 >( b ) );
47}
48
58template < class T1, class T2, class S1, class S2 >
59inline bool
60operator<( const cons< T1, T2 >& lhs, const cons< S1, S2 >& rhs )
61{
62 // check that tuple lengths are equal
63 BOOST_STATIC_ASSERT( length< T2 >::value == length< S2 >::value );
64
65 return lhs.get_head() < rhs.get_head();
66}
67} // namespace tuples
68} // namespace boost
69
75template < class sort_iter_type_, class perm_iter_type_ >
77{
78 using value_type = boost::tuple< typename std::iterator_traits< sort_iter_type_ >::value_type,
79 typename std::iterator_traits< perm_iter_type_ >::value_type >;
80 using ref_type = boost::tuple< typename std::iterator_traits< sort_iter_type_ >::reference,
81 typename std::iterator_traits< perm_iter_type_ >::reference >;
82 using difference_type = typename std::iterator_traits< sort_iter_type_ >::difference_type;
83};
84
91template < typename sort_iter_type_, typename perm_iter_type_ >
92class IteratorPair : public boost::iterator_facade< IteratorPair< sort_iter_type_, perm_iter_type_ >,
93 typename iterator_pair_types< sort_iter_type_, perm_iter_type_ >::value_type,
94 std::random_access_iterator_tag,
95 typename iterator_pair_types< sort_iter_type_, perm_iter_type_ >::ref_type,
96 typename iterator_pair_types< sort_iter_type_, perm_iter_type_ >::difference_type >
97{
98public:
101
102 IteratorPair() = default;
103 IteratorPair( sort_iter_type_, perm_iter_type_ );
104
107 {
108 return *( *this + n );
109 }
110
111private:
113
115 sort_iter_type_ sort_iter_;
117 perm_iter_type_ perm_iter_;
118
119 // The following methods are required as building blocks by
120 // Boost's iterator_facade.
121
125 void increment();
126
130 void decrement();
131
135 bool equal( IteratorPair const& ) const;
136
141
147
152 IteratorPair const& ) const;
153};
154
161template < class sort_iter_type_, class perm_iter_type_ >
163make_iterator_pair( sort_iter_type_ sort_iter, perm_iter_type_ perm_iter )
164{
165 return { sort_iter, perm_iter };
166}
167
173{
174 template < typename T >
175 inline int
176 operator()( boost::tuples::tuple< int&, T& > s, unsigned offset )
177 {
178 return boost::get< 0 >( s ) >> offset;
179 }
180
181 template < typename T >
182 inline size_t
183 operator()( boost::tuples::tuple< nest::Source&, T& > s, unsigned offset )
184 {
185 return boost::get< 0 >( s ).get_node_id() >> offset;
186 }
187};
188
189template < typename sort_iter_type_, typename perm_iter_type_ >
191 perm_iter_type_ perm_iter )
192 : sort_iter_( sort_iter )
193 , perm_iter_( perm_iter )
194{
195}
196
197template < typename sort_iter_type_, typename perm_iter_type_ >
198inline void
200{
201 ++sort_iter_;
202 ++perm_iter_;
203}
204
205template < typename sort_iter_type_, typename perm_iter_type_ >
206inline void
208{
209 --sort_iter_;
210 --perm_iter_;
211}
212
213template < typename sort_iter_type_, typename perm_iter_type_ >
214inline bool
216{
217 return ( sort_iter_ == other.sort_iter_ );
218}
219
220template < typename sort_iter_type_, typename perm_iter_type_ >
221inline void
228
229template < typename sort_iter_type_, typename perm_iter_type_ >
235
236template < typename sort_iter_type_, typename perm_iter_type_ >
239{
240 return ( other.sort_iter_ - sort_iter_ );
241}
242
243#endif
A combinator of two iterators that can be used to apply permutations on a second container when sorti...
Definition iterator_pair.h:97
void decrement()
Retreat by one position.
Definition iterator_pair.h:207
sort_iter_type_ sort_iter_
Iterator of the container being sorted.
Definition iterator_pair.h:115
perm_iter_type_ perm_iter_
Iterator of the container being permuted.
Definition iterator_pair.h:117
void advance(typename iterator_pair_types< sort_iter_type_, perm_iter_type_ >::difference_type)
Advance by a number of positions.
Definition iterator_pair.h:222
iterator_pair_types< sort_iter_type_, perm_iter_type_ >::difference_type distance_to(IteratorPair const &) const
Measure the distance to another iterator.
Definition iterator_pair.h:238
iterator_pair_types< sort_iter_type_, perm_iter_type_ >::difference_type DifferenceType
Definition iterator_pair.h:100
iterator_pair_types< sort_iter_type_, perm_iter_type_ >::ref_type dereference() const
Access the value referred to.
Definition iterator_pair.h:231
bool equal(IteratorPair const &) const
Compare for equality.
Definition iterator_pair.h:215
ReferenceType operator[](DifferenceType n) const
Definition iterator_pair.h:106
iterator_pair_types< sort_iter_type_, perm_iter_type_ >::ref_type ReferenceType
Definition iterator_pair.h:99
friend class boost::iterator_core_access
Definition iterator_pair.h:112
void increment()
Advance by one position.
Definition iterator_pair.h:199
IteratorPair()=default
IteratorPair< sort_iter_type_, perm_iter_type_ > make_iterator_pair(sort_iter_type_ sort_iter, perm_iter_type_ perm_iter)
Creates an IteratorPair object, deducing iterator types from the types of arguments.
Definition iterator_pair.h:163
bool operator<(const cons< T1, T2 > &lhs, const cons< S1, S2 > &rhs)
Less than relational operator for Boost tuples.
Definition iterator_pair.h:60
void swap(boost::tuple< T &, U & > a, boost::tuple< T &, U & > b) noexcept
Exchanges values of two two-element tuples.
Definition iterator_pair.h:42
Definition iterator_pair.h:32
Helper class holding type definitions.
Definition iterator_pair.h:77
typename std::iterator_traits< sort_iter_type_ >::difference_type difference_type
Definition iterator_pair.h:82
boost::tuple< typename std::iterator_traits< sort_iter_type_ >::reference, typename std::iterator_traits< perm_iter_type_ >::reference > ref_type
Definition iterator_pair.h:81
boost::tuple< typename std::iterator_traits< sort_iter_type_ >::value_type, typename std::iterator_traits< perm_iter_type_ >::value_type > value_type
Definition iterator_pair.h:79
A rightshift functor for tuples to be used with Boost's sorting function.
Definition iterator_pair.h:173
int operator()(boost::tuples::tuple< int &, T & > s, unsigned offset)
Definition iterator_pair.h:176
size_t operator()(boost::tuples::tuple< nest::Source &, T & > s, unsigned offset)
Definition iterator_pair.h:183