blob: fc86458b3a737478e418e681c62de284d416785d (
plain)
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
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include "td/utils/common.h"
#include <utility>
namespace td {
template <class T>
class fixed_vector {
public:
fixed_vector() = default;
explicit fixed_vector(size_t size) : ptr_(new T[size]), size_(size) {
}
fixed_vector(fixed_vector &&other) noexcept {
swap(other);
}
fixed_vector &operator=(fixed_vector &&other) noexcept {
swap(other);
return *this;
}
fixed_vector(const fixed_vector &) = delete;
fixed_vector &operator=(const fixed_vector &) = delete;
~fixed_vector() {
delete[] ptr_;
}
using iterator = T *;
using const_iterator = const T *;
T &operator[](size_t i) {
return ptr_[i];
}
const T &operator[](size_t i) const {
return ptr_[i];
}
T *begin() {
return ptr_;
}
const T *begin() const {
return ptr_;
}
T *end() {
return ptr_ + size_;
}
const T *end() const {
return ptr_ + size_;
}
bool empty() const {
return size() == 0;
}
size_t size() const {
return size_;
}
void swap(fixed_vector<T> &other) {
std::swap(ptr_, other.ptr_);
std::swap(size_, other.size_);
}
private:
T *ptr_{};
size_t size_{0};
};
template <class T>
void swap(fixed_vector<T> &lhs, fixed_vector<T> &rhs) {
lhs.swap(rhs);
}
} // namespace td
|