ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
array_ref.h
Go to the documentation of this file.
1#ifndef BASE_UTILS_ARRAY_REF_H_
2#define BASE_UTILS_ARRAY_REF_H_
3
4#include <cstddef>
5#include <cstdint>
7
8namespace base {
9namespace utils {
10
11template <typename T>
12class array_ref final {
13
14 private:
16 size_t length_;
17
18 public:
19 /* implicit */ constexpr array_ref() : data_(nullptr), length_(0) {}
20 /* implicit */ constexpr array_ref(T* data, size_t length) : data_(data), length_(length) {}
21 /* implicit */ constexpr array_ref(T* begin, T* end) : data_(begin), length_(end - begin) {}
22 explicit constexpr array_ref(const T& item) : data_(&item), length_(1) {}
23
24 // Construct from a std::vector.
25 template <typename A>
26 /* implicit */ array_ref(const std::vector<T, A>& vec)
27 : data_(vec.data()), length_(vec.size()) {
28 static_assert(
29 !std::is_same<T, bool>::value,
30 "array_ref<bool> cannot be constructed from a std::vector<bool> bitfield.");
31 }
32
33 // Construct from a std::array.
34 template <size_t N>
35 /* implicit */ constexpr array_ref(const std::array<T, N>& arr) : data_(arr.data()), length_(N) {}
36
37 // Construct from a std::initializer_list.
38 /* implicit */ constexpr array_ref(const std::initializer_list<T>& list)
39 : data_(list.begin()), length_(list.size()) {}
40
41 constexpr const T* begin() const { return data_; }
42 constexpr const T* end() const { return data_ + length_; }
43
44 constexpr bool empty() const {
45 return length_ == 0;
46 }
47
48 constexpr const T* data() const {
49 return data_;
50 }
51
52 constexpr size_t size() const {
53 return length_;
54 }
55
56 constexpr const T& front() const {
57 return data_[0];
58 }
59
60 constexpr const T& back() const {
61 return data_[length_ - 1];
62 }
63
64 constexpr bool equals(const array_ref<T>& rhs) const {
65 return length_ == rhs.size() && std::equal(begin(), end(), rhs.begin());
66 }
67
68 constexpr const T& operator[](size_t index) const {
69 return data_[index];
70 }
71
72 template <typename U>
73 typename std::enable_if<std::is_same<U, T>::value, array_ref<T>>::type&
74 operator=(U&& Temporary) = delete;
75
76 template <typename U>
77 typename std::enable_if<std::is_same<U, T>::value, array_ref<T>>::type&
78 operator=(std::initializer_list<U>) = delete;
79
80 std::vector<T> vec() const {
81 return std::vector<T>(data_, data_ + length_);
82 }
83};
84
85template <typename T>
86std::ostream& operator<<(std::ostream& out, array_ref<T> arr) {
87 int ii = 0;
88 out << "[";
89 for (const auto& item : arr) {
90 if (ii++ > 0)
91 out << ", ";
92 out << item;
93 }
94 out << "]";
95 return out;
96}
97
98template <typename T>
100 return a1.equals(a2);
101}
102
103template <typename T>
105 return !a1.equals(a2);
106}
107
108
109} // namespace utils
110} // namespace base
111
113
114#endif // BASE_UTILS_ARRAY_REF_H_
Definition array_ref.h:12
constexpr const T & back() const
Definition array_ref.h:60
constexpr array_ref(T *data, size_t length)
Definition array_ref.h:20
constexpr const T & operator[](size_t index) const
Definition array_ref.h:68
constexpr const T * data() const
Definition array_ref.h:48
constexpr const T & front() const
Definition array_ref.h:56
constexpr bool empty() const
Definition array_ref.h:44
std::enable_if< std::is_same< U, T >::value, array_ref< T > >::type & operator=(std::initializer_list< U >)=delete
std::vector< T > vec() const
Definition array_ref.h:80
size_t length_
Definition array_ref.h:16
constexpr array_ref(const T &item)
Definition array_ref.h:22
constexpr size_t size() const
Definition array_ref.h:52
constexpr array_ref()
Definition array_ref.h:19
array_ref(const std::vector< T, A > &vec)
Definition array_ref.h:26
constexpr const T * end() const
Definition array_ref.h:42
constexpr array_ref(T *begin, T *end)
Definition array_ref.h:21
constexpr array_ref(const std::array< T, N > &arr)
Definition array_ref.h:35
T * data_
Definition array_ref.h:15
constexpr array_ref(const std::initializer_list< T > &list)
Definition array_ref.h:38
constexpr bool equals(const array_ref< T > &rhs) const
Definition array_ref.h:64
std::enable_if< std::is_same< U, T >::value, array_ref< T > >::type & operator=(U &&Temporary)=delete
constexpr const T * begin() const
Definition array_ref.h:41
#define N
Definition exp.cpp:24
#define T
Definition exp.cpp:237
std::ostream & operator<<(std::ostream &out, array_ref< T > arr)
Definition array_ref.h:86
bool operator==(array_ref< T > a1, array_ref< T > a2)
Definition array_ref.h:99
bool operator!=(array_ref< T > a1, array_ref< T > a2)
Definition array_ref.h:104
Definition allocator.h:6
list(APPEND objects esolver.cpp esolver_ks.cpp esolver_fp.cpp esolver_ks_pw.cpp esolver_ks_lcaopw.cpp esolver_sdft_pw.cpp esolver_lj.cpp esolver_dp.cpp esolver_of.cpp esolver_of_interface.cpp esolver_of_tool.cpp pw_others.cpp pw_setup.cpp) if(ENABLE_LCAO) list(APPEND objects esolver_ks_lcao.cpp esolver_ks_lcao_tddft.cpp lcao_before_scf.cpp lcao_after_scf.cpp esolver_gets.cpp lcao_others.cpp esolver_dm2rho.cpp) endif() add_library(esolver OBJECT $
Definition CMakeLists.txt:1