-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse.hpp
More file actions
137 lines (101 loc) · 3.43 KB
/
sparse.hpp
File metadata and controls
137 lines (101 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef COMPRESSED_HPP
#define COMPRESSED_HPP
// TODO rename sparse.hpp
#include <array>
#include <memory>
#include <cassert>
#include <stdexcept>
#include <iostream>
namespace sparse {
static std::size_t index(std::size_t mask, std::size_t index) {
const std::size_t res = __builtin_popcount(mask & ((1ul << index) - 1));
return res;
}
class base {
protected:
const std::size_t mask;
base(std::size_t mask): mask(mask) {}
public:
virtual ~base() {}
// TODO make this portable
std::size_t size() const { return __builtin_popcount(mask); }
bool has(std::size_t index) const { return mask & (1ul << index); }
};
template<class T>
class array: public base {
using base::base;
T data[0] = {};
public:
T& get(std::size_t index) {
assert(((1ul << index) & this->mask) && "index error");
return data[sparse::index(this->mask, index)];
}
const T& get(std::size_t index) const {
assert(((1ul << index) & this->mask) && "index error");
return data[sparse::index(this->mask, index)];
}
virtual std::shared_ptr<array> set(std::size_t index, T&& value) const = 0;
template<class Cont>
void iter(std::size_t start, Cont cont) const {
static constexpr std::size_t bits = 8 * sizeof(std::size_t);
std::size_t mask = this->mask;
std::size_t tz, index = start;
const T* it = data;
while(mask) {
tz = __builtin_ctz(mask);
index += tz;
cont(index++, *it++);
mask >>= (tz + 1);
}
}
};
template<class T, std::size_t M, std::size_t N>
struct storage;
template<class T, std::size_t M, class... Args>
static std::enable_if_t<(sizeof...(Args) <= M),
std::shared_ptr<storage<T, M, sizeof...(Args)>>>
make_storage(std::size_t mask, Args&&... args) {
return std::make_shared<storage<T, M, sizeof...(Args)>>(
mask, std::forward<Args>(args)...);
}
template<class T, std::size_t M, class... Args>
static std::enable_if_t<(sizeof...(Args) > M), std::shared_ptr<array<T>>>
make_storage(std::size_t mask, Args&&... args) {
throw std::logic_error("size error");
}
template<class T, std::size_t M, std::size_t N>
struct storage: array<T> {
static_assert(N > 0, "empty array");
static_assert(N <= M, "size error");
using values_type = std::array<T, N>;
values_type values;
// general
template<class... Args>
storage(std::size_t mask, Args&&... args):
array<T>(mask), values{std::forward<Args>(args)...} {
static_assert(sizeof...(Args) == N, "size error");
assert(mask && "empty mask");
}
template<std::size_t... Is>
std::shared_ptr<array<T>> set(std::size_t index, T&& value,
std::index_sequence<Is...>) const {
const std::size_t compressed = sparse::index(this->mask, index);
if(this->mask & (1ul << index)) {
return make_storage<T, M>(
this->mask,
((Is == compressed) ? std::move(value) : T{values[Is]})...);
} else {
// insert value into values: {values[Is]..., value, values[Is - 1]...}
return make_storage<T, M>(
this->mask | (1ul << index),
((Is == compressed) ? std::move(value)
: T{values[Is > compressed ? Is - 1 : Is]})...,
((compressed == N) ? std::move(value) : T{values[N - 1]}));
}
}
std::shared_ptr<array<T>> set(std::size_t index, T&& value) const override {
return set(index, std::move(value), std::make_index_sequence<N>{});
}
};
} // namespace sparse
#endif