1use super::{Context, Error, LibISLError, PwAff, Set};
5use libc::uintptr_t;
6use std::ffi::{CStr, CString};
7use std::os::raw::c_char;
8
9pub struct PwAffList {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_pw_aff_list_add(list: uintptr_t, el: uintptr_t) -> uintptr_t;
18
19 fn isl_pw_aff_list_alloc(ctx: uintptr_t, n: i32) -> uintptr_t;
20
21 fn isl_pw_aff_list_clear(list: uintptr_t) -> uintptr_t;
22
23 fn isl_pw_aff_list_concat(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
24
25 fn isl_pw_aff_list_copy(list: uintptr_t) -> uintptr_t;
26
27 fn isl_pw_aff_list_drop(list: uintptr_t, first: u32, n: u32) -> uintptr_t;
28
29 fn isl_pw_aff_list_dump(list: uintptr_t) -> ();
30
31 fn isl_pw_aff_list_eq_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
32
33 fn isl_pw_aff_list_free(list: uintptr_t) -> uintptr_t;
34
35 fn isl_pw_aff_list_from_pw_aff(el: uintptr_t) -> uintptr_t;
36
37 fn isl_pw_aff_list_ge_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
38
39 fn isl_pw_aff_list_get_at(list: uintptr_t, index: i32) -> uintptr_t;
40
41 fn isl_pw_aff_list_get_ctx(list: uintptr_t) -> uintptr_t;
42
43 fn isl_pw_aff_list_get_pw_aff(list: uintptr_t, index: i32) -> uintptr_t;
44
45 fn isl_pw_aff_list_gt_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
46
47 fn isl_pw_aff_list_insert(list: uintptr_t, pos: u32, el: uintptr_t) -> uintptr_t;
48
49 fn isl_pw_aff_list_le_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
50
51 fn isl_pw_aff_list_lt_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
52
53 fn isl_pw_aff_list_max(list: uintptr_t) -> uintptr_t;
54
55 fn isl_pw_aff_list_min(list: uintptr_t) -> uintptr_t;
56
57 fn isl_pw_aff_list_n_pw_aff(list: uintptr_t) -> i32;
58
59 fn isl_pw_aff_list_ne_set(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
60
61 fn isl_pw_aff_list_read_from_str(ctx: uintptr_t, str_: *const c_char) -> uintptr_t;
62
63 fn isl_pw_aff_list_reverse(list: uintptr_t) -> uintptr_t;
64
65 fn isl_pw_aff_list_set_at(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
66
67 fn isl_pw_aff_list_set_pw_aff(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
68
69 fn isl_pw_aff_list_size(list: uintptr_t) -> i32;
70
71 fn isl_pw_aff_list_swap(list: uintptr_t, pos1: u32, pos2: u32) -> uintptr_t;
72
73 fn isl_pw_aff_list_to_str(list: uintptr_t) -> *const c_char;
74
75}
76
77impl PwAffList {
78 pub fn add(self, el: PwAff) -> Result<PwAffList, LibISLError> {
80 let list = self;
81 let isl_rs_ctx = list.get_ctx();
82 let mut list = list;
83 list.do_not_free_on_drop();
84 let list = list.ptr;
85 let mut el = el;
86 el.do_not_free_on_drop();
87 let el = el.ptr;
88 let isl_rs_result = unsafe { isl_pw_aff_list_add(list, el) };
89 let isl_rs_result = PwAffList { ptr: isl_rs_result,
90 should_free_on_drop: true };
91 let err = isl_rs_ctx.last_error();
92 if err != Error::None_ {
93 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
94 }
95 Ok(isl_rs_result)
96 }
97
98 pub fn alloc(ctx: &Context, n: i32) -> Result<PwAffList, LibISLError> {
100 let isl_rs_ctx = Context { ptr: ctx.ptr,
101 should_free_on_drop: false };
102 let ctx = ctx.ptr;
103 let isl_rs_result = unsafe { isl_pw_aff_list_alloc(ctx, n) };
104 let isl_rs_result = PwAffList { ptr: isl_rs_result,
105 should_free_on_drop: true };
106 let err = isl_rs_ctx.last_error();
107 if err != Error::None_ {
108 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
109 }
110 Ok(isl_rs_result)
111 }
112
113 pub fn clear(self) -> Result<PwAffList, LibISLError> {
115 let list = self;
116 let isl_rs_ctx = list.get_ctx();
117 let mut list = list;
118 list.do_not_free_on_drop();
119 let list = list.ptr;
120 let isl_rs_result = unsafe { isl_pw_aff_list_clear(list) };
121 let isl_rs_result = PwAffList { ptr: isl_rs_result,
122 should_free_on_drop: true };
123 let err = isl_rs_ctx.last_error();
124 if err != Error::None_ {
125 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
126 }
127 Ok(isl_rs_result)
128 }
129
130 pub fn concat(self, list2: PwAffList) -> Result<PwAffList, LibISLError> {
132 let list1 = self;
133 let isl_rs_ctx = list1.get_ctx();
134 let mut list1 = list1;
135 list1.do_not_free_on_drop();
136 let list1 = list1.ptr;
137 let mut list2 = list2;
138 list2.do_not_free_on_drop();
139 let list2 = list2.ptr;
140 let isl_rs_result = unsafe { isl_pw_aff_list_concat(list1, list2) };
141 let isl_rs_result = PwAffList { ptr: isl_rs_result,
142 should_free_on_drop: true };
143 let err = isl_rs_ctx.last_error();
144 if err != Error::None_ {
145 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
146 }
147 Ok(isl_rs_result)
148 }
149
150 pub fn copy(&self) -> Result<PwAffList, LibISLError> {
152 let list = self;
153 let isl_rs_ctx = list.get_ctx();
154 let list = list.ptr;
155 let isl_rs_result = unsafe { isl_pw_aff_list_copy(list) };
156 let isl_rs_result = PwAffList { ptr: isl_rs_result,
157 should_free_on_drop: true };
158 let err = isl_rs_ctx.last_error();
159 if err != Error::None_ {
160 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
161 }
162 Ok(isl_rs_result)
163 }
164
165 pub fn drop(self, first: u32, n: u32) -> Result<PwAffList, LibISLError> {
167 let list = self;
168 let isl_rs_ctx = list.get_ctx();
169 let mut list = list;
170 list.do_not_free_on_drop();
171 let list = list.ptr;
172 let isl_rs_result = unsafe { isl_pw_aff_list_drop(list, first, n) };
173 let isl_rs_result = PwAffList { ptr: isl_rs_result,
174 should_free_on_drop: true };
175 let err = isl_rs_ctx.last_error();
176 if err != Error::None_ {
177 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
178 }
179 Ok(isl_rs_result)
180 }
181
182 pub fn dump(&self) -> Result<(), LibISLError> {
184 let list = self;
185 let isl_rs_ctx = list.get_ctx();
186 let list = list.ptr;
187 let isl_rs_result = unsafe { isl_pw_aff_list_dump(list) };
188 let err = isl_rs_ctx.last_error();
189 if err != Error::None_ {
190 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
191 }
192 Ok(isl_rs_result)
193 }
194
195 pub fn eq_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
197 let list1 = self;
198 let isl_rs_ctx = list1.get_ctx();
199 let mut list1 = list1;
200 list1.do_not_free_on_drop();
201 let list1 = list1.ptr;
202 let mut list2 = list2;
203 list2.do_not_free_on_drop();
204 let list2 = list2.ptr;
205 let isl_rs_result = unsafe { isl_pw_aff_list_eq_set(list1, list2) };
206 let isl_rs_result = Set { ptr: isl_rs_result,
207 should_free_on_drop: true };
208 let err = isl_rs_ctx.last_error();
209 if err != Error::None_ {
210 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
211 }
212 Ok(isl_rs_result)
213 }
214
215 pub fn free(self) -> Result<PwAffList, LibISLError> {
217 let list = self;
218 let isl_rs_ctx = list.get_ctx();
219 let mut list = list;
220 list.do_not_free_on_drop();
221 let list = list.ptr;
222 let isl_rs_result = unsafe { isl_pw_aff_list_free(list) };
223 let isl_rs_result = PwAffList { ptr: isl_rs_result,
224 should_free_on_drop: true };
225 let err = isl_rs_ctx.last_error();
226 if err != Error::None_ {
227 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
228 }
229 Ok(isl_rs_result)
230 }
231
232 pub fn from_pw_aff(el: PwAff) -> Result<PwAffList, LibISLError> {
234 let isl_rs_ctx = el.get_ctx();
235 let mut el = el;
236 el.do_not_free_on_drop();
237 let el = el.ptr;
238 let isl_rs_result = unsafe { isl_pw_aff_list_from_pw_aff(el) };
239 let isl_rs_result = PwAffList { ptr: isl_rs_result,
240 should_free_on_drop: true };
241 let err = isl_rs_ctx.last_error();
242 if err != Error::None_ {
243 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
244 }
245 Ok(isl_rs_result)
246 }
247
248 pub fn ge_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
250 let list1 = self;
251 let isl_rs_ctx = list1.get_ctx();
252 let mut list1 = list1;
253 list1.do_not_free_on_drop();
254 let list1 = list1.ptr;
255 let mut list2 = list2;
256 list2.do_not_free_on_drop();
257 let list2 = list2.ptr;
258 let isl_rs_result = unsafe { isl_pw_aff_list_ge_set(list1, list2) };
259 let isl_rs_result = Set { ptr: isl_rs_result,
260 should_free_on_drop: true };
261 let err = isl_rs_ctx.last_error();
262 if err != Error::None_ {
263 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
264 }
265 Ok(isl_rs_result)
266 }
267
268 pub fn get_at(&self, index: i32) -> Result<PwAff, LibISLError> {
270 let list = self;
271 let isl_rs_ctx = list.get_ctx();
272 let list = list.ptr;
273 let isl_rs_result = unsafe { isl_pw_aff_list_get_at(list, index) };
274 let isl_rs_result = PwAff { ptr: isl_rs_result,
275 should_free_on_drop: true };
276 let err = isl_rs_ctx.last_error();
277 if err != Error::None_ {
278 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
279 }
280 Ok(isl_rs_result)
281 }
282
283 pub fn get_ctx(&self) -> Context {
285 let list = self;
286 let list = list.ptr;
287 let isl_rs_result = unsafe { isl_pw_aff_list_get_ctx(list) };
288 let isl_rs_result = Context { ptr: isl_rs_result,
289 should_free_on_drop: false };
290 isl_rs_result
291 }
292
293 pub fn get_pw_aff(&self, index: i32) -> Result<PwAff, LibISLError> {
295 let list = self;
296 let isl_rs_ctx = list.get_ctx();
297 let list = list.ptr;
298 let isl_rs_result = unsafe { isl_pw_aff_list_get_pw_aff(list, index) };
299 let isl_rs_result = PwAff { ptr: isl_rs_result,
300 should_free_on_drop: true };
301 let err = isl_rs_ctx.last_error();
302 if err != Error::None_ {
303 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
304 }
305 Ok(isl_rs_result)
306 }
307
308 pub fn gt_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
310 let list1 = self;
311 let isl_rs_ctx = list1.get_ctx();
312 let mut list1 = list1;
313 list1.do_not_free_on_drop();
314 let list1 = list1.ptr;
315 let mut list2 = list2;
316 list2.do_not_free_on_drop();
317 let list2 = list2.ptr;
318 let isl_rs_result = unsafe { isl_pw_aff_list_gt_set(list1, list2) };
319 let isl_rs_result = Set { ptr: isl_rs_result,
320 should_free_on_drop: true };
321 let err = isl_rs_ctx.last_error();
322 if err != Error::None_ {
323 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
324 }
325 Ok(isl_rs_result)
326 }
327
328 pub fn insert(self, pos: u32, el: PwAff) -> Result<PwAffList, LibISLError> {
330 let list = self;
331 let isl_rs_ctx = list.get_ctx();
332 let mut list = list;
333 list.do_not_free_on_drop();
334 let list = list.ptr;
335 let mut el = el;
336 el.do_not_free_on_drop();
337 let el = el.ptr;
338 let isl_rs_result = unsafe { isl_pw_aff_list_insert(list, pos, el) };
339 let isl_rs_result = PwAffList { ptr: isl_rs_result,
340 should_free_on_drop: true };
341 let err = isl_rs_ctx.last_error();
342 if err != Error::None_ {
343 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
344 }
345 Ok(isl_rs_result)
346 }
347
348 pub fn le_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
350 let list1 = self;
351 let isl_rs_ctx = list1.get_ctx();
352 let mut list1 = list1;
353 list1.do_not_free_on_drop();
354 let list1 = list1.ptr;
355 let mut list2 = list2;
356 list2.do_not_free_on_drop();
357 let list2 = list2.ptr;
358 let isl_rs_result = unsafe { isl_pw_aff_list_le_set(list1, list2) };
359 let isl_rs_result = Set { ptr: isl_rs_result,
360 should_free_on_drop: true };
361 let err = isl_rs_ctx.last_error();
362 if err != Error::None_ {
363 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
364 }
365 Ok(isl_rs_result)
366 }
367
368 pub fn lt_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
370 let list1 = self;
371 let isl_rs_ctx = list1.get_ctx();
372 let mut list1 = list1;
373 list1.do_not_free_on_drop();
374 let list1 = list1.ptr;
375 let mut list2 = list2;
376 list2.do_not_free_on_drop();
377 let list2 = list2.ptr;
378 let isl_rs_result = unsafe { isl_pw_aff_list_lt_set(list1, list2) };
379 let isl_rs_result = Set { ptr: isl_rs_result,
380 should_free_on_drop: true };
381 let err = isl_rs_ctx.last_error();
382 if err != Error::None_ {
383 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
384 }
385 Ok(isl_rs_result)
386 }
387
388 pub fn max(self) -> Result<PwAff, LibISLError> {
390 let list = self;
391 let isl_rs_ctx = list.get_ctx();
392 let mut list = list;
393 list.do_not_free_on_drop();
394 let list = list.ptr;
395 let isl_rs_result = unsafe { isl_pw_aff_list_max(list) };
396 let isl_rs_result = PwAff { ptr: isl_rs_result,
397 should_free_on_drop: true };
398 let err = isl_rs_ctx.last_error();
399 if err != Error::None_ {
400 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
401 }
402 Ok(isl_rs_result)
403 }
404
405 pub fn min(self) -> Result<PwAff, LibISLError> {
407 let list = self;
408 let isl_rs_ctx = list.get_ctx();
409 let mut list = list;
410 list.do_not_free_on_drop();
411 let list = list.ptr;
412 let isl_rs_result = unsafe { isl_pw_aff_list_min(list) };
413 let isl_rs_result = PwAff { ptr: isl_rs_result,
414 should_free_on_drop: true };
415 let err = isl_rs_ctx.last_error();
416 if err != Error::None_ {
417 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
418 }
419 Ok(isl_rs_result)
420 }
421
422 pub fn n_pw_aff(&self) -> Result<i32, LibISLError> {
424 let list = self;
425 let isl_rs_ctx = list.get_ctx();
426 let list = list.ptr;
427 let isl_rs_result = unsafe { isl_pw_aff_list_n_pw_aff(list) };
428 let err = isl_rs_ctx.last_error();
429 if err != Error::None_ {
430 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
431 }
432 Ok(isl_rs_result)
433 }
434
435 pub fn ne_set(self, list2: PwAffList) -> Result<Set, LibISLError> {
437 let list1 = self;
438 let isl_rs_ctx = list1.get_ctx();
439 let mut list1 = list1;
440 list1.do_not_free_on_drop();
441 let list1 = list1.ptr;
442 let mut list2 = list2;
443 list2.do_not_free_on_drop();
444 let list2 = list2.ptr;
445 let isl_rs_result = unsafe { isl_pw_aff_list_ne_set(list1, list2) };
446 let isl_rs_result = Set { ptr: isl_rs_result,
447 should_free_on_drop: true };
448 let err = isl_rs_ctx.last_error();
449 if err != Error::None_ {
450 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
451 }
452 Ok(isl_rs_result)
453 }
454
455 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<PwAffList, LibISLError> {
457 let isl_rs_ctx = Context { ptr: ctx.ptr,
458 should_free_on_drop: false };
459 let ctx = ctx.ptr;
460 let str_ = CString::new(str_).unwrap();
461 let str_ = str_.as_ptr();
462 let isl_rs_result = unsafe { isl_pw_aff_list_read_from_str(ctx, str_) };
463 let isl_rs_result = PwAffList { ptr: isl_rs_result,
464 should_free_on_drop: true };
465 let err = isl_rs_ctx.last_error();
466 if err != Error::None_ {
467 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
468 }
469 Ok(isl_rs_result)
470 }
471
472 pub fn reverse(self) -> Result<PwAffList, LibISLError> {
474 let list = self;
475 let isl_rs_ctx = list.get_ctx();
476 let mut list = list;
477 list.do_not_free_on_drop();
478 let list = list.ptr;
479 let isl_rs_result = unsafe { isl_pw_aff_list_reverse(list) };
480 let isl_rs_result = PwAffList { ptr: isl_rs_result,
481 should_free_on_drop: true };
482 let err = isl_rs_ctx.last_error();
483 if err != Error::None_ {
484 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
485 }
486 Ok(isl_rs_result)
487 }
488
489 pub fn set_at(self, index: i32, el: PwAff) -> Result<PwAffList, LibISLError> {
491 let list = self;
492 let isl_rs_ctx = list.get_ctx();
493 let mut list = list;
494 list.do_not_free_on_drop();
495 let list = list.ptr;
496 let mut el = el;
497 el.do_not_free_on_drop();
498 let el = el.ptr;
499 let isl_rs_result = unsafe { isl_pw_aff_list_set_at(list, index, el) };
500 let isl_rs_result = PwAffList { ptr: isl_rs_result,
501 should_free_on_drop: true };
502 let err = isl_rs_ctx.last_error();
503 if err != Error::None_ {
504 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
505 }
506 Ok(isl_rs_result)
507 }
508
509 pub fn set_pw_aff(self, index: i32, el: PwAff) -> Result<PwAffList, LibISLError> {
511 let list = self;
512 let isl_rs_ctx = list.get_ctx();
513 let mut list = list;
514 list.do_not_free_on_drop();
515 let list = list.ptr;
516 let mut el = el;
517 el.do_not_free_on_drop();
518 let el = el.ptr;
519 let isl_rs_result = unsafe { isl_pw_aff_list_set_pw_aff(list, index, el) };
520 let isl_rs_result = PwAffList { ptr: isl_rs_result,
521 should_free_on_drop: true };
522 let err = isl_rs_ctx.last_error();
523 if err != Error::None_ {
524 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
525 }
526 Ok(isl_rs_result)
527 }
528
529 pub fn size(&self) -> Result<i32, LibISLError> {
531 let list = self;
532 let isl_rs_ctx = list.get_ctx();
533 let list = list.ptr;
534 let isl_rs_result = unsafe { isl_pw_aff_list_size(list) };
535 let err = isl_rs_ctx.last_error();
536 if err != Error::None_ {
537 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
538 }
539 Ok(isl_rs_result)
540 }
541
542 pub fn swap(self, pos1: u32, pos2: u32) -> Result<PwAffList, LibISLError> {
544 let list = self;
545 let isl_rs_ctx = list.get_ctx();
546 let mut list = list;
547 list.do_not_free_on_drop();
548 let list = list.ptr;
549 let isl_rs_result = unsafe { isl_pw_aff_list_swap(list, pos1, pos2) };
550 let isl_rs_result = PwAffList { ptr: isl_rs_result,
551 should_free_on_drop: true };
552 let err = isl_rs_ctx.last_error();
553 if err != Error::None_ {
554 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
555 }
556 Ok(isl_rs_result)
557 }
558
559 pub fn to_str(&self) -> Result<&str, LibISLError> {
561 let list = self;
562 let isl_rs_ctx = list.get_ctx();
563 let list = list.ptr;
564 let isl_rs_result = unsafe { isl_pw_aff_list_to_str(list) };
565 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
566 let isl_rs_result = isl_rs_result.to_str().unwrap();
567 let err = isl_rs_ctx.last_error();
568 if err != Error::None_ {
569 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
570 }
571 Ok(isl_rs_result)
572 }
573
574 pub fn do_not_free_on_drop(&mut self) {
577 self.should_free_on_drop = false;
578 }
579}
580
581impl Drop for PwAffList {
582 fn drop(&mut self) {
583 if self.should_free_on_drop {
584 unsafe {
585 isl_pw_aff_list_free(self.ptr);
586 }
587 }
588 }
589}