-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.c
executable file
·168 lines (124 loc) · 3.33 KB
/
test4.c
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
#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "libmapreduce.h"
#define CHARS_PER_INPUT 30000
#define INPUTS_NEEDED 10
typedef struct _list_t
{
struct _list_t *next;
char *word;
int count;
} list_t;
list_t *head = NULL;
void map(int fd, const char *data)
{
list_t *thru;
while (1)
{
char *word = malloc(100); int word_len = 0;
const char *word_end = data;
/* Get each word... */
while (1)
{
if ( ((*word_end == ' ' || *word_end == '\n') && word_len > 0) || *word_end == '\0' || word_len == 99)
break;
else if (isalpha(*word_end))
word[word_len++] = tolower(*word_end);
word_end++;
}
if (*word_end == '\0') { break; }
word[word_len] = '\0';
/* Update our linked list... */
thru = head;
while (thru != NULL)
{
if ( strcmp(thru->word, word) == 0 )
{
thru->count++;
break;
}
thru = thru->next;
}
if (thru == NULL)
{
list_t *node = malloc(sizeof(list_t));
node->next = head;
node->word = word;
node->count = 1;
head = node;
}
else
free(word);
data = word_end + 1;
}
thru = head;
char s[200];
while (thru != NULL)
{
int len = snprintf(s, 200, "%s: %d\n", thru->word, thru->count);
write(fd, s, len);
list_t *thru_next = thru->next;
free(thru->word);
free(thru);
thru = thru_next;
}
close(fd);
}
const char *reduce(const char *value1, const char *value2)
{
int i1 = atoi(value1);
int i2 = atoi(value2);
char *result;
asprintf(&result, "%d", (i1 + i2));
return result;
}
int main()
{
FILE *file = fopen("alice.txt", "r");
char s[1024];
int i;
char **values = malloc(INPUTS_NEEDED * sizeof(char *));
int values_cur = 0;
values[0] = malloc(CHARS_PER_INPUT + 1);
values[0][0] = '\0';
while (fgets(s, 1024, file) != NULL)
{
if (strlen(values[values_cur]) + strlen(s) < CHARS_PER_INPUT)
strcat(values[values_cur], s);
else
{
values_cur++;
values[values_cur] = malloc(CHARS_PER_INPUT + 1);
values[values_cur][0] = '\0';
strcat(values[values_cur], s);
}
}
values_cur++;
values[values_cur] = NULL;
fclose(file);
mapreduce_t mr;
mapreduce_init(&mr, map, reduce);
mapreduce_map_all(&mr, (const char **)values);
mapreduce_reduce_all(&mr);
const char *result_the = mapreduce_get_value(&mr, "the");
const char *result_and = mapreduce_get_value(&mr, "and");
const char *result_alice = mapreduce_get_value(&mr, "alice");
const char *result_nonexist = mapreduce_get_value(&mr, "some-word-that-wont-exist");
if (result_the == NULL) { printf("the: (null)\n"); }
else { printf("the: %s\n", result_the); free((void *)result_the); }
if (result_and == NULL) { printf("and: (null)\n"); }
else { printf("and: %s\n", result_and); free((void *)result_and); }
if (result_alice == NULL) { printf("alice: (null)\n"); }
else { printf("alice: %s\n", result_alice); free((void *)result_alice); }
if (result_nonexist == NULL) { printf("some-word-that-wont-exist: (null)\n"); }
else { printf("some-word-that-wont-exist: %s\n", result_nonexist); free((void *)result_nonexist); }
mapreduce_destroy(&mr);
for (i = 0; i < values_cur; i++)
free(values[i]);
free(values);
return 0;
}