File: ArrayContainer2D.h

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (111 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#pragma once

#include <array>

#if SAFE_MATH
    #include <utility/Exceptions.h>
#endif

namespace ausaxs::container {
    template <typename T, int N, int M>
    class ArrayContainer2D {
        public:
            constexpr ArrayContainer2D() noexcept = default;

            /**
             * @brief Get the value at index i, j, k. 
             */
            constexpr T& index(int i, int j) noexcept {return data[j + M*i];}

            /**
             * @brief Get the value at index i, j, k. 
             */
            constexpr const T& index(int i, int j) const noexcept {return data[j + M*i];}

            /**
             * @brief Get an iterator to the beginning of the vector at index i.
             */
            const typename std::array<T, M>::const_iterator begin(int i) const {
                #if SAFE_MATH
                    if (i >= static_cast<int>(N)) {
                        throw except::out_of_bounds(
                            "ArrayContainer2D::begin: Index out of bounds "
                            "(" + std::to_string(N) + ") <= (" + std::to_string(i) + ")"
                        );
                    }
                #endif
                return data.begin() + i*M;
            }

            /**
             * @brief Get an iterator to the end of the vector at index i.
             */
            const typename std::array<T, M>::const_iterator end(unsigned int i) const {
                #if SAFE_MATH
                    if (i >= static_cast<int>(N)) {
                        throw except::out_of_bounds(
                            "ArrayContainer2D::end: Index out of bounds "
                            "(" + std::to_string(N) + ") <= (" + std::to_string(i) + ")"
                        );
                    }
                #endif
                return data.begin() + i*M + M;
            }

            /**
             * @brief Get an iterator to the beginning of the vector at index i.
             */
            typename std::array<T, M>::iterator begin(unsigned int i) {
                #if SAFE_MATH
                    if (i >= static_cast<int>(N)) {
                        throw except::out_of_bounds(
                            "ArrayContainer2D::begin: Index out of bounds "
                            "(" + std::to_string(N) + ") <= (" + std::to_string(i) + ")"
                        );
                    }
                #endif            
                return data.begin() + i*M;
            }

            /**
             * @brief Get an iterator to the end of the vector at index i.
             */
            typename std::array<T, M>::iterator end(unsigned int i) {
                #if SAFE_MATH
                    if (i >= static_cast<int>(N)) {
                        throw except::out_of_bounds(
                            "ArrayContainer2D::end: Index out of bounds "
                            "(" + std::to_string(N) + ") <= (" + std::to_string(i) + ")"
                        );
                    }
                #endif            
                return data.begin() + i*M + M;
            }

            /**
             * @brief Get an iterator to the beginning of the entire container.
             */
            const typename std::array<T, M>::const_iterator begin() const {return data.begin();}

            /**
             * @brief Get an iterator to the beginning of the entire container.
             */
            const typename std::array<T, M>::const_iterator end() const {return data.end();}

            /**
             * @brief Get an iterator to the beginning of the entire container.
             */
            typename std::array<T, M>::iterator begin() {return data.begin();}

            /**
             * @brief Get an iterator to the beginning of the entire container.
             */
            typename std::array<T, M>::iterator end() {return data.end();}

        protected:
            std::array<T, N*M> data;
    };
}