IMP logo
IMP Reference Guide  2.9.0
The Integrative Modeling Platform
Vector.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Vector.h
3  * \brief A class for storing lists of IMP items.
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_CONVERTIBLE_VECTOR_H
10 #define IMPKERNEL_CONVERTIBLE_VECTOR_H
11 #include <IMP/kernel_config.h>
12 // do not include anything more from base
13 #include "Showable.h"
14 #include "Value.h"
15 #include <sstream>
16 #include "hash.h"
17 
18 #if defined(_MSC_VER) && _MSC_VER == 1500
19 # include <boost/type_traits.hpp>
20 # include <boost/utility.hpp>
21 #endif
22 
23 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
24 #include <debug/vector>
25 #else
26 #include <vector>
27 #endif
28 
29 IMPKERNEL_BEGIN_NAMESPACE
30 //! A more \imp-like version of the \c std::vector.
31 /** Specifically this class adds functionality from \c Python arrays such as
32  - hashing
33  - output to streams
34  - use of \c +=es
35  - implicit conversion when the contents are implicitly convertible
36  - bounds checking in debug mode
37  */
38 template <class T>
39 class Vector : public Value
40 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
41 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
42  ,
43  public __gnu_debug::vector<T>
44 #else
45  ,
46  public std::vector<T>
47 #endif
48 #endif
49  {
50 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
51  typedef __gnu_debug::vector<T> V;
52 #else
53  typedef std::vector<T> V;
54 #endif
55  public:
56  Vector() {}
57  explicit Vector(unsigned int sz, const T &t = T()) : V(sz, t) {}
58 #if defined(_MSC_VER) && _MSC_VER == 1500
59  template <class It>
60  Vector(It b, It e,
61  typename boost::disable_if<boost::is_integral<It> >::type *t=0) {
62  for (It it = b; it != e; ++it) {
63  push_back(T(*it));
64  }
65  }
66  template <class VO>
67  explicit Vector(const std::vector<VO> &o) {
68  reserve(o.size());
69  for (std::vector<VO>::const_iterator it = o.begin();
70  it != o.end(); ++it) {
71  push_back(T(*it));
72  }
73  }
74 
75  // MSVC 2008 doesn't support std::vector::data() (technically it requires
76  // a C++11 compiler), so provide an implementation
77  T* data() {
78  if (empty()) {
79  return NULL;
80  } else {
81  return &front();
82  }
83  }
84  const T* data() const {
85  if (empty()) {
86  return NULL;
87  } else {
88  return &front();
89  }
90  }
91 
92 #else
93  template <class It>
94  Vector(It b, It e)
95  : V(b, e) {}
96  template <class VO>
97  explicit Vector(const std::vector<VO> &o)
98  : V(o.begin(), o.end()) {}
99 #endif
100  template <class O>
101  operator Vector<O>() const {
102  return Vector<O>(V::begin(), V::end());
103  }
104  template <class OV>
105  Vector<T> operator+=(const OV &o) {
106  V::insert(V::end(), o.begin(), o.end());
107  return *this;
108  }
109 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
110  void show(std::ostream &out = std::cout) const {
111  out << "[";
112  for (unsigned int i = 0; i < V::size(); ++i) {
113  if (i > 0) out << ", ";
114  if (i > 10) {
115  out << ",...";
116  break;
117  }
118  out << Showable(V::operator[](i));
119  }
120  out << "]";
121  }
122  operator Showable() const {
123  std::ostringstream oss;
124  show(oss);
125  return Showable(oss.str());
126  }
127  std::size_t __hash__() const {
128  return boost::hash_range(V::begin(), V::end());
129  }
130 #endif
131 };
132 
133 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
134 template <class T>
135 void swap(Vector<T> &a, Vector<T> &b) {
136  a.swap(b);
137 }
138 
139 template <class T>
140 inline Vector<T> operator+(Vector<T> ret, const Vector<T> &o) {
141  ret.insert(ret.end(), o.begin(), o.end());
142  return ret;
143 }
144 
145 #endif
146 
147 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
148 template <class T>
149 inline std::size_t hash_value(const __gnu_debug::vector<T> &t) {
150  return boost::hash_range(t.begin(), t.end());
151 }
152 #endif
153 
154 IMPKERNEL_END_NAMESPACE
155 
156 #endif /* IMPKERNEL_CONVERTIBLE_VECTOR_H */
IO support.
IO support.
A more IMP-like version of the std::vector.
Definition: Vector.h:39
Base for a simple primitive-like type.
Definition: Value.h:21
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
Basic types used by IMP.