-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.hpp
More file actions
635 lines (566 loc) · 15.6 KB
/
set.hpp
File metadata and controls
635 lines (566 loc) · 15.6 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
isl-cpp: C++ bindings to the ISL (Integer Set Library)
Copyright (C) 2014 Jakob Leben <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef ISL_CPP_SET_INCLUDED
#define ISL_CPP_SET_INCLUDED
#include "context.hpp"
#include "object.hpp"
#include "point.hpp"
#include "space.hpp"
#include "matrix.hpp"
#include "printer.hpp"
#include <isl/set.h>
#include <isl/union_set.h>
#include <isl/ilp.h>
namespace isl {
class basic_map;
class map;
class union_map;
class expression;
class constraint;
template<>
struct object_behavior<isl_basic_set>
{
static isl_basic_set * copy( isl_basic_set * obj )
{
return isl_basic_set_copy(obj);
}
static void destroy( isl_basic_set *obj )
{
isl_basic_set_free(obj);
}
static isl_ctx * get_context( isl_basic_set * obj )
{
return isl_basic_set_get_ctx(obj);
}
};
template<>
struct object_behavior<isl_set>
{
static isl_set * copy( isl_set * obj )
{
return isl_set_copy(obj);
}
static void destroy( isl_set *obj )
{
isl_set_free(obj);
}
static isl_ctx * get_context( isl_set * obj )
{
return isl_set_get_ctx(obj);
}
};
template<>
struct object_behavior<isl_union_set>
{
static isl_union_set * copy( isl_union_set * obj )
{
return isl_union_set_copy(obj);
}
static void destroy( isl_union_set *obj )
{
isl_union_set_free(obj);
}
static isl_ctx * get_context( isl_union_set * obj )
{
return isl_union_set_get_ctx(obj);
}
};
class basic_set : public object<isl_basic_set>
{
public:
basic_set( isl_basic_set * ptr ): object( ptr ) {}
basic_set( const space & spc ):
object(spc.ctx(), isl_basic_set_empty(spc.copy()))
{}
basic_set( const space & spc,
const matrix & equalities, const matrix & inequalities ):
object(isl_basic_set_from_constraint_matrices(spc.copy(),
equalities.copy(),
inequalities.copy(),
isl_dim_param,
isl_dim_div,
isl_dim_set,
isl_dim_cst))
{}
basic_set( context & ctx, const string & text ):
object(ctx, isl_basic_set_read_from_str(ctx.get(), text.c_str()))
{}
static basic_set universe( const space & s )
{
return isl_basic_set_universe(s.copy());
}
space get_space() const
{
return space( isl_basic_set_get_space(get()) );
}
isl::local_space local_space() const
{
return isl_basic_set_get_local_space(get());
}
unsigned dimensions() const
{
return isl_basic_set_dim( get(), isl_dim_set );
}
bool is_empty() const
{
return isl_basic_set_is_empty(get());
}
void insert_dimensions( space::dimension_type t, unsigned i, unsigned n=1 )
{
m_object = isl_basic_set_insert_dims(m_object, (isl_dim_type)t, i, n);
}
void add_dimensions( space::dimension_type t, unsigned n=1 )
{
m_object = isl_basic_set_add_dims(m_object, (isl_dim_type)t, n);
}
void project_out_dimensions( space::dimension_type t, unsigned i, unsigned n=1 )
{
m_object = isl_basic_set_project_out(m_object, (isl_dim_type)t, i, n);
}
void add_constraint( const constraint & c);
void drop_constraints_with( space::dimension_type t, unsigned i, unsigned n=1)
{
m_object = isl_basic_set_drop_constraints_involving_dims
(m_object, (isl_dim_type) t, i, n);
}
basic_map unwrapped();
basic_set lifted() const
{
return isl_basic_set_lift(copy());
}
basic_set flattened() const
{
return isl_basic_set_flatten(copy());
}
static bool are_disjoint( const basic_set & a, const basic_set & b )
{
return isl_basic_set_is_disjoint(a.get(), b.get());
}
bool is_disjoint(const basic_set & b) const
{
return are_disjoint(*this, b);
}
bool is_subset_of(const basic_set & other) const
{
return isl_basic_set_is_subset(get(), other.get());
}
point single_point() const
{
isl_point *p = isl_basic_set_sample_point(copy());
if (!p)
throw error("No single point.");
return p;
}
value maximum( const expression & expr ) const;
set lex_minimum() const;
set lex_maximum() const;
#if 0 // Not available?
basic_set & limit_above(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_basic_set_upper_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
basic_set & limit_below(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_basic_set_lower_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
#endif
};
class set : public object<isl_set>
{
public:
set( isl_set * ptr ): object(ptr) {}
set( const space & spc ):
object(spc.ctx(), isl_set_empty(spc.copy()))
{}
set( const basic_set & bset ):
object(bset.ctx(), isl_set_from_basic_set(bset.copy()))
{}
set( context & ctx, const string & text ):
object(ctx, isl_set_read_from_str(ctx.get(), text.c_str()))
{}
static set universe( const space & s )
{
return isl_set_universe(s.copy());
}
space get_space() const
{
return space( isl_set_get_space(get()) );
}
unsigned dimensions() const
{
return isl_set_dim( get(), isl_dim_set );
}
identifier id() const
{
isl_id *c_id = isl_set_get_tuple_id(get());
identifier id(c_id);
isl_id_free(c_id);
return id;
}
void set_id(const identifier & id )
{
isl_id *c_id = id.c_id(m_ctx.get());
if (c_id)
m_object = isl_set_set_tuple_id(get(), c_id);
}
void clear_id()
{
m_object = isl_set_reset_tuple_id(m_object);
}
string name() const
{
return isl_set_get_tuple_name(get());
}
void set_name( const string & name )
{
m_object = isl_set_set_tuple_name(m_object, name.c_str());
}
bool is_empty() const
{
return isl_set_is_empty(get());
}
bool is_plain_universe() const
{
return isl_set_plain_is_universe(get());
}
bool is_subset_of(const set & other) const
{
return isl_set_is_subset(get(), other.get());
}
bool is_strict_subset_of(const set & other) const
{
return isl_set_is_strict_subset(get(), other.get());
}
void add_dimensions( space::dimension_type t, unsigned n=1 )
{
m_object = isl_set_add_dims(m_object, (isl_dim_type) t, n);
}
void project_out_dimensions( space::dimension_type t, unsigned i, unsigned n=1 )
{
m_object = isl_set_project_out(m_object, (isl_dim_type)t, i, n);
}
value minimum( const expression & expr ) const;
value maximum( const expression & expr ) const;
set lex_minimum() const
{
return isl_set_lexmin(copy());
}
set lex_maximum() const
{
return isl_set_lexmax(copy());
}
void coalesce()
{
m_object = isl_set_coalesce(m_object);
}
void insert_dimensions( unsigned pos, unsigned count )
{
m_object = isl_set_insert_dims(m_object, isl_dim_set, pos, count);
}
void add_constraint( const constraint & c);
void drop_constraints_with( space::dimension_type t, unsigned i, unsigned n=1)
{
m_object = isl_set_drop_constraints_involving_dims
(m_object, (isl_dim_type) t, i, n);
}
set & limit_above(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_set_upper_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
set & limit_below(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_set_lower_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
map unwrapped();
set lifted() const
{
return isl_set_lift(copy());
}
set flattened() const
{
return isl_set_flatten(copy());
}
set parameters() const
{
return isl_set_params(copy());
}
set equate(int dim1, int dim2) const
{
return isl_set_equate(copy(), isl_dim_set, dim1, isl_dim_set, dim2);
}
basic_set convex_hull() const
{
return isl_set_convex_hull(copy());
}
basic_set simple_hull() const
{
return isl_set_simple_hull(copy());
}
bool is_singleton() const
{
return isl_set_is_singleton(get());
}
point single_point() const
{
isl_point *p = isl_set_sample_point(copy());
if (!p)
throw error("No single point.");
return p;
}
static bool are_disjoint( const set & a, const set & b )
{
return isl_set_is_disjoint(a.get(), b.get());
}
bool is_disjoint(const set & b) const
{
return are_disjoint(*this, b);
}
template <typename F>
void for_each( F f ) const
{
isl_set_foreach_basic_set(get(), &for_each_basic_set_helper<F>, &f);
}
private:
template <typename F>
static isl_stat for_each_basic_set_helper(isl_basic_set *bs_ptr, void *data_ptr)
{
auto f_ptr = reinterpret_cast<F*>(data_ptr);
basic_set bs(bs_ptr);
bool result = (*f_ptr)(bs);
return result ? isl_stat_ok : isl_stat_error;
}
};
class union_set : public object<isl_union_set>
{
public:
union_set( isl_union_set * ptr ): object(ptr) {}
union_set( const context & ctx ):
object(ctx, isl_union_set_empty(isl_space_params_alloc(ctx.get(),0)))
{}
union_set( const space & param_space ):
object(param_space.ctx(), isl_union_set_empty(param_space.copy()))
{}
union_set( context & ctx, const string & text ):
object(ctx, isl_union_set_read_from_str(ctx.get(), text.c_str()))
{}
union_set( const basic_set & bs ):
object(bs.ctx(), isl_union_set_from_basic_set(bs.copy()))
{}
union_set( const set & s ):
object(s.ctx(), isl_union_set_from_set(s.copy()))
{}
space get_space() const
{
return space( isl_union_set_get_space(get()) );
}
bool is_empty() const
{
return isl_union_set_is_empty(get());
}
bool is_subset_of(const union_set & other) const
{
return isl_union_set_is_subset(get(), other.get());
}
bool is_strict_subset_of(const union_set & other) const
{
return isl_union_set_is_strict_subset(get(), other.get());
}
union_map unwrapped();
union_set lifted() const
{
return isl_union_set_lift(copy());
}
union_set universe() const
{
return isl_union_set_universe(copy());
}
set set_for( const space & spc ) const
{
return isl_union_set_extract_set(get(), spc.copy());
}
set single_set() const
{
auto the_set = isl_set_from_union_set(copy());
if (!the_set)
throw error("No single set.");
return the_set;
}
template <typename F>
void for_each( F f ) const
{
isl_union_set_foreach_set(get(), &for_each_helper<F>, &f);
}
private:
template <typename F>
static isl_stat for_each_helper(isl_set *set_ptr, void *data_ptr)
{
auto f_ptr = reinterpret_cast<F*>(data_ptr);
set s(set_ptr);
bool result = (*f_ptr)(s);
return result ? isl_stat_ok : isl_stat_error;
}
};
inline
set basic_set::lex_minimum() const
{
return isl_basic_set_lexmin(copy());
}
inline
set basic_set::lex_maximum() const
{
return isl_basic_set_lexmax(copy());
}
inline
set operator!( const set & s )
{
isl_set *x = isl_set_complement(s.copy());
return set(x);
}
inline
set operator&( const set & lhs, const set & rhs )
{
isl_set *x = isl_set_intersect(lhs.copy(), rhs.copy());
return set(x);
}
inline
basic_set operator&( const basic_set & lhs, const basic_set & rhs )
{
isl_basic_set *x = isl_basic_set_intersect(lhs.copy(), rhs.copy());
return basic_set(x);
}
inline
union_set operator&( const union_set & lhs, const union_set & rhs )
{
return isl_union_set_intersect(lhs.copy(), rhs.copy());
}
inline
set & operator&=(set & lhs, const set & rhs )
{
lhs = lhs & rhs;
return lhs;
}
inline
union_set & operator&=(union_set & lhs, const union_set & rhs )
{
lhs = lhs & rhs;
return lhs;
}
inline
set operator|( const set & lhs, const set & rhs )
{
isl_set *u = isl_set_union(lhs.copy(), rhs.copy());
return set(u);
}
inline
set operator|( const basic_set & lhs, const basic_set & rhs )
{
isl_set *u = isl_basic_set_union(lhs.copy(), rhs.copy());
return set(u);
}
inline
union_set operator| (const union_set &lhs, const union_set & rhs)
{
return isl_union_set_union(lhs.copy(), rhs.copy());
}
inline
union_set operator| (const union_set &lhs, const set & rhs)
{
return isl_union_set_union(lhs.copy(), isl_union_set_from_set(rhs.copy()));
}
inline
union_set operator| (const union_set &lhs, const basic_set & rhs)
{
return isl_union_set_union(lhs.copy(), isl_union_set_from_basic_set(rhs.copy()));
}
inline
set & operator|=(set & lhs, const set & rhs )
{
lhs = lhs | rhs;
return lhs;
}
inline
union_set & operator|=(union_set & lhs, const union_set & rhs )
{
lhs = lhs | rhs;
return lhs;
}
inline
set operator* ( const set & lhs, const set & rhs )
{
return isl_set_product(lhs.copy(), rhs.copy());
}
inline
union_set operator* ( const union_set & lhs, const union_set & rhs )
{
return isl_union_set_product(lhs.copy(), rhs.copy());
}
inline
set operator- (const set & lhs, const set & rhs)
{
return isl_set_subtract(lhs.copy(), rhs.copy());
}
inline
union_set operator- (const union_set & lhs, const union_set & rhs)
{
return isl_union_set_subtract(lhs.copy(), rhs.copy());
}
inline
bool operator==( const basic_set & lhs, const basic_set & rhs)
{
return isl_basic_set_is_equal(lhs.get(), rhs.get());
}
inline
bool operator==( const set & lhs, const set & rhs)
{
return isl_set_is_equal(lhs.get(), rhs.get());
}
template <> inline
void printer::print<basic_set>( const basic_set & s )
{
m_printer = isl_printer_print_basic_set(m_printer, s.get());
}
template <> inline
void printer::print<set>( const set & s )
{
m_printer = isl_printer_print_set(m_printer, s.get());
}
template <> inline
void printer::print<union_set>( const union_set & s )
{
m_printer = isl_printer_print_union_set(m_printer, s.get());
}
template <> inline
void printer::print_each_in<set>(const set & u)
{
u.for_each([this](const basic_set & s){
print(s); std::cout << std::endl;
return true;
});
}
template <> inline
void printer::print_each_in<union_set>(const union_set & us)
{
us.for_each([this](const set & s){
print(s); std::cout << std::endl;
return true;
});
}
}
#endif // ISL_CPP_SET_INCLUDED