IMP logo
IMP Reference Guide  develop.29b24c6ad6,2021/05/15
The Integrative Modeling Platform
Index.h
Go to the documentation of this file.
1 /**
2  * \file IMP/Index.h
3  * \brief Utility types to refer to various types of indices
4  *
5  * Copyright 2007-2021 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_INDEX_H
10 #define IMPKERNEL_INDEX_H
11 
12 #include <IMP/kernel_config.h>
13 #include "bracket_macros.h"
14 #include "showable_macros.h"
15 #include "Value.h"
16 #include <IMP/Vector.h>
17 
18 IMPKERNEL_BEGIN_NAMESPACE
19 
20 //! A typed index.
21 /** This can help disambiguate different integer based indexes floating
22  around to help avoid bugs caused by mixing them up. Care has been taken
23  so that it can be replaced by an integer everywhere, if needed. */
24 template <class Tag>
25 class Index : public Value {
26  int i_;
27 
28  public:
29  explicit Index(int i) : i_(i) {}
30  Index() : i_(-2) {}
31  int get_index() const {
32  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
33  IMP_INTERNAL_CHECK(i_ >= 0, "Invalid index");
34  return i_;
35  }
38  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
39  out << i_;
40  });
42  IMP_INTERNAL_CHECK(i_ != -2, "Uninitialized index");
43  return i_;
44  });
45 };
46 template <class Tag>
47 inline unsigned int get_as_unsigned_int(Index<Tag> i) {
48  return i.get_index();
49 }
50 template <class Tag>
51 inline Index<Tag> get_invalid_index() {
52  return Index<Tag>(-1);
53 }
54 
55 //! Implements a vector tied to a particular index of type Index<Tag>.
56 template <class Tag, class T>
57 class IndexVector : public Vector<T> {
58  typedef Vector<T> P;
59 
60  public:
61  IndexVector(unsigned int sz, const T &t = T()) : P(sz, t) {}
62  IndexVector() {}
63  IMP_BRACKET(T, Index<Tag>, get_as_unsigned_int(i) < P::size(),
64  return P::operator[](get_as_unsigned_int(i)));
65 };
66 
67 template <class Tag, class Container, class T>
68 void resize_to_fit(Container &v, Index<Tag> i, const T &default_value = T()) {
69  if (v.size() <= get_as_unsigned_int(i)) {
70  v.resize(get_as_unsigned_int(i) + 1, default_value);
71  }
72 }
73 
74 IMPKERNEL_END_NAMESPACE
75 
76 #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:25
#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:40
#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 class for a simple primitive-like type.
Definition: Value.h:23
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
Implements a vector tied to a particular index of type Index<Tag>.
Definition: Index.h:57
A class for storing lists of IMP items.
Base class for a simple primitive-like type.
#define IMP_COMPARISONS_1(Name, field)
Implement comparison in a class using field as the variable to compare.
Abstract class for containers of particles.
Macros to help with objects that can be printed to a stream.