Merge pull request #14827 from YashasSamaga:cuda4dnn-csl-low
[platform/upstream/opencv.git] / modules / dnn / src / cuda / array.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 #ifndef OPENCV_DNN_SRC_CUDA_ARRAY_HPP
6 #define OPENCV_DNN_SRC_CUDA_ARRAY_HPP
7
8 #include <cuda_runtime.h>
9
10 #include "types.hpp"
11
12 #include <cstddef>
13 #include <type_traits>
14 #include <iterator>
15
16 namespace cv { namespace dnn { namespace cuda4dnn { namespace csl { namespace device {
17
18     template <class T, std::size_t N>
19     struct array {
20         using value_type        = T;
21         using size_type         = device::size_type;
22         using difference_type   = std::ptrdiff_t;
23         using reference         = typename std::add_lvalue_reference<value_type>::type;
24         using const_reference   = typename std::add_lvalue_reference<typename std::add_const<value_type>::type>::type;
25         using pointer           = typename std::add_pointer<value_type>::type;
26         using const_pointer     = typename std::add_pointer<typename std::add_const<value_type>::type>::type;
27         using iterator          = pointer;
28         using const_iterator    = const_pointer;
29         using reverse_iterator  = std::reverse_iterator<iterator>;
30         using const_reverse_iterator = std::reverse_iterator<const_iterator>;
31
32         __host__ __device__ bool empty() const noexcept { return N == 0; }
33         __host__ __device__ size_type size() const noexcept { return N; }
34
35         __host__ __device__ iterator begin() noexcept { return ptr; }
36         __host__ __device__ iterator end() noexcept { return ptr + N; }
37         __host__ __device__ const_iterator begin() const noexcept { return ptr; }
38         __host__ __device__ const_iterator end() const noexcept { return ptr + N; }
39
40         __host__ __device__ const_iterator cbegin() const noexcept { return ptr; }
41         __host__ __device__ const_iterator cend() const noexcept { return ptr + N; }
42
43         __host__ __device__ reverse_iterator rbegin() noexcept { return ptr + N; }
44         __host__ __device__ reverse_iterator rend() noexcept { return ptr; }
45         __host__ __device__ const_reverse_iterator rbegin() const noexcept { return ptr + N; }
46         __host__ __device__ const_reverse_iterator rend() const noexcept { return ptr; }
47
48         __host__ __device__ const_reverse_iterator crbegin() const noexcept { return ptr + N; }
49         __host__ __device__ const_reverse_iterator crend() const noexcept { return ptr; }
50
51         template <class InputItr>
52         __host__ void assign(InputItr first, InputItr last) {
53             std::copy(first, last, std::begin(ptr));
54         }
55
56         __host__ __device__ reference operator[](int idx) { return ptr[idx]; }
57         __host__ __device__ const_reference operator[](int idx) const { return ptr[idx]; }
58
59         __host__ __device__ reference front() { return ptr[0]; }
60         __host__ __device__ const_reference front() const { return ptr[0]; }
61
62         __host__ __device__ reference back() { return ptr[N - 1]; }
63         __host__ __device__ const_reference back() const { return ptr[N - 1]; }
64
65         __host__ __device__ pointer data() noexcept { return ptr; }
66         __host__ __device__ const_pointer data() const noexcept { return ptr; }
67
68         T ptr[N];
69     };
70
71 }}}}} /* namespace cv::dnn::cuda4dnn::csl::device */
72
73 #endif /* OPENCV_DNN_SRC_CUDA_ARRAY_HPP */