-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray_id_func.h
228 lines (187 loc) · 5.53 KB
/
array_id_func.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef ARRAY_ID_FUNC_H
#define ARRAY_ID_FUNC_H
#include "id_func.h"
#include <type_traits>
#include <algorithm>
#include <cassert>
template<class T>
class ArrayIDFunc{
public:
ArrayIDFunc()noexcept:preimage_count_(0), data_(nullptr){}
explicit ArrayIDFunc(int preimage_count)
:preimage_count_(preimage_count){
assert(preimage_count >= 0 && "ids may not be negative");
if(preimage_count == 0)
data_ = nullptr;
else
data_ = new T[preimage_count_];
}
template<class IDFunc>
ArrayIDFunc(const IDFunc&o)
:preimage_count_(o.preimage_count()){
if(preimage_count_ == 0)
data_ = nullptr;
else{
data_ = new T[preimage_count_];
try{
for(int id=0; id<preimage_count_; ++id)
data_[id] = o(id);
}catch(...){
delete[]data_;
throw;
}
}
}
ArrayIDFunc(const ArrayIDFunc&o)
:preimage_count_(o.preimage_count_){
if(preimage_count_ == 0)
data_ = nullptr;
else{
data_ = new T[preimage_count_];
try{
std::copy(o.data_, o.data_ + o.preimage_count_, data_);
}catch(...){
delete[]data_;
throw;
}
}
}
ArrayIDFunc(ArrayIDFunc&&o)noexcept
:preimage_count_(o.preimage_count_), data_(o.data_){
o.preimage_count_ = 0;
o.data_ = nullptr;
}
~ArrayIDFunc(){
delete[]data_;
}
void swap(ArrayIDFunc&o)noexcept{
std::swap(preimage_count_, o.preimage_count_);
std::swap(data_, o.data_);
}
template<class IDFunc>
typename std::enable_if<
is_id_func<IDFunc>::value &&
std::is_convertible<typename id_func_image_type<IDFunc>::type, T>::value,
ArrayIDFunc&>::type operator=(const IDFunc&o){
ArrayIDFunc(o).swap(*this);
return *this;
}
ArrayIDFunc&operator=(const ArrayIDFunc&o){
ArrayIDFunc(o).swap(*this);
return *this;
}
ArrayIDFunc&operator=(ArrayIDFunc&&o)noexcept{
this->~ArrayIDFunc();
data_ = nullptr;
preimage_count_ = 0;
swap(o);
return *this;
}
// IDFunc
int preimage_count() const{return preimage_count_;}
const T&operator()(int id) const{
assert(0 <= id && id < preimage_count_ && "id out of bounds");
return data_[id];
}
// Mutable IDFunc
void set(int id, T t){
assert(0 <= id && id < preimage_count_ && "id out of bounds");
data_[id] = std::move(t);
}
T move(int id){
assert(0 <= id && id < preimage_count_ && "id out of bounds");
return std::move(data_[id]);
}
void fill(const T&t){
std::fill(data_, data_+preimage_count_, t);
}
// Array only functionality
T&operator[](int id){
assert(0 <= id && id < preimage_count_ && "id out of bounds");
return data_[id];
}
const T&operator[](int id) const{
assert(0 <= id && id < preimage_count_ && "id out of bounds");
return data_[id];
}
T*begin(){ return data_; }
const T*begin() const{ return data_; }
T*end(){ return data_ + preimage_count_; }
const T*end()const{ return data_ + preimage_count_; }
int preimage_count_;
T*data_;
};
struct ArrayIDIDFunc : public ArrayIDFunc<int>{
ArrayIDIDFunc()noexcept :image_count_(0){}
ArrayIDIDFunc(int preimage_count, int image_count)
:ArrayIDFunc<int>(preimage_count), image_count_(image_count){}
ArrayIDIDFunc(const ArrayIDIDFunc&o) = default;
ArrayIDIDFunc(ArrayIDIDFunc&&) = default;
ArrayIDIDFunc&operator=(const ArrayIDIDFunc&) = default;
ArrayIDIDFunc&operator=(ArrayIDIDFunc&&) = default;
void swap(ArrayIDIDFunc&o){
std::swap(image_count_, o.image_count_);
ArrayIDFunc<int>::swap(static_cast<ArrayIDFunc<int>&>(o));
}
template<class IDFunc>
ArrayIDIDFunc(const IDFunc&f, int image_count_)
: ArrayIDFunc<int>(f), image_count_(image_count_){}
template<class IDIDFunc>
ArrayIDIDFunc(const IDIDFunc&f/*,
typename std::enable_if<is_id_id_func<IDIDFunc>::value, void>::type*dummy=0*/)
: ArrayIDFunc<int>(f), image_count_(f.image_count()){}
template<class IDIDFunc>
typename std::enable_if<
is_id_id_func<IDIDFunc>::value,
ArrayIDIDFunc&
>::type operator=(const IDIDFunc&o){
ArrayIDIDFunc(o).swap(*this);
return *this;
}
int image_count()const { return image_count_; }
int operator()(int x) const{
assert(0 <= x && x < preimage_count_ && "preimage id out of bounds");
int y = data_[x];
assert(0 <= y && y < image_count_ && "image id out of bounds");
return y;
}
void set_image_count(int new_image_count){
image_count_ = new_image_count;
}
int image_count_;
};
template<class IDFunc>
typename std::enable_if<is_only_id_func<IDFunc>::value, ArrayIDFunc<typename id_func_image_type<IDFunc>::type>>::type
add_preimage_at_end(const IDFunc&in, int n){
ArrayIDFunc<typename id_func_image_type<IDFunc>::type> out(in.preimage_count() + n);
std::copy(in.begin(), in.end(), out.begin());
return out; // NVRO
}
template<class T>
ArrayIDFunc<T>add_preimage_at_end(const ArrayIDFunc<T>&in, int n){
ArrayIDFunc<T> out(in.preimage_count() + n);
std::copy(in.begin(), in.end(), out.begin());
return out; // NVRO
}
template<class T>
ArrayIDFunc<T>add_preimage_at_end(ArrayIDFunc<T>&&in, int n){
ArrayIDFunc<T> out(in.preimage_count() + n);
std::move(in.begin(), in.end(), out.begin());
return out; // NVRO
}
template<class IDIDFunc>
typename std::enable_if<is_id_id_func<IDIDFunc>::value, ArrayIDIDFunc>::type
add_preimage_at_end(const IDIDFunc&in, int n){
ArrayIDIDFunc out(in.preimage_count() + n, in.image_count());
std::copy(in.begin(), in.end(), out.begin());
return out; // NVRO
}
template<class F, class T>
auto add_preimage_at_end(F&&f, int n, const T&t)->decltype(add_preimage_at_end(std::forward<F>(f), n)){
int old_preimage_count = f.preimage_count();
auto x = add_preimage_at_end(std::forward<F>(f), n);
for(int i=old_preimage_count; i<old_preimage_count+n; ++i)
x[i] = t;
return std::move(x);
}
#endif