File: sized_array.hpp

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (292 lines) | stat: -rw-r--r-- 7,169 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//  templated sized array class
//  Copyright (C) 2009 Tim Blechmann
//
//  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; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#ifndef UTILITIES_SIZED_ARRAY_HPP
#define UTILITIES_SIZED_ARRAY_HPP

#include <cassert>
#include <memory>               /* std::allocator */
#include <type_traits>

#include <boost/mpl/if.hpp>

namespace nova {

/** dynamically sized array
 *
 *  array class, inspired by boost.array, where the size is specified
 *  during the time of the construction.
 *  in contrary to std::vector, it is guaranteed to use continuous
 *  memory. the memory is allocated in the constructor an freed in the
 *  destructor, these are the only places, where the allocator
 *  functions are called.
 *
 * */
template<typename T,
         class Alloc = std::allocator<T> >
class sized_array:
    private Alloc::template rebind<T>::other
{
    typedef typename Alloc::template rebind<T>::other Allocator;

public:
    // types
    typedef T                                     value_type;
    typedef T*                                    iterator;
    typedef const T*                              const_iterator;
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    typedef T&                                    reference;
    typedef const T&                              const_reference;
    typedef std::size_t                           size_type;
    typedef std::ptrdiff_t                        difference_type;

    // construct/copy/destruct
    explicit sized_array(size_type size = 0, T const & def = T()):
        data_( size ? Allocator::allocate(size) : 0), size_(size)
    {
        for (size_type i = 0; i != size; ++i)
            Allocator::construct(data_ + i, def);
    }

    sized_array(sized_array & arg) = delete;
    sized_array & operator=(sized_array & arg) = delete;

private:
    template <typename int_type>
    void init_from_int(int_type size)
    {
        data_ = Allocator::allocate(size);
        size_ = size;
        for (size_type i = 0; i != size_; ++i)
            Alloc::construct(data_ + i, T());
    }

    template <typename Container>
    void init_from_container(Container const & container)
    {
        data_ = Allocator::allocate(container.size());
        size_ = container.size();

        size_type index = 0;
        typedef typename Container::const_iterator iterator;
        for (iterator it = container.begin(); it != container.end(); ++it)
            Allocator::construct(data_ + index++, *it);
        assert(index == size());
    }

    struct call_int_ctor
    {
        template <typename int_type>
        static void init(sized_array & array, int_type const & i)
        {
            array.init_from_int<int_type>(i);
        }
    };

    struct call_container_ctor
    {
        template <typename Container>
        static void init(sized_array & array, Container const & c)
        {
            array.init_from_container<Container>(c);
        }
    };

public:
    template<typename Constructor_arg>
    explicit sized_array(Constructor_arg const & arg)
    {
        typedef typename boost::mpl::if_<std::is_integral<Constructor_arg>,
                                         call_int_ctor,
                                         call_container_ctor>::type ctor;
        ctor::init(*this, arg);
    }

    explicit sized_array(sized_array && arg)
    {
        data_ = arg.data_;
        size_ = arg.size();
        arg.data_ = 0;
        arg.size_ = 0;
    }

    /** move assignment */
    sized_array & operator=(sized_array && arg)
    {
        data_ = arg.data_;
        size_ = arg.size();
        arg.data_ = 0;
        arg.size_ = 0;
        return *this;
    }

    ~sized_array(void)
    {
        for (size_type i = 0; i != size(); ++i)
            Allocator::destroy(data_ + i);
        if (size())
            Allocator::deallocate(data_, size());
    }

    // iterator support
    iterator begin()
    {
        return data_;
    }

    const_iterator begin() const
    {
        return data_;
    }

    iterator end()
    {
        return data_ + size();
    }

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

    // reverse iterator support
    reverse_iterator rbegin()
    {
        return reverse_iterator(data_ + size());
    }

    const_reverse_iterator rbegin() const
    {
        return const_reverse_iterator(data_ + size());
    }

    reverse_iterator rend()
    {
        return reverse_iterator(data_);
    }

    const_reverse_iterator rend() const
    {
        return const_reverse_iterator(data_);
    }

    // capacity
    size_type size() const
    {
        return size_;
    }

    bool empty() const
    {
        return size_ == 0;
    }

    size_type max_size() const
    {
        return size_;
    }

    // element access
    reference operator[](size_type i)
    {
        assert(i < size());
        return data_[i];
    }

    const_reference operator[](size_type i) const
    {
        assert(i < size());
        return data_[i];
    }

    reference at(size_type i)
    {
        assert(i < size());
        return data_[i];
    }

    const_reference at(size_type i) const
    {
        assert(i < size());
        return data_[i];
    }

    reference front()
    {
        return data_[0];
    }

    const_reference front() const
    {
        return data_[0];
    }

    reference back()
    {
        return data_[size_ - 1];
    }

    const_reference back() const
    {
        return data_[size_ - 1];
    }

    const T* data() const
    {
        return data_;
    }

    T* c_array()
    {
        return data_;
    }

    // modifiers
    void assign(const T& t)
    {
        for (size_type i = 0; i != size_; ++i)
            data_[i] = t;
    }

    void resize(size_type new_size, T const & t = T())
    {
        T * new_data = Allocator::allocate(new_size);

        for (size_type i = 0; i != new_size; ++i)
            Allocator::construct(new_data+i, t);

        std::copy(data_, data_+std::min(new_size, size_), new_data);

        T * old_data = data_;
        data_ = new_data;
        for (size_type i = 0; i != size_; ++i)
            Allocator::destroy(old_data+i);
        if (size_)
            Allocator::deallocate(old_data, size_);
        size_ = new_size;
    }

private:
    T * data_;
    size_type size_;
};

} /* namespace nova */

#endif /* UTILITIES_SIZED_ARRAY_HPP */