-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32177 from vespa-engine/toregge/remove-arrayref-u…
…nit-test Remove arrayref unit test.
- Loading branch information
Showing
9 changed files
with
7 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,20 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <vector> | ||
#include <array> | ||
#include <span> | ||
|
||
namespace vespalib { | ||
|
||
#define VESPALIB_ARRAYREF_IS_STD_SPAN 1 | ||
|
||
#if VESPALIB_ARRAYREF_IS_STD_SPAN | ||
|
||
template <typename T> | ||
using ArrayRef = std::span<T>; | ||
|
||
template <typename T> | ||
using ConstArrayRef = std::span<const T>; | ||
|
||
#else | ||
/** | ||
* This is a simple wrapper class for a typed array with no memory ownership. | ||
* It is similar to std::string_view | ||
**/ | ||
template <typename T> | ||
class ArrayRef { | ||
public: | ||
constexpr ArrayRef() noexcept : _v(nullptr), _sz(0) { } | ||
constexpr ArrayRef(T * v, size_t sz) noexcept : _v(v), _sz(sz) { } | ||
template<typename A=std::allocator<T>> | ||
ArrayRef(std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { } | ||
template<size_t SZ> | ||
ArrayRef(std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { } | ||
T & operator [] (size_t i) noexcept { return _v[i]; } | ||
const T & operator [] (size_t i) const noexcept { return _v[i]; } | ||
T * data() noexcept { return _v; } | ||
const T * data() const noexcept { return _v; } | ||
[[nodiscard]] size_t size() const noexcept { return _sz; } | ||
[[nodiscard]] bool empty() const noexcept { return _sz == 0; } | ||
T *begin() noexcept { return _v; } | ||
T *end() noexcept { return _v + _sz; } | ||
private: | ||
T * _v; | ||
size_t _sz; | ||
}; | ||
|
||
template <typename T> | ||
class ConstArrayRef { | ||
public: | ||
constexpr ConstArrayRef(const T *v, size_t sz) noexcept : _v(v), _sz(sz) { } | ||
template<typename A=std::allocator<T>> | ||
ConstArrayRef(const std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { } | ||
template<size_t SZ> | ||
ConstArrayRef(const std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { } | ||
ConstArrayRef(const ArrayRef<T> & v) noexcept : _v(v.data()), _sz(v.size()) { } | ||
constexpr ConstArrayRef() noexcept : _v(nullptr), _sz(0) {} | ||
const T & operator [] (size_t i) const noexcept { return _v[i]; } | ||
[[nodiscard]] size_t size() const noexcept { return _sz; } | ||
[[nodiscard]] bool empty() const noexcept { return _sz == 0; } | ||
const T *cbegin() const noexcept { return _v; } | ||
const T *cend() const noexcept { return _v + _sz; } | ||
const T *begin() const noexcept { return _v; } | ||
const T *end() const noexcept { return _v + _sz; } | ||
const T *data() const noexcept { return _v; } | ||
private: | ||
const T *_v; | ||
size_t _sz; | ||
}; | ||
#endif | ||
|
||
// const-cast for array references; use with care | ||
template <typename T> | ||
ArrayRef<T> unconstify(const ConstArrayRef<T> &ref) { | ||
return ArrayRef<T>(const_cast<T*>(ref.data()), ref.size()); | ||
std::span<T> unconstify(const std::span<const T>& ref) { | ||
return std::span<T>(const_cast<T*>(ref.data()), ref.size()); | ||
} | ||
|
||
} |