IMP  2.0.1
The Integrative Modeling Platform
Vector.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/Vector.h
3  * \brief A class for storing lists of IMP items.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_CONVERTIBLE_VECTOR_H
10 #define IMPBASE_CONVERTIBLE_VECTOR_H
11 #include <IMP/base/base_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 IMP_COMPILER_HAS_DEBUG_VECTOR && IMP_HAS_CHECKS >= IMP_INTERNAL
19 #include <debug/vector>
20 #else
21 #include <vector>
22 #endif
23 
24 
25 IMPBASE_BEGIN_NAMESPACE
26 
27 /** This class provides a more \imp-like version of the \c std::vector.
28  Specifically it adds functionality from \c Python arrays such as
29  - hashing
30  - output to streams
31  - use of \c +=es
32  - implicit conversion when the contents are implicitly convertible
33  - bounds checking in debug mode
34 */
35 template <class T>
36 class Vector: public Value
37 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
38 #if IMP_COMPILER_HAS_DEBUG_VECTOR && IMP_HAS_CHECKS >= IMP_INTERNAL
39  , public __gnu_debug::vector<T>
40 #else
41  , public std::vector<T>
42 #endif
43 #endif
44  {
45 #if IMP_COMPILER_HAS_DEBUG_VECTOR && IMP_HAS_CHECKS >= IMP_INTERNAL
46  typedef __gnu_debug::vector<T> V;
47 #else
48  typedef std::vector<T> V;
49 #endif
50  public:
51  Vector(){}
52  explicit Vector(unsigned int sz, const T&t=T()): V(sz, t){}
53  template <class It>
54  Vector(It b, It e): V(b,e){}
55  template <class VO>
56  explicit Vector(const std::vector<VO> &o): V(o.begin(), o.end()){}
57  template <class O>
58  operator Vector<O>() const {
59  return Vector<O>(V::begin(), V::end());
60  }
61  template <class OV>
62  Vector<T> operator+=(const OV &o) {
63  V::insert(V::end(), o.begin(), o.end());
64  return *this;
65  }
66 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
67  void show(std::ostream &out=std::cout) const{
68  out << "[";
69  for (unsigned int i=0; i< V::size(); ++i) {
70  if (i >0) out << ", ";
71  if (i > 10) {
72  out << ",...";
73  break;
74  }
75  out << Showable(V::operator[](i));
76  }
77  out<< "]";
78  }
79  operator Showable() const {
80  std::ostringstream oss;
81  show(oss);
82  return Showable(oss.str());
83  }
84  std::size_t __hash__() const {
85  return boost::hash_range(V::begin(),
86  V::end());
87  }
88 #endif
89 };
90 
91 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
92 template <class T>
93 void swap(Vector<T> &a,
94  Vector<T> &b) {
95  a.swap(b);
96 }
97 
98 template <class T>
99 inline Vector<T> operator+( Vector<T> ret,
100  const Vector<T> &o) {
101  ret.insert(ret.end(), o.begin(), o.end());
102  return ret;
103 }
104 
105 #endif
106 
107 #if IMP_COMPILER_HAS_DEBUG_VECTOR && IMP_HAS_CHECKS >= IMP_INTERNAL
108 template <class T>
109 inline std::size_t hash_value(const __gnu_debug::vector<T> &t) {
110  return boost::hash_range(t.begin(), t.end());
111 }
112 #endif
113 
114 IMPBASE_END_NAMESPACE
115 
116 #endif /* IMPBASE_CONVERTIBLE_VECTOR_H */