IMP logo
IMP Reference Guide  2.7.0
The Integrative Modeling Platform
Index.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Index.h \brief Basic types used by IMP.
3  *
4  * Copyright 2007-2017 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPKERNEL_INDEX_H
9 #define IMPKERNEL_INDEX_H
10 
11 #include <IMP/kernel_config.h>
12 #include "bracket_macros.h"
13 #include "showable_macros.h"
14 #include "Value.h"
15 #include <IMP/Vector.h>
16 
17 IMPKERNEL_BEGIN_NAMESPACE
18 
19 //! A typed index.
20 /** This can help disambiguate different integer based indexes floating
21  around to help avoid bugs caused by mixing them up. Care has been taken
22  so that it can be replaced by an integer everywhere, if needed. */
23 template <class Tag>
24 class Index : public Value {
25  int i_;
26 
27  public:
28  explicit Index(int i) : i_(i) {}
29  Index() : i_(-2) {}
30  int get_index() const {
31  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
32  IMP_INTERNAL_CHECK(i_ >= 0, "Invalid index");
33  return i_;
34  }
37  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
38  out << i_;
39  });
41  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
42  return i_;
43  });
44 };
45 template <class Tag>
46 inline unsigned int get_as_unsigned_int(Index<Tag> i) {
47  return i.get_index();
48 }
49 template <class Tag>
50 inline Index<Tag> get_invalid_index() {
51  return Index<Tag>(-1);
52 }
53 
54 /** This class implements a vector tied to a particular index
55  of type Index<Tag>.
56  */
57 template <class Tag, class T>
58 class IndexVector : public Vector<T> {
59  typedef Vector<T> P;
60 
61  public:
62  IndexVector(unsigned int sz, const T &t = T()) : P(sz, t) {}
63  IndexVector() {}
64  IMP_BRACKET(T, Index<Tag>, get_as_unsigned_int(i) < P::size(),
65  return P::operator[](get_as_unsigned_int(i)));
66 };
67 
68 template <class Tag, class Container, class T>
69 void resize_to_fit(Container &v, Index<Tag> i, const T &default_value = T()) {
70  if (v.size() <= get_as_unsigned_int(i)) {
71  v.resize(get_as_unsigned_int(i) + 1, default_value);
72  }
73 }
74 
75 IMPKERNEL_END_NAMESPACE
76 
77 #endif /* IMPKERNEL_INDEX_H */
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_HASHABLE_INLINE(name, hashret)
Definition: hash_macros.h:18
A typed index.
Definition: Index.h:24
#define IMP_BRACKET(Value, Index, bounds_check_expr, expr)
Implement operator[] and at() for C++, and getitem for Python.
A more IMP-like version of the std::vector.
Definition: Vector.h:39
#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:139
Macros to handle array indexing.
Base for a simple primitive-like type.
Definition: Value.h:21
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
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.
Abstract class for containers of particles.
Various general useful macros for IMP.