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
33 friend class cereal::access;
35 template<
class Archive>
void serialize(Archive &ar) {
40 explicit Index(
int i) : i_(i) {}
58 inline unsigned int get_as_unsigned_int(
Index<Tag> i) {
62 inline Index<Tag> get_invalid_index() {
63 return Index<Tag>(-1);
72 static const unsigned char COMP_NONE = 0;
75 static const unsigned char COMP_RLE = 1;
80 static const unsigned char COMP_END = 100;
88 template <
class Tag,
class T,
class Allocator = std::allocator<T>,
89 class Equal = std::equal_to<T> >
93 template<
class Archive>
void write_no_compression(
94 Archive &ar,
typename P::const_iterator start,
95 typename P::const_iterator end)
const {
96 size_t sz = end - start;
97 ar(COMP_NONE); ar(sz);
103 template<
class Archive>
void write_rle(
104 Archive &ar,
typename P::const_iterator start,
105 typename P::const_iterator end)
const {
106 size_t sz = end - start;
107 ar(COMP_RLE); ar(sz);
111 friend class cereal::access;
112 template<
class Archive>
void save(Archive &ar)
const {
113 size_t sz = P::size();
115 typename P::const_iterator pos = P::begin(), start = P::begin(), runend;
117 while (pos != P::end()) {
121 for (runend = pos + 1; runend != P::end() && cmp(*runend, val);
124 if (runend - pos > 10) {
125 if (pos > P::begin() && pos > start) {
127 write_no_compression(ar, start, pos);
129 write_rle(ar, pos, runend);
134 if (start != P::end()) {
135 write_no_compression(ar, start, P::end());
141 template<
class Archive>
void load(Archive &ar) {
146 unsigned char comp_type;
148 while(comp_type != COMP_END) {
149 if (comp_type == COMP_NONE) {
156 }
else if (comp_type == COMP_RLE) {
171 IndexVector(
unsigned int sz,
const T &t = T()) :
P(sz, t) {}
174 return P::operator[](get_as_unsigned_int(i)));
177 template <
class Tag,
class Container,
class T>
179 if (v.size() <= get_as_unsigned_int(i)) {
180 v.resize(get_as_unsigned_int(i) + 1, default_value);
184 IMPKERNEL_END_NAMESPACE
187 template <
class Archive,
class Tag,
class T>
188 struct specialize<Archive, IMP::IndexVector<Tag, T>,
189 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.
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.