summaryrefslogtreecommitdiff
path: root/euler/include/vector
blob: f5083edbd36a9b555d18ffbce0e1286a49088424 (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
#pragma once

#include <stddef.h>
#include <utility>

namespace std {

  template <class t>
  class vector {

    t *buffer;
    size_t buffer_length;//always positive
    size_t count;

  public:
    vector() : buffer(new t[16]), buffer_length(16), count(0) {}

    ~vector() {
      delete[] buffer;
    }

    t &operator[](size_t pos) {
      return buffer[pos];
    }

    const t &operator[](size_t pos) const {
      return buffer[pos];
    }

    t *data() {
      return buffer;
    }

    const t *data() const {
      return buffer;
    }

    size_t size() const {
      return count;
    }

    void reserve(size_t new_length) {
      if (new_length <= buffer_length)
        return;
      t *new_buffer = new t[new_length];
      for (size_t i = 0; i < count; ++i)
        new_buffer[i] = std::move(buffer[i]);
      delete[] buffer;
      buffer = new_buffer;
      buffer_length = new_length;
    }

    void push_back(t &&value) {
      if (count == buffer_length)
        reserve(count * 2);
      buffer[count++] = std::move(value);
    }

    //TODO
  };

}