IMP  2.0.0
The Integrative Modeling Platform
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-2013 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()) return -1;
32  else if (size() > o.size()) return 1;
33  for (unsigned int i=0; i< size(); ++i) {
34  if (v_[i] < o.v_[i]) return -1;
35  else if (v_[i] > o.v_[i]) return 1;
36  }
37  return 0;
38  }
39  void create(unsigned int sz) {
40  if (sz==0) {
41  v_.reset(nullptr);
42  } else {
43  v_.reset(new Data[sz]);
44  }
45  sz_=sz;
46  }
47  template <class It>
48  void copy_from(It b, It e) {
49  create(std::distance(b,e));
50  std::copy(b,e, v_.get());
51  }
52 public:
53  ~ConstVector() {
54  }
55  ConstVector(unsigned int sz, Data fill) {
56  create(sz);
57  std::fill(v_.get(), v_.get()+sz, fill);
58  }
59  ConstVector(): v_(0), sz_(0){}
60  template <class It>
61  ConstVector(It b, It e) {
62  copy_from(b,e);
63  }
64  template <class Vector>
65  explicit ConstVector(const Vector &i) {
66  copy_from(i.begin(), i.end());
67  }
68 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
69  /* Add the arguments to attempt to make VC happy as it tries to
70  use the templated version instead.
71  */
72  ConstVector(const ConstVector<Data, SwigData> &o): Value(), sz_(0) {
73  copy_from(o.begin(), o.end());
74  }
76  copy_from(o.begin(), o.end());
77  return *this;
78  }
79  ConstVector(int sz) {
80  create(sz);
81  }
82 #endif
84 #ifndef SWIG
85  Data operator[](unsigned int i) const {
86  IMP_USAGE_CHECK(i < sz_, "Out of range");
87  return v_[i];
88  }
89 #ifndef IMP_DOXYGEN
90  void set_item(unsigned int i, SwigData v) const {
91  IMP_USAGE_CHECK(i < sz_, "Out of range");
92  v_[i]=v;;
93  }
94 #endif
95 #endif
96 #ifndef IMP_DOXYGEN
97  SwigData __getitem__(unsigned int i) const {
98  if (i >= sz_) IMP_THROW("Out of bound " << i << " vs " << sz_,
100  return operator[](i);
101  }
102  unsigned int __len__() const {return sz_;}
103 #endif
104 #ifndef SWIG
105  unsigned int size() const {
106  return sz_;
107  }
108 #endif
110  out << "(";
111  for (unsigned int i=0; i< size(); ++i) {
112  out << Showable(v_[i]);
113  if (i != size()-1) out << " ";
114  }
115  out << ")";
116  });
117 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
118  typedef const Data * const_iterator;
119  const_iterator begin() const {
120  return v_.get();
121  }
122  const_iterator end() const {
123  return v_.get()+size();
124  }
125  void swap_with(ConstVector &o) {
126  std::swap(sz_, o.sz_);
127  v_.swap(o.v_);
128  }
129 #endif
130  IMP_HASHABLE_INLINE(ConstVector, return boost::hash_range(begin(),
131  end()););
132 };
133 
134 IMP_SWAP_1(ConstVector);
135 
136 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
137 template <class D>
138 inline std::size_t hash_value(const ConstVector<D> &t) {
139  return t.__hash__();
140 }
141 #endif
142 
143 IMPBASE_END_NAMESPACE
144 
145 #endif /* IMPBASE_CONST_VECTOR_H */