473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

It was just a memory aid when reading C code

BACKGROUND READING:
http://www.geocities.com/logan.lee30..._analysis.html
http://www.geocities.com/logan.lee30..._analysis.html

Like this:

CODE:
#include "sort.h"
40
41
#ifndef lint
42
__RCSID("$NetBSD: append.c,v 1.10 2001/02/19 20:50:17 jdolecek Exp $");
43
__SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
44
#endif /* not lint */
45
46
#include <stdlib.h>
47
#include <string.h>
48
49
#define OUTPUT { \
50
if ((n = cpos - ppos) 1) { \
51
for (; ppos < cpos; ++ppos) \
52
*ppos -= odepth; \
53
ppos -= n; \
54
if (stable_sort) \
55
sradixsort(ppos, n, wts1, REC_D); \
56
else \
57
radixsort(ppos, n, wts1, REC_D); \
58
for (; ppos < cpos; ppos++) { \
59
prec = (const RECHEADER *) (*ppos - sizeof(TRECHEADER));\
60
put(prec, fp); \
61
} \
62
} else put(prec, fp); \
63
}
64
65
/*
66
* copy sorted lines to output; check for uniqueness
67
*/
68
void
69
append(keylist, nelem, depth, fp, put, ftbl)
70
const u_char **keylist;
71
int nelem;
72
int depth;
73
FILE *fp;
74
put_func_t put;
75
struct field *ftbl;
76
{
77
u_char *wts, *wts1;
78
int n, odepth=0;
79
const u_char **cpos, **ppos, **lastkey;
80
const u_char *cend, *pend, *start;
81
const struct recheader *crec, *prec;
82
83
if (*keylist == '\0' && UNIQUE)
84
return;
85
wts1 = wts = ftbl[0].weights;
86
if ((!UNIQUE) && SINGL_FLD) {
87
if ((ftbl[0].flags & F) && (ftbl[0].flags & R))
88
wts1 = Rascii;
89
else if (ftbl[0].flags & F)
90
wts1 = ascii;
91
odepth = depth;
92
}
93
lastkey = keylist + nelem;
94
depth += sizeof(TRECHEADER);
95
if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
96
ppos = keylist;
97
prec = (const RECHEADER *) (*ppos - depth);
98
if (UNIQUE)
99
put(prec, fp);
100
for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
101
crec = (const RECHEADER *) (*cpos - depth);
102
if (crec->length == prec->length) {
103
/*
104
* Set pend and cend so that trailing NUL and
105
* record separator is ignored.
106
*/
107
pend = (const u_char *) &prec->data + prec->length - 2;
108
cend = (const u_char *) &crec->data + crec->length - 2;
109
for (start = *cpos; cend >= start; cend--) {
110
if (wts[*cend] != wts[*pend])
111
break;
112
pend--;
113
}
114
if (pend + 1 != *ppos) {
115
if (!UNIQUE) {
116
OUTPUT;
117
} else
118
put(crec, fp);
119
ppos = cpos;
120
prec = crec;
121
}
122
} else {
123
if (!UNIQUE) {
124
OUTPUT;
125
} else
126
put(crec, fp);
127
ppos = cpos;
128
prec = crec;
129
}
130
}
131
if (!UNIQUE) { OUTPUT; }
132
} else if (UNIQUE) {
133
ppos = keylist;
134
prec = (const RECHEADER *) (*ppos - depth);
135
put(prec, fp);
136
for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
137
crec = (const RECHEADER *) (*cpos - depth);
138
if (crec->offset == prec->offset) {
139
/*
140
* Set pend and cend so that trailing NUL and
141
* record separator is ignored.
142
*/
143
pend = (const u_char *) &prec->data + prec->offset - 2;
144
cend = (const u_char *) &crec->data + crec->offset - 2;
145
for (start = *cpos; cend >= start; cend--) {
146
if (wts[*cend] != wts[*pend])
147
break;
148
pend--;
149
}
150
if (pend + 1 != *ppos) {
151
ppos = cpos;
152
prec = crec;
153
put(prec, fp);
154
}
155
} else {
156
ppos = cpos;
157
prec = crec;
158
put(prec, fp);
159
}
160
}
161
} else for (cpos = keylist; cpos < lastkey; cpos++) {
162
crec = (const RECHEADER *) (*cpos - depth);
163
put(crec, fp);
164
}
165
}
166
167
/*
168
* output the already sorted eol bin.
169
*/
170
void
171
rd_append(binno, infl0, nfiles, outfp, buffer, bufend)
172
u_char *buffer;
173
int infl0;
174
int binno, nfiles;
175
FILE *outfp;
176
u_char *bufend;
177
{
178
RECHEADER *rec;
179
180
rec = (RECHEADER *) buffer;
181
if (!getnext(binno, infl0, NULL, nfiles,
182
(RECHEADER *) buffer, bufend, 0)) {
183
putline(rec, outfp);
184
while (getnext(binno, infl0, NULL, nfiles, (RECHEADER *) buffer,
185
bufend, 0) == 0) {
186
if (!UNIQUE)
187
putline(rec, outfp);
188
}
189
}
190
}
191
192
/*
193
* append plain text--used after sorting the biggest bin.
194
*/
195
void
196
concat(a, b)
197
FILE *a, *b;
198
{
199
int nread;
200
char buffer[4096];
201
202
rewind(b);
203
while ((nread = fread(buffer, 1, 4096, b)) 0)
204
EWRITE(buffer, 1, nread, a);
205
}

------------------------------------
SUMMARY:
sort
append.c
#include 3
#define OUTPUT
(I)[2]((I))_1[1,2,1]

append 6 200101
10211
I
I(I)[2]
(I)[3]((I))_2[1,1,1]
(I)[2]((I))_1[1,1]I
(I)[2]
(I)[2]
F(I)[2]((I))_1[1,1]I
F

rd_append 6 10011
1
IWI

concat 2 11
01
W
------------------------------------

You see it's much more simpler and aids human understanding of the code.
Dec 24 '07 #1
2 1434
Mon, 24 Dec 2007 05:32:54 +0000에, Logan Lee 썼습니다:
BACKGROUND READING:
http://www.geocities.com/logan.lee30..._analysis.html
http://www.geocities.com/logan.lee30..._analysis.html

Like this:

CODE:
#include "sort.h"
40
41
#ifndef lint
42
__RCSID("$NetBSD: append.c,v 1.10 2001/02/19 20:50:17 jdolecek Exp $");
43
__SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
44
#endif /* not lint */
45
46
#include <stdlib.h>
47
#include <string.h>
48
49
#define OUTPUT { \
50
if ((n = cpos - ppos) 1) { \
51
for (; ppos < cpos; ++ppos) \
52
*ppos -= odepth; \
53
ppos -= n; \
54
if (stable_sort) \
55
sradixsort(ppos, n, wts1, REC_D); \
56
else \
57
radixsort(ppos, n, wts1, REC_D); \
58
for (; ppos < cpos; ppos++) { \
59
prec = (const RECHEADER *) (*ppos - sizeof(TRECHEADER));\
60
put(prec, fp); \
61
} \
62
} else put(prec, fp); \
63
}
64
65
/*
66
* copy sorted lines to output; check for uniqueness
67
*/
68
void
69
append(keylist, nelem, depth, fp, put, ftbl)
70
const u_char **keylist;
71
int nelem;
72
int depth;
73
FILE *fp;
74
put_func_t put;
75
struct field *ftbl;
76
{
77
u_char *wts, *wts1;
78
int n, odepth=0;
79
const u_char **cpos, **ppos, **lastkey;
80
const u_char *cend, *pend, *start;
81
const struct recheader *crec, *prec;
82
83
if (*keylist == '\0' && UNIQUE)
84
return;
85
wts1 = wts = ftbl[0].weights;
86
if ((!UNIQUE) && SINGL_FLD) {
87
if ((ftbl[0].flags & F) && (ftbl[0].flags & R))
88
wts1 = Rascii;
89
else if (ftbl[0].flags & F)
90
wts1 = ascii;
91
odepth = depth;
92
}
93
lastkey = keylist + nelem;
94
depth += sizeof(TRECHEADER);
95
if (SINGL_FLD && (UNIQUE || wts1 != wts)) {
96
ppos = keylist;
97
prec = (const RECHEADER *) (*ppos - depth);
98
if (UNIQUE)
99
put(prec, fp);
100
for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
101
crec = (const RECHEADER *) (*cpos - depth);
102
if (crec->length == prec->length) {
103
/*
104
* Set pend and cend so that trailing NUL and
105
* record separator is ignored.
106
*/
107
pend = (const u_char *) &prec->data + prec->length - 2;
108
cend = (const u_char *) &crec->data + crec->length - 2;
109
for (start = *cpos; cend >= start; cend--) {
110
if (wts[*cend] != wts[*pend])
111
break;
112
pend--;
113
}
114
if (pend + 1 != *ppos) {
115
if (!UNIQUE) {
116
OUTPUT;
117
} else
118
put(crec, fp);
119
ppos = cpos;
120
prec = crec;
121
}
122
} else {
123
if (!UNIQUE) {
124
OUTPUT;
125
} else
126
put(crec, fp);
127
ppos = cpos;
128
prec = crec;
129
}
130
}
131
if (!UNIQUE) { OUTPUT; }
132
} else if (UNIQUE) {
133
ppos = keylist;
134
prec = (const RECHEADER *) (*ppos - depth);
135
put(prec, fp);
136
for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
137
crec = (const RECHEADER *) (*cpos - depth);
138
if (crec->offset == prec->offset) {
139
/*
140
* Set pend and cend so that trailing NUL and
141
* record separator is ignored.
142
*/
143
pend = (const u_char *) &prec->data + prec->offset - 2;
144
cend = (const u_char *) &crec->data + crec->offset - 2;
145
for (start = *cpos; cend >= start; cend--) {
146
if (wts[*cend] != wts[*pend])
147
break;
148
pend--;
149
}
150
if (pend + 1 != *ppos) {
151
ppos = cpos;
152
prec = crec;
153
put(prec, fp);
154
}
155
} else {
156
ppos = cpos;
157
prec = crec;
158
put(prec, fp);
159
}
160
}
161
} else for (cpos = keylist; cpos < lastkey; cpos++) {
162
crec = (const RECHEADER *) (*cpos - depth);
163
put(crec, fp);
164
}
165
}
166
167
/*
168
* output the already sorted eol bin.
169
*/
170
void
171
rd_append(binno, infl0, nfiles, outfp, buffer, bufend)
172
u_char *buffer;
173
int infl0;
174
int binno, nfiles;
175
FILE *outfp;
176
u_char *bufend;
177
{
178
RECHEADER *rec;
179
180
rec = (RECHEADER *) buffer;
181
if (!getnext(binno, infl0, NULL, nfiles,
182
(RECHEADER *) buffer, bufend, 0)) {
183
putline(rec, outfp);
184
while (getnext(binno, infl0, NULL, nfiles, (RECHEADER *) buffer,
185
bufend, 0) == 0) {
186
if (!UNIQUE)
187
putline(rec, outfp);
188
}
189
}
190
}
191
192
/*
193
* append plain text--used after sorting the biggest bin.
194
*/
195
void
196
concat(a, b)
197
FILE *a, *b;
198
{
199
int nread;
200
char buffer[4096];
201
202
rewind(b);
203
while ((nread = fread(buffer, 1, 4096, b)) 0)
204
EWRITE(buffer, 1, nread, a);
205
}

------------------------------------
SUMMARY:
sort
append.c
#include 3
#define OUTPUT
(I)[2]((I))_1[1,2,1]

append 6 200101
10211
I
I(I)[2]
(I)[3]((I))_2[1,1,1]
(I)[2]((I))_1[1,1]I
(I)[2]
(I)[2]
F(I)[2]((I))_1[1,1]I
F

rd_append 6 10011
1
IWI

concat 2 11
01
W
------------------------------------

You see it's much more simpler and aids human understanding of the code.
http://www.geocities.com/logan.lee30/code_example.pdf
Dec 24 '07 #2
Logan Lee wrote:
BACKGROUND READING:
http://www.geocities.com/logan.lee30..._analysis.html
http://www.geocities.com/logan.lee30..._analysis.html

Like this:

CODE:
#include "sort.h"
40
41
#ifndef lint
42
__RCSID("$NetBSD: append.c,v 1.10 2001/02/19 20:50:17 jdolecek Exp $");
43
__SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
44
....
------------------------------------
SUMMARY:
sort
append.c
#include 3
#define OUTPUT
(I)[2]((I))_1[1,2,1]

append 6 200101
10211
I
I(I)[2]
(I)[3]((I))_2[1,1,1]
(I)[2]((I))_1[1,1]I
(I)[2]
(I)[2]
F(I)[2]((I))_1[1,1]I
F

rd_append 6 10011
1
IWI

concat 2 11
01
W
------------------------------------

You see it's much more simpler and aids human understanding of the code.
Whatever your point was, it didn't come across. Perhaps this is useful for
something, but it isn't apparent to me from your post or reference. You
haven't explained what you are trying to achieve.

--
Thad
Dec 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: assi | last post by:
Hello all We are developing a large dotnet application, which includes ~ 120 assemblies. (total size of all binaries is ~ 20MB). Our application also references the following dotnet assemblies:...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
4
by: Sebastian Becker | last post by:
Hello NG, is fscanf capable of reading a memory adress from a file? I tried to find the right format-parameter, but my code doesn't seem to work... Regards, Sebastian
6
by: hauger | last post by:
(DB2 V.8.1) Hi @ll, when i start my DB2 and my application connects and makes some SQL Tests my system is running out of memory (UNITED LINUX 1.0) goes slow and needs very very long time to...
19
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used...
2
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some...
3
by: growse | last post by:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is to send a simple string from B to A. A is always running. I've overridden the WndProc method to give me messages that are...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.