stdgpu/iterator.h Source File

stdgpu/iterator.h Source File#

stdgpu: stdgpu/iterator.h Source File
stdgpu Latest
Efficient STL-like Data Structures on the GPU
iterator.h
Go to the documentation of this file.
1/*
2 * Copyright 2019 Patrick Stotko
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef STDGPU_ITERATOR_H
17#define STDGPU_ITERATOR_H
18
28#include <thrust/detail/pointer.h>
29#include <thrust/detail/reference.h> // Only forward declaration included by thrust/detail/pointer.h
30
31#include <stdgpu/cstddef.h>
32#include <stdgpu/platform.h>
33
34namespace stdgpu
35{
36
42template <typename T>
43using device_ptr = thrust::pointer<T, thrust::device_system_tag>;
44
50template <typename T>
51using host_ptr = thrust::pointer<T, thrust::host_system_tag>;
52
60template <typename T>
62make_device(T* device_array);
63
71template <typename T>
73make_host(T* host_array);
74
83template <typename T>
85size(T* array);
86
94template <>
96size(void* array);
97
105template <typename T>
107host_begin(T* host_array);
108
116template <typename T>
118host_end(T* host_array);
119
127template <typename T>
129device_begin(T* device_array);
130
138template <typename T>
140device_end(T* device_array);
141
149template <typename T>
151host_begin(const T* host_array);
152
160template <typename T>
162host_end(const T* host_array);
163
171template <typename T>
173device_begin(const T* device_array);
174
182template <typename T>
184device_end(const T* device_array);
185
193template <typename T>
195host_cbegin(const T* host_array);
196
204template <typename T>
206host_cend(const T* host_array);
207
215template <typename T>
217device_cbegin(const T* device_array);
218
226template <typename T>
228device_cend(const T* device_array);
229
237template <typename C>
238auto
239host_begin(C& host_container) -> decltype(host_container.host_begin());
240
248template <typename C>
249auto
250host_end(C& host_container) -> decltype(host_container.host_end());
251
259template <typename C>
260auto
261device_begin(C& device_container) -> decltype(device_container.device_begin());
262
270template <typename C>
271auto
272device_end(C& device_container) -> decltype(device_container.device_end());
273
281template <typename C>
282auto
283host_begin(const C& host_container) -> decltype(host_container.host_begin());
284
292template <typename C>
293auto
294host_end(const C& host_container) -> decltype(host_container.host_end());
295
303template <typename C>
304auto
305device_begin(const C& device_container) -> decltype(device_container.device_begin());
306
314template <typename C>
315auto
316device_end(const C& device_container) -> decltype(device_container.device_end());
317
325template <typename C>
326auto
327host_cbegin(const C& host_container) -> decltype(host_begin(host_container));
328
336template <typename C>
337auto
338host_cend(const C& host_container) -> decltype(host_end(host_container));
339
347template <typename C>
348auto
349device_cbegin(const C& device_container) -> decltype(device_begin(device_container));
350
358template <typename C>
359auto
360device_cend(const C& device_container) -> decltype(device_end(device_container));
361
362namespace detail
363{
364
365template <typename Container>
366struct back_insert_iterator_base;
367
368template <typename Container>
369struct front_insert_iterator_base;
370
371template <typename Container>
372struct insert_iterator_base;
373
374} // namespace detail
375
381template <typename Container>
382class back_insert_iterator : public detail::back_insert_iterator_base<Container>::type
383{
384public:
385 using container_type = Container;
388 using super_t = typename detail::back_insert_iterator_base<Container>::type;
389
390 friend class thrust::iterator_core_access;
392
398 explicit back_insert_iterator(Container& c);
399
400private:
401 STDGPU_HOST_DEVICE typename super_t::reference
402 dereference() const;
403
404 Container _c;
405};
406
413template <typename Container>
415back_inserter(Container& c);
416
422template <typename Container>
423class front_insert_iterator : public detail::front_insert_iterator_base<Container>::type
424{
425public:
426 using container_type = Container;
429 using super_t = typename detail::front_insert_iterator_base<Container>::type;
430
431 friend class thrust::iterator_core_access;
433
439 explicit front_insert_iterator(Container& c);
440
441private:
442 STDGPU_HOST_DEVICE typename super_t::reference
443 dereference() const;
444
445 Container _c;
446};
447
454template <typename Container>
456front_inserter(Container& c);
457
466template <typename Container>
467class insert_iterator : public detail::insert_iterator_base<Container>::type
468{
469public:
470 using container_type = Container;
473 using super_t = typename detail::insert_iterator_base<Container>::type;
474
475 friend class thrust::iterator_core_access;
477
483 explicit insert_iterator(Container& c);
484
485private:
486 STDGPU_HOST_DEVICE typename super_t::reference
487 dereference() const;
488
489 Container _c;
490};
491
498template <typename Container>
500inserter(Container& c);
501
502} // namespace stdgpu
503
504#include <stdgpu/impl/iterator_detail.h>
505
506#endif // STDGPU_ITERATOR_H
STDGPU_HOST_DEVICE back_insert_iterator(Container &c)
Constructor.
Container container_type
Definition: iterator.h:385
An output iterator which inserts elements into a container using push_back.
Definition: iterator.h:383
Container container_type
Definition: iterator.h:426
STDGPU_HOST_DEVICE front_insert_iterator(Container &c)
Constructor.
An output iterator which inserts elements into a container using push_front.
Definition: iterator.h:424
STDGPU_HOST_DEVICE insert_iterator(Container &c)
Constructor.
Container container_type
Definition: iterator.h:470
An output iterator which inserts elements into a container using insert.
Definition: iterator.h:468
std::ptrdiff_t index64_t
std::ptrdiff_t
Definition: cstddef.h:48
STDGPU_HOST_DEVICE insert_iterator< Container > inserter(Container &c)
Constructs an insert_iterator.
thrust::pointer< T, thrust::device_system_tag > device_ptr
A host pointer class allowing to call thrust algorithms without explicitly using the respective execu...
Definition: iterator.h:43
host_ptr< const T > host_cend(const T *host_array)
Creates a constant pointer to the end of the given host array.
host_ptr< T > host_begin(T *host_array)
Creates a pointer to the begin of the given host array.
device_ptr< const T > device_cbegin(const T *device_array)
Creates a constant pointer to the begin of the given device array.
device_ptr< const T > device_cend(const T *device_array)
Creates a constant pointer to the end of the given device array.
host_ptr< const T > host_cbegin(const T *host_array)
Creates a constant pointer to the begin of the given host array.
STDGPU_HOST_DEVICE back_insert_iterator< Container > back_inserter(Container &c)
Constructs a back_insert_iterator.
device_ptr< T > device_begin(T *device_array)
Creates a pointer to the begin of the given device array.
STDGPU_HOST_DEVICE device_ptr< T > make_device(T *device_array)
Constructs a device_ptr object.
device_ptr< T > device_end(T *device_array)
Creates a pointer to the end of the given device array.
STDGPU_HOST_DEVICE host_ptr< T > make_host(T *host_array)
Constructs a host_ptr object.
index64_t size(T *array)
Finds the size (number of elements) of the given dynamically allocated array.
host_ptr< T > host_end(T *host_array)
Creates a pointer to the end of the given host array.
STDGPU_HOST_DEVICE front_insert_iterator< Container > front_inserter(Container &c)
Constructs a front_insert_iterator.
thrust::pointer< T, thrust::host_system_tag > host_ptr
A host pointer class allowing to call thrust algorithms without explicitly using the respective execu...
Definition: iterator.h:51
#define STDGPU_HOST_DEVICE
Platform-independent host device function annotation.
Definition: platform.h:77