~ubuntu-branches/ubuntu/intrepid/enigma/intrepid

« back to all changes in this revision

Viewing changes to src/px/array2.hh

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2005-08-28 15:30:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050828153009-sky64kb6tcq37xt5
Tags: 0.92.1-1
* New upstream subversion checkout
* Remove menu.s3m, which we are allowed to distributed but not to modify
  also copyright notice is confusing... (Closes: #321669)
* Rebuild with new libzipios (Closes: #325405)
  I hope this works without a versioned build-dependency
* Added "enigma replaces enigma-data" for upgrades (Closes: #308558)
* Added notes about the fonts copyright.
* updated to policy 3.6.2.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002,2003 Daniel Heck
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
16
 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
 
 *
18
 
 * $Id: array2.hh,v 1.6 2004/04/24 11:46:01 dheck Exp $
19
 
 */
20
 
#ifndef PX_ARRAY2_HH
21
 
#define PX_ARRAY2_HH
22
 
 
23
 
/*
24
 
** This file implements templated two-dimensional arrays.
25
 
*/
26
 
 
27
 
#include <memory>
28
 
 
29
 
namespace px
30
 
{
31
 
    template <class T, class A = std::allocator<T> >
32
 
    struct Array2Base {
33
 
        A alloc;                // allocator
34
 
        T *first, *last;        // start/end of allocated space
35
 
 
36
 
        Array2Base(const A &a, typename A::size_type n)
37
 
        : alloc(a), first(alloc.allocate(n)), last(first+n)
38
 
        {}
39
 
 
40
 
        ~Array2Base() {
41
 
            // allocate(0) returns 0 on GCC 2.95 -- standard?
42
 
            if (first)
43
 
                alloc.deallocate(first, last-first);
44
 
        }
45
 
        void resize (typename A::size_type n)
46
 
        {
47
 
            if (first)
48
 
                alloc.deallocate(first, last-first);
49
 
            first = alloc.allocate(n);
50
 
            last = first+n;
51
 
        }
52
 
    };
53
 
 
54
 
    template <class T, class A=std::allocator<T> >
55
 
    class Array2 : private Array2Base<T,A> {
56
 
        A alloc;
57
 
    public:
58
 
        typedef T           value_type;
59
 
        typedef T*          iterator;
60
 
        typedef const T *   const_iterator;
61
 
        typedef T &         reference;
62
 
        typedef const T &   const_reference;
63
 
        typedef typename A::size_type size_type;
64
 
 
65
 
        // Construction / Copying
66
 
        explicit Array2(int ww=0, int hh=0, const T& val=T(), const A& a=A());
67
 
        Array2(const Array2<T,A> &a);
68
 
        Array2<T,A> &operator=(Array2<T,A> a2); // call by value!
69
 
 
70
 
        // Destructor
71
 
        ~Array2() { destroy_elements(); }
72
 
 
73
 
        iterator begin() { return this->first; }
74
 
        iterator end() { return this->last; }
75
 
        const_iterator begin() const { return this->first; }
76
 
        const_iterator end() const { return this->last; }
77
 
        iterator row_begin(size_type y) { return this->first + y*w; }
78
 
        iterator row_end(size_type y) { return this->first + y*w + w; }
79
 
        const_iterator row_begin(size_type y) const { return this->first + y*w; }
80
 
        const_iterator row_end(size_type y) const { return this->first + y*w + w; }
81
 
 
82
 
 
83
 
        void swap(Array2<T,A> &a2);
84
 
 
85
 
        size_type width() const { return w; }
86
 
        size_type height()const { return h; }
87
 
 
88
 
 
89
 
        T&       get(size_type x, size_type y) { return this->first[y*w+x]; }
90
 
        const T& get(size_type x, size_type y) const { return this->first[y*w+x]; }
91
 
        T&       operator()(size_type x, size_type y) { return get(x,y); }
92
 
        const T& operator()(size_type x, size_type y) const { return get(x,y); }
93
 
 
94
 
        void     set(size_type x, size_type y, const T& val) {
95
 
            this->first[y*w+x] = val;
96
 
        }
97
 
 
98
 
        /*! Fill the array with some value or the default value. */
99
 
        void fill (const T& val=T());
100
 
 
101
 
        /*! Resize the array in place, but discard any old array
102
 
          entries */
103
 
        void resize (int w, int h, const T& val=T());
104
 
 
105
 
    private:
106
 
        void destroy_elements();
107
 
 
108
 
        size_type w, h;
109
 
    };
110
 
 
111
 
    template <class T, class A>
112
 
    Array2<T,A>::Array2(int ww, int hh, const T& val, const A& a)
113
 
    : Array2Base<T,A>(a, ww*hh), w(ww), h(hh)
114
 
    {
115
 
        std::uninitialized_fill(this->first, this->last, val);
116
 
    }
117
 
 
118
 
    template <class T, class A>
119
 
    Array2<T,A>::Array2(const Array2<T,A> &a)
120
 
    : Array2Base<T,A>(a.alloc, a.last-a.first)
121
 
    {
122
 
        std::uninitialized_copy(a.begin(), a.end(), this->first);
123
 
    }
124
 
 
125
 
    template <class T, class A>
126
 
    void Array2<T,A>::destroy_elements() {
127
 
        for (T* p=this->first; p!=this->last; ++p)
128
 
            p->~T();
129
 
    }
130
 
 
131
 
    template <class T, class A>
132
 
    void Array2<T,A>::fill (const T& val)
133
 
    {
134
 
        destroy_elements();
135
 
        std::uninitialized_fill(this->first, this->last, val);
136
 
    }
137
 
 
138
 
    /*! Resize the array in place, but discard any old array
139
 
      entries */
140
 
    template <class T, class A>
141
 
    void Array2<T,A>::resize (int w_, int h_, const T& val)
142
 
    {
143
 
        destroy_elements();
144
 
        Array2Base<T,A>::resize(w_*h_);
145
 
        std::uninitialized_fill(this->first, this->last, val);
146
 
        w = w_;
147
 
        h = h_;
148
 
    }
149
 
 
150
 
    template <class T, class A>
151
 
    void Array2<T,A>::swap(Array2<T,A> &a2)
152
 
    {
153
 
        std::swap(this->first, a2.first);
154
 
        std::swap(this->last, a2.last);
155
 
        std::swap(w, a2.w);
156
 
        std::swap(h, a2.h);
157
 
    }
158
 
 
159
 
    template <class T, class A>
160
 
    void swap (Array2<T,A> &a, Array2<T,A> &b)
161
 
    {
162
 
        a.swap(b);
163
 
    }
164
 
 
165
 
    template <class T, class A> Array2<T,A>&
166
 
    Array2<T,A>::operator= (Array2<T,A> a2)
167
 
    {
168
 
        px::swap(*this, a2);
169
 
        return *this;
170
 
    }
171
 
}
172
 
 
173
 
#endif