NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cached_iterator.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
33 #pragma once
34 
35 #include <nvbio/basic/types.h>
36 #include <nvbio/basic/iterator.h>
37 
38 namespace nvbio {
39 
42 
45 
68 template <typename InputStream>
70 {
71  typedef typename std::iterator_traits<InputStream>::value_type value_type;
72  typedef typename std::iterator_traits<InputStream>::reference reference;
73  typedef typename std::iterator_traits<InputStream>::pointer pointer;
74  typedef typename std::iterator_traits<InputStream>::difference_type difference_type;
75  //typedef typename std::iterator_traits<InputStream>::distance_type distance_type;
76  typedef typename std::random_access_iterator_tag iterator_category;
77 
81  : m_cache_idx(uint32(-1)) {}
82 
86  : m_stream( stream ), m_cache_idx(uint32(-1)), m_cache_val(value_type()) {}
87 
91  {
92  if (m_cache_idx != uint32(-1))
93  m_stream[ m_cache_idx ] = m_cache_val;
94  }
95 
100  {
101  if (m_cache_idx == i)
102  return m_cache_val;
103 
104  if (m_cache_idx != uint32(-1))
105  m_stream[ m_cache_idx ] = m_cache_val;
106 
107  m_cache_idx = i;
108  m_cache_val = m_stream[i];
109  return m_cache_val;
110  }
111 
112 private:
113  InputStream m_stream;
114  uint32 m_cache_idx;
115  value_type m_cache_val;
116 };
117 
139 template <typename InputStream>
141 {
142  typedef typename std::iterator_traits<InputStream>::value_type value_type;
143  typedef typename std::iterator_traits<InputStream>::reference reference;
144  typedef typename std::iterator_traits<InputStream>::pointer pointer;
145  typedef typename std::iterator_traits<InputStream>::difference_type difference_type;
146  //typedef typename std::iterator_traits<InputStream>::distance_type distance_type;
147  typedef typename std::random_access_iterator_tag iterator_category;
148 
152 
156  : m_stream( stream ), m_cache_idx(uint32(-1)), m_cache_val(value_type()) {}
157 
162  {
163  if (m_cache_idx == i)
164  return m_cache_val;
165 
166  m_cache_idx = i;
167  m_cache_val = m_stream[i];
168  return m_cache_val;
169  }
170 
174  InputStream stream() const { return m_stream; }
175 
176  InputStream m_stream;
179 };
180 
183 template <typename InputStream>
186 {
187  return cached_iterator<InputStream>( it );
188 }
189 
192 template <typename InputStream>
195 {
196  return cached_iterator<InputStream>( it );
197 }
198 
201 template <typename Stream>
203  const const_cached_iterator<Stream>& it1,
204  const const_cached_iterator<Stream>& it2);
205 
208 template <typename Stream>
210  const const_cached_iterator<Stream>& it1,
211  const const_cached_iterator<Stream>& it2);
212 
215 template <typename Stream>
217  const const_cached_iterator<Stream>& it1,
218  const const_cached_iterator<Stream>& it2);
219 
222 template <typename Stream>
224  const const_cached_iterator<Stream>& it1,
225  const const_cached_iterator<Stream>& it2);
226 
229 template <typename Stream>
231  const const_cached_iterator<Stream>& it1,
232  const const_cached_iterator<Stream>& it2);
233 
236 template <typename Stream>
238  const const_cached_iterator<Stream>& it1,
239  const const_cached_iterator<Stream>& it2);
240 
243 template <typename Stream>
245 const_cached_iterator<Stream>& operator++ (const_cached_iterator<Stream>& it);
246 
249 template <typename Stream>
251 const_cached_iterator<Stream> operator++ (const_cached_iterator<Stream>& it, int dummy);
252 
255 template <typename Stream>
257 const_cached_iterator<Stream>& operator-- (const_cached_iterator<Stream>& it);
258 
261 template <typename Stream>
263 const_cached_iterator<Stream> operator-- (const_cached_iterator<Stream>& it, int dummy);
264 
267 template <typename Stream>
269 const_cached_iterator<Stream>& operator+= (const_cached_iterator<Stream>& it, const typename const_cached_iterator<Stream>::difference_type distance);
270 
273 template <typename Stream>
275 const_cached_iterator<Stream>& operator-= (const_cached_iterator<Stream>& it, const typename const_cached_iterator<Stream>::difference_type distance);
276 
279 template <typename Stream>
281 const_cached_iterator<Stream> operator+ (const const_cached_iterator<Stream> it, const typename const_cached_iterator<Stream>::difference_type distance);
282 
285 template <typename Stream>
287 const_cached_iterator<Stream> operator- (const const_cached_iterator<Stream> it, const typename const_cached_iterator<Stream>::difference_type distance);
288 
291 template <typename Stream>
293 typename const_cached_iterator<Stream>::difference_type operator- (const const_cached_iterator<Stream> it1, const const_cached_iterator<Stream> it2);
294 
297 
298 } // namespace nvbio
299