1 //
2 // BAGEL - Brilliantly Advanced General Electronic Structure Library
3 // Filename: vec.h
4 // Copyright (C) 2015 Toru Shiozaki
5 //
6 // Author: Toru Shiozaki <shiozaki@northwestern.edu>
7 // Maintainer: Shiozaki group
8 //
9 // This file is part of the BAGEL package.
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 //
24 
25 #ifndef __SRC_UTIL_VEC_H
26 #define __SRC_UTIL_VEC_H
27 
28 #include <map>
29 #include <src/util/serialization.h>
30 
31 namespace bagel {
32 
33 // Type has to have a copy() function
34 
35 template<class Type>
36 class Vec {
37   protected:
38     typename std::map<std::pair<int,int>, std::shared_ptr<Type>> data_;
39 
40   private:
41     friend class boost::serialization::access;
42     template<class Archive>
serialize(Archive & ar,const unsigned int)43     void serialize(Archive& ar, const unsigned int) { ar & data_; }
44 
45   public:
Vec()46     Vec() { }
Vec(const Vec<Type> & o)47     Vec(const Vec<Type>& o) {
48       for (auto& i : o.data_)
49         data_.emplace(i.first, i.second->copy());
50     }
51 
size()52     size_t size() const { return data_.size(); }
53 
begin()54     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::iterator begin() { return data_.begin(); }
end()55     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::iterator end() { return data_.end(); }
begin()56     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::const_iterator begin() const { return data_.cbegin(); }
end()57     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::const_iterator end() const { return data_.cend(); }
cbegin()58     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::const_iterator cbegin() const { return data_.cbegin(); }
cend()59     typename std::map<std::pair<int,int>, std::shared_ptr<Type>>::const_iterator cend() const { return data_.cend(); }
60 
61     // adding elements (i and j are bra and ket state indices)
emplace(const int i,const int j,std::shared_ptr<Type> d)62     void emplace(const int i, const int j, std::shared_ptr<Type> d) {
63       // if this key is present, the element is deleted
64       auto key = std::make_pair(i, j);
65       if (data_.find(key) != data_.end())
66         data_.erase(key);
67       data_.emplace(key, d);
68     }
69     // special function for diagonal RDM
emplace(const int i,std::shared_ptr<Type> d)70     void emplace(const int i, std::shared_ptr<Type> d) { emplace(i, i, d); }
71 
72     // get RDMs
at(const int i)73     std::shared_ptr<Type> at(const int i) { return at(i, i); }
at(const int i,const int j)74     std::shared_ptr<Type> at(const int i, const int j) { return data_.at(std::make_pair(i, j)); }
at(const int i)75     std::shared_ptr<const Type> at(const int i) const { return at(i, i); }
at(const int i,const int j)76     std::shared_ptr<const Type> at(const int i, const int j) const { return data_.at(std::make_pair(i, j)); }
77 
exist(const int i,const int j)78     bool exist(const int i, const int j) const { return data_.find(std::make_pair(i, j)) != data_.end(); }
79 };
80 
81 }
82 
83 #endif
84