1use super::{Context, Error, LibISLError, Set};
5use libc::uintptr_t;
6use std::ffi::{CStr, CString};
7use std::os::raw::c_char;
8
9pub struct SetList {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_set_list_add(list: uintptr_t, el: uintptr_t) -> uintptr_t;
18
19 fn isl_set_list_alloc(ctx: uintptr_t, n: i32) -> uintptr_t;
20
21 fn isl_set_list_clear(list: uintptr_t) -> uintptr_t;
22
23 fn isl_set_list_concat(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
24
25 fn isl_set_list_copy(list: uintptr_t) -> uintptr_t;
26
27 fn isl_set_list_drop(list: uintptr_t, first: u32, n: u32) -> uintptr_t;
28
29 fn isl_set_list_dump(list: uintptr_t) -> ();
30
31 fn isl_set_list_free(list: uintptr_t) -> uintptr_t;
32
33 fn isl_set_list_from_set(el: uintptr_t) -> uintptr_t;
34
35 fn isl_set_list_get_at(list: uintptr_t, index: i32) -> uintptr_t;
36
37 fn isl_set_list_get_ctx(list: uintptr_t) -> uintptr_t;
38
39 fn isl_set_list_get_set(list: uintptr_t, index: i32) -> uintptr_t;
40
41 fn isl_set_list_insert(list: uintptr_t, pos: u32, el: uintptr_t) -> uintptr_t;
42
43 fn isl_set_list_n_set(list: uintptr_t) -> i32;
44
45 fn isl_set_list_read_from_str(ctx: uintptr_t, str_: *const c_char) -> uintptr_t;
46
47 fn isl_set_list_reverse(list: uintptr_t) -> uintptr_t;
48
49 fn isl_set_list_set_at(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
50
51 fn isl_set_list_set_set(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
52
53 fn isl_set_list_size(list: uintptr_t) -> i32;
54
55 fn isl_set_list_swap(list: uintptr_t, pos1: u32, pos2: u32) -> uintptr_t;
56
57 fn isl_set_list_to_str(list: uintptr_t) -> *const c_char;
58
59 fn isl_set_list_union(list: uintptr_t) -> uintptr_t;
60
61}
62
63impl SetList {
64 pub fn add(self, el: Set) -> Result<SetList, LibISLError> {
66 let list = self;
67 let isl_rs_ctx = list.get_ctx();
68 let mut list = list;
69 list.do_not_free_on_drop();
70 let list = list.ptr;
71 let mut el = el;
72 el.do_not_free_on_drop();
73 let el = el.ptr;
74 let isl_rs_result = unsafe { isl_set_list_add(list, el) };
75 let isl_rs_result = SetList { ptr: isl_rs_result,
76 should_free_on_drop: true };
77 let err = isl_rs_ctx.last_error();
78 if err != Error::None_ {
79 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
80 }
81 Ok(isl_rs_result)
82 }
83
84 pub fn alloc(ctx: &Context, n: i32) -> Result<SetList, LibISLError> {
86 let isl_rs_ctx = Context { ptr: ctx.ptr,
87 should_free_on_drop: false };
88 let ctx = ctx.ptr;
89 let isl_rs_result = unsafe { isl_set_list_alloc(ctx, n) };
90 let isl_rs_result = SetList { ptr: isl_rs_result,
91 should_free_on_drop: true };
92 let err = isl_rs_ctx.last_error();
93 if err != Error::None_ {
94 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
95 }
96 Ok(isl_rs_result)
97 }
98
99 pub fn clear(self) -> Result<SetList, LibISLError> {
101 let list = self;
102 let isl_rs_ctx = list.get_ctx();
103 let mut list = list;
104 list.do_not_free_on_drop();
105 let list = list.ptr;
106 let isl_rs_result = unsafe { isl_set_list_clear(list) };
107 let isl_rs_result = SetList { ptr: isl_rs_result,
108 should_free_on_drop: true };
109 let err = isl_rs_ctx.last_error();
110 if err != Error::None_ {
111 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
112 }
113 Ok(isl_rs_result)
114 }
115
116 pub fn concat(self, list2: SetList) -> Result<SetList, LibISLError> {
118 let list1 = self;
119 let isl_rs_ctx = list1.get_ctx();
120 let mut list1 = list1;
121 list1.do_not_free_on_drop();
122 let list1 = list1.ptr;
123 let mut list2 = list2;
124 list2.do_not_free_on_drop();
125 let list2 = list2.ptr;
126 let isl_rs_result = unsafe { isl_set_list_concat(list1, list2) };
127 let isl_rs_result = SetList { ptr: isl_rs_result,
128 should_free_on_drop: true };
129 let err = isl_rs_ctx.last_error();
130 if err != Error::None_ {
131 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
132 }
133 Ok(isl_rs_result)
134 }
135
136 pub fn copy(&self) -> Result<SetList, LibISLError> {
138 let list = self;
139 let isl_rs_ctx = list.get_ctx();
140 let list = list.ptr;
141 let isl_rs_result = unsafe { isl_set_list_copy(list) };
142 let isl_rs_result = SetList { ptr: isl_rs_result,
143 should_free_on_drop: true };
144 let err = isl_rs_ctx.last_error();
145 if err != Error::None_ {
146 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
147 }
148 Ok(isl_rs_result)
149 }
150
151 pub fn drop(self, first: u32, n: u32) -> Result<SetList, LibISLError> {
153 let list = self;
154 let isl_rs_ctx = list.get_ctx();
155 let mut list = list;
156 list.do_not_free_on_drop();
157 let list = list.ptr;
158 let isl_rs_result = unsafe { isl_set_list_drop(list, first, n) };
159 let isl_rs_result = SetList { ptr: isl_rs_result,
160 should_free_on_drop: true };
161 let err = isl_rs_ctx.last_error();
162 if err != Error::None_ {
163 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
164 }
165 Ok(isl_rs_result)
166 }
167
168 pub fn dump(&self) -> Result<(), LibISLError> {
170 let list = self;
171 let isl_rs_ctx = list.get_ctx();
172 let list = list.ptr;
173 let isl_rs_result = unsafe { isl_set_list_dump(list) };
174 let err = isl_rs_ctx.last_error();
175 if err != Error::None_ {
176 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
177 }
178 Ok(isl_rs_result)
179 }
180
181 pub fn free(self) -> Result<SetList, LibISLError> {
183 let list = self;
184 let isl_rs_ctx = list.get_ctx();
185 let mut list = list;
186 list.do_not_free_on_drop();
187 let list = list.ptr;
188 let isl_rs_result = unsafe { isl_set_list_free(list) };
189 let isl_rs_result = SetList { ptr: isl_rs_result,
190 should_free_on_drop: true };
191 let err = isl_rs_ctx.last_error();
192 if err != Error::None_ {
193 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
194 }
195 Ok(isl_rs_result)
196 }
197
198 pub fn from_set(el: Set) -> Result<SetList, LibISLError> {
200 let isl_rs_ctx = el.get_ctx();
201 let mut el = el;
202 el.do_not_free_on_drop();
203 let el = el.ptr;
204 let isl_rs_result = unsafe { isl_set_list_from_set(el) };
205 let isl_rs_result = SetList { ptr: isl_rs_result,
206 should_free_on_drop: true };
207 let err = isl_rs_ctx.last_error();
208 if err != Error::None_ {
209 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
210 }
211 Ok(isl_rs_result)
212 }
213
214 pub fn get_at(&self, index: i32) -> Result<Set, LibISLError> {
216 let list = self;
217 let isl_rs_ctx = list.get_ctx();
218 let list = list.ptr;
219 let isl_rs_result = unsafe { isl_set_list_get_at(list, index) };
220 let isl_rs_result = Set { ptr: isl_rs_result,
221 should_free_on_drop: true };
222 let err = isl_rs_ctx.last_error();
223 if err != Error::None_ {
224 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
225 }
226 Ok(isl_rs_result)
227 }
228
229 pub fn get_ctx(&self) -> Context {
231 let list = self;
232 let list = list.ptr;
233 let isl_rs_result = unsafe { isl_set_list_get_ctx(list) };
234 let isl_rs_result = Context { ptr: isl_rs_result,
235 should_free_on_drop: false };
236 isl_rs_result
237 }
238
239 pub fn get_set(&self, index: i32) -> Result<Set, LibISLError> {
241 let list = self;
242 let isl_rs_ctx = list.get_ctx();
243 let list = list.ptr;
244 let isl_rs_result = unsafe { isl_set_list_get_set(list, index) };
245 let isl_rs_result = Set { ptr: isl_rs_result,
246 should_free_on_drop: true };
247 let err = isl_rs_ctx.last_error();
248 if err != Error::None_ {
249 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
250 }
251 Ok(isl_rs_result)
252 }
253
254 pub fn insert(self, pos: u32, el: Set) -> Result<SetList, LibISLError> {
256 let list = self;
257 let isl_rs_ctx = list.get_ctx();
258 let mut list = list;
259 list.do_not_free_on_drop();
260 let list = list.ptr;
261 let mut el = el;
262 el.do_not_free_on_drop();
263 let el = el.ptr;
264 let isl_rs_result = unsafe { isl_set_list_insert(list, pos, el) };
265 let isl_rs_result = SetList { ptr: isl_rs_result,
266 should_free_on_drop: true };
267 let err = isl_rs_ctx.last_error();
268 if err != Error::None_ {
269 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
270 }
271 Ok(isl_rs_result)
272 }
273
274 pub fn n_set(&self) -> Result<i32, LibISLError> {
276 let list = self;
277 let isl_rs_ctx = list.get_ctx();
278 let list = list.ptr;
279 let isl_rs_result = unsafe { isl_set_list_n_set(list) };
280 let err = isl_rs_ctx.last_error();
281 if err != Error::None_ {
282 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
283 }
284 Ok(isl_rs_result)
285 }
286
287 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<SetList, LibISLError> {
289 let isl_rs_ctx = Context { ptr: ctx.ptr,
290 should_free_on_drop: false };
291 let ctx = ctx.ptr;
292 let str_ = CString::new(str_).unwrap();
293 let str_ = str_.as_ptr();
294 let isl_rs_result = unsafe { isl_set_list_read_from_str(ctx, str_) };
295 let isl_rs_result = SetList { ptr: isl_rs_result,
296 should_free_on_drop: true };
297 let err = isl_rs_ctx.last_error();
298 if err != Error::None_ {
299 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
300 }
301 Ok(isl_rs_result)
302 }
303
304 pub fn reverse(self) -> Result<SetList, LibISLError> {
306 let list = self;
307 let isl_rs_ctx = list.get_ctx();
308 let mut list = list;
309 list.do_not_free_on_drop();
310 let list = list.ptr;
311 let isl_rs_result = unsafe { isl_set_list_reverse(list) };
312 let isl_rs_result = SetList { ptr: isl_rs_result,
313 should_free_on_drop: true };
314 let err = isl_rs_ctx.last_error();
315 if err != Error::None_ {
316 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
317 }
318 Ok(isl_rs_result)
319 }
320
321 pub fn set_at(self, index: i32, el: Set) -> Result<SetList, LibISLError> {
323 let list = self;
324 let isl_rs_ctx = list.get_ctx();
325 let mut list = list;
326 list.do_not_free_on_drop();
327 let list = list.ptr;
328 let mut el = el;
329 el.do_not_free_on_drop();
330 let el = el.ptr;
331 let isl_rs_result = unsafe { isl_set_list_set_at(list, index, el) };
332 let isl_rs_result = SetList { ptr: isl_rs_result,
333 should_free_on_drop: true };
334 let err = isl_rs_ctx.last_error();
335 if err != Error::None_ {
336 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
337 }
338 Ok(isl_rs_result)
339 }
340
341 pub fn set_set(self, index: i32, el: Set) -> Result<SetList, LibISLError> {
343 let list = self;
344 let isl_rs_ctx = list.get_ctx();
345 let mut list = list;
346 list.do_not_free_on_drop();
347 let list = list.ptr;
348 let mut el = el;
349 el.do_not_free_on_drop();
350 let el = el.ptr;
351 let isl_rs_result = unsafe { isl_set_list_set_set(list, index, el) };
352 let isl_rs_result = SetList { ptr: isl_rs_result,
353 should_free_on_drop: true };
354 let err = isl_rs_ctx.last_error();
355 if err != Error::None_ {
356 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
357 }
358 Ok(isl_rs_result)
359 }
360
361 pub fn size(&self) -> Result<i32, LibISLError> {
363 let list = self;
364 let isl_rs_ctx = list.get_ctx();
365 let list = list.ptr;
366 let isl_rs_result = unsafe { isl_set_list_size(list) };
367 let err = isl_rs_ctx.last_error();
368 if err != Error::None_ {
369 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
370 }
371 Ok(isl_rs_result)
372 }
373
374 pub fn swap(self, pos1: u32, pos2: u32) -> Result<SetList, LibISLError> {
376 let list = self;
377 let isl_rs_ctx = list.get_ctx();
378 let mut list = list;
379 list.do_not_free_on_drop();
380 let list = list.ptr;
381 let isl_rs_result = unsafe { isl_set_list_swap(list, pos1, pos2) };
382 let isl_rs_result = SetList { ptr: isl_rs_result,
383 should_free_on_drop: true };
384 let err = isl_rs_ctx.last_error();
385 if err != Error::None_ {
386 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
387 }
388 Ok(isl_rs_result)
389 }
390
391 pub fn to_str(&self) -> Result<&str, LibISLError> {
393 let list = self;
394 let isl_rs_ctx = list.get_ctx();
395 let list = list.ptr;
396 let isl_rs_result = unsafe { isl_set_list_to_str(list) };
397 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
398 let isl_rs_result = isl_rs_result.to_str().unwrap();
399 let err = isl_rs_ctx.last_error();
400 if err != Error::None_ {
401 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
402 }
403 Ok(isl_rs_result)
404 }
405
406 pub fn union(self) -> Result<Set, LibISLError> {
408 let list = self;
409 let isl_rs_ctx = list.get_ctx();
410 let mut list = list;
411 list.do_not_free_on_drop();
412 let list = list.ptr;
413 let isl_rs_result = unsafe { isl_set_list_union(list) };
414 let isl_rs_result = Set { ptr: isl_rs_result,
415 should_free_on_drop: true };
416 let err = isl_rs_ctx.last_error();
417 if err != Error::None_ {
418 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
419 }
420 Ok(isl_rs_result)
421 }
422
423 pub fn do_not_free_on_drop(&mut self) {
426 self.should_free_on_drop = false;
427 }
428}
429
430impl Drop for SetList {
431 fn drop(&mut self) {
432 if self.should_free_on_drop {
433 unsafe {
434 isl_set_list_free(self.ptr);
435 }
436 }
437 }
438}