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