File: tarray1.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 (193 lines) | stat: -rw-r--r-- 3,725 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
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
#ifndef T_ARRAY1_H
#define T_ARRAY1_H

#include <cassert>
#include <algorithm>

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

    TArray1D() : size_(0), capacity_(0), data_(0)
    {
    }

    TArray1D(size_t size, T value): size_(0), capacity_(0), data_(0)
    {
        resize(size, 0);
        fill(value);
    }

    TArray1D(size_t size) : size_(0), capacity_(0), data_(0)
    {
        resize(size,0);
    }

    TArray1D(const TArray1D<T>& a) : size_(0), capacity_(0), data_(0)
    {
        resize(a.size(),0);
    }
    TArray1D(const T* data, size_t size)  : size_(0), capacity_(0), data_(0)
    {
        resize(size,data);
    }

    TArray1D& operator = (const TArray1D<T>& a)
    {
        resize(a.size(), a.data());
        return *this;
    }

    ~TArray1D()
    {
        delete[] data_;
    }

    inline size_t size() const
    {
        return size_;
    }
    inline size_t capacity() const
    {
        return capacity_;
    }
    inline size_t bytes() const
    {
        return size()*sizeof(value_type);
    }

    void fill(value_type val)
    {
        //std::fill_n(data_, size(), val);
        assert(data_);
        for(unsigned int i=0; i<size(); ++i) data_[i]=val;
    }

    void swap(TArray1D<T>& a)
    {
        std::swap(size_, a.size_);
        std::swap(capacity_, a.capacity_);
        std::swap(data_, a.data_);
    }

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

    inline T& at(size_t i)
    {
        return data_[checkedIndex(i)];
    }

    inline const T* data() const
    {
        return data_;
    }
    inline T* data()
    {
        return data_;
    }
    void resize(size_t width)
    {
        resize(width,0);
    }
    void reserve(size_t cap)
    {
        if(cap<size_) cap=size_;
        if(cap!=capacity_)
        {
            T* newbuf=0;
            capacity_=cap;
            if(capacity_)
            {
                newbuf=new value_type[capacity_];
                std::copy(begin(), end(), newbuf);
                std::swap(data_, newbuf);
                delete[] newbuf;
            }
        }
    }
    inline bool empty()
    {
        return size_==0;
    }
    inline void push_back(const T& value)
    {
        resize(size_+1, &value);
    }
    const T& front() const
    {
        assert(size_);
        return data_[0];
    }
    const T& back() const
    {
        assert(size_);
        return (data_+size_)[0];
    }

private:

    void resize(size_t size, const T* src)
    {
        if(size>=size_)
        {
            if(size>capacity_)
            {
                if(!capacity_) capacity_=size;
                else
                {
                    while(capacity_<size) capacity_+=(capacity_+1) >> 1;
                }

                T* newbuf=new value_type[capacity_];
                std::copy(begin(), end(), newbuf);
                std::swap(data_, newbuf);
                delete[] newbuf;
            }
            if(src) std::copy(src,src+(size-size_),data_+size_);
        }
        size_=size;
    }

    inline size_t checkedIndex(size_t s)
    {
        assert(s<size_);
        return s;
    }

    size_t size_, capacity_;
    T* data_;
};
};

#endif