1 | |
---|
2 | #include <NTL/GF2XVec.h> |
---|
3 | |
---|
4 | #include <NTL/new.h> |
---|
5 | |
---|
6 | NTL_START_IMPL |
---|
7 | |
---|
8 | const long MaxAllocBlock = 10000; |
---|
9 | |
---|
10 | |
---|
11 | void GF2XVec::SetSize(long n, long d) |
---|
12 | { |
---|
13 | if (n < 0 || d <= 0) Error("bad args to GF2XVec::SetSize()"); |
---|
14 | if (v) |
---|
15 | Error("GF2XVec initialized more than once"); |
---|
16 | |
---|
17 | if (d >= (1L << (NTL_BITS_PER_LONG-4))/NTL_BITS_PER_LONG) |
---|
18 | Error("size too big in GF2XVec::SetSize"); |
---|
19 | |
---|
20 | if (n >= long((1L << (NTL_BITS_PER_LONG-4))/sizeof(GF2X))) |
---|
21 | Error("length too big in GF2XVec::SetSize"); |
---|
22 | |
---|
23 | if (n == 0) return; |
---|
24 | |
---|
25 | long size = (d+2); |
---|
26 | long AllocAmt = MaxAllocBlock / size; |
---|
27 | if (AllocAmt == 0) AllocAmt = 1; |
---|
28 | |
---|
29 | v = (GF2X*) malloc(n * (sizeof (GF2X))); |
---|
30 | if (!v) Error("out of memory in GF2XVec::SetSize()"); |
---|
31 | |
---|
32 | long i = 0; |
---|
33 | long m; |
---|
34 | _ntl_ulong *p, *q; |
---|
35 | long j; |
---|
36 | |
---|
37 | while (i < n) { |
---|
38 | m = min((n-i), AllocAmt); |
---|
39 | p = (_ntl_ulong *) malloc(m*size*(sizeof (_ntl_ulong))); |
---|
40 | if (!p) Error("out of memory in GF2XVec::SetSize()"); |
---|
41 | for (j = 0, q = p+2; j < m; j++, i++, q += size) { |
---|
42 | q[-2] = (d << 1) | 1; |
---|
43 | q[-1] = 0; |
---|
44 | v[i].xrep.rep = q; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | len = n; |
---|
49 | bsize = d; |
---|
50 | } |
---|
51 | |
---|
52 | void GF2XVec::kill() |
---|
53 | { |
---|
54 | long n = len; |
---|
55 | long d = bsize; |
---|
56 | if (n == 0) return; |
---|
57 | |
---|
58 | long size = (d+2); |
---|
59 | long AllocAmt = MaxAllocBlock / size; |
---|
60 | if (AllocAmt == 0) AllocAmt = 1; |
---|
61 | |
---|
62 | long i = 0; |
---|
63 | long m; |
---|
64 | |
---|
65 | while (i < n) { |
---|
66 | m = min((n-i), AllocAmt); |
---|
67 | free(v[i].xrep.rep-2); |
---|
68 | i += m; |
---|
69 | } |
---|
70 | |
---|
71 | free(v); |
---|
72 | |
---|
73 | v = 0; len = 0; bsize = 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | GF2XVec& GF2XVec::operator=(const GF2XVec& a) |
---|
78 | { |
---|
79 | if (this == &a) |
---|
80 | return *this; |
---|
81 | |
---|
82 | kill(); |
---|
83 | SetSize(a.len, a.bsize); |
---|
84 | |
---|
85 | long i; |
---|
86 | for (i = 0; i < a.len; i++) |
---|
87 | v[i] = (a.v)[i]; |
---|
88 | |
---|
89 | return *this; |
---|
90 | } |
---|
91 | |
---|
92 | GF2XVec::GF2XVec(const GF2XVec& a) |
---|
93 | { |
---|
94 | v = 0; len = 0; bsize = 0; |
---|
95 | |
---|
96 | SetSize(a.len, a.bsize); |
---|
97 | |
---|
98 | long i; |
---|
99 | for (i = 0; i < a.len; i++) |
---|
100 | v[i] = (a.v)[i]; |
---|
101 | } |
---|
102 | |
---|
103 | void GF2XVec::swap_impl(GF2XVec& x, GF2XVec& y) |
---|
104 | { |
---|
105 | GF2X* t1; |
---|
106 | long t2; |
---|
107 | |
---|
108 | t1 = x.v; |
---|
109 | x.v = y.v; |
---|
110 | y.v = t1; |
---|
111 | |
---|
112 | t2 = x.len; |
---|
113 | x.len = y.len; |
---|
114 | y.len = t2; |
---|
115 | |
---|
116 | t2 = x.bsize; |
---|
117 | x.bsize = y.bsize; |
---|
118 | y.bsize = t2; |
---|
119 | } |
---|
120 | |
---|
121 | NTL_END_IMPL |
---|