-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.hpp
More file actions
699 lines (664 loc) · 19.1 KB
/
map.hpp
File metadata and controls
699 lines (664 loc) · 19.1 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
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_MAP_INCLUDED
#define ISL_CPP_MAP_INCLUDED
#include "context.hpp"
#include "object.hpp"
#include "space.hpp"
#include "set.hpp"
#include "expression.hpp"
#include "constraint.hpp"
#include "matrix.hpp"
#include "printer.hpp"
#include <isl/map.h>
#include <iostream>
namespace isl {
template<>
struct object_behavior<isl_basic_map>
{
static isl_basic_map * copy( isl_basic_map * obj )
{
return isl_basic_map_copy(obj);
}
static void destroy( isl_basic_map *obj )
{
isl_basic_map_free(obj);
}
static isl_ctx * get_context( isl_basic_map * obj )
{
return isl_basic_map_get_ctx(obj);
}
};
template<>
struct object_behavior<isl_map>
{
static isl_map * copy( isl_map * obj )
{
return isl_map_copy(obj);
}
static void destroy( isl_map *obj )
{
isl_map_free(obj);
}
static isl_ctx * get_context( isl_map * obj )
{
return isl_map_get_ctx(obj);
}
};
template<>
struct object_behavior<isl_union_map>
{
static isl_union_map * copy( isl_union_map * obj )
{
return isl_union_map_copy(obj);
}
static void destroy( isl_union_map *obj )
{
isl_union_map_free(obj);
}
static isl_ctx * get_context( isl_union_map * obj )
{
return isl_union_map_get_ctx(obj);
}
};
class basic_map : public object<isl_basic_map>
{
public:
basic_map( isl_basic_map * map ): object(map) {}
basic_map( const space & spc ):
object(spc.ctx(), isl_basic_map_empty(spc.copy()))
{}
basic_map( context & ctx, const string & text ):
object(isl_basic_map_read_from_str(ctx.get(), text.c_str()))
{}
basic_map( const space & spc,
const matrix & equalities, const matrix & inequalities ):
object(isl_basic_map_from_constraint_matrices(spc.copy(),
equalities.copy(),
inequalities.copy(),
isl_dim_param,
isl_dim_div,
isl_dim_in,
isl_dim_out,
isl_dim_cst))
{}
basic_map( const expression & expr ):
object(expr.ctx(), isl_basic_map_from_aff(expr.copy()))
{}
static basic_map universe( const space & s )
{
return isl_basic_map_universe(s.copy());
}
static basic_map identity( const space & in, const space & out )
{
auto map_space = isl_space_map_from_domain_and_range(in.copy(), out.copy());
return isl_basic_map_identity(map_space);
}
static basic_map between(const basic_set & in, const basic_set & out)
{
auto map_space = isl_space_map_from_domain_and_range(in.get_space().copy(), out.get_space().copy());
auto m = isl_basic_map_universe(map_space);
m = isl_basic_map_intersect_domain(m, in.copy());
m = isl_basic_map_intersect_range(m, out.copy());
return m;
}
space get_space() const
{
return space( isl_basic_map_get_space(get()) );
}
isl::local_space local_space() const
{
return isl_basic_map_get_local_space(get());
}
basic_set range() const
{
return isl_basic_map_range(copy());
}
basic_set domain() const
{
return isl_basic_map_domain(copy());
}
bool is_empty() const
{
return isl_basic_map_is_empty(get());
}
bool is_single_valued() const
{
return isl_basic_map_is_single_valued(get());
}
bool is_subset_of(const basic_map & other) const
{
return isl_basic_map_is_subset(get(), other.get());
}
bool is_strict_subset_of(const basic_map & other) const
{
return isl_basic_map_is_strict_subset(get(), other.get());
}
basic_map inverse() const
{
return basic_map( isl_basic_map_reverse(copy()) );
}
basic_set wrapped() const
{
return isl_basic_map_wrap(copy());
}
void project_out_dimensions( space::dimension_type type, unsigned i, unsigned n=1 )
{
m_object = isl_basic_map_project_out(m_object, (isl_dim_type)type, i, n);
}
void add_constraint( const constraint & c )
{
auto isl_c = c.copy();
if (c.local_space().is_wrapping())
isl_c = isl_constraint_unwrap_local_space(isl_c);
m_object = isl_basic_map_add_constraint(m_object, isl_c);
}
void drop_constraints_with( space::dimension_type t, unsigned i, unsigned n=1)
{
m_object = isl_basic_map_drop_constraints_involving_dims
(m_object, (isl_dim_type) t, i, n);
}
matrix equalities_matrix() const
{
return isl_basic_map_equalities_matrix
(get(),
isl_dim_param,
isl_dim_div,
isl_dim_in,
isl_dim_out,
isl_dim_cst);
}
matrix inequalities_matrix() const
{
return isl_basic_map_inequalities_matrix
(get(),
isl_dim_param,
isl_dim_div,
isl_dim_in,
isl_dim_out,
isl_dim_cst);
}
basic_map in_domain( const basic_set & domain ) const
{
return isl_basic_map_intersect_domain(copy(), domain.copy());
}
basic_map in_range( const basic_set & range ) const
{
return isl_basic_map_intersect_range(copy(), range.copy());
}
basic_map & limit_above(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_basic_map_upper_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
basic_map & limit_below(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_basic_map_lower_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
basic_map cross( const basic_map & rhs ) const
{
return isl_basic_map_product(copy(), rhs.copy());
}
basic_map equate(int in_dim, int out_dim) const
{
return isl_basic_map_equate(copy(), isl_dim_in, in_dim, isl_dim_out, out_dim);
}
basic_set deltas() const
{
return isl_basic_map_deltas(copy());
}
};
class map : public object<isl_map>
{
public:
map( isl_map * ptr ): object(ptr) {}
map( const space & spc ):
object(spc.ctx(), isl_map_empty(spc.copy()))
{}
map( context & ctx, const string & text ):
object(ctx, isl_map_read_from_str(ctx.get(), text.c_str()))
{}
map( const expression & expr ):
object(expr.ctx(), isl_map_from_aff(expr.copy()))
{}
map( const basic_map &bm ):
object(bm.ctx(), isl_map_from_basic_map(bm.copy()))
{}
static map universe( const space & s )
{
return isl_map_universe(s.copy());
}
static map identity( const space & in, const space & out )
{
auto map_space = isl_space_map_from_domain_and_range(in.copy(), out.copy());
return isl_map_identity(map_space);
}
static map between(const set & in, const set & out)
{
auto map_space = isl_space_map_from_domain_and_range(in.get_space().copy(), out.get_space().copy());
auto m = isl_map_universe(map_space);
m = isl_map_intersect_domain(m, in.copy());
m = isl_map_intersect_range(m, out.copy());
return m;
}
space get_space() const
{
return space( isl_map_get_space(get()) );
}
#if 0
space domain_space()
{
return space( isl_space_domain_map(isl_map_get_space(get())) );
}
space range_space()
{
return space( isl_space_range_map(isl_map_get_space(get())) );
}
#endif
set range() const
{
return isl_map_range(copy());
}
set domain() const
{
return isl_map_domain(copy());
}
bool is_single_valued() const
{
return isl_map_is_single_valued(get());
}
bool is_empty() const
{
return isl_map_is_empty(get());
}
bool is_subset_of(const map & other) const
{
return isl_map_is_subset(get(), other.get());
}
bool is_strict_subset_of(const map & other) const
{
return isl_map_is_strict_subset(get(), other.get());
}
map inverse() const
{
return map( isl_map_reverse(copy()) );
}
set wrapped() const
{
return isl_map_wrap(copy());
}
map lex_minimum() const
{
return isl_map_lexmin(copy());
}
map lex_maximum() const
{
return isl_map_lexmax(copy());
}
void coalesce()
{
m_object = isl_map_coalesce(m_object);
}
map in_domain( const set & domain ) const
{
return isl_map_intersect_domain(copy(), domain.copy());
}
map in_range( const set & range ) const
{
return isl_map_intersect_range(copy(), range.copy());
}
set operator() ( const set & arg ) const
{
return isl_set_apply( arg.copy(), copy() );
}
map operator() ( const map & arg ) const
{
return isl_map_apply_range( arg.copy(), copy() );
}
map & subtract (const map & rhs)
{
m_object = isl_map_subtract(m_object, rhs.copy());
return *this;
}
map & limit_above(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_map_upper_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
map & limit_below(isl::space::dimension_type dim, unsigned pos, int value)
{
m_object = isl_map_lower_bound_si(m_object, (isl_dim_type)dim, pos, value);
return *this;
}
map cross( const map & rhs ) const
{
return isl_map_product(copy(), rhs.copy());
}
basic_map convex_hull() const
{
return isl_map_convex_hull(copy());
}
basic_map simple_hull() const
{
return isl_map_simple_hull(copy());
}
map equate(int in_dim, int out_dim) const
{
return isl_map_equate(copy(), isl_dim_in, in_dim, isl_dim_out, out_dim);
}
set deltas() const
{
return isl_map_deltas(copy());
}
void map_domain_through( const map & other )
{
m_object = isl_map_apply_domain(m_object, other.copy());
}
void map_range_through( const map & other )
{
m_object = isl_map_apply_range(m_object, other.copy());
}
identifier id( space::dimension_type type ) const
{
isl_id *c_id = isl_map_get_tuple_id(get(), (isl_dim_type) type);
identifier id(c_id);
isl_id_free(c_id);
return id;
}
void set_id( space::dimension_type type, const identifier & id )
{
isl_id *c_id = id.c_id(m_ctx.get());
if (c_id)
m_object = isl_map_set_tuple_id(get(), (isl_dim_type)type, c_id);
}
void set_name( space::dimension_type type, const string & name )
{
m_object = isl_map_set_tuple_name(get(), (isl_dim_type)type, name.c_str());
}
string name( space::dimension_type type ) const
{
return isl_map_get_tuple_name(get(), (isl_dim_type) type);
}
void add_dimensions( space::dimension_type type, unsigned count )
{
m_object = isl_map_add_dims(m_object, (isl_dim_type) type, count);
}
void insert_dimensions( space::dimension_type type, unsigned pos, unsigned count )
{
m_object = isl_map_insert_dims(m_object, (isl_dim_type) type, pos, count);
}
void project_out_dimensions( space::dimension_type type, unsigned i, unsigned n=1 )
{
m_object = isl_map_project_out(m_object, (isl_dim_type)type, i, n);
}
void add_constraint( const constraint & c )
{
auto isl_c = c.copy();
if (c.local_space().is_wrapping())
isl_c = isl_constraint_unwrap_local_space(isl_c);
m_object = isl_map_add_constraint(m_object, isl_c);
}
void drop_constraints_with( space::dimension_type t, unsigned i, unsigned n=1)
{
m_object = isl_map_drop_constraints_involving_dims
(m_object, (isl_dim_type) t, i, n);
}
template <typename F>
void for_each( F f ) const
{
isl_map_foreach_basic_map(get(), &for_each_helper<F>, &f);
}
private:
template <typename F>
static isl_stat for_each_helper(isl_basic_map *basic_map_ptr, void *data_ptr)
{
auto f_ptr = reinterpret_cast<F*>(data_ptr);
basic_map m(basic_map_ptr);
bool result = (*f_ptr)(m);
return result ? isl_stat_ok : isl_stat_error;
}
};
class union_map : public object<isl_union_map>
{
public:
union_map( isl_union_map * ptr ): object(ptr) {}
union_map( const context & ctx ):
object(ctx, isl_union_map_empty(isl_space_params_alloc(ctx.get(),0)))
{}
union_map( const space & param_space ):
object(param_space.ctx(), isl_union_map_empty(param_space.copy()))
{}
union_map( context & ctx, const string & text ):
object(ctx, isl_union_map_read_from_str(ctx.get(), text.c_str()))
{}
union_map( const basic_map & bm ):
object(bm.ctx(), isl_union_map_from_basic_map(bm.copy()))
{}
union_map( const map & m ):
object(m.ctx(), isl_union_map_from_map(m.copy()))
{}
space get_space() const
{
return space( isl_union_map_get_space(get()) );
}
bool is_empty() const
{
return isl_union_map_is_empty(get());
}
bool is_subset_of(const union_map & other) const
{
return isl_union_map_is_subset(get(), other.get());
}
bool is_strict_subset_of(const union_map & other) const
{
return isl_union_map_is_strict_subset(get(), other.get());
}
union_set range() const
{
return isl_union_map_range(copy());
}
union_set domain() const
{
return isl_union_map_domain(copy());
}
union_map inverse() const
{
return union_map( isl_union_map_reverse(copy()) );
}
union_set wrapped() const
{
return isl_union_map_wrap(copy());
}
union_map universe() const
{
return isl_union_map_universe(copy());
}
map map_for( const space & spc ) const
{
return isl_union_map_extract_map(get(), spc.copy());
}
map single_map() const
{
auto the_map = isl_map_from_union_map(copy());
if (!the_map)
throw error("No single map.");
return the_map;
}
union_map in_domain( const union_set & domain ) const
{
return isl_union_map_intersect_domain(copy(), domain.copy());
}
union_map in_range( const union_set & range ) const
{
return isl_union_map_intersect_range(copy(), range.copy());
}
void map_domain_through( const union_map & other )
{
m_object = isl_union_map_apply_domain(m_object, other.copy());
}
void map_range_through( const union_map & other )
{
m_object = isl_union_map_apply_range(m_object, other.copy());
}
union_set operator() ( const union_set & arg ) const
{
return isl_union_set_apply( arg.copy(), copy() );
}
union_map operator() ( const union_map & arg ) const
{
return isl_union_map_apply_range( arg.copy(), copy() );
}
union_map & subtract(const union_map & rhs)
{
m_object = isl_union_map_subtract(m_object, rhs.copy());
return *this;
}
void coalesce()
{
m_object = isl_union_map_coalesce(m_object);
}
union_set deltas()
{
return isl_union_map_deltas(copy());
}
template <typename F>
void for_each( F f ) const
{
isl_union_map_foreach_map(get(), &for_each_helper<F>, &f);
}
private:
template <typename F>
static isl_stat for_each_helper(isl_map *map_ptr, void *data_ptr)
{
auto f_ptr = reinterpret_cast<F*>(data_ptr);
map m(map_ptr);
bool result = (*f_ptr)(m);
return result ? isl_stat_ok : isl_stat_error;
}
};
inline basic_map basic_set::unwrapped()
{
return isl_basic_set_unwrap(copy());
}
inline map set::unwrapped()
{
return isl_set_unwrap(copy());
}
inline union_map union_set::unwrapped()
{
return isl_union_set_unwrap(copy());
}
inline
basic_map operator& (const basic_map & lhs, const basic_map & rhs)
{
return isl_basic_map_intersect(lhs.copy(), rhs.copy());
}
inline
map operator& (const map & lhs, const map & rhs)
{
return isl_map_intersect(lhs.copy(), rhs.copy());
}
inline
union_map operator& (const union_map & lhs, const union_map & rhs)
{
return isl_union_map_intersect(lhs.copy(), rhs.copy());
}
inline
map & operator&=(map & lhs, const map & rhs )
{
lhs = lhs & rhs;
return lhs;
}
inline
union_map & operator&=(union_map & lhs, const union_map & rhs )
{
lhs = lhs & rhs;
return lhs;
}
inline
map operator| (const map &lhs, const map & rhs)
{
auto u = isl_map_union(lhs.copy(), rhs.copy());
if (!u)
throw error();
return u;
}
inline
union_map operator| (const union_map &lhs, const union_map & rhs)
{
return isl_union_map_union(lhs.copy(), rhs.copy());
}
inline
union_map operator| (const union_map &lhs, const map & rhs)
{
return isl_union_map_union(lhs.copy(), isl_union_map_from_map(rhs.copy()));
}
inline
union_map operator| (const union_map &lhs, const basic_map & rhs)
{
return isl_union_map_union(lhs.copy(), isl_union_map_from_basic_map(rhs.copy()));
}
inline
map & operator|=(map & lhs, const map & rhs )
{
lhs = lhs | rhs;
return lhs;
}
inline
union_map & operator|=(union_map & lhs, const union_map & rhs )
{
lhs = lhs | rhs;
return lhs;
}
inline
map operator* ( const map & lhs, const map & rhs )
{
return isl_map_range_product(lhs.copy(), rhs.copy());
}
template <> inline
void printer::print<basic_map>( const basic_map & m )
{
m_printer = isl_printer_print_basic_map(m_printer, m.get());
}
template <> inline
void printer::print<map>( const map & m )
{
m_printer = isl_printer_print_map(m_printer, m.get());
}
template <> inline
void printer::print<union_map>( const union_map & m )
{
m_printer = isl_printer_print_union_map(m_printer, m.get());
}
template <> inline
void printer::print_each_in<map>(const map & u)
{
u.for_each([this](const basic_map & m){
print(m); std::cout << std::endl;
return true;
});
}
template <> inline
void printer::print_each_in<union_map>(const union_map & u)
{
u.for_each([this](const map & m){
print(m); std::cout << std::endl;
return true;
});
}
}
#endif // ISL_CPP_MAP_INCLUDED