LIBINT 2.9.0
compressed_pair.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint library.
5 *
6 * Libint library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Libint library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with Libint library. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef _libint2_src_lib_libint_util_compressedpair_h_
22#define _libint2_src_lib_libint_util_compressedpair_h_
23
24namespace libint2 {
25namespace detail {
26
27// a bare-bones implementation that assumes T1 and T2 are classes, and T1 != T2
28template <class T1, class T2>
29class compressed_pair : private T1, private T2 {
30 public:
31 typedef T1 first_type;
32 typedef T2 second_type;
33 typedef typename std::add_const<first_type>::type first_const_type;
34 typedef typename std::add_const<second_type>::type second_const_type;
35 typedef typename std::add_lvalue_reference<first_type>::type first_reference;
36 typedef
37 typename std::add_lvalue_reference<second_type>::type second_reference;
38 typedef typename std::add_lvalue_reference<first_const_type>::type
39 first_const_reference;
40 typedef typename std::add_lvalue_reference<second_const_type>::type
41 second_const_reference;
42 typedef typename std::add_rvalue_reference<first_type>::type
43 first_rvalue_reference;
44 typedef typename std::add_rvalue_reference<second_type>::type
45 second_rvalue_reference;
46
47 compressed_pair() = default;
48 compressed_pair(const first_type& x, const second_type& y)
49 : first_type(x), second_type(y) {}
50 explicit compressed_pair(const first_type& x) : first_type(x) {}
51 explicit compressed_pair(const second_type& y) : second_type(y) {}
52
53 compressed_pair(const compressed_pair& other) = default;
55 : first_type(std::move(other.first_rvalref())),
56 second_type(std::move(other.second_rvalref())) {}
57 compressed_pair& operator=(const compressed_pair&) = default;
58 compressed_pair& operator=(compressed_pair&& other) {
59 this->first() = std::move(other.first_rvalref());
60 this->second() = std::move(other.second_rvalref());
61 return *this;
62 }
63
64 first_reference first() { return static_cast<first_reference>(*this); }
65 first_const_reference first() const {
66 return static_cast<first_const_reference>(*this);
67 }
68 first_rvalue_reference first_rvalref() {
69 return static_cast<first_rvalue_reference>(*this);
70 }
71
72 second_reference second() { return static_cast<second_reference>(*this); }
73 second_const_reference second() const {
74 return static_cast<second_const_reference>(*this);
75 }
76 second_rvalue_reference second_rvalref() {
77 return static_cast<second_rvalue_reference>(*this);
78 }
79
80 void swap(compressed_pair& other) {
81 swap(this->first(), other.first());
82 swap(this->second(), other.second());
83 }
84};
85
86template <class T1, class T2>
87compressed_pair<T1, T2> make_compressed_pair(const T1& x, const T2& y) {
88 return compressed_pair<T1, T2>(x, y);
89}
90
91} // namespace detail
92} // namespace libint2
93#endif // header guard
Definition compressed_pair.h:29
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24