35 #ifndef __K2VectorBase__ 36 #define __K2VectorBase__ 43 #include "K2Allocator.h" 44 #include "K2Iterator.h" 45 #include "MetaProgramming.h" 46 #include "K2VectorHelpers.h" 52 enum { RET = std::numeric_limits<T>::is_integer};
55 template <
typename T,
typename U>
68 enum { RET = RESULT::RET};
73 template <
class T,
class A>
76 namespace K2Internals {
84 template <
typename Arg>
92 template <
typename Arg>
98 typedef typename RESULT1::RET RET;
102 template <
class T,
class Allocator = K2Allocator<T> >
104 enum { kInitialAllocSize = 8};
105 friend class K2Vector<T, Allocator>;
109 typedef T value_type;
110 typedef value_type* pointer;
111 typedef const value_type* const_pointer;
112 typedef value_type* iterator;
113 typedef const value_type* const_iterator;
114 typedef value_type& reference;
115 typedef const value_type& const_reference;
116 typedef uint32 size_type;
117 typedef std::ptrdiff_t difference_type;
118 typedef Allocator allocator_type;
127 explicit K2VectorBase(
const Allocator& a = Allocator());
128 K2VectorBase(size_type count,
const T& value,
const Allocator& a = Allocator());
136 template <
class InputIterator>
137 K2VectorBase(InputIterator first, InputIterator last,
const Allocator& a)
140 fArray = InitialArrayValue();
142 VectorHelpers::initialize(first, last,
this,
Hole(),
typename CHOOSE_HELPER<InputIterator>::RET());
146 template <
class InputIterator>
148 : fAllocator(Allocator())
150 fArray = InitialArrayValue();
152 VectorHelpers::initialize(first, last,
this,
Hole(),
typename CHOOSE_HELPER<InputIterator>::RET());
156 void initialize(size_type count,
const T& value);
157 void fill_assign(size_type,
const T&);
158 void fill_insert(iterator, size_type,
const T&);
168 {
return v->fAllocator; }
170 {
return v->fLength; }
172 {
return v->fArray; }
173 static inline size_type InitialAllocSize()
182 : fAllocator(rhs.fAllocator),
183 fLength(rhs.fLength),
186 if (rhs.fArray == (pointer)rhs.fPrivateBuffer)
188 memcpy(fPrivateBuffer, rhs.fPrivateBuffer, kPrivateBufferBytes);
189 fArray = (pointer)fPrivateBuffer;
197 void CopyConstructHelper(
const K2VectorBase& rhs);
200 iterator begin() {
return fArray;}
201 const_iterator begin()
const {
return fArray;}
202 iterator end() {
return fArray + fLength;}
203 const_iterator end()
const {
return fArray + fLength;}
204 reverse_iterator rbegin() {
return reverse_iterator(end());}
205 const_reverse_iterator rbegin()
const {
return const_reverse_iterator(end());}
206 reverse_iterator rend() {
return reverse_iterator(begin());}
207 const_reverse_iterator rend()
const {
return const_reverse_iterator(begin());}
209 size_type size()
const {
return fLength;}
210 size_type max_size()
const {
return static_cast<size_type
>(fAllocator.max_size());}
211 size_type capacity()
const {
return fArray == (pointer)fPrivateBuffer ? kPrivateBufferBytes /
sizeof (value_type) : fAllocator.f;}
212 bool16 empty()
const {
return fLength == 0;}
214 allocator_type get_allocator()
const {
return fAllocator;}
215 K2VectorBase& operator=(
const K2VectorBase& rhs)
218 assign(rhs.begin(), rhs.end());
223 K2VectorBase& operator=(K2VectorBase&& rhs) noexcept
228 void reserve(size_type capacity);
230 template <
class InputIter>
231 void assign(InputIter first, InputIter last)
233 VectorHelpers::assign(first, last,
this, Hole(),
typename CHOOSE_HELPER<InputIter>::RET());
235 void assign(size_type count,
const T& u) { fill_assign(count, u); }
238 reference operator[](size_type i) {ASSERT(i < fLength);
return fArray[i];}
239 const_reference operator[](size_type i)
const {ASSERT(i < fLength);
return fArray[i];}
240 const_reference at(size_type i)
const {ASSERT(i < fLength);
return fArray[i];}
241 reference at(size_type i) {ASSERT(i < fLength);
return fArray[i];}
242 reference front() {ASSERT(fLength > 0);
return fArray[0];}
243 const_reference front()
const {ASSERT(fLength > 0);
return fArray[0];}
244 reference back() {ASSERT(fLength > 0);
return fArray[fLength - 1];}
245 const_reference back()
const {ASSERT(fLength > 0);
return fArray[fLength - 1];}
247 void push_back(
const T& x);
249 iterator insert(iterator position,
const T& x);
250 void insert(iterator position, size_type n,
const T& x);
251 template <
class InputIter>
252 void insert(iterator pos, InputIter first, InputIter last)
254 VectorHelpers::insert(pos, first, last,
this, Hole(),
typename CHOOSE_HELPER<InputIter>::RET());
256 iterator erase(iterator position);
257 iterator erase(iterator first, iterator last);
258 void swap(K2VectorBase&) noexcept;
261 void resize(size_type newsize, const T& value);
267 size_type DoGetCapacity(size_type newLength);
268 void DoReset(pointer newData, size_type newLength, size_type newCap);
275 enum {kPrivateBufferBytes = 32};
278 double forAlignmentPurposes;
279 char fPrivateBuffer [kPrivateBufferBytes];
282 pointer InitialArrayValue()
const {
return kPrivateBufferBytes /
sizeof (value_type) ? (pointer)fPrivateBuffer : 0; }
288 template <
typename T>
289 inline bool16 operator==(
const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
291 template <
typename T>
292 inline bool16 operator!=(
const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return !(lhs == rhs);}
294 template <
typename T>
295 inline bool16 operator< (const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());}
297 template <
typename T>
298 inline bool16 operator> (
const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return rhs < lhs;}
300 template <
typename T>
301 inline bool16 operator>=(
const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return !(lhs < rhs);}
303 template <
typename T>
304 inline bool16 operator<=(const K2VectorBase<T>& lhs,
const K2VectorBase<T>& rhs) {
return !(rhs < lhs);}
306 template <
class T,
class Allocator>
308 : fAllocator(rhs.fAllocator, rhs.fLength)
310 CopyConstructHelper(rhs);
313 template <
class T,
class Allocator>
314 inline K2VectorBase<T, Allocator>::K2VectorBase(
const Allocator& a)
317 fArray(InitialArrayValue())
321 template <
class T,
class Allocator>
322 inline K2VectorBase<T, Allocator>::K2VectorBase(size_type n,
const T& value,
const Allocator& a)
325 fArray(InitialArrayValue())
327 initialize(n, value);
332 template <
class T,
class Allocator>
333 inline typename K2VectorBase<T, Allocator>::iterator
334 K2VectorBase<T, Allocator>::insert(iterator position,
const T& x)
336 size_type pos = size_type(position - fArray);
337 insert(position, 1, x);
342 template <
class T,
class Allocator>
344 K2VectorBase<T, Allocator>::insert(iterator position, size_type count,
const T& x)
346 fill_insert(position, count, x);
349 template <
class T,
class Allocator>
350 inline void swap(K2VectorBase<T, Allocator>& x, K2VectorBase<T, Allocator>& y) noexcept
356 #endif // __K2VectorBase__