IMP  2.3.0
The Integrative Modeling Platform
Index.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/Index.h \brief Basic types used by IMP.
3  *
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPBASE_INDEX_H
9 #define IMPBASE_INDEX_H
10 
11 #include <IMP/base/base_config.h>
12 #include "bracket_macros.h"
13 #include "showable_macros.h"
14 #include "Value.h"
15 #include <IMP/base/Vector.h>
16 
17 IMPBASE_BEGIN_NAMESPACE /** Define a typed index. This can help disambiguate
18  different
19  integer based indexes floating around to help avoid
20  bugs caused by mixing them up. Care has been taken
21  so
22  that it can be replaced by an integer everywhere, if
23  needed.
24  */
25  template <class Tag>
26 class Index : public Value {
27  int i_;
28 
29  public:
30  explicit Index(int i) : i_(i) {}
31  Index() : i_(-2) {}
32  int get_index() const {
33  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
34  IMP_INTERNAL_CHECK(i_ >= 0, "Invalid index");
35  return i_;
36  }
39  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
40  out << i_;
41  });
43  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
44  return i_;
45  });
46 };
47 template <class Tag>
48 inline unsigned int get_as_unsigned_int(Index<Tag> i) {
49  return i.get_index();
50 }
51 template <class Tag>
52 inline Index<Tag> get_invalid_index() {
53  return Index<Tag>(-1);
54 }
55 
56 /** This class implements a vector tied to a particular index.
57  */
58 template <class Tag, class T>
59 class IndexVector : public Vector<T> {
60  typedef Vector<T> P;
61 
62  public:
63  IndexVector(unsigned int sz, const T &t = T()) : P(sz, t) {}
64  IndexVector() {}
65  IMP_BRACKET(T, Index<Tag>, get_as_unsigned_int(i) < P::size(),
66  return P::operator[](get_as_unsigned_int(i)));
67 };
68 
69 template <class Tag, class Container, class T>
70 void resize_to_fit(Container &v, Index<Tag> i, const T &default_value = T()) {
71  if (v.size() <= get_as_unsigned_int(i)) {
72  v.resize(get_as_unsigned_int(i) + 1, default_value);
73  }
74 }
75 
76 IMPBASE_END_NAMESPACE
77 
78 #endif /* IMPBASE_INDEX_H */
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Ints get_index(const kernel::ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
#define IMP_HASHABLE_INLINE(name, hashret)
Definition: hash_macros.h:18
Base for a simple primitive-like type.
Definition: Value.h:21
#define IMP_BRACKET(Value, Index, bounds_check_expr, expr)
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:141
Various general useful macros for IMP.
Abstract class for containers of particles.
A class for storing lists of IMP items.
Basic types used by IMP.
#define IMP_COMPARISONS_1(Name, field)
Implement comparison in a class using field as the variable to compare.
Various general useful macros for IMP.