J
jacob navia
Hi
I would like to compare the C containers library (written in C for C
programmers) against the STL.
Here is the code for the CCL. Maybe one C++ wizard would solve the
same problem using the STL?
I would be very ingterested in comparing code size, complexity, etc.
Thanks in advance, and here is the C part:
--------------------------------------------------------------
Unique
------
Given a text file, print in standard output the lines that are
unique in it, i.e. filtering all duplicated lines.
Algorithm:
---------
Normally this involves keeping a sorted list/array of lines
and testing if a line is in the set or not.
Solution using the CCL.
----------------------
1 #include <containers.h>
2 int main(int argc,char *argv[])
3 {
4 FILE *f;
5 int i=1,r;
6 Dictionary *dict;
7 char buf[8192];
8
9 if (argc < 2) {
10 fprintf(stderr,"%s <file name>\n",argv[0]);
11 return -1;
12 }
13 f = fopen(argv[1],"r");
14 if (f == NULL)
15 return -1;
16 dict = iDictionary.Create(0,500);
17 if (dict == NULL)
18 return -1;
19 while (fgets(buf,sizeof(buf),f)) {
20 r= iDictionary.Add(dict,buf,NULL);
21 if (r > 0)
22 printf("[%3d] %s",i,buf);
23 else if (r < 0) break;
24 i++;
25 }
26 iDictionary.Finalize(dict);
27 fclose(f);
28 }
Algorithm
---------
A hash table will be used to determine if a line is a duplicate
or not.
Commentary
----------
We use the following local variables (lines 4-7):
Name Usage
f Input stream bound to the file to read
i Counter for lines read
r Result of adding a line
dict Dictionary (Hash table)
buf Line buffer limited to 8K per line
Lines 9-15 are concerned with opening the input file, with some error
checking.
In line 16 we create a dictionary, requesting a size of zero for the
data associated with the key since we aren't storing any data, just the
key, and we suppose that the table will contain more or less 500
entries. If the file contains much more lines performance could
suffer but the algorithm would still work.
Lines 19-25 are the main loop of the program. We read each line into
the buffer and add it to then dictionary. If the "Add" API returns
a positive number the line wasn't there, if it returns zero the
line was already in the dictionary. If the result is negative it
is an error code and we stop the loop aborting the operation. Failure
can be provoked only by lack of memory.
If the result is positive we print the line.
Cleanup is performed in lines 26 and 27: we dispose of the dictionary
and close the file.
The C containers library web page is:
http://code.google.com/p/ccl/
You will find there the source code and the documentation.
-----------------------------------------------------------------------
I would like to compare the C containers library (written in C for C
programmers) against the STL.
Here is the code for the CCL. Maybe one C++ wizard would solve the
same problem using the STL?
I would be very ingterested in comparing code size, complexity, etc.
Thanks in advance, and here is the C part:
--------------------------------------------------------------
Unique
------
Given a text file, print in standard output the lines that are
unique in it, i.e. filtering all duplicated lines.
Algorithm:
---------
Normally this involves keeping a sorted list/array of lines
and testing if a line is in the set or not.
Solution using the CCL.
----------------------
1 #include <containers.h>
2 int main(int argc,char *argv[])
3 {
4 FILE *f;
5 int i=1,r;
6 Dictionary *dict;
7 char buf[8192];
8
9 if (argc < 2) {
10 fprintf(stderr,"%s <file name>\n",argv[0]);
11 return -1;
12 }
13 f = fopen(argv[1],"r");
14 if (f == NULL)
15 return -1;
16 dict = iDictionary.Create(0,500);
17 if (dict == NULL)
18 return -1;
19 while (fgets(buf,sizeof(buf),f)) {
20 r= iDictionary.Add(dict,buf,NULL);
21 if (r > 0)
22 printf("[%3d] %s",i,buf);
23 else if (r < 0) break;
24 i++;
25 }
26 iDictionary.Finalize(dict);
27 fclose(f);
28 }
Algorithm
---------
A hash table will be used to determine if a line is a duplicate
or not.
Commentary
----------
We use the following local variables (lines 4-7):
Name Usage
f Input stream bound to the file to read
i Counter for lines read
r Result of adding a line
dict Dictionary (Hash table)
buf Line buffer limited to 8K per line
Lines 9-15 are concerned with opening the input file, with some error
checking.
In line 16 we create a dictionary, requesting a size of zero for the
data associated with the key since we aren't storing any data, just the
key, and we suppose that the table will contain more or less 500
entries. If the file contains much more lines performance could
suffer but the algorithm would still work.
Lines 19-25 are the main loop of the program. We read each line into
the buffer and add it to then dictionary. If the "Add" API returns
a positive number the line wasn't there, if it returns zero the
line was already in the dictionary. If the result is negative it
is an error code and we stop the loop aborting the operation. Failure
can be provoked only by lack of memory.
If the result is positive we print the line.
Cleanup is performed in lines 26 and 27: we dispose of the dictionary
and close the file.
The C containers library web page is:
http://code.google.com/p/ccl/
You will find there the source code and the documentation.
-----------------------------------------------------------------------