File: tarray3.h

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (155 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download | duplicates (3)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef T_ARRAY3_H
#define T_ARRAY3_H

#include <cassert>
#include <algorithm>

namespace anl
{
template<typename T>
class TArray3D
{
public:
    typedef T value_type;
    typedef value_type* iterator;
    typedef const value_type* const_iterator;

    TArray3D(size_t width=1, size_t height=1, size_t depth=1) : width_(0), height_(0), depth_(0), data_(0)
    {
        resize(width, height, depth);
    }

    TArray3D(const TArray3D<T>& a)
    {
        resize(a.width(), a.height(), a.depth());
        std::copy(a.begin(), a.end(), data_);
    }

    TArray3D& operator = (const TArray3D<T>& a)
    {
        resize(a.width(), a.height(), a.depth());
        std::copy(a.begin(), a.end(), data_);
        return *this;
    }

    ~TArray3D()
    {
        delete[] data_;
    }

    inline size_t width() const
    {
        return width_;
    }
    inline size_t height() const
    {
        return height_;
    }
    inline size_t depth() const
    {
        return depth_;
    }
    inline size_t size() const
    {
        return width_*height_*depth_;
    }
    inline size_t bytes() const
    {
        return size()*sizeof(value_type);
    }

    void fill(value_type val)
    {
        std::uninitialized_fill_n(data_, size(), val);
    }

    void swap(TArray3D<T>& a)
    {
        std::swap(width_, a.width_);
        std::swap(height_, a.height_);
        std::swap(depth_, a.depth_);
        std::swap(data_, a.data_);
    }

    void resize(size_t width, size_t height, size_t depth)
    {
        size_t nelements=width*height*depth;
        assert(nelements>0);

        if(data_!=0)
        {
            if(width==width_ && height==height_ && depth==depth_) return;
            delete[] data_;
            data_=0;
        }

        data_=new value_type[nelements];
        width_=width;
        height_=height;
        depth_=depth;
    }

    inline const_iterator begin() const
    {
        return data_;
    }
    inline const_iterator end() const
    {
        return data_+size();
    }
    inline iterator begin()
    {
        return data_;
    }
    inline iterator end()
    {
        return data_+size();
    }

    inline const T& operator () (size_t i) const
    {
        return data_[checkedIndex(i)];
    }
    inline const T& operator () (size_t i, size_t j, size_t k) const
    {
        return data_[checkedIndex(i,j,k)];
    }
    inline T& operator () (size_t i)
    {
        return data_[checkedIndex(i)];
    }
    inline T& operator () (size_t i, size_t j, size_t k)
    {
        return data_[checkedIndex(i,j,k)];
    }

    inline const T* c_data() const
    {
        return data_;
    }
    inline T* c_data()
    {
        return data_;
    }

private:

    size_t checkedIndex(size_t i) const
    {
        assert(i<size());
        return i;
    }

    size_t checkedIndex(size_t i, size_t j, size_t k) const
    {
        size_t s=k*width_*depth_+width_*j+i;
        assert(s<size());
        return s;
    }

    size_t width_, height_;
    T* data_;
};
};

#endif