IMP  2.2.0
The Integrative Modeling Platform
base/Array.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/Array.h
3  * \brief Classes to handle static sized arrays of things.
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_ARRAY_H
10 #define IMPBASE_ARRAY_H
11 
12 #include <IMP/base/base_config.h>
13 #include "Value.h"
14 #include "comparison_macros.h"
15 #include "hash_macros.h"
16 #include "check_macros.h"
17 #include "showable_macros.h"
18 #include <boost/array.hpp>
19 
20 IMPBASE_BEGIN_NAMESPACE
21 
22 //! A class to store an fixed array of same-typed values.
23 /** Only the constructor with the correct number of arguments for the
24  dimensionality can be used.
25 
26  \note These are mapped to/from python tuples, so there is
27  no need to use types that are typedefs of this on the python
28  side.
29 
30  \see ConstVector
31  */
32 template <unsigned int D, class Data, class SwigData = Data>
33 class Array : public Value {
34  typedef boost::array<Data, D> Storage;
35  Storage d_;
36  int compare(const Array<D, Data, SwigData>& o) const {
37  for (unsigned int i = 0; i < D; ++i) {
38  if (d_[i] < o[i])
39  return -1;
40  else if (d_[i] > o[i])
41  return 1;
42  }
43  return 0;
44  }
45 
46  public:
47 #ifndef IMP_DOXYGEN
48  typedef SwigData value_type;
49 #endif
50  unsigned int get_dimension() {
51  return D;
52  };
53  Array() {}
54  Array(SwigData x, SwigData y) {
55  IMP_USAGE_CHECK(D == 2, "Need " << D << " to construct a " << D
56  << "-tuple.");
57  d_[0] = x;
58  d_[1] = y;
59  }
60  Array(SwigData x, SwigData y, SwigData z) {
61  IMP_USAGE_CHECK(D == 3, "Need " << D << " to construct a " << D
62  << "-tuple.");
63  d_[0] = x;
64  d_[1] = y;
65  d_[2] = z;
66  }
67  Array(SwigData x0, SwigData x1, SwigData x2, SwigData x3) {
68  IMP_USAGE_CHECK(D == 4, "Need " << D << " to construct a " << D
69  << "-tuple.");
70  d_[0] = x0;
71  d_[1] = x1;
72  d_[2] = x2;
73  d_[3] = x3;
74  }
75  SwigData get(unsigned int i) const { return d_[i]; }
76  IMP_HASHABLE_INLINE(Array, std::size_t seed = 0;
77  for (unsigned int i = 0; i < D; ++i) {
78  boost::hash_combine(seed, d_[i]);
79  } return seed;);
81 #ifndef SWIG
82  const Data operator[](unsigned int i) const {
83  IMP_USAGE_CHECK(i < D, "Out of range");
84  return d_[i];
85  }
86  Data& operator[](unsigned int i) {
87  IMP_USAGE_CHECK(i < D, "Out of range");
88  return d_[i];
89  }
90 #ifndef IMP_DOXYGEN
91  void set_item(unsigned int i, SwigData v) const {
92  IMP_USAGE_CHECK(i < D, "Out of range");
93  d_[i] = v;
94  }
95 #endif
96 #endif
97 #ifndef IMP_DOXYGEN
98  SwigData __getitem__(unsigned int i) const {
99  if (i >= D) IMP_THROW("Out of bound " << i << " vs " << D, IndexException);
100  return operator[](i);
101  }
102  unsigned int __len__() const { return D; }
103 #endif
104 #ifndef SWIG
105  unsigned int size() const { return D; }
106 #endif
107  std::string get_name() const {
108  std::ostringstream oss;
109  oss << "\"";
110  for (unsigned int i = 0; i < D; ++i) {
111  if (i > 0) {
112  oss << "\" and \"";
113  }
114  oss << d_[i];
115  }
116  oss << "\"";
117  return oss.str();
118  }
119  IMP_SHOWABLE_INLINE(Array, { out << get_name(); });
120  typedef typename Storage::iterator iterator;
121  iterator begin() { return d_.begin(); }
122  iterator end() { return d_.end(); }
123  typedef typename Storage::const_iterator const_iterator;
124  const_iterator begin() const { return d_.begin(); }
125  const_iterator end() const { return d_.end(); }
126 };
127 
128 IMPBASE_END_NAMESPACE
129 
130 #endif /* IMPBASE_ARRAY_H */
An exception for a request for an invalid member of a container.
Various general useful macros for IMP.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Various general useful macros for IMP.
#define IMP_HASHABLE_INLINE(name, hashret)
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
A class to store an fixed array of same-typed values.
Definition: base/Array.h:33
Basic types used by IMP.
#define IMP_COMPARISONS(Name)
Implement comparison in a class using a compare function.
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
Definition: VectorD.h:179
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Various general useful macros for IMP.
Exception definitions and assertions.