1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\r
12 template <class T, class Allocator = std::allocator<T> >
\r
16 typedef typename Allocator::reference reference; ///
\r
17 typedef typename Allocator::const_reference const_reference; ///
\r
18 typedef typename Allocator::pointer iterator; ///
\r
19 typedef typename Allocator::const_pointer const_iterator; ///
\r
20 typedef typename Allocator::size_type size_type; ///
\r
21 typedef typename Allocator::difference_type difference_type; ///
\r
22 typedef T value_type; ///
\r
23 typedef Allocator allocator_type; ///
\r
24 typedef typename Allocator::pointer pointer; ///
\r
25 typedef typename Allocator::const_pointer const_pointer; ///
\r
27 typedef std::reverse_iterator<iterator> reverse_iterator; ///
\r
28 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; ///
\r
31 Allocator m_allocator; ///
\r
32 size_type m_size; ///
\r
33 pointer m_buf; /// array buffer
\r
37 explicit Array(const Allocator& i_allocator = Allocator())
\r
38 : m_allocator(i_allocator), m_size(0), m_buf(NULL) { }
\r
41 explicit Array(size_type i_size, const T& i_value = T(),
\r
42 const Allocator& i_allocator = Allocator())
\r
43 : m_allocator(i_allocator), m_size(i_size),
\r
44 m_buf(m_allocator.allocate(m_size, 0)) {
\r
45 std::uninitialized_fill_n(m_buf, i_size, i_value);
\r
49 template <class InputIterator>
\r
50 Array(InputIterator i_begin, InputIterator i_end,
\r
51 const Allocator& i_allocator = Allocator())
\r
52 : m_allocator(i_allocator), m_size(distance(i_begin, i_end)),
\r
53 m_buf(Allocator::allocate(m_size, 0)) {
\r
54 std::uninitialized_copy(i_begin, i_end, m_buf);
\r
57 /// copy constructor
\r
58 Array(const Array& i_o) : m_size(0), m_buf(NULL) {
\r
68 Array& operator=(const Array& i_o) {
\r
71 m_size = i_o.m_size;
\r
72 m_buf = m_allocator.allocate(m_size, 0);
\r
73 std::uninitialized_copy(i_o.m_buf, i_o.m_buf + m_size, m_buf);
\r
78 allocator_type get_allocator() const {
\r
81 /// return pointer to the array buffer
\r
82 typename allocator_type::pointer get() {
\r
85 /// return pointer to the array buffer
\r
86 typename allocator_type::const_pointer get() const {
\r
94 const_iterator begin() const {
\r
99 return m_buf + m_size;
\r
102 const_iterator end() const {
\r
103 return m_buf + m_size;
\r
107 reverse_iterator rbegin() {
\r
108 reverse_iterator(end());
\r
111 const_reverse_iterator rbegin() const {
\r
112 const_reverse_iterator(end());
\r
115 reverse_iterator rend() {
\r
116 reverse_iterator(begin());
\r
119 const_reverse_iterator rend() const {
\r
120 const_reverse_iterator(begin());
\r
124 size_type size() const {
\r
128 size_type max_size() const {
\r
131 /// resize the array buffer. NOTE: the original contents are cleared.
\r
132 void resize(size_type i_size, const T& i_value = T()) {
\r
135 m_buf = m_allocator.allocate(m_size, 0);
\r
136 std::uninitialized_fill_n(m_buf, i_size, i_value);
\r
138 /// resize the array buffer.
\r
139 template <class InputIterator>
\r
140 void resize(InputIterator i_begin, InputIterator i_end) {
\r
142 m_size = distance(i_begin, i_end);
\r
143 m_buf = m_allocator.allocate(m_size, 0);
\r
144 std::uninitialized_copy(i_begin, i_end, m_buf);
\r
146 /// expand the array buffer. the contents of it are copied to the new one
\r
147 void expand(size_type i_size, const T& i_value = T()) {
\r
148 ASSERT( m_size <= i_size );
\r
150 resize(i_size, i_value);
\r
152 pointer buf = m_allocator.allocate(i_size, 0);
\r
153 std::uninitialized_copy(m_buf, m_buf + m_size, buf);
\r
154 std::uninitialized_fill_n(buf + m_size, i_size - m_size, i_value);
\r
161 bool empty() const {
\r
165 reference operator[](size_type i_n) {
\r
166 return *(m_buf + i_n);
\r
169 const_reference operator[](size_type i_n) const {
\r
170 return *(m_buf + i_n);
\r
173 const_reference at(size_type i_n) const {
\r
174 return *(m_buf + i_n);
\r
177 reference at(size_type i_n) {
\r
178 return *(m_buf + i_n);
\r
181 reference front() {
\r
185 const_reference front() const {
\r
190 return *(m_buf + m_size - 1);
\r
193 const_reference back() const {
\r
194 return *(m_buf + m_size - 1);
\r
197 void swap(Array &i_o) {
\r
198 if (&i_o != this) {
\r
199 pointer buf = m_buf;
\r
200 size_type size = m_size;
\r
202 m_size = i_o.m_size;
\r
210 for (size_type i = 0; i < m_size; i ++)
\r
211 m_allocator.destroy(&m_buf[i]);
\r
212 m_allocator.deallocate(m_buf, m_size);
\r