2016-12-09 22:02:51 +00:00
|
|
|
#include "list.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the number of elements by which the list expands.
|
|
|
|
*/
|
|
|
|
#define BLOCK_SIZE 1024
|
|
|
|
|
2016-12-27 23:30:31 +00:00
|
|
|
int initList(List *restrict l) {
|
2016-12-09 22:02:51 +00:00
|
|
|
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
|
2016-12-27 23:30:31 +00:00
|
|
|
if(!l->list) return 1;
|
2016-12-09 22:02:51 +00:00
|
|
|
l->size = BLOCK_SIZE;
|
|
|
|
l->end = 0;
|
2016-12-27 23:30:31 +00:00
|
|
|
return 0;
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 10:26:12 +00:00
|
|
|
void deInitList(List *restrict l) {
|
2016-12-14 13:13:32 +00:00
|
|
|
for(size_t i = 0; i < l->size; ++i) {
|
2016-12-10 10:20:01 +00:00
|
|
|
mpz_clear(l->list[i]);
|
|
|
|
}
|
2016-12-09 22:02:51 +00:00
|
|
|
free(l->list);
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:30:31 +00:00
|
|
|
int addToList(List *restrict l, mpz_t n) {
|
2016-12-10 16:11:21 +00:00
|
|
|
if(l->end == l->size) {
|
2016-12-09 22:02:51 +00:00
|
|
|
l->size += BLOCK_SIZE;
|
|
|
|
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
2016-12-27 23:30:31 +00:00
|
|
|
if(!tmp) return 1;
|
2016-12-15 14:16:04 +00:00
|
|
|
l->list = tmp;
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|
|
|
|
mpz_init(l->list[l->end]);
|
|
|
|
mpz_set(l->list[l->end++], n);
|
2016-12-27 23:30:31 +00:00
|
|
|
return 0;
|
2016-12-09 22:02:51 +00:00
|
|
|
}
|