-
Notifications
You must be signed in to change notification settings - Fork 0
/
MpzList.cc
98 lines (82 loc) · 2.08 KB
/
MpzList.cc
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
/*
* =====================================================================================
*
* Filename: MpzList.cc
*
* Description: Implementation of class for dynamic list of mpz_t.
*
* Version: 1.0
* Created: 11/02/2008 10:40:00 AM
* Revision: none
* Compiler: gcc
*
* Author: Bryce Allen (bda), [email protected]
* Company:
*
* =====================================================================================
*/
#include <gmp.h>
#include <stdlib.h>
#include "MpzList.h"
//#define MPZLIST_INITIAL_SIZE 5
//#define MPZLIST_SIZE_INCREMENT 5
MpzList::MpzList (size_t initialSize, size_t sizeInc) {
mallocSize = initialSize;
mallocSizeIncrement = sizeInc;
initCount = 0;
size = 0;
list = (mpz_t *) malloc (mallocSize * sizeof (*list));
}
MpzList::~MpzList () {
for (size_t i = 0; i < initCount; i++) {
mpz_clear (list[i]);
}
free (list);
}
size_t MpzList::append (mpz_t value) {
// assert size <= mallocSize
if (size >= mallocSize) {
mallocSize += mallocSizeIncrement;
list = (mpz_t *) realloc (list, mallocSize * sizeof (*list));
}
if (size >= initCount) {
initCount++;
mpz_init_set (list[size], value);
} else {
mpz_set (list[size], value);
}
return ++size;
}
void MpzList::clear () {
size = 0;
}
size_t MpzList::compactify () {
for (size_t i = size; i < initCount; i++) {
mpz_clear (list[i]);
}
initCount = size;
if (mallocSize > size) {
mallocSize = size;
list = (mpz_t *) realloc (list, mallocSize * sizeof (*list));
}
return size;
}
size_t MpzList::getSize () {
return size;
}
size_t MpzList::getInitCount () {
return initCount;
}
mpz_t &MpzList::operator [] (const size_t i) {
return (list[i]);
}
bool MpzList::find (size_t *index, mpz_t value) {
for (size_t i=0; i < size; i++) {
if (mpz_cmp (list[i], value) == 0) {
if (index != NULL)
*index = i;
return true;
}
}
return false;
}