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