File: Container2D.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 (129 lines) | stat: -rw-r--r-- 5,008 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#pragma once

#include <utility/Exceptions.h>
#include <math/indexers/Indexer2D.h>

#include <vector>

namespace ausaxs::container {
    /**
     * @brief Representation of a dense 2D container. 
     * 
     * This is just a convenience class supporting only indexing.
     */
    template <typename T>
    class Container2D : utility::indexer::Indexer2D<Container2D<T>> {
        friend class utility::indexer::Indexer2D<Container2D<T>>;
        public:
            Container2D() : N(0), M(0), data(0) {}
            Container2D(int width, int height) : N(width), M(height), data(width*height) {}
            Container2D(int width, int height, const T& value) : N(width), M(height), data(width*height, value) {}

            using utility::indexer::Indexer2D<Container2D<T>>::index;
            using utility::indexer::Indexer2D<Container2D<T>>::linear_index;
            T& operator()(int i, int j) {return this->index(i, j);}
            const T& operator()(int i, int j) const {return this->index(i, j);}

            /**
             * @brief Get an iterator to the beginning of the vector at index i.
             */
            const typename std::vector<T>::const_iterator begin(int i) const {
                #if (SAFE_MATH)
                    if (i >= static_cast<int>(N) || i < 0) {
                        throw except::out_of_bounds("Container2D::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::vector<T>::const_iterator end(int i) const {
                #if (SAFE_MATH)
                    if (i >= static_cast<int>(N) || i < 0) {
                        throw except::out_of_bounds("Container2D::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::vector<T>::iterator begin(int i) {
                #if (SAFE_MATH)
                    if (i >= static_cast<int>(N) || i < 0) {
                        throw except::out_of_bounds("Container2D::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::vector<T>::iterator end(int i) {
                #if (SAFE_MATH)
                    if (i >= static_cast<int>(N) || i < 0) {
                        throw except::out_of_bounds("Container2D::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::vector<T>::const_iterator begin() const {return data.begin();}

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

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

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

            /**
             * @brief Get the number of contained x-elements.
             */
            std::size_t size_x() const {return N;}

            /**
             * @brief Get the length of each x-element.
             */
            std::size_t size_y() const {return M;}

            /**
             * @brief Resize the container to contain @a size elements for each x index.
             */
            void resize(int size) {
                Container2D tmp(N, size);
                for (int i = 0; i < static_cast<int>(N); i++) {
                    std::move(begin(i), begin(i)+std::min<int>(size, M), tmp.begin(i));
                }
                M = size;
                data = std::move(tmp.data);                
            }

            /**
             * @brief Check if the container is empty.
             */
            bool empty() const {return data.empty();}

        protected:
            std::size_t N, M;
            std::vector<T> data;
    };
}