1
1
/*
2
2
* Exercise 5-7. Rewrite readlines to store lines in an array supplied by main,
3
3
* 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
+ *
8
6
* By Faisal Saadatmand
9
7
*/
10
8
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
+
11
16
#include <stdio.h>
12
17
#include <string.h>
13
18
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 */
17
22
18
23
/* functions */
19
- int readlines (char * [], int , char * );
24
+ int readlines (char * [], int , char [] );
20
25
void writelines (char * [], int );
21
26
int getLine (char * , int );
22
27
void qsort (char * [], int , int );
23
28
void swap (char * [], int , int );
24
29
25
- /* globals */
26
- char * lineptr [MAXLINES ]; /* pointers to text lines */
27
-
28
30
/* readlines: read input lines */
29
- int readlines (char * lineptr [], int maxlines , char * p )
31
+ int readlines (char * lineptr [], int maxlines , char text [] )
30
32
{
31
- int len , nlines , usedMemory ;
32
- char line [MAXLEN ];
33
+ int len , nlines ;
34
+ char * p , line [MAXLEN ];
33
35
34
- usedMemory = 0 ;
36
+ p = text ;
35
37
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 )
38
40
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
+ }
46
46
return nlines ;
47
47
}
48
48
@@ -76,13 +76,10 @@ void qsort(char *v[], int left, int right)
76
76
{
77
77
int i , last ;
78
78
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 */
83
81
swap (v , left , (left + right ) / 2 );
84
82
last = left ;
85
-
86
83
for (i = left + 1 ; i <= right ; i ++ )
87
84
if (strcmp (v [i ], v [left ]) < 0 )
88
85
swap (v , ++ last , i );
@@ -104,15 +101,15 @@ void swap(char *v[], int i, int j)
104
101
/* sort lines */
105
102
int main (void )
106
103
{
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 */
109
107
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 ) {
115
109
printf ("error: input too big to sort\n" );
116
- return 1 ;
110
+ return - 1 ;
117
111
}
112
+ qsort (lineptr , 0 , nlines - 1 );
113
+ writelines (lineptr , nlines );
114
+ return 0 ;
118
115
}
0 commit comments