File: ecl_array2.hh

package info (click to toggle)
enigma 1.20-dfsg.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 64,620 kB
  • ctags: 16,428
  • sloc: xml: 153,614; cpp: 63,581; ansic: 31,088; sh: 4,825; makefile: 1,858; yacc: 288; perl: 84; sed: 16
file content (172 lines) | stat: -rw-r--r-- 5,288 bytes parent folder | download | duplicates (4)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2002,2003 Daniel Heck
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#ifndef ECL_ARRAY2_HH
#define ECL_ARRAY2_HH

/*
** This file implements templated two-dimensional arrays.
*/

#include <memory>

namespace ecl
{
    template <class T, class A = std::allocator<T> >
    struct Array2Base {
        A alloc;                // allocator
        T *first, *last;        // start/end of allocated space

        Array2Base(const A &a, typename A::size_type n)
        : alloc(a), first(alloc.allocate(n)), last(first+n)
        {}

        ~Array2Base() {
            // allocate(0) returns 0 on GCC 2.95 -- standard?
            if (first)
                alloc.deallocate(first, last-first);
        }
        void resize (typename A::size_type n)
        {
            if (first)
                alloc.deallocate(first, last-first);
            first = alloc.allocate(n);
            last = first+n;
        }
    };

    template <class T, class A=std::allocator<T> >
    class Array2 : private Array2Base<T,A> {
        A alloc;
    public:
        typedef T           value_type;
        typedef T*          iterator;
        typedef const T *   const_iterator;
        typedef T &         reference;
        typedef const T &   const_reference;
        typedef typename A::size_type size_type;

        // Construction / Copying
        explicit Array2(int ww=0, int hh=0, const T& val=T(), const A& a=A());
        Array2(const Array2<T,A> &a);
        Array2<T,A> &operator=(Array2<T,A> a2); // call by value!

        // Destructor
        ~Array2() { destroy_elements(); }

        iterator begin() { return this->first; }
        iterator end() { return this->last; }
        const_iterator begin() const { return this->first; }
        const_iterator end() const { return this->last; }
        iterator row_begin(size_type y) { return this->first + y*w; }
        iterator row_end(size_type y) { return this->first + y*w + w; }
        const_iterator row_begin(size_type y) const { return this->first + y*w; }
        const_iterator row_end(size_type y) const { return this->first + y*w + w; }


        void swap(Array2<T,A> &a2);

        size_type width() const { return w; }
        size_type height()const { return h; }


        T&	 get(size_type x, size_type y) { return this->first[y*w+x]; }
        const T& get(size_type x, size_type y) const { return this->first[y*w+x]; }
        T& 	 operator()(size_type x, size_type y) { return get(x,y); }
        const T& operator()(size_type x, size_type y) const { return get(x,y); }

        void 	 set(size_type x, size_type y, const T& val) {
            this->first[y*w+x] = val;
        }

        /*! Fill the array with some value or the default value. */
        void fill (const T& val=T());

        /*! Resize the array in place, but discard any old array
          entries */
        void resize (int w, int h, const T& val=T());

    private:
        void destroy_elements();

        size_type w, h;
    };

    template <class T, class A>
    Array2<T,A>::Array2(int ww, int hh, const T& val, const A& a)
    : Array2Base<T,A>(a, ww*hh), w(ww), h(hh)
    {
        std::uninitialized_fill(this->first, this->last, val);
    }

    template <class T, class A>
    Array2<T,A>::Array2(const Array2<T,A> &a)
    : Array2Base<T,A>(a.alloc, a.last-a.first)
    {
        std::uninitialized_copy(a.begin(), a.end(), this->first);
    }

    template <class T, class A>
    void Array2<T,A>::destroy_elements() {
        for (T* p=this->first; p!=this->last; ++p)
            p->~T();
    }

    template <class T, class A>
    void Array2<T,A>::fill (const T& val)
    {
        destroy_elements();
        std::uninitialized_fill(this->first, this->last, val);
    }

    /*! Resize the array in place, but discard any old array
      entries */
    template <class T, class A>
    void Array2<T,A>::resize (int w_, int h_, const T& val)
    {
        destroy_elements();
        Array2Base<T,A>::resize(w_*h_);
        std::uninitialized_fill(this->first, this->last, val);
        w = w_;
        h = h_;
    }

    template <class T, class A>
    void Array2<T,A>::swap(Array2<T,A> &a2)
    {
        std::swap(this->first, a2.first);
        std::swap(this->last, a2.last);
        std::swap(w, a2.w);
        std::swap(h, a2.h);
    }

    template <class T, class A>
    void swap (Array2<T,A> &a, Array2<T,A> &b)
    {
        a.swap(b);
    }

    template <class T, class A> Array2<T,A>&
    Array2<T,A>::operator= (Array2<T,A> a2)
    {
        ecl::swap(*this, a2);
        return *this;
    }
}

#endif