fork(1) download
  1. #ifndef VECTOR_H_
  2. #define VECTOR_H_
  3.  
  4. #include <memory>
  5. #include <stdexcept>
  6. #include <type_traits>
  7.  
  8. namespace lab_07 {
  9. template <typename T, typename Alloc = std::allocator<T>>
  10. class vector {
  11. static_assert(std::is_nothrow_move_constructible_v<T>);
  12. static_assert(std::is_nothrow_move_assignable_v<T>);
  13. static_assert(std::is_nothrow_destructible_v<T>);
  14. private:
  15. std::size_t Capacity;
  16. std::size_t Size;
  17. std::unique_ptr<T[]> Data;
  18. public:
  19. vector() : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {}
  20.  
  21. explicit vector(std::size_t n) {
  22. Capacity = 1;
  23. while (Capacity < n) {
  24. Capacity *= 2;
  25. }
  26. Size = n;
  27. Data = std::unique_ptr<T[]>{Alloc().allocate(Capacity)};
  28. }
  29.  
  30. vector(std::size_t n, const T & value) : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
  31. for (std::size_t i = 0; i < n; i++) {
  32. push_back(value);
  33. }
  34. }
  35.  
  36. vector (const vector & other) : Capacity(other.Capacity), Size(other.Size), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
  37. for (std::size_t i = 0; i < Size; i++) {
  38. Data[i] = other.Data[i];
  39. }
  40. }
  41.  
  42. vector (vector &&) noexcept = default;
  43.  
  44. vector & operator=(const vector & other) {
  45. Capacity = other.Capacity;
  46. Size = other.Size;
  47. Data.reset(Alloc().allocate(Capacity));
  48. for (std::size_t i = 0; i < Size; i++) {
  49. Data[i] = other.Data[i];
  50. }
  51. return *this;
  52. }
  53.  
  54. vector & operator=(vector &&) noexcept = default;
  55.  
  56. [[nodiscard]] bool empty() const {
  57. return Size == 0;
  58. }
  59.  
  60. [[nodiscard]] std::size_t size() const {
  61. return Size;
  62. }
  63.  
  64. [[nodiscard]] std::size_t capacity() const {
  65. return Capacity;
  66. }
  67.  
  68. void push_back(const T & value) {
  69. if constexpr (std::is_move_assignable<T>::value) {
  70. Data[Size] = std::move(value);
  71. } else {
  72. Data[Size] = value;
  73. }
  74. Size++;
  75. if (Size == Capacity) {
  76. Capacity *= 2;
  77. std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
  78. for (std::size_t i = 0; i < Size; i++) {
  79. if constexpr (std::is_move_assignable<T>::value) {
  80. NewData[i] = std::move(Data[i]);
  81. } else {
  82. NewData[i] = Data[i];
  83. }
  84. }
  85. Data = std::move(NewData);
  86. }
  87. }
  88.  
  89. void push_back(T && value) {
  90. Data[Size] = std::move(value);
  91. Size++;
  92. if (Size == Capacity) {
  93. Capacity *= 2;
  94. std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
  95. for (std::size_t i = 0; i < Size; i++) {
  96. NewData[i] = std::move(Data[i]);
  97. }
  98. Data = std::move(NewData);
  99. }
  100. }
  101.  
  102. void pop_back() {
  103. Size--;
  104. }
  105.  
  106. void clear() {
  107. Size = 0;
  108. Capacity = 1;
  109. Data.reset(Alloc().allocate(Capacity));
  110. }
  111.  
  112. void resize(std::size_t k) {
  113. Size = k;
  114. if (Size >= Capacity) {
  115. while (Size >= Capacity) {
  116. Capacity *= 2;
  117. }
  118. std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
  119. for (std::size_t i = 0; i < Size; i++) {
  120. if constexpr (std::is_move_assignable<T>::value) {
  121. NewData[i] = std::move(Data[i]);
  122. } else {
  123. NewData[i] = Data[i];
  124. }
  125. }
  126. Data = std::move(NewData);
  127. }
  128. }
  129.  
  130. void resize(std::size_t k, const T & value) {
  131. std::size_t OldSize = Size;
  132. Size = k;
  133. if (Size >= Capacity) {
  134. while (Size >= Capacity) {
  135. Capacity *= 2;
  136. }
  137. std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
  138. for (std::size_t i = 0; i < Size; i++) {
  139. if constexpr (std::is_move_assignable<T>::value) {
  140. NewData[i] = std::move(Data[i]);
  141. } else {
  142. NewData[i] = Data[i];
  143. }
  144. }
  145. Data = std::move(NewData);
  146. }
  147. for (std::size_t i = OldSize; i < Size; i++) {
  148. Data[i] = value;
  149. }
  150. }
  151.  
  152. void reserve(std::size_t k) {
  153. resize(k);
  154. }
  155.  
  156. T & at(std::size_t k) {
  157. if (k >= Size) {
  158. throw std::out_of_range(":) KAKAR-TO XYNHR (:");
  159. }
  160. return Data[k];
  161. }
  162.  
  163. const T & at(std::size_t k) const {
  164. if (k >= Size) {
  165. throw std::out_of_range(":) KAKAR-TO XYNHR (:");
  166. }
  167. return Data[k];
  168. }
  169.  
  170. T & operator[](std::size_t k) {
  171. return Data[k];
  172. }
  173.  
  174. const T & operator[](std::size_t k) const {
  175. return Data[k];
  176. }
  177.  
  178. ~vector() = default;
  179. };
  180.  
  181. } // namespace lab_07
  182.  
  183. #endif // VECTOR_H_
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. struct MinimalObj {
  193. int id; // NOLINT(misc-non-private-member-variables-in-classes)
  194.  
  195. // Draft check for leaks, double-frees and non-inits.
  196. // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
  197. std::string data = std::string(500U, 'x');
  198.  
  199. explicit MinimalObj(int id_) : id(id_) {}
  200. MinimalObj(MinimalObj &&) = default;
  201. MinimalObj &operator=(MinimalObj &&) = default;
  202.  
  203. MinimalObj(const MinimalObj &) = delete;
  204. MinimalObj &operator=(const MinimalObj &) = delete;
  205.  
  206. ~MinimalObj() = default;
  207. };
  208.  
  209. signed main() {
  210. lab_07::vector<MinimalObj> v;
  211. MinimalObj a{5};
  212. v.push_back(a);
  213. v.pop_back();
  214. if (v.size() == 4) {}
  215. if (v.empty()) {}
  216. return 0;
  217. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: illegal character: '#'
#ifndef VECTOR_H_
^
Main.java:2: error: illegal character: '#'
#define VECTOR_H_
^
Main.java:4: error: illegal character: '#'
#include <memory>
^
Main.java:5: error: illegal character: '#'
#include <stdexcept>
^
Main.java:6: error: illegal character: '#'
#include <type_traits>
^
Main.java:11: error: invalid method declaration; return type required
        static_assert(std::is_nothrow_move_constructible_v<T>);
        ^
Main.java:11: error: <identifier> expected
        static_assert(std::is_nothrow_move_constructible_v<T>);
                         ^
Main.java:11: error: illegal start of type
        static_assert(std::is_nothrow_move_constructible_v<T>);
                                                             ^
Main.java:12: error: invalid method declaration; return type required
        static_assert(std::is_nothrow_move_assignable_v<T>);
        ^
Main.java:12: error: <identifier> expected
        static_assert(std::is_nothrow_move_assignable_v<T>);
                         ^
Main.java:12: error: illegal start of type
        static_assert(std::is_nothrow_move_assignable_v<T>);
                                                          ^
Main.java:13: error: invalid method declaration; return type required
        static_assert(std::is_nothrow_destructible_v<T>);
        ^
Main.java:13: error: <identifier> expected
        static_assert(std::is_nothrow_destructible_v<T>);
                         ^
Main.java:13: error: illegal start of type
        static_assert(std::is_nothrow_destructible_v<T>);
                                                       ^
Main.java:14: error: illegal start of type
    private:
           ^
Main.java:15: error: <identifier> expected
        std::size_t Capacity;
           ^
Main.java:16: error: <identifier> expected
        std::size_t Size;
           ^
Main.java:17: error: <identifier> expected
        std::unique_ptr<T[]> Data;
           ^
Main.java:18: error: illegal start of type
    public:
          ^
Main.java:19: error: ';' expected
        vector() : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {}
                ^
Main.java:19: error: > expected
        vector() : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {}
                                                               ^
Main.java:19: error: ';' expected
        vector() : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {}
                                                                                             ^
Main.java:19: error: illegal start of type
        vector() : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {}
                                                                                              ^
Main.java:21: error: <identifier> expected
        explicit vector(std::size_t n) {
                           ^
Main.java:27: error: '.class' expected
            Data = std::unique_ptr<T[]>{Alloc().allocate(Capacity)};
                                      ^
Main.java:27: error: illegal start of expression
            Data = std::unique_ptr<T[]>{Alloc().allocate(Capacity)};
                                       ^
Main.java:27: error: ';' expected
            Data = std::unique_ptr<T[]>{Alloc().allocate(Capacity)};
                                                                  ^
Main.java:30: error: <identifier> expected
        vector(std::size_t n, const T & value) : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                  ^
Main.java:30: error: > expected
        vector(std::size_t n, const T & value) : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                             ^
Main.java:30: error: ';' expected
        vector(std::size_t n, const T & value) : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                                                           ^
Main.java:30: error: illegal start of type
        vector(std::size_t n, const T & value) : Capacity(1), Size(0), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                                                            ^
Main.java:31: error: not a statement
            for (std::size_t i = 0; i < n; i++) {
                 ^
Main.java:31: error: ';' expected
            for (std::size_t i = 0; i < n; i++) {
                            ^
Main.java:31: error: not a statement
            for (std::size_t i = 0; i < n; i++) {
                                      ^
Main.java:31: error: ')' expected
            for (std::size_t i = 0; i < n; i++) {
                                         ^
Main.java:31: error: ';' expected
            for (std::size_t i = 0; i < n; i++) {
                                              ^
Main.java:36: error: illegal start of type
        vector (const vector & other) : Capacity(other.Capacity), Size(other.Size), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                ^
Main.java:36: error: > expected
        vector (const vector & other) : Capacity(other.Capacity), Size(other.Size), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                                          ^
Main.java:36: error: ';' expected
        vector (const vector & other) : Capacity(other.Capacity), Size(other.Size), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                                                                        ^
Main.java:36: error: illegal start of type
        vector (const vector & other) : Capacity(other.Capacity), Size(other.Size), Data(std::unique_ptr<T[]>{Alloc().allocate(Capacity)}) {
                                                                                                                                         ^
Main.java:37: error: not a statement
            for (std::size_t i = 0; i < Size; i++) {
                 ^
Main.java:37: error: ';' expected
            for (std::size_t i = 0; i < Size; i++) {
                            ^
Main.java:37: error: not a statement
            for (std::size_t i = 0; i < Size; i++) {
                                      ^
Main.java:37: error: ')' expected
            for (std::size_t i = 0; i < Size; i++) {
                                            ^
Main.java:37: error: ';' expected
            for (std::size_t i = 0; i < Size; i++) {
                                                 ^
Main.java:42: error: <identifier> expected
        vector (vector &&) noexcept = default;
                      ^
Main.java:44: error: <identifier> expected
        vector & operator=(const vector & other) {
              ^
Main.java:44: error: <identifier> expected
        vector & operator=(const vector & other) {
                         ^
Main.java:44: error: illegal start of expression
        vector & operator=(const vector & other) {
                           ^
Main.java:44: error: <identifier> expected
        vector & operator=(const vector & other) {
                                       ^
Main.java:44: error: <identifier> expected
        vector & operator=(const vector & other) {
                                               ^
Main.java:48: error: not a statement
            for (std::size_t i = 0; i < Size; i++) {
                 ^
Main.java:48: error: ';' expected
            for (std::size_t i = 0; i < Size; i++) {
                            ^
Main.java:48: error: not a statement
            for (std::size_t i = 0; i < Size; i++) {
                                      ^
Main.java:48: error: ')' expected
            for (std::size_t i = 0; i < Size; i++) {
                                            ^
Main.java:48: error: ';' expected
            for (std::size_t i = 0; i < Size; i++) {
                                                 ^
Main.java:51: error: illegal start of expression
            return *this;
                   ^
Main.java:54: error: <identifier> expected
        vector & operator=(vector &&) noexcept = default;
              ^
Main.java:54: error: <identifier> expected
        vector & operator=(vector &&) noexcept = default;
                         ^
Main.java:54: error: illegal start of expression
        vector & operator=(vector &&) noexcept = default;
                                    ^
Main.java:54: error: ';' expected
        vector & operator=(vector &&) noexcept = default;
                                     ^
Main.java:54: error: <identifier> expected
        vector & operator=(vector &&) noexcept = default;
                                              ^
Main.java:54: error: illegal start of expression
        vector & operator=(vector &&) noexcept = default;
                                                 ^
Main.java:56: error: illegal start of type
        [[nodiscard]] bool empty() const {
        ^
Main.java:56: error: ']' expected
        [[nodiscard]] bool empty() const {
         ^
Main.java:56: error: ']' expected
        [[nodiscard]] bool empty() const {
          ^
Main.java:56: error: <identifier> expected
        [[nodiscard]] bool empty() const {
                   ^
Main.java:56: error: ';' expected
        [[nodiscard]] bool empty() const {
                                  ^
Main.java:60: error: illegal start of type
        [[nodiscard]] std::size_t size() const {
        ^
Main.java:60: error: ']' expected
        [[nodiscard]] std::size_t size() const {
         ^
Main.java:60: error: ']' expected
        [[nodiscard]] std::size_t size() const {
          ^
Main.java:60: error: <identifier> expected
        [[nodiscard]] std::size_t size() const {
                   ^
Main.java:60: error: <identifier> expected
        [[nodiscard]] std::size_t size() const {
                         ^
Main.java:60: error: ';' expected
        [[nodiscard]] std::size_t size() const {
                                        ^
Main.java:64: error: illegal start of type
        [[nodiscard]] std::size_t capacity() const {
        ^
Main.java:64: error: ']' expected
        [[nodiscard]] std::size_t capacity() const {
         ^
Main.java:64: error: ']' expected
        [[nodiscard]] std::size_t capacity() const {
          ^
Main.java:64: error: <identifier> expected
        [[nodiscard]] std::size_t capacity() const {
                   ^
Main.java:64: error: <identifier> expected
        [[nodiscard]] std::size_t capacity() const {
                         ^
Main.java:64: error: ';' expected
        [[nodiscard]] std::size_t capacity() const {
                                            ^
Main.java:68: error: illegal start of type
        void push_back(const T & value) {
                       ^
Main.java:69: error: '(' expected
            if constexpr (std::is_move_assignable<T>::value) {
              ^
Main.java:69: error: illegal start of expression
            if constexpr (std::is_move_assignable<T>::value) {
                                                    ^
Main.java:70: error: ';' expected
                Data[Size] = std::move(value);
                                      ^
Main.java:70: error: not a statement
                Data[Size] = std::move(value);
                                       ^
Main.java:70: error: ';' expected
                Data[Size] = std::move(value);
                                            ^
Main.java:77: error: '.class' expected
                std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
                                   ^
Main.java:77: error: '.class' expected
                std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
                                                                ^
Main.java:77: error: illegal start of expression
                std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
                                                                 ^
Main.java:77: error: ';' expected
                std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
                                                                                            ^
Main.java:77: error: illegal start of expression
                std::unique_ptr<T[]> NewData(std::unique_ptr<T[]>{Alloc().allocate(Capacity)});
                                                                                             ^
Main.java:78: error: not a statement
                for (std::size_t i = 0; i < Size; i++) {
                     ^
Main.java:78: error: ';' expected
                for (std::size_t i = 0; i < Size; i++) {
                                ^
Main.java:78: error: not a statement
                for (std::size_t i = 0; i < Size; i++) {
                                          ^
Main.java:78: error: ')' expected
                for (std::size_t i = 0; i < Size; i++) {
                                                ^
Main.java:78: error: ';' expected
                for (std::size_t i = 0; i < Size; i++) {
                                                     ^
Main.java:79: error: '(' expected
                    if constexpr (std::is_move_assignable<T>::value) {
                      ^
Main.java:79: error: illegal start of expression
                    if constexpr (std::is_move_assignable<T>::value) {
                                                            ^
Main.java:80: error: ';' expected
                        NewData[i] = std::move(Data[i]);
                                              ^
Main.java:80: error: not a statement
                        NewData[i] = std::move(Data[i]);
                                                   ^
100 errors
stdout
Standard output is empty