IMP  2.2.0
The Integrative Modeling Platform
base/ConstVector.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/ConstVector.h
3  * \brief A beyesian infererence-based sampler.
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_CONST_VECTOR_H
10 #define IMPBASE_CONST_VECTOR_H
11 
12 #include <IMP/base/base_config.h>
13 #include "base_macros.h"
14 #include "exception.h"
15 #include "Value.h"
16 #include <IMP/base/nullptr.h>
17 #include <boost/scoped_array.hpp>
18 #include <IMP/base/hash.h>
19 #include <iterator>
20 
21 IMPBASE_BEGIN_NAMESPACE
22 
23 //! Store an array of values of the same type.
24 /** Items must be comparable and hashable and the arrays
25  cannote be changed after creation.*/
26 template <class Data, class SwigData = Data>
27 class ConstVector : public Value {
28  boost::scoped_array<Data> v_;
29  unsigned int sz_;
30  int compare(const ConstVector &o) const {
31  if (size() < o.size())
32  return -1;
33  else if (size() > o.size())
34  return 1;
35  for (unsigned int i = 0; i < size(); ++i) {
36  if (v_[i] < o.v_[i])
37  return -1;
38  else if (v_[i] > o.v_[i])
39  return 1;
40  }
41  return 0;
42  }
43  void create(unsigned int sz) {
44  if (sz == 0) {
45  v_.reset(nullptr);
46  } else {
47  v_.reset(new Data[sz]);
48  }
49  sz_ = sz;
50  }
51  template <class It>
52  void copy_from(It b, It e) {
53  create(std::distance(b, e));
54  std::copy(b, e, v_.get());
55  }
56 
57  public:
58  ~ConstVector() {}
59  ConstVector(unsigned int sz, Data fill) {
60  create(sz);
61  std::fill(v_.get(), v_.get() + sz, fill);
62  }
63  ConstVector() : v_(0), sz_(0) {}
64  template <class It>
65  ConstVector(It b, It e) {
66  copy_from(b, e);
67  }
68  template <class Vector>
69  explicit ConstVector(const Vector &i) {
70  copy_from(i.begin(), i.end());
71  }
72 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
73  /* Add the arguments to attempt to make VC happy as it tries to
74  use the templated version instead.
75  */
76  ConstVector(const ConstVector<Data, SwigData> &o) : Value(), sz_(0) {
77  copy_from(o.begin(), o.end());
78  }
80  copy_from(o.begin(), o.end());
81  return *this;
82  }
83  ConstVector(int sz) { create(sz); }
84 #endif
86 #ifndef SWIG
87  Data operator[](unsigned int i) const {
88  IMP_USAGE_CHECK(i < sz_, "Out of range");
89  return v_[i];
90  }
91 #ifndef IMP_DOXYGEN
92  void set_item(unsigned int i, SwigData v) const {
93  IMP_USAGE_CHECK(i < sz_, "Out of range");
94  v_[i] = v;
95  ;
96  }
97 #endif
98 #endif
99 #ifndef IMP_DOXYGEN
100  SwigData __getitem__(unsigned int i) const {
101  if (i >= sz_)
102  IMP_THROW("Out of bound " << i << " vs " << sz_, IndexException);
103  return operator[](i);
104  }
105  unsigned int __len__() const { return sz_; }
106 #endif
107 #ifndef SWIG
108  unsigned int size() const { return sz_; }
109 #endif
111  out << "(";
112  for (unsigned int i = 0; i < size(); ++i) {
113  out << Showable(v_[i]);
114  if (i != size() - 1)
115  out << " ";
116  }
117  out << ")";
118  });
119 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
120  typedef const Data *const_iterator;
121  const_iterator begin() const { return v_.get(); }
122  const_iterator end() const { return v_.get() + size(); }
123  void swap_with(ConstVector &o) {
124  std::swap(sz_, o.sz_);
125  v_.swap(o.v_);
126  }
127 #endif
128  IMP_HASHABLE_INLINE(ConstVector, return boost::hash_range(begin(), end()););
129 };
130 
131 IMP_SWAP_1(ConstVector);
132 
133 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
134 template <class D>
135 inline std::size_t hash_value(const ConstVector<D> &t) {
136  return t.__hash__();
137 }
138 #endif
139 
140 IMPBASE_END_NAMESPACE
141 
142 #endif /* IMPBASE_CONST_VECTOR_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.
Provide a nullptr keyword analog.
#define IMP_HASHABLE_INLINE(name, hashret)
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Store an array of values of the same type.
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
IO support.
Exception definitions and assertions.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.