1 /* ///////////////////////////////////////////////////////////////////////
7 * Brief: The array_base class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_CONTAINER_ARRAY_BASE_H
14 #define EXTL_CONTAINER_ARRAY_BASE_H
17 * \brief array_base class
20 /* ///////////////////////////////////////////////////////////////////////
24 /* ///////////////////////////////////////////////////////////////////////
31 * \param D The derived type
32 * \param B The buffer type
34 * \ingroup extl_group_container
36 template< typename_param_k D
44 typedef array_base class_type
;
45 typedef D derived_type
;
46 typedef B buffer_type
;
47 typedef typename_type_k
buffer_type::allocator_type allocator_type
;
48 typedef typename_type_k
buffer_type::value_type value_type
;
49 typedef typename_type_k
buffer_type::pointer pointer
;
50 typedef typename_type_k
buffer_type::const_pointer const_pointer
;
51 typedef typename_type_k
buffer_type::reference reference
;
52 typedef typename_type_k
buffer_type::const_reference const_reference
;
53 typedef typename_type_k
buffer_type::iterator iterator
;
54 typedef typename_type_k
buffer_type::const_iterator const_iterator
;
55 typedef typename_type_k
buffer_type::reverse_iterator reverse_iterator
;
56 typedef typename_type_k
buffer_type::const_reverse_iterator const_reverse_iterator
;
57 typedef typename_type_k
buffer_type::size_type size_type
;
58 typedef typename_type_k
buffer_type::bool_type bool_type
;
59 typedef typename_type_k
buffer_type::difference_type difference_type
;
60 typedef e_int_t int_type
;
61 typedef size_type index_type
;
65 /// The dimension of the matrix
66 EXTL_STATIC_MEMBER_CONST(size_type
, dimension
= 1);
75 /*!\brief Prohibit the following case:
84 array_base(class_type
const&);
85 class_type
& operator=(class_type
const&);
87 /// \name Constructors
94 explicit_k
array_base(derived_type
const& rhs
)
95 : m_buffer(static_cast<class_type
const&>(rhs
).buffer())
98 array_base(const_pointer ar
, size_type n
)
102 array_base(const_reference value
, size_type n
)
111 allocator_type
allocator() const { return buffer().allocator(); }
112 index_type
dim0() const { return buffer().size(); }
113 size_type
size() const { return buffer().size(); }
114 size_type
capacity() const { return buffer().capacity(); }
115 bool_type
is_empty() const { return buffer().is_empty(); }
116 bool_type
empty() const { return buffer().is_empty(); }
117 size_type
max_size() const { return buffer().max_size(); }
123 pointer
data() { return buffer().data(); }
124 const_pointer
data() const { return buffer().data(); }
126 reference
front() { return buffer().front(); }
127 const_reference
front() const { return buffer().front(); }
129 reference
back() { return buffer().back(); }
130 const_reference
back() const { return buffer().back(); }
132 reference
operator[](index_type index
) { return derive().at_unchecked(index
); }
133 const_reference
operator[](index_type index
) const { return derive().at_unchecked(index
); }
135 reference
at(index_type index
)
137 return const_cast<reference
>(static_cast<derived_type
const&>(*this).at(index
));
139 const_reference
at(index_type index
) const
141 EXTL_ASSERT_THROW(index
< derive().dim0(), index_error("out of range"));
142 return derive().at_unchecked(index
);
145 reference
at_unchecked(index_type index
)
147 return const_cast<reference
>(static_cast<derived_type
const&>(*this).at(index
));
149 const_reference
at_unchecked(index_type index
) const
151 EXTL_ASSERT(NULL
!= derive().data());
152 EXTL_ASSERT(index
< derive().dim0());
153 return derive().data()[index
];
160 derived_type
& operator =(derived_type
const& rhs
)
162 if (this != static_cast<class_type
const*>(&rhs
))
163 buffer().assign(static_cast<class_type
const&>(rhs
).buffer());
166 derived_type
& operator =(const_reference value
)
168 buffer().assign(value
, buffer().size());
172 void push_back(const_reference value
) { buffer().push_back(value
); }
173 void pop_back() { buffer().pop_back(); }
174 void swap(derived_type
& rhs
) { buffer().swap(static_cast<class_type
&>(rhs
).buffer()); }
175 void clear() { buffer().clear(); }
176 void resize(size_type n
) { buffer().resize(n
); }
182 const_iterator
begin() const { return const_iterator(derive().data()); }
183 iterator
begin() { return iterator(derive().data()); }
185 const_iterator
end() const { return const_iterator(derive().data() + derive().size()); }
186 iterator
end() { return iterator(derive().data() + derive().size()); }
188 const_reverse_iterator
rbegin() const { return const_reverse_iterator(derive().end()); }
189 reverse_iterator
rbegin() { return reverse_iterator(derive().end()); }
191 const_reverse_iterator
rend() const { return const_reverse_iterator(derive().begin()); }
192 reverse_iterator
rend() { return reverse_iterator(derive().begin()); }
198 derived_type
& derive() { return static_cast<derived_type
&>(*this); }
199 derived_type
const& derive() const { return static_cast<derived_type
const&>(*this); }
201 buffer_type
& buffer() { return m_buffer
; }
202 buffer_type
const& buffer() const { return m_buffer
; }
206 /* /////////////////////////////////////////////////////////////////////////
209 template< typename_param_k D
212 EXTL_INLINE
void swap(array_base
<D
, B
>& lhs
, array_base
<D
, B
>& rhs
)
214 static_cast<D
&>(lhs
).swap(static_cast<D
&>(rhs
));
216 /* /////////////////////////////////////////////////////////////////////////
219 template< typename_param_k D
222 EXTL_INLINE typename_type_ret_k array_base
<D
, B
>::
223 const_pointer
get_ptr(array_base
<D
, B
> const& ar
)
225 return static_cast<D
const&>(ar
).data();
228 template< typename_param_k D
231 EXTL_INLINE typename_type_ret_k array_base
<D
, B
>::
232 size_type
get_size(array_base
<D
, B
> const& ar
)
234 return static_cast<D
const&>(ar
).size();
237 /* ///////////////////////////////////////////////////////////////////////
242 /* ///////////////////////////////////////////////////////////////////////
245 #if !defined(EXTL_NO_STL) && \
246 !defined(EXTL_NO_NAMESPACE)
247 /* ::std namespace */
248 EXTL_STD_BEGIN_NAMESPACE
250 template< typename_param_k D
253 EXTL_INLINE
void swap(EXTL_NS(array_base
)<D
, B
>& lhs
,
254 EXTL_NS(array_base
)<D
, B
>& rhs
)
256 static_cast<D
&>(lhs
).swap(static_cast<D
&>(rhs
));
258 /* ::std namespace */
259 EXTL_STD_END_NAMESPACE
262 /* //////////////////////////////////////////////////////////////////// */
263 #endif /* EXTL_CONTAINER_ARRAY_BASE_H */
264 /* //////////////////////////////////////////////////////////////////// */