IMP logo
IMP Reference Guide  2.17.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-2022 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 #else
76  template <class It>
77  Vector(It b, It e)
78  : V(b, e) {}
79  template <class VO>
80  explicit Vector(const std::vector<VO> &o)
81  : V(o.begin(), o.end()) {}
82 #endif
83  template <class O>
84  operator Vector<O>() const {
85  return Vector<O>(V::begin(), V::end());
86  }
87  template <class OV>
88  Vector<T> operator+=(const OV &o) {
89  V::insert(V::end(), o.begin(), o.end());
90  return *this;
91  }
92 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
93  void show(std::ostream &out = std::cout) const {
94  out << "[";
95  for (unsigned int i = 0; i < V::size(); ++i) {
96  if (i > 0) out << ", ";
97  if (i > 10) {
98  out << ",...";
99  break;
100  }
101  out << Showable(V::operator[](i));
102  }
103  out << "]";
104  }
105  operator Showable() const {
106  std::ostringstream oss;
107  show(oss);
108  return Showable(oss.str());
109  }
110  std::size_t __hash__() const {
111  return boost::hash_range(V::begin(), V::end());
112  }
113 #endif
114 };
115 
116 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
117 template <class T>
118 void swap(Vector<T> &a, Vector<T> &b) {
119  a.swap(b);
120 }
121 
122 template <class T>
123 inline Vector<T> operator+(Vector<T> ret, const Vector<T> &o) {
124  ret.insert(ret.end(), o.begin(), o.end());
125  return ret;
126 }
127 
128 #endif
129 
130 #if IMP_COMPILER_HAS_DEBUG_VECTOR &&IMP_HAS_CHECKS >= IMP_INTERNAL
131 template <class T>
132 inline std::size_t hash_value(const __gnu_debug::vector<T> &t) {
133  return boost::hash_range(t.begin(), t.end());
134 }
135 #endif
136 
137 IMPKERNEL_END_NAMESPACE
138 
139 #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