IMP logo
IMP Reference Guide  2.15.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-2021 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 
31 //! A more \imp-like version of the \c std::vector.
32 /** Specifically this class adds functionality from \c Python arrays such as
33  - hashing
34  - output to streams
35  - use of \c +=es
36  - implicit conversion when the contents are implicitly convertible
37  - bounds checking in debug mode
38  */
39 template <class T>
40 class Vector : public Value
41 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
42 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
43  ,
44  public __gnu_debug::vector<T>
45 #else
46  ,
47  public std::vector<T>
48 #endif
49 #endif
50  {
51 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
52  typedef __gnu_debug::vector<T> V;
53 #else
54  typedef std::vector<T> V;
55 #endif
56  public:
57  Vector() {}
58  explicit Vector(unsigned int sz, const T &t = T()) : V(sz, t) {}
59 #if defined(_MSC_VER) && _MSC_VER == 1500
60  template <class It>
61  Vector(It b, It e,
62  typename boost::disable_if<boost::is_integral<It> >::type *t=0) {
63  for (It it = b; it != e; ++it) {
64  push_back(T(*it));
65  }
66  }
67  template <class VO>
68  explicit Vector(const std::vector<VO> &o) {
69  reserve(o.size());
70  for (std::vector<VO>::const_iterator it = o.begin();
71  it != o.end(); ++it) {
72  push_back(T(*it));
73  }
74  }
75 
76  // MSVC 2008 doesn't support std::vector::data() (technically it requires
77  // a C++11 compiler), so provide an implementation
78  T* data() {
79  if (empty()) {
80  return NULL;
81  } else {
82  return &front();
83  }
84  }
85  const T* data() const {
86  if (empty()) {
87  return NULL;
88  } else {
89  return &front();
90  }
91  }
92 
93 #else
94  template <class It>
95  Vector(It b, It e)
96  : V(b, e) {}
97  template <class VO>
98  explicit Vector(const std::vector<VO> &o)
99  : V(o.begin(), o.end()) {}
100 #endif
101  template <class O>
102  operator Vector<O>() const {
103  return Vector<O>(V::begin(), V::end());
104  }
105  template <class OV>
106  Vector<T> operator+=(const OV &o) {
107  V::insert(V::end(), o.begin(), o.end());
108  return *this;
109  }
110 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
111  void show(std::ostream &out = std::cout) const {
112  out << "[";
113  for (unsigned int i = 0; i < V::size(); ++i) {
114  if (i > 0) out << ", ";
115  if (i > 10) {
116  out << ",...";
117  break;
118  }
119  out << Showable(V::operator[](i));
120  }
121  out << "]";
122  }
123  operator Showable() const {
124  std::ostringstream oss;
125  show(oss);
126  return Showable(oss.str());
127  }
128  std::size_t __hash__() const {
129  return boost::hash_range(V::begin(), V::end());
130  }
131 #endif
132 };
133 
134 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
135 template <class T>
136 void swap(Vector<T> &a, Vector<T> &b) {
137  a.swap(b);
138 }
139 
140 template <class T>
141 inline Vector<T> operator+(Vector<T> ret, const Vector<T> &o) {
142  ret.insert(ret.end(), o.begin(), o.end());
143  return ret;
144 }
145 
146 #endif
147 
148 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
149 template <class T>
150 inline std::size_t hash_value(const __gnu_debug::vector<T> &t) {
151  return boost::hash_range(t.begin(), t.end());
152 }
153 #endif
154 
155 IMPKERNEL_END_NAMESPACE
156 
157 #endif /* IMPKERNEL_CONVERTIBLE_VECTOR_H */
Helper functions for implementing hashes.
Helper class to aid in output of IMP classes to streams.
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
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
Base class for a simple primitive-like type.
Helper class to aid in output of IMP classes to streams.
Definition: Showable.h:25