Skip to content

Commit 8133a1c

Browse files
committed
simplify code
1 parent 9478d5f commit 8133a1c

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

chapter05/5-7.c

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
/*
22
* Exercise 5-7. Rewrite readlines to store lines in an array supplied by main,
33
* rather than calling alloc to maintain storage. How much faster is the
4-
* program?
5-
* Answer: With the current implementation both clock_t and unix time command
6-
* report similar runtimes. Perhaps a much bigger data would show a
7-
* discrepancy in runtimes.
4+
* program?
5+
*
86
* By Faisal Saadatmand
97
*/
108

9+
/*
10+
* Answer: With the current implementation both clock_t and unix time command
11+
* report similar runtimes. This readlines, however, should be faster because
12+
* it doesn't have the overhead of calling the alloc function on each iteration
13+
* of its loop.
14+
*/
15+
1116
#include <stdio.h>
1217
#include <string.h>
1318

14-
#define MAXLINES 5000 /* max #lines to be stored */
15-
#define MAXLEN 1000 /* max length of any input line */
16-
#define ALLOCSIZE 100000 /* storage for alloc */
19+
#define MAXLINES 5000 /* max #lines to be stored */
20+
#define MAXLEN 1000 /* max length of any input line */
21+
#define MAXSIZE 10000 /* size of available space */
1722

1823
/* functions */
19-
int readlines(char *[], int, char *);
24+
int readlines(char *[], int, char []);
2025
void writelines(char *[], int);
2126
int getLine(char *, int);
2227
void qsort(char *[], int, int);
2328
void swap(char *[], int, int);
2429

25-
/* globals */
26-
char *lineptr[MAXLINES]; /* pointers to text lines */
27-
2830
/* readlines: read input lines */
29-
int readlines(char *lineptr[], int maxlines, char *p)
31+
int readlines(char *lineptr[], int maxlines, char text[])
3032
{
31-
int len, nlines, usedMemory;
32-
char line[MAXLEN];
33+
int len, nlines;
34+
char *p, line[MAXLEN];
3335

34-
usedMemory = 0;
36+
p = text;
3537
nlines = 0;
36-
while ((len = getLine(line, MAXLEN)) > 0)
37-
if (nlines >= maxlines || ALLOCSIZE - usedMemory < len)
38+
while ((len = getLine(line, MAXLEN)) > 0) {
39+
if (nlines >= maxlines || p >= text + MAXSIZE)
3840
return -1;
39-
else {
40-
line[len - 1] = '\0'; /* delete newline character */
41-
strcpy(p, line);
42-
lineptr[nlines++] = p;
43-
p += len; /* increment pointer */
44-
usedMemory += len; /* track memory usage */
45-
}
41+
line[len - 1] = '\0'; /* delete newline character */
42+
strcpy(p, line);
43+
lineptr[nlines++] = p;
44+
p += len;
45+
}
4646
return nlines;
4747
}
4848

@@ -76,13 +76,10 @@ void qsort(char *v[], int left, int right)
7676
{
7777
int i, last;
7878
void swap(char *v[], int i, int j);
79-
80-
if (left >= right) /* do nothing if array contains */
81-
return; /* fewer than two elements */
82-
79+
if (left >= right) /* do nothing if array contains */
80+
return; /* fewer than two elements */
8381
swap(v, left, (left + right) / 2);
8482
last = left;
85-
8683
for (i = left + 1; i <= right; i++)
8784
if (strcmp(v[i], v[left]) < 0)
8885
swap(v, ++last, i);
@@ -104,15 +101,15 @@ void swap(char *v[], int i, int j)
104101
/* sort lines */
105102
int main(void)
106103
{
107-
int nlines; /* number of input lines read */
108-
char memory[ALLOCSIZE]; /* stored lines */
104+
int nlines;
105+
char *lineptr[MAXLINES]; /* pointers to text lines */
106+
char text[MAXSIZE]; /* array to store lines */
109107

110-
if ((nlines = readlines(lineptr, MAXLINES, memory)) >= 0) {
111-
qsort(lineptr, 0, nlines - 1);
112-
writelines(lineptr, nlines);
113-
return 0;
114-
} else {
108+
if ((nlines = readlines(lineptr, MAXLINES, text)) < 0) {
115109
printf("error: input too big to sort\n");
116-
return 1;
110+
return -1;
117111
}
112+
qsort(lineptr, 0, nlines - 1);
113+
writelines(lineptr, nlines);
114+
return 0;
118115
}

0 commit comments

Comments
 (0)