summaryrefslogtreecommitdiff
path: root/euler/include/std/vector.hpp
blob: 1c35d9dccb2fd47256d4dbb5c26e0afce612a91b (plain) (blame)
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
#pragma once

#include <std/fwd/vector.hpp>

#include <memory>

namespace std {

  template <class T, class Allocator>
  class vector {

  public:
    using size_type = size_t;
    using value_type = T;
    using reference = value_type &;
    using const_reference = const value_type &;

  private:
    Allocator _alloc;
    size_type _capacity;
    size_type _size;
    T *_data;

    constexpr void expand_capacity_to(size_type at_least) {

      size_type new_capacity = _capacity == 0 ? 1 : _capacity;
      while (new_capacity < at_least)
        new_capacity *= 2;
      if (new_capacity == _capacity)
        return;

      T *new_data = _alloc.allocate(new_capacity);
      for (size_type i = 0; i < _size; ++i) {
        std::construct_at(new_data + i, std::move(_data[i]));
        std::destroy_at(_data + i);
      }
      if (_capacity > 0)
        _alloc.deallocate(_data, _capacity);

      _capacity = new_capacity;
      _data = new_data;

    }

  public:
    constexpr vector() noexcept(noexcept(Allocator()))
    : _alloc(), _capacity(0), _size(0), _data(0) {}

    constexpr vector(const vector &other)
    : _alloc(), _capacity(other._size), _size(other._size),
      _data(_alloc.allocate(_capacity)) {
      for (size_type i = 0; i < _size; ++i)
        std::construct_at(_data + i, other._data[i]);
    }

    constexpr vector(vector &&other) noexcept
    : _alloc(std::move(other._alloc)), _capacity(other._capacity),
      _size(other._size), _data(other._data) {
      other._capacity = 0;
      other._size = 0;
    }

    constexpr vector(
      std::initializer_list<T> init, const Allocator &alloc = Allocator())
    : _alloc(alloc), _capacity(init.size()),
      _size(init.size()), _data(_alloc.allocate(_capacity)) {
      for (size_type i = 0; i < _size; ++i)
        std::construct_at(_data + i, init.begin()[i]);
    }

    explicit vector(size_type count)
    : _alloc(), _capacity(count), _size(count),
      _data(_alloc.allocate(_capacity)) {
      for (size_type i = 0; i < _size; ++i)
        std::construct_at(_data + i);
    }

    constexpr ~vector() {
      if (_capacity > 0) {
        for (size_type i = 0; i < _size; ++i)
          std::destroy_at(_data + i);
        _alloc.deallocate(_data, _capacity);
      }
    }

    constexpr vector &operator =(const vector &other) {

      if (&other == this)
        return *this;

      expand_capacity_to(other._size);

      for (size_type i = 0; i < _size; ++i)
        std::destroy_at(_data + i);

      for (size_type i = 0; i < other._size; ++i) {
        std::construct_at(_data + i, std::move(other._data[i]));
        std::destroy_at(other._data + i);
      }

      _size = other._size;
      return *this;

    }

    constexpr vector &operator =(vector &&other)
      noexcept(
        std::allocator_traits<Allocator>::
          propagate_on_container_move_assignment::value ||
        std::allocator_traits<Allocator>::
          is_always_equal::value) {

      if (&other == this)
        return *this;

      if (_capacity > 0) {
        for (size_type i = 0; i < _size; ++i)
          std::destroy_at(_data + i);
        _alloc.deallocate(_data, _capacity);
      }

      _alloc = std::move(other._alloc);
      _capacity = other._capacity;
      _size = other._size;
      _data = other._data;

      other._capacity = 0;
      other._size = 0;

      return *this;

    }

    constexpr size_type size() const noexcept {
      return _size;
    }

    constexpr T *data() noexcept {
      return _data;
    }

    constexpr const T *data() const noexcept {
      return _data;
    }

    constexpr reference operator[](size_type pos) {
      return _data[pos];
    }

    constexpr const_reference operator[](size_type pos) const {
      return _data[pos];
    }

    constexpr reference back() {
      return _data[_size - 1];
    }

    constexpr const_reference back() const {
      return _data[_size - 1];
    }

    constexpr void resize(size_type count) {

      if (count < _size) {
        for (size_type i = count; i < _size; ++i)
          std::destroy_at(_data + i);
        _size = count;
      }

      else if (count > _size) {
        expand_capacity_to(count);
        for (size_type i = _size; i < count; ++i)
          std::construct_at(_data + i);
        _size = count;
      }

    }

    constexpr void push_back(const T &value) {
      expand_capacity_to(_size + 1);
      std::construct_at(_data + _size, value);
      ++_size;
    }

    constexpr void push_back(T &&value) {
      expand_capacity_to(_size + 1);
      std::construct_at(_data + _size, std::move(value));
      ++_size;
    }

  };

}