2018-08-13 19:15:09 +02:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-08-13 19:15:09 +02: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
|
2018-09-07 02:41:21 +02:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
#include <array>
|
2020-06-09 03:48:08 +02:00
|
|
|
#include <iterator>
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
namespace td {
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace detail {
|
|
|
|
template <class T, class InnerT>
|
|
|
|
class SpanImpl {
|
|
|
|
InnerT *data_{nullptr};
|
|
|
|
size_t size_{0};
|
|
|
|
|
|
|
|
public:
|
|
|
|
SpanImpl() = default;
|
|
|
|
SpanImpl(InnerT *data, size_t size) : data_(data), size_(size) {
|
|
|
|
}
|
|
|
|
SpanImpl(InnerT &data) : SpanImpl(&data, 1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherInnerT>
|
|
|
|
SpanImpl(const SpanImpl<T, OtherInnerT> &other) : SpanImpl(other.data(), other.size()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
SpanImpl(const std::array<T, N> &arr) : SpanImpl(arr.data(), arr.size()) {
|
|
|
|
}
|
|
|
|
template <size_t N>
|
|
|
|
SpanImpl(std::array<T, N> &arr) : SpanImpl(arr.data(), arr.size()) {
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
template <size_t N>
|
|
|
|
SpanImpl(const T (&arr)[N]) : SpanImpl(arr, N) {
|
|
|
|
}
|
|
|
|
template <size_t N>
|
|
|
|
SpanImpl(T (&arr)[N]) : SpanImpl(arr, N) {
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
SpanImpl(const vector<T> &v) : SpanImpl(v.data(), v.size()) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
SpanImpl(vector<T> &v) : SpanImpl(v.data(), v.size()) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherInnerT>
|
|
|
|
SpanImpl &operator=(const SpanImpl<T, OtherInnerT> &other) {
|
|
|
|
SpanImpl copy{other};
|
|
|
|
*this = copy;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class OtherInnerT>
|
|
|
|
bool operator==(const SpanImpl<T, OtherInnerT> &other) const {
|
|
|
|
if (size() != other.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < size(); i++) {
|
2019-07-21 20:07:07 +02:00
|
|
|
if (!((*this)[i] == other[i])) {
|
2019-07-06 13:29:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
InnerT &operator[](size_t i) {
|
|
|
|
DCHECK(i < size());
|
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
const InnerT &operator[](size_t i) const {
|
|
|
|
DCHECK(i < size());
|
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
|
2020-06-09 03:48:08 +02:00
|
|
|
InnerT &back() {
|
|
|
|
DCHECK(!empty());
|
|
|
|
return data_[size() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const InnerT &back() const {
|
|
|
|
DCHECK(!empty());
|
|
|
|
return data_[size() - 1];
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
InnerT *data() const {
|
|
|
|
return data_;
|
|
|
|
}
|
2020-06-09 03:48:08 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
InnerT *begin() const {
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
InnerT *end() const {
|
|
|
|
return data_ + size_;
|
|
|
|
}
|
2020-06-09 03:48:08 +02:00
|
|
|
|
|
|
|
std::reverse_iterator<InnerT *> rbegin() const {
|
|
|
|
return std::reverse_iterator<InnerT *>(end());
|
|
|
|
}
|
|
|
|
std::reverse_iterator<InnerT *> rend() const {
|
|
|
|
return std::reverse_iterator<InnerT *>(begin());
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
size_t size() const {
|
|
|
|
return size_;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
bool empty() const {
|
|
|
|
return size() == 0;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
SpanImpl &truncate(size_t size) {
|
|
|
|
CHECK(size <= size_);
|
|
|
|
size_ = size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
SpanImpl substr(size_t offset) const {
|
2018-08-13 19:15:09 +02:00
|
|
|
CHECK(offset <= size_);
|
|
|
|
return SpanImpl(begin() + offset, size_ - offset);
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
SpanImpl substr(size_t offset, size_t size) const {
|
|
|
|
CHECK(offset <= size_);
|
|
|
|
CHECK(size_ - offset >= size);
|
|
|
|
return SpanImpl(begin() + offset, size);
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
};
|
|
|
|
} // namespace detail
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
template <class T>
|
|
|
|
using Span = detail::SpanImpl<T, const T>;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
using MutableSpan = detail::SpanImpl<T, T>;
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class T>
|
2019-07-21 20:07:07 +02:00
|
|
|
Span<T> span(const T *ptr, size_t size) {
|
2019-07-06 13:29:15 +02:00
|
|
|
return Span<T>(ptr, size);
|
|
|
|
}
|
|
|
|
template <class T>
|
2019-07-21 20:07:07 +02:00
|
|
|
MutableSpan<T> mutable_span(T *ptr, size_t size) {
|
2019-07-06 13:29:15 +02:00
|
|
|
return MutableSpan<T>(ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
} // namespace td
|