9 #ifndef IMPKERNEL_INDEX_H
10 #define IMPKERNEL_INDEX_H
12 #include <IMP/kernel_config.h>
16 #include <cereal/access.hpp>
20 IMPKERNEL_BEGIN_NAMESPACE
30 friend class cereal::access;
32 template<
class Archive>
void serialize(Archive &ar) {
37 explicit Index(
int i) : i_(i) {}
55 inline unsigned int get_as_unsigned_int(
Index<Tag> i) {
59 inline Index<Tag> get_invalid_index() {
60 return Index<Tag>(-1);
69 static const unsigned char COMP_NONE = 0;
72 static const unsigned char COMP_RLE = 1;
77 static const unsigned char COMP_END = 100;
85 template <
class Tag,
class T,
class Equal = std::equal_to<T> >
89 template<
class Archive>
void write_no_compression(
90 Archive &ar,
typename P::const_iterator start,
91 typename P::const_iterator end)
const {
92 size_t sz = end - start;
93 ar(COMP_NONE); ar(sz);
99 template<
class Archive>
void write_rle(
100 Archive &ar,
typename P::const_iterator start,
101 typename P::const_iterator end)
const {
102 size_t sz = end - start;
103 ar(COMP_RLE); ar(sz);
107 friend class cereal::access;
108 template<
class Archive>
void save(Archive &ar)
const {
109 size_t sz = P::size();
111 typename P::const_iterator pos = P::begin(), start = P::begin(), runend;
113 while (pos != P::end()) {
117 for (runend = pos + 1; runend != P::end() && cmp(*runend, val);
120 if (runend - pos > 10) {
121 if (pos > P::begin() && pos > start) {
123 write_no_compression(ar, start, pos);
125 write_rle(ar, pos, runend);
130 if (start != P::end()) {
131 write_no_compression(ar, start, P::end());
137 template<
class Archive>
void load(Archive &ar) {
142 unsigned char comp_type;
144 while(comp_type != COMP_END) {
145 if (comp_type == COMP_NONE) {
152 }
else if (comp_type == COMP_RLE) {
167 IndexVector(
unsigned int sz,
const T &t = T()) :
P(sz, t) {}
170 return P::operator[](get_as_unsigned_int(i)));
173 template <
class Tag,
class Container,
class T>
175 if (v.size() <= get_as_unsigned_int(i)) {
176 v.resize(get_as_unsigned_int(i) + 1, default_value);
180 IMPKERNEL_END_NAMESPACE
183 template <
class Archive,
class Tag,
class T>
184 struct specialize<Archive, IMP::IndexVector<Tag, T>,
185 cereal::specialization::member_load_save> {};
#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)
#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.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Macros to handle array indexing.
Base class for a simple primitive-like type.
Ints get_index(const ParticlesTemp &particles, const Subset &subset, const Subsets &excluded)
Implements a vector tied to a particular index of type Index<Tag>.
A class for storing lists of IMP items.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
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.
An exception for an invalid value being passed to IMP.
Abstract class for containers of particles.
Macros to help with objects that can be printed to a stream.