2016-12-09 22:02:51 +00:00
|
|
|
#include "list.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the number of elements by which the list expands.
|
|
|
|
* WARNING: Always use doubles for this number (2^X)
|
|
|
|
*/
|
|
|
|
#define BLOCK_SIZE 1024
|
|
|
|
|
|
|
|
void initList(List *l) {
|
|
|
|
l->list = malloc(sizeof(mpz_t) * BLOCK_SIZE);
|
|
|
|
if(!l->list) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory to list!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
l->size = BLOCK_SIZE;
|
|
|
|
l->end = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deInitList(List *l) {
|
2016-12-10 10:20:01 +00:00
|
|
|
for(ulli i = 0; i < l->size; ++i) {
|
|
|
|
mpz_clear(l->list[i]);
|
|
|
|
}
|
2016-12-09 22:02:51 +00:00
|
|
|
free(l->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addToList(List *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;
|
2016-12-10 16:11:21 +00:00
|
|
|
if(l->size == 0) {
|
2016-12-10 10:51:32 +00:00
|
|
|
fprintf(stderr, "`l->size' has overflowed!\n");
|
2016-12-09 22:02:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
|
2016-12-10 16:11:21 +00:00
|
|
|
if(!tmp) {
|
2016-12-09 22:02:51 +00:00
|
|
|
fprintf(stderr, "Failed to allocate more memory to list!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
l->list = (mpz_t*)tmp;
|
|
|
|
}
|
|
|
|
mpz_init(l->list[l->end]);
|
|
|
|
mpz_set(l->list[l->end++], n);
|
|
|
|
}
|