tdlight/tdutils/td/utils/fixed_vector.h

80 lines
1.5 KiB
C
Raw Normal View History

2022-02-18 21:04:25 +01:00
//
2024-01-01 01:07:21 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
2022-02-18 21:04:25 +01:00
//
// 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
2022-02-18 21:04:25 +01:00
#include "td/utils/common.h"
2022-02-18 21:04:25 +01:00
#include <utility>
namespace td {
2022-02-18 21:04:25 +01:00
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_;
}
2022-02-18 21:04:25 +01:00
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];
}
2022-02-18 21:04:25 +01:00
T *begin() {
return ptr_;
}
const T *begin() const {
return ptr_;
}
T *end() {
return ptr_ + size_;
}
const T *end() const {
return ptr_ + size_;
}
2022-02-18 21:04:25 +01:00
bool empty() const {
return size() == 0;
}
size_t size() const {
return size_;
}
2022-02-18 21:04:25 +01:00
void swap(fixed_vector<T> &other) {
std::swap(ptr_, other.ptr_);
std::swap(size_, other.size_);
}
private:
T *ptr_{};
size_t size_{0};
};
2022-02-18 21:04:25 +01:00
2022-02-21 03:49:59 +01:00
template <class T>
void swap(fixed_vector<T> &lhs, fixed_vector<T> &rhs) {
lhs.swap(rhs);
}
2022-02-18 21:04:25 +01:00
} // namespace td