VinaLC: Parallel Molecular Docking Program

Biochemical and Biophysical Systems Group
VinaLC version: 1.1.2

array3d.h
Go to the documentation of this file.
1 /*
2 
3  Copyright (c) 2006-2010, The Scripps Research Institute
4 
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16 
17  Author: Dr. Oleg Trott <ot14@columbia.edu>,
18  The Olson Lab,
19  The Scripps Research Institute
20 
21 */
22 
23 #ifndef VINA_ARRAY3D_H
24 #define VINA_ARRAY3D_H
25 
26 #include <exception> // std::bad_alloc
27 #include "common.h"
28 
29 inline sz checked_multiply(sz i, sz j) {
30  if(i == 0 || j == 0) return 0;
31  const sz tmp = i * j;
32  if(tmp < i || tmp < j || tmp / i != j)
33  throw std::bad_alloc(); // can't alloc if the size makes sz wrap around
34  return tmp;
35 }
36 
37 inline sz checked_multiply(sz i, sz j, sz k) {
38  return checked_multiply(checked_multiply(i, j), k);
39 }
40 
41 template<typename T>
42 class array3d {
44  std::vector<T> m_data;
45  friend class boost::serialization::access;
46  template<typename Archive>
47  void serialize(Archive& ar, const unsigned version) {
48  ar & m_i;
49  ar & m_j;
50  ar & m_k;
51  ar & m_data;
52  }
53 public:
54  array3d() : m_i(0), m_j(0), m_k(0) {}
55  array3d(sz i, sz j, sz k) : m_i(i), m_j(j), m_k(k), m_data(checked_multiply(i, j, k)) {}
56  sz dim0() const { return m_i; }
57  sz dim1() const { return m_j; }
58  sz dim2() const { return m_k; }
59  sz dim(sz i) const {
60  switch(i) {
61  case 0: return m_i;
62  case 1: return m_j;
63  case 2: return m_k;
64  default: assert(false); return 0; // to get rid of the warning
65  }
66  }
67  void resize(sz i, sz j, sz k) { // data is essentially garbled
68  m_i = i;
69  m_j = j;
70  m_k = k;
71  m_data.resize(checked_multiply(i, j, k));
72  }
73  T& operator()(sz i, sz j, sz k) { return m_data[i + m_i*(j + m_j*k)]; }
74  const T& operator()(sz i, sz j, sz k) const { return m_data[i + m_i*(j + m_j*k)]; }
75 };
76 
77 #endif