File: tarray2.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 (149 lines) | stat: -rw-r--r-- 2,748 bytes parent folder | download | duplicates (2)
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
#ifndef T_ARRAY2_H
#define T_ARRAY2_H

#include <cassert>
#include <algorithm>

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

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

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

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

    ~TArray2D()
    {
        delete[] data_;
    }

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

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

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

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

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

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

    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) const
    {
        return data_[checkedIndex(i,j)];
    }
    inline T& operator () (size_t i)
    {
        return data_[checkedIndex(i)];
    }
    inline T& operator () (size_t i, size_t j)
    {
        return data_[checkedIndex(i,j)];
    }

    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) const
    {
        size_t s=width_*i+j;
        assert(s<size());
        return s;
    }

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

#endif