Psi4
multiarr.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2016-2017 Robert A. Shaw.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 /*
30  * Helpful lightweight multi-index arrays to make the code easier to write and test.
31  * These should probably be replaced at some point in the interests of speed.
32  *
33  * Robert A Shaw 2016
34  */
35 
36 #ifndef MULTIARR_HEAD
37 #define MULTIARR_HEAD
38 
39 #include <vector>
40 
41 namespace psi {
42 
44 template <typename T>
45 struct TwoIndex {
46  int dims[2];
47  std::vector<T> data;
48  T& operator()(int i, int j) { return data[i * dims[1] + j]; }
49  T operator()(int i, int j) const { return data[i * dims[1] + j]; }
50  void assign(int dim1, int dim2, T value) {
51  dims[0] = dim1;
52  dims[1] = dim2;
53  data.resize(dim1 * dim2);
54  std::fill(data.begin(), data.end(), value);
55  }
56  TwoIndex() { dims[0] = dims[1] = 0; }
57  TwoIndex(int dim1, int dim2) {
58  dims[0] = dim1;
59  dims[1] = dim2;
60  data.resize(dim1 * dim2);
61  }
62  TwoIndex(int dim1, int dim2, T value) { assign(dim1, dim2, value); }
63  TwoIndex(const TwoIndex<T>& other) {
64  data = other.data;
65  dims[0] = other.dims[0];
66  dims[1] = other.dims[1];
67  }
68 };
69 
71 template <typename T>
72 struct ThreeIndex {
73  int dims[3];
74  std::vector<T> data;
75  T& operator()(int i, int j, int k) { return data[i * dims[2] * dims[1] + j * dims[2] + k]; }
76  T operator()(int i, int j, int k) const { return data[i * dims[2] * dims[1] + j * dims[2] + k]; }
78  dims[0] = 0;
79  dims[1] = 0;
80  dims[2] = 0;
81  }
82  ThreeIndex(int dim1, int dim2, int dim3) {
83  dims[0] = dim1;
84  dims[1] = dim2;
85  dims[2] = dim3;
86  data.resize(dim1 * dim2 * dim3);
87  }
88  ThreeIndex(const ThreeIndex<T>& other) {
89  data = other.data;
90  for (int n = 0; n < 3; n++) dims[n] = other.dims[n];
91  }
92  void fill(T value) { std::fill(data.begin(), data.end(), value); }
93 };
94 
96 template <typename T>
97 struct FiveIndex {
98  int dims[5];
99  std::vector<T> data;
100  T& operator()(int i, int j, int k, int l, int m) {
101  return data[m + dims[4] * (l + dims[3] * (k + dims[2] * (j + dims[1] * i)))];
102  }
103  T operator()(int i, int j, int k, int l, int m) const {
104  return data[m + dims[4] * (l + dims[3] * (k + dims[2] * (j + dims[1] * i)))];
105  }
106  FiveIndex() { dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = 0; }
107  FiveIndex(int dim1, int dim2, int dim3, int dim4, int dim5) {
108  dims[0] = dim1;
109  dims[1] = dim2;
110  dims[2] = dim3;
111  dims[3] = dim4;
112  dims[4] = dim5;
113  data.resize(dim1 * dim2 * dim3 * dim4 * dim5);
114  }
115  FiveIndex(const FiveIndex<T>& other) {
116  data = other.data;
117  for (int n = 0; n < 5; n++) dims[n] = other.dims[n];
118  }
119 };
120 
122 template <typename T>
123 struct SevenIndex {
124  int dims[7];
125  std::vector<T> data;
126  T& operator()(int i, int j, int k, int l, int m, int n, int p) {
127  return data[p + dims[6] * (n + dims[5] * (m + dims[4] * (l + dims[3] * (k + dims[2] * (j + dims[1] * i)))))];
128  }
129  T operator()(int i, int j, int k, int l, int m, int n, int p) const {
130  return data[p + dims[6] * (n + dims[5] * (m + dims[4] * (l + dims[3] * (k + dims[2] * (j + dims[1] * i)))))];
131  }
132  SevenIndex() { dims[0] = dims[1] = dims[2] = dims[3] = dims[4] = dims[5] = dims[6] = 0; }
133  SevenIndex(int dim1, int dim2, int dim3, int dim4, int dim5, int dim6, int dim7) {
134  dims[0] = dim1;
135  dims[1] = dim2;
136  dims[2] = dim3;
137  dims[3] = dim4;
138  dims[4] = dim5;
139  dims[5] = dim6;
140  dims[6] = dim7;
141  data.resize(dim1 * dim2 * dim3 * dim4 * dim5 * dim6 * dim7);
142  }
143  SevenIndex(const SevenIndex<T>& other) {
144  data = other.data;
145  for (int n = 0; n < 7; n++) dims[n] = other.dims[n];
146  }
147 };
148 } // namespace psi
149 #endif
int dims[3]
Definition: multiarr.h:73
int dims[2]
Definition: multiarr.h:46
T & operator()(int i, int j, int k, int l, int m, int n, int p)
Definition: multiarr.h:126
TwoIndex()
Definition: multiarr.h:56
std::vector< T > data
Definition: multiarr.h:99
T & operator()(int i, int j, int k)
Definition: multiarr.h:75
Definition: pointgrp.h:104
T operator()(int i, int j, int k, int l, int m) const
Definition: multiarr.h:103
Templated skeleton five index array for convenience.
Definition: multiarr.h:97
T operator()(int i, int j, int k, int l, int m, int n, int p) const
Definition: multiarr.h:129
std::vector< T > data
Definition: multiarr.h:74
Templated skeleton two index array for convenience.
Definition: multiarr.h:45
int * T
Definition: stringlist.cc:66
FiveIndex(int dim1, int dim2, int dim3, int dim4, int dim5)
Definition: multiarr.h:107
SevenIndex()
Definition: multiarr.h:132
ThreeIndex()
Definition: multiarr.h:77
SevenIndex(int dim1, int dim2, int dim3, int dim4, int dim5, int dim6, int dim7)
Definition: multiarr.h:133
std::vector< T > data
Definition: multiarr.h:47
FiveIndex()
Definition: multiarr.h:106
T & operator()(int i, int j)
Definition: multiarr.h:48
TwoIndex(int dim1, int dim2, T value)
Definition: multiarr.h:62
int dims[7]
Definition: multiarr.h:124
TwoIndex(const TwoIndex< T > &other)
Definition: multiarr.h:63
T operator()(int i, int j, int k) const
Definition: multiarr.h:76
ThreeIndex(int dim1, int dim2, int dim3)
Definition: multiarr.h:82
T & operator()(int i, int j, int k, int l, int m)
Definition: multiarr.h:100
TwoIndex(int dim1, int dim2)
Definition: multiarr.h:57
void assign(int dim1, int dim2, T value)
Definition: multiarr.h:50
SevenIndex(const SevenIndex< T > &other)
Definition: multiarr.h:143
Templated skeleton seven index array for convenience.
Definition: multiarr.h:123
ThreeIndex(const ThreeIndex< T > &other)
Definition: multiarr.h:88
FiveIndex(const FiveIndex< T > &other)
Definition: multiarr.h:115
std::vector< T > data
Definition: multiarr.h:125
T operator()(int i, int j) const
Definition: multiarr.h:49
void fill(T value)
Definition: multiarr.h:92
Templated skeleton three index array for convenience.
Definition: multiarr.h:72
int dims[5]
Definition: multiarr.h:98