IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
ConstVector.h
Go to the documentation of this file.
1 /**
2  * \file IMP/ConstVector.h
3  * \brief Store an array of values of the same type.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_CONST_VECTOR_H
10 #define IMPKERNEL_CONST_VECTOR_H
11 
12 #include <IMP/kernel_config.h>
13 #include "base_macros.h"
14 #include "exception.h"
15 #include "Value.h"
16 #include <boost/scoped_array.hpp>
17 #include <IMP/hash.h>
18 #include <iterator>
19 
20 IMPKERNEL_BEGIN_NAMESPACE
21 
22 //! Store an array of values of the same type.
23 /** Items must be comparable and hashable and the arrays
24  cannot be changed after creation.*/
25 template <class Data, class SwigData = Data>
26 class ConstVector : public Value {
27  boost::scoped_array<Data> v_;
28  unsigned int sz_;
29  int compare(const ConstVector &o) const {
30  if (size() < o.size())
31  return -1;
32  else if (size() > o.size())
33  return 1;
34  for (unsigned int i = 0; i < size(); ++i) {
35  if (v_[i] < o.v_[i])
36  return -1;
37  else if (v_[i] > o.v_[i])
38  return 1;
39  }
40  return 0;
41  }
42  void create(unsigned int sz) {
43  if (sz == 0) {
44  v_.reset(nullptr);
45  } else {
46  v_.reset(new Data[sz]);
47  }
48  sz_ = sz;
49  }
50  template <class It>
51  void copy_from(It b, It e) {
52  create(std::distance(b, e));
53  std::copy(b, e, v_.get());
54  }
55 
56  public:
57  ~ConstVector() {}
58  ConstVector(unsigned int sz, Data fill) {
59  create(sz);
60  std::fill(v_.get(), v_.get() + sz, fill);
61  }
62  ConstVector() : v_(0), sz_(0) {}
63  template <class It>
64  ConstVector(It b, It e) {
65  copy_from(b, e);
66  }
67  template <class Vector>
68  explicit ConstVector(const Vector &i) {
69  copy_from(i.begin(), i.end());
70  }
71 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
72  /* Add the arguments to attempt to make VC happy as it tries to
73  use the templated version instead.
74  */
75  ConstVector(const ConstVector<Data, SwigData> &o) : Value(), sz_(0) {
76  copy_from(o.begin(), o.end());
77  }
79  copy_from(o.begin(), o.end());
80  return *this;
81  }
82  ConstVector(int sz) { create(sz); }
83 #endif
85 #ifndef SWIG
86  Data operator[](unsigned int i) const {
87  IMP_USAGE_CHECK(i < sz_, "Out of range");
88  return v_[i];
89  }
90 #ifndef IMP_DOXYGEN
91  void set_item(unsigned int i, SwigData v) const {
92  IMP_USAGE_CHECK(i < sz_, "Out of range");
93  v_[i] = v;
94  ;
95  }
96 #endif
97 #endif
98 #ifndef IMP_DOXYGEN
99  SwigData __getitem__(unsigned int i) const {
100  if (i >= sz_)
101  IMP_THROW("Out of bound " << i << " vs " << sz_, IndexException);
102  return operator[](i);
103  }
104  unsigned int __len__() const { return sz_; }
105 #endif
106 #ifndef SWIG
107  unsigned int size() const { return sz_; }
108 #endif
110  out << "(";
111  for (unsigned int i = 0; i < size(); ++i) {
112  out << Showable(v_[i]);
113  if (i != size() - 1)
114  out << " ";
115  }
116  out << ")";
117  });
118 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
119  typedef const Data *const_iterator;
120  const_iterator begin() const { return v_.get(); }
121  const_iterator end() const { return v_.get() + size(); }
122  void swap_with(ConstVector &o) {
123  std::swap(sz_, o.sz_);
124  v_.swap(o.v_);
125  }
126 #endif
127  IMP_HASHABLE_INLINE(ConstVector, return boost::hash_range(begin(), end()););
128 };
129 
130 IMP_SWAP_1(ConstVector);
131 
132 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
133 template <class D>
134 inline std::size_t hash_value(const ConstVector<D> &t) {
135  return t.__hash__();
136 }
137 #endif
138 
139 IMPKERNEL_END_NAMESPACE
140 
141 #endif /* IMPKERNEL_CONST_VECTOR_H */
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_HASHABLE_INLINE(name, hashret)
Definition: hash_macros.h:18
Helper functions for implementing hashes.
#define IMP_COMPARISONS(Name)
Implement comparison in a class using a compare function.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:40
Base class for a simple primitive-like type.
Definition: Value.h:23
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
Definition: VectorD.h:179
An exception for a request for an invalid member of a container.
Definition: exception.h:156
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
Base class for a simple primitive-like type.
Store an array of values of the same type.
Definition: ConstVector.h:26
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Helper class to aid in output of IMP classes to streams.
Definition: Showable.h:25
Various general useful macros for IMP.