-
Notifications
You must be signed in to change notification settings - Fork 2
/
Basis_table.cpp
259 lines (223 loc) · 9.37 KB
/
Basis_table.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************/
/*** FILE : Basis_table.c ***/
/*** AUTHOR: David P Jacobs ***/
/*** PROGRAMMER: Sekhar Muddana ***/
/*** DATE WRITTEN: May 1990. ***/
/*** MODIFIED: 9/93 - Trent Whiteley ***/
/*** Added support for view, output, & save ***/
/*** PUBLIC ROUTINES: ***/
/*** int CreateBasisTable() ***/
/*** Basis EnterBasis() ***/
/*** int GetNextBasisTobeFilled() ***/
/*** Basis LeftFactor() ***/
/*** Basis RightFactor() ***/
/*** int Getdeg() ***/
/*** Type GetType() ***/
/*** PRIVATE ROUTINES: ***/
/*** MODULE DESCRIPTION: ***/
/*** This module contains routines dealing with Basis Table.***/
/*******************************************************************/
#include <vector>
using std::vector;
using std::pair;
using std::make_pair;
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "Basis_table.h"
#include "Build_defs.h"
#include "Generators.h"
#include "Help.h"
#include "Memory_routines.h"
#include "Type_table.h"
static void UpdateDegToBasisTable(int Deg, Basis Cur_basis);
static void PrintBasis(Basis b, FILE *filePtr);
typedef struct {
Basis left_factor;
Basis right_factor;
Name type;
} BT_rec;
static vector<BT_rec> Basis_table;
static vector<pair<Basis, Basis> > Deg_to_basis_table; // maps degree to first and last indices in Basis_table associated with that degree
/*******************************************************************/
/* GLOBALS INITIALIZED: */
/* Basis_table -- to zeroes. */
/* Nextbasistobefilled -- to 1. */
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* 1 if successfull. */
/* 0 otherwise. */
/* FUNCTION: */
/* Initialize the Basis Table and Nextbasistobefilled. */
/*******************************************************************/
int CreateBasisTable()
{
Basis_table.clear();
{ // First entry is expected to be zero, but this may not be required
BT_rec br;
br.left_factor = 0;
br.right_factor = 0;
br.type = 0;
Basis_table.push_back(br);
}
Deg_to_basis_table.clear();
return OK;
}
/*******************************************************************/
/* GLOBALS MODIFIED: */
/* Basis_table -- New Basis is entered. */
/* Nextbasistobefilled -- Incremented. */
/* MODIFIES: None. */
/* REQUIRES: */
/* Left_factor -- of the new Basis. */
/* Right_factor -- of the new Basis. */
/* Type -- of the new Basis. */
/* RETURNS: */
/* Basis Number of the new Basis entered. */
/* FUNCTION: */
/* Enter a New Basis into Basis Table. */
/*******************************************************************/
Basis EnterBasis(Basis Left_factor, Basis Right_factor, Name Cur_type)
{
Basis bn = Basis_table.size();
BT_rec br;
br.left_factor = Left_factor;
br.right_factor = Right_factor;
br.type = Cur_type;
Basis_table.push_back(br);
UpdateDegToBasisTable(GetDegreeName(Cur_type), bn);
return bn;
}
void UpdateDegToBasisTable(int Deg, Basis Cur_basis)
{
if(Deg > (int)Deg_to_basis_table.size()) {
Deg_to_basis_table.push_back(make_pair(Cur_basis, Cur_basis));
} else {
Deg_to_basis_table[Deg-1].second++;
}
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* Nextbasistobefilled -- */
/*******************************************************************/
Basis GetNextBasisTobeFilled()
{
return Basis_table.size();
}
Basis BasisStart(Degree Deg)
{
return Deg_to_basis_table[Deg-1].first;
}
Basis BasisEnd(Degree Deg)
{
return Deg_to_basis_table[Deg-1].second;
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* B -- Basis element. */
/* RETURNS: */
/* Type of the Basis B. */
/*******************************************************************/
Name GetType(Basis B)
{
return Basis_table[B].type;
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* FILE *filePtr - pointer to output file, temp file, or stdout */
/* int outputType - indicates screen, file, or printer */
/* RETURNS: */
/* Type of the Basis B. */
/*******************************************************************/
void PrintBasisTable(FILE *filePtr) /* TW 9/19/93 - added 2 params to support view, save, & output */
{
if(Basis_table.size() > 1){
fprintf(filePtr, "Basis Table: \n");
for (int i=1; i<(int)Basis_table.size(); i++) {
fprintf(filePtr, " %3d. %3d %3d ",i,Basis_table[i].left_factor,
Basis_table[i].right_factor);
PrintTypeName(Basis_table[i].type, filePtr);
fprintf(filePtr, " ");
PrintBasis(i, filePtr);
fprintf(filePtr, "\n");
}
}
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* B -- Basis element. */
/* RETURNS: */
/* Type of the Basis B. */
/*******************************************************************/
void PrintBasis(Basis b, FILE *filePtr) /* TW 9/19/93 - added param to support view, save, & output */
{
if ( (Basis_table[b].left_factor == 0) &&
(Basis_table[b].right_factor == 0) ) {
fprintf(filePtr, "%c", GetLetterofBasis(b) );
} else {
fprintf(filePtr, "(");
PrintBasis(Basis_table[b].left_factor, filePtr);
PrintBasis(Basis_table[b].right_factor, filePtr);
fprintf(filePtr, ")");
}
}
bool save_basis_table(FILE *f) {
// typedef int Basis;
// typedef int Name;
// typedef struct {
// Basis left_factor;
// Basis right_factor;
// Name type;
// } BT_rec;
// static vector<BT_rec> Basis_table;
// static vector<pair<Basis, Basis> > Deg_to_basis_table; // maps degree to first and last indices in Basis_table associated with that degree
{
int n = Basis_table.size();
fwrite(&n, sizeof(n), 1, f);
for(int i=0; i<n; i++) {
auto a = Basis_table[i];
fwrite(&a, sizeof(a), 1, f);
}
}
{
int n = Deg_to_basis_table.size();
fwrite(&n, sizeof(n), 1, f);
for(int i=0; i<n; i++) {
auto a = Deg_to_basis_table[i].first;
auto b = Deg_to_basis_table[i].second;
fwrite(&a, sizeof(a), 1, f);
fwrite(&b, sizeof(b), 1, f);
}
}
return true;
}
bool restore_basis_table(FILE *f) {
Basis_table.clear();
{
int n = 0;
if(fread(&n, sizeof(n), 1, f) != 1) return false;
Basis_table.resize(n);
for(int i=0; i<n; i++) {
if(fread(&Basis_table[i], sizeof(Basis_table[i]), 1, f) != 1) return false;
}
}
Deg_to_basis_table.clear();
{
int n = 0;
if(fread(&n, sizeof(n), 1, f) != 1) return false;
Deg_to_basis_table.resize(n);
for(int i=0; i<n; i++) {
Basis a, b;
if(fread(&a, sizeof(a), 1, f) != 1) return false;
if(fread(&b, sizeof(b), 1, f) != 1) return false;
Deg_to_basis_table[i] = make_pair(a, b);
}
}
return true;
}