source: mainline/uspace/lib/cpp/include/impl/vector.hpp@ b9f897c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b9f897c was 56521a2, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fixed vector::insert to work when no reallocation is needed and made the code much more elegant/readable in insert and emplace

  • Property mode set to 100644
File size: 18.8 KB
RevLine 
[289b2dd]1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_VECTOR
30#define LIBCPP_VECTOR
31
[35584b19]32#include <algorithm>
[289b2dd]33#include <initializer_list>
34#include <iterator>
35#include <memory>
[35584b19]36#include <utility>
[289b2dd]37
38namespace std
39{
40 /**
41 * 23.3.6, vector:
42 */
43
44 template<class T, class Allocator = allocator<T>>
45 class vector
46 {
47 public:
48 using value_type = T;
49 using reference = value_type&;
50 using const_reference = const value_type&;
51 using allocator_type = Allocator;
52 using size_type = size_t;
53 using difference_type = ptrdiff_t;
54 using pointer = typename allocator_traits<Allocator>::pointer;
55 using const_pointer = typename allocator_traits<Allocator>::pointer;
56 using iterator = pointer;
57 using const_iterator = const_pointer;
58 using reverse_iterator = std::reverse_iterator<iterator>;
59 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
60
61 vector() noexcept
62 : vector{Allocator{}}
63 { /* DUMMY BODY */ }
64
[35584b19]65 explicit vector(const Allocator& alloc)
66 : data_{nullptr}, size_{}, capacity_{},
67 allocator_{alloc}
68 { /* DUMMY BODY */ }
69
70 explicit vector(size_type n, const Allocator& alloc = Allocator{})
71 : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
72 {
73 data_ = allocator_.allocate(capacity_);
74
75 for (size_type i = 0; i < size_; ++i)
76 allocator_traits<Allocator>::construct(allocator_, data_ + i);
77 }
[289b2dd]78
[35584b19]79 vector(size_type n, const T& val, const Allocator& alloc = Allocator{})
80 : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
81 {
82 data_ = allocator_.allocate(capacity_);
[289b2dd]83
[35584b19]84 for (size_type i = 0; i < size_; ++i)
85 data_[i] = val;
86 }
[289b2dd]87
88 template<class InputIterator>
89 vector(InputIterator first, InputIterator last,
[35584b19]90 const Allocator& alloc = Allocator{})
91 {
92 // TODO: research constraints and provide multiple
93 // implementations via enable_if
94 }
95
96 vector(const vector& other)
97 : data_{nullptr}, size_{other.size_}, capacity_{other.capacity_},
98 allocator_{other.allocator_}
99 {
100 data_ = allocator_.allocate(capacity_);
101
102 for (size_type i = 0; i < size_; ++i)
103 data_[i] = other.data_[i];
104 }
105
106 vector(vector&& other) noexcept
107 : data_{other.data_}, size_{other.size_}, capacity_{other.capacity_},
108 allocator_{move(other.allocator_)}
109 {
110 // other is guaranteed to be empty()
111 other.size_ = other.capacity_ = 0;
112 other.data_ = nullptr;
113 }
[289b2dd]114
[35584b19]115 vector(const vector& other, const Allocator& alloc)
116 : data_{nullptr}, size_{other.size_}, capacity_{other.capacity_},
117 allocator_{alloc}
118 {
119 data_ = allocator_.allocate(capacity_);
[289b2dd]120
[35584b19]121 for (size_type i = 0; i < size_; ++i)
122 data_[i] = other.data_[i];
123 }
[289b2dd]124
[35584b19]125 vector(initializer_list<T> init, const Allocator& alloc = Allocator{})
126 : data_{nullptr}, size_{init.size()}, capacity_{init.size()},
127 allocator_{alloc}
128 {
129 data_ = allocator_.allocate(capacity_);
[289b2dd]130
[35584b19]131 auto it = init.begin();
132 for (size_type i = 0; it != init.end(); ++i, ++it)
133 {
134 data_[i] = *it;
135 }
136 }
[289b2dd]137
[35584b19]138 ~vector()
139 {
140 allocator_.deallocate(data_, capacity_);
141 }
[289b2dd]142
[35584b19]143 vector& operator=(const vector& other)
144 {
145 vector tmp{other};
146 swap(tmp);
147
148 return *this;
149 }
[289b2dd]150
151 vector& operator=(vector&& other)
152 noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
153 allocator_traits<Allocator>::is_always_equal::value)
154 {
[35584b19]155 // TODO: implement
156 return *this;
157 }
158
159 vector& operator=(initializer_list<T> init)
160 {
161 vector tmp{init, allocator_};
162 swap(tmp);
163
[289b2dd]164 return *this;
165 }
166
[35584b19]167 template<class InputIterator>
168 void assign(InputIterator first, InputIterator last)
169 {
170 vector tmp{first, last};
171 swap(tmp);
172 }
173
174 void assign(size_type size, const T& val)
175 {
176 // Parenthesies required to avoid initializer list
177 // construction.
178 vector tmp(size, val);
179 swap(tmp);
180 }
181
182 void assign(initializer_list<T> init)
183 {
184 vector tmp{init};
185 swap(tmp);
186 }
187
188 allocator_type get_allocator() const noexcept
189 {
190 return allocator_type{};
191 }
192
193 iterator begin() noexcept
194 {
195 return &data_[0];
196 }
197
198 const_iterator begin() const noexcept
199 {
200 return &data_[0];
201 }
202
203 iterator end() noexcept
204 {
205 return begin() + size_;
206 }
207
208 const_iterator end() const noexcept
209 {
210 return begin() + size_;
211 }
212
213 reverse_iterator rbegin() noexcept
214 {
215 return make_reverse_iterator(begin());
216 }
217
218 const_reverse_iterator rbegin() const noexcept
219 {
220 return make_reverse_iterator(cbegin());
221 }
222
223 reverse_iterator rend() noexcept
224 {
225 return make_reverse_iterator(end());
226 }
227
228 const_reverse_iterator rend() const noexcept
229 {
230 return make_reverse_iterator(cend());
231 }
232
233 const_iterator cbegin() const noexcept
234 {
235 return &data_[0];
236 }
237
238 const_iterator cend() const noexcept
239 {
240 return cbegin() + size_;
241 }
242
243 const_reverse_iterator rcbegin() const noexcept
244 {
245 return rbegin();
246 }
247
248 const_reverse_iterator rcend() const noexcept
249 {
250 return rend();
251 }
252
253 size_type size() const noexcept
254 {
255 return size_;
256 }
257
258 size_type max_size() const noexcept
259 {
260 return std::allocator_traits<Allocator>::max_size(allocator_);
261 }
262
263 void resize(size_type sz)
264 {
265 resize_with_copy_(size_, capacity_);
266 }
267
268 void resize(size_type sz, const value_type& val)
269 {
270 auto old_size = size_;
271 resize_with_copy_(sz, capacity_);
272
273 for (size_type i = old_size - 1; i < size_; ++i)
274 data_[i] = val;
275 }
276
277 size_type capacity() const noexcept
278 {
279 return capacity_;
280 }
281
282 bool empty() const noexcept
283 {
284 return size_ == 0;
285 }
286
287 void reserve(size_type new_capacity)
288 {
289 // TODO: if new_capacity > max_size() throw
290 // length_error (this function shall have no
291 // effect in such case)
292 if (new_capacity > capacity_)
293 resize_with_copy_(size_, new_capacity);
294 }
295
296 void shrink_to_fit()
297 {
298 resize_with_copy_(size_, size_);
299 }
300
301 reference operator[](size_type idx)
302 {
303 return data_[idx];
304 }
305
306 const_reference operator[](size_type idx) const
307 {
308 return data_[idx];
309 }
310
311 reference at(size_type idx)
312 {
313 // TODO: bounds checking
314 return data_[idx];
315 }
316
317 const_reference at(size_type idx) const
318 {
319 // TODO: bounds checking
320 return data_[idx];
321 }
322
323 reference front()
324 {
325 /**
326 * Note: Calling front/back on an empty container
327 * is undefined, we opted for at-like
328 * behavior to provide our users with means
329 * to protect their programs from accidental
330 * accesses to an empty vector.
331 */
332 return at(0);
333 }
334
335 const_reference front() const
336 {
337 return at(0);
338 }
339
340 reference back()
341 {
342 return at(size_ - 1);
343 }
344
345 const_reference back() const
346 {
347 return at(size - 1);
348 }
349
350 T* data() noexcept
351 {
352 return data_;
353 }
354
355 const T* data() const noexcept
356 {
357 return data_;
358 }
359
360 template<class... Args>
361 reference emplace_back(Args&&... args)
362 {
363 if (size_ >= capacity_)
364 resize_with_copy_(size_, next_capacity_());
365
366 allocator_traits<Allocator>::construct(begin() + size_, forward<Args>(args)...);
367
368 return back();
369 }
370
371 // TODO: assert CopyInstertable etc with enable_if!
372 void push_back(const T& x)
373 {
374 if (size_ >= capacity_)
375 resize_with_copy_(size_, next_capacity_());
376 data_[size_++] = x;
377 }
378
379 void push_back(T&& x)
380 {
381 if (size_ >= capacity_)
382 resize_with_copy_(size_, next_capacity_());
383 data_[size_++] = forward<T>(x);
384 }
385
386 void pop_back()
387 {
388 destroy_from_end_until_(end() - 1);
389 }
390
391 template<class... Args>
392 iterator emplace(const_iterator position, Args&&... args)
393 {
[56521a2]394 auto pos = const_cast<iterator>(position);
[35584b19]395
[56521a2]396 shift_(pos, 1);
397 allocator_.construct(pos, std::forward<Args>(args)...);
398
399 return pos;
[35584b19]400 }
401
402 iterator insert(const_iterator position, const value_type& x)
403 {
[56521a2]404 auto pos = const_cast<iterator>(position);
[35584b19]405
[56521a2]406 shift_(pos, 1);
407 *pos = x;
[35584b19]408
[56521a2]409 ++size_;
410 return pos;
[35584b19]411 }
412
413 iterator insert(const_iterator position, value_type&& x)
414 {
[56521a2]415 auto pos = const_cast<iterator>(position);
[35584b19]416
[56521a2]417 shift_(pos, 1);
418 *pos = forward<value_type>(x);
[35584b19]419
[56521a2]420 ++size_;
421 return pos;
[35584b19]422 }
423
424 iterator insert(const_iterator position, size_type count, const value_type& x)
425 {
[56521a2]426 auto pos = const_cast<iterator>(position);
[35584b19]427
[56521a2]428 shift_(pos, count);
429 auto copy_target = pos;
430 for (size_type i = 0; i < count; ++i)
431 *copy_target++ = x;
[35584b19]432
[56521a2]433 size_ += count;
434 return pos;
[35584b19]435 }
[289b2dd]436
437 template<class InputIterator>
[35584b19]438 iterator insert(const_iterator position, InputIterator first,
439 InputIterator last)
440 {
[56521a2]441 auto pos = const_cast<iterator>(position);
442 auto count = static_cast<size_type>(last - first);
[35584b19]443
[56521a2]444 shift_(pos, count);
445 std::copy(first, last, pos);
446
447 size_ += count;
448 return pos;
[35584b19]449 }
450
451 iterator insert(const_iterator position, initializer_list<T> init)
452 {
[56521a2]453 auto pos = const_cast<iterator>(position);
[35584b19]454
[56521a2]455 shift_(pos, init.size());
456 std::copy(init.begin(), init.end(), pos);
457
458 size_ += init.size();
459 return pos;
[35584b19]460 }
461
462 iterator erase(const_iterator position)
463 {
464 iterator pos = const_cast<iterator>(position);
465 copy(position + 1, cend(), pos);
466 --size_;
467
468 return pos;
469 }
470
471 iterator erase(const_iterator first, const_iterator last)
472 {
473 iterator pos = const_cast<iterator>(first);
474 copy(last, cend(), pos);
475 size_ -= static_cast<size_type>(last - first);
476
477 return pos;
478 }
479
480 void swap(vector& other)
481 noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
482 allocator_traits<Allocator>::is_always_equal::value)
483 {
484 std::swap(data_, other.data_);
485 std::swap(size_, other.size_);
486 std::swap(capacity_, other.capacity_);
487 }
488
489 void clear() noexcept
490 {
491 // Note: Capacity remains unchanged.
492 destroy_from_end_until_(begin());
493 size_ = 0;
494 }
495
496 private:
497 value_type* data_;
498 size_type size_;
499 size_type capacity_;
500 Allocator allocator_;
501
502 void resize_without_copy_(size_type capacity)
503 {
504 if (data_)
505 allocator_.deallocate(data_, capacity_);
506
507 data_ = allocator_.allocate(capacity);
508 size_ = 0;
509 capacity_ = capacity;
510 }
511
512 void resize_with_copy_(size_type size, size_type capacity)
513 {
514 if (size < size_)
515 destroy_from_end_until_(begin() + size);
516
517 if(capacity_ == 0 || capacity_ < capacity)
518 {
519 auto new_data = allocator_.allocate(capacity);
520
521 auto to_copy = min(size, size_);
522 for (size_type i = 0; i < to_copy; ++i)
523 new_data[i] = move(data_[i]);
524
525 std::swap(data_, new_data);
526
527 allocator_.deallocate(new_data, capacity_);
528 }
529
530 capacity_ = capacity;
531 size_ = size;
532 }
533
534 void destroy_from_end_until_(iterator target)
535 {
536 if (!empty())
537 {
538 auto last = end();
539 while(last != target)
540 allocator_traits<Allocator>::destroy(allocator_, --last);
541 }
542 }
[289b2dd]543
[35584b19]544 size_type next_capacity_(size_type hint = 0) const noexcept
545 {
546 if (hint != 0)
547 return max(capacity_ * 2, hint);
548 else
549 return max(capacity_ * 2, 2ul);
550 }
[289b2dd]551
[56521a2]552 void shift_(iterator position, size_type count)
[35584b19]553 {
[56521a2]554 if (size_ + count < capacity_)
555 std::copy_backwards(pos, end(), end() + count);
556 else
557 {
558 auto start_idx = static_cast<size_type>(position - begin());
559 auto end_idx = start_idx + count;
560 auto new_size = size_ + count;
[35584b19]561
[56521a2]562 // Auxiliary vector for easier swap.
563 vector tmp{};
564 tmp.resize_without_copy_(max(new_size, capacity_));
565 tmp.size_ = new_size;
[35584b19]566
[56521a2]567 // Copy before insertion index.
568 std::copy(tmp.begin(), tmp.begin() + start_idx, begin());
[35584b19]569
[56521a2]570 // Copy after insertion index.
571 std::copy(tmp.begin() + end_idx, tmp.end(), begin() + start_idx);
[35584b19]572
[56521a2]573 swap(tmp);
574 }
[35584b19]575 }
[289b2dd]576 };
[35584b19]577
578 template<class T, class Alloc>
579 bool operator==(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
580 {
581 // TODO: implement
582 return false;
583 }
584
585 template<class T, class Alloc>
586 bool operator<(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
587 {
588 // TODO: implement
589 return false;
590 }
591
592 template<class T, class Alloc>
593 bool operator!=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
594 {
595 return !(lhs == rhs);
596 }
597
598 template<class T, class Alloc>
599 bool operator>(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
600 {
601 // TODO: implement
602 return false;
603 }
604
605 template<class T, class Alloc>
606 bool operator>=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
607 {
608 // TODO: implement
609 return false;
610 }
611
612 template<class T, class Alloc>
613 bool operator<=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
614 {
615 // TODO: implement
616 return false;
617 }
618
619 /**
620 * 23.3.6.6, specialized algorithms:
621 */
622 template<class T, class Alloc>
623 void swap(vector<T, Alloc>& lhs, vector<T, Alloc>& rhs)
624 noexcept(noexcept(lhs.swap(rhs)))
625 {
626 lhs.swap(rhs);
627 }
[289b2dd]628}
629
630#endif
Note: See TracBrowser for help on using the repository browser.