-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
235 lines (192 loc) · 6.93 KB
/
main.c
File metadata and controls
235 lines (192 loc) · 6.93 KB
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include "insertion_series/insertionSeries.h"
#include "constant-weight_words/constantWeightWord.h"
/**
* Function that reads the initial list.
*
* @param intList the intList to fill.
*/
void read_intline(IntList *intList){
/// The buffer useful to read the input list.
char buffer[4096];
if(!fgets(buffer, sizeof buffer, stdin)) {
return;
}
/// The list of all input inserted by the user.
char *inputList = strtok(buffer, " \t\r\n");
while(inputList){
intlist_append(intList, atoi(inputList));
inputList = strtok(NULL, " \t\r\n");
}
}
/**
* Function that reads the pair position, value to insert.
*
* @param pairList the pairList to fill.
*/
void read_pairs(PairList *pairList){
printf("Insert the pair <position, value>, one per row;\n"
"Click enter on an empty row to stop the procedure.\n");
/// The buffer useful to read the pair
char buffer[256];
while(1){
fputs("> ",stdout);
fflush(stdout);
if(!fgets(buffer, sizeof buffer, stdin))
break;
if(buffer[0] == '\n' || buffer[0] == '\r')
break;
/// The first index of the pair.
int index0;
/// The second index of the pair.
int index1;
if(sscanf(buffer, "%d %d", &index0, &index1) == 2) {
pairlist_append(pairList, index0, index1);
}
else {
printf("Not a valid format");
}
}
}
/**
* Function that print the intList.
*
* @param intList the intList to print.
*/
void print_intlist(const IntList *intList){
putchar('[');
for(size_t i = 0; i < intList->listSize; ++i){
if(i) {
printf(", ");
}
printf("%d", intList->list[i]);
}
puts("]");
}
/**
* Function that displays a help message that describes how to use the program, including the available short and long command-line options.
*
* @param progName the program name.
*/
void print_help(const char *progName) {
printf("Usage: %s [options]\n", progName);
printf("Options:\n");
printf(" --insertionseries Execute the insertionSeries function\n");
printf(" --cww Execute the constant-weight word creation function\n");
printf(" -p, --parallel Run in parallel mode\n");
printf(" -s, --serial Run in serial mode (default)\n");
printf(" -h, --help Show this help message\n");
}
/**
* Function that shows the Daniel J. Bernstein's insertion series algorithm.
*
* @param serialOrParallel the type of algorithm execution, either parallel mode, 1, or serial mode, 0.
*/
void mainInsertionSeries(short serialOrParallel) {
printf("Insertion Series of DJB - %s\n\n", serialOrParallel ? "parallel version" : "serial version");
/// The intList to fill.
IntList intList;
intlist_init(&intList);
printf("Insert the list (number space separated):\n");
fflush(stdout);
read_intline(&intList);
printf("The list before the insertion:\n");
print_intlist(&intList);
/// The pairList - position, value.
PairList pairList;
pairlist_init(&pairList);
read_pairs(&pairList);
/// The list with the required elements inserted in the required positions.
IntList listWithNewElement = insertionseries(&intList, &pairList, serialOrParallel);
printf("\nThe list after all the insertion\n");
print_intlist(&listWithNewElement);
intlist_free(&listWithNewElement);
intlist_free(&intList);
pairlist_free(&pairList);
}
/**
* Function that shows the Daniel J. Bernstein's constant-weight word creation algorithm.
*
* @param serialOrParallel the type of algorithm execution, either parallel mode, 1, or serial mode, 0.
*/
void mainCww(short serialOrParallel) {
printf("Constant-Weight Word Construction of DJB - %s\n\n", serialOrParallel ? "parallel version" : "serial version");
/// The number of 0s in the constant-weight word.
int numberOfZero = 0;
printf("Insert the number of 0s: ");
int returnScanf = scanf("%d", &numberOfZero);
assert(returnScanf > 0);
/// Integer used only to read the \n or EOF after the scanf
int c;
while ((c = getchar()) != '\n' && c != EOF);
/// The intList of positions where the 1s will go.
IntList positionOfOne;
intlist_init(&positionOfOne);
printf("Insert the list of positions where to insert the 1s (number space separated):\n");
fflush(stdout);
read_intline(&positionOfOne);
/// The constant-weight word created using the cww function.
IntList result_cww = cww(numberOfZero, &positionOfOne, serialOrParallel);
/// The constant-weight word created using the cww_via_insertionseries function.
IntList result_cww_via_insertionseries = cww_via_insertionseries(numberOfZero, &positionOfOne, serialOrParallel);
printf("Creation of cww using the cww function:\n");
print_intlist(&result_cww);
printf("\nCreation of cww using the cww_via_insertionseries function:\n");
print_intlist(&result_cww_via_insertionseries);
intlist_free(&positionOfOne);
intlist_free(&result_cww);
intlist_free(&result_cww_via_insertionseries);
}
int main(int argc, char **argv) {
/// Selects the type of algorithm execution, either parallel mode, 1, or serial mode, 0.
short serialOrParallel = 0;
/// Selects the algorithm to be execute, either cww, 1, or insertionSeries, 0.
short algorithm = 0;
/// Defines the possible long options.
static struct option longOptions[] = {
{"insertionseries", no_argument, 0, 0},
{"cww", no_argument, 0, 0},
{"parallel", no_argument, 0, 'p'},
{"serial", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/// Gets the return value of the getopt function, i.e. the chosen option.
int opt;
/// Index in the longOptions array indicating which long option has been selected.
int option_index = 0;
while ((opt = getopt_long(argc, argv, "psh", longOptions, &option_index)) != -1) {
switch (opt) {
case 'p':
serialOrParallel = PARALLEL;
break;
case 's':
serialOrParallel = SERIAL;
break;
case 'h':
print_help(argv[0]);
return 0;
case 0: // only long options
if (!strncmp(longOptions[option_index].name, "insertionseries", strlen("insertionseries"))) {
algorithm = 0;
}
else if (!strncmp(longOptions[option_index].name, "cww", strlen("cww"))) {
algorithm = 1;
}
break;
default:
print_help(argv[0]);
return 1;
}
}
if (algorithm) {
mainCww(serialOrParallel);
}
else {
mainInsertionSeries(serialOrParallel);
}
return 0;
}