IMP  2.1.1
The Integrative Modeling Platform
base/Array.h
Go to the documentation of this file.
1 /**
2  * \file base/Array.h
3  * \brief Classes to handle static sized arrays of things.
4  *
5  * Copyright 2007-2013 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() { return D; }
51  ;
52  Array() {}
53  Array(SwigData x, SwigData y) {
54  IMP_USAGE_CHECK(D == 2, "Need " << D << " to construct a " << D
55  << "-tuple.");
56  d_[0] = x;
57  d_[1] = y;
58  }
59  Array(SwigData x, SwigData y, SwigData z) {
60  IMP_USAGE_CHECK(D == 3, "Need " << D << " to construct a " << D
61  << "-tuple.");
62  d_[0] = x;
63  d_[1] = y;
64  d_[2] = z;
65  }
66  Array(SwigData x0, SwigData x1, SwigData x2, SwigData x3) {
67  IMP_USAGE_CHECK(D == 4, "Need " << D << " to construct a " << D
68  << "-tuple.");
69  d_[0] = x0;
70  d_[1] = x1;
71  d_[2] = x2;
72  d_[3] = x3;
73  }
74  SwigData get(unsigned int i) const { return d_[i]; }
75  IMP_HASHABLE_INLINE(Array, std::size_t seed = 0;
76  for (unsigned int i = 0; i < D; ++i) {
77  boost::hash_combine(seed, d_[i]);
78  } return seed;);
80 #ifndef SWIG
81  const Data operator[](unsigned int i) const {
82  IMP_USAGE_CHECK(i < D, "Out of range");
83  return d_[i];
84  }
85  Data& operator[](unsigned int i) {
86  IMP_USAGE_CHECK(i < D, "Out of range");
87  return d_[i];
88  }
89 #ifndef IMP_DOXYGEN
90  void set_item(unsigned int i, SwigData v) const {
91  IMP_USAGE_CHECK(i < D, "Out of range");
92  d_[i] = v;
93  }
94 #endif
95 #endif
96 #ifndef IMP_DOXYGEN
97  SwigData __getitem__(unsigned int i) const {
98  if (i >= D) IMP_THROW("Out of bound " << i << " vs " << D, IndexException);
99  return operator[](i);
100  }
101  unsigned int __len__() const { return D; }
102 #endif
103 #ifndef SWIG
104  unsigned int size() const { return D; }
105 #endif
106  std::string get_name() const {
107  std::ostringstream oss;
108  oss << "\"";
109  for (unsigned int i = 0; i < D; ++i) {
110  if (i > 0) {
111  oss << "\" and \"";
112  }
113  oss << d_[i];
114  }
115  oss << "\"";
116  return oss.str();
117  }
118  IMP_SHOWABLE_INLINE(Array, { out << get_name(); });
119  typedef typename Storage::iterator iterator;
120  iterator begin() { return d_.begin(); }
121  iterator end() { return d_.end(); }
122  typedef typename Storage::const_iterator const_iterator;
123  const_iterator begin() const { return d_.begin(); }
124  const_iterator end() const { return d_.end(); }
125 };
126 
127 IMPBASE_END_NAMESPACE
128 
129 #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:371
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Various general useful macros for IMP.
Exception definitions and assertions.