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