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