| 1 |
<HTML> |
| 2 |
|
| 3 |
<HEAD> |
| 4 |
|
| 5 |
<TITLE> ACEDB source code documentation: the graph library </TITLE> |
| 6 |
|
| 7 |
<LINK rel=stylesheet type="text/css" |
| 8 |
href="./wdoc.css" |
| 9 |
title="Style sheet for wdoc developer documents"> |
| 10 |
|
| 11 |
</HEAD> |
| 12 |
|
| 13 |
<BODY> |
| 14 |
|
| 15 |
<H1>Application programmers' guide to the arraysubs package</H1> |
| 16 |
|
| 17 |
<PRE> |
| 18 |
|
| 19 |
The arraysubs package contains a number of widely used utility |
| 20 |
packages. The code is in arraysub.c. The public interface is in |
| 21 |
array.h. The code relies on the messubs package for error handling |
| 22 |
(messubs.c, regular.h). |
| 23 |
|
| 24 |
Array package |
| 25 |
------------- |
| 26 |
|
| 27 |
This is pervasisve in ACEDB. It gives arbitrary length extendable |
| 28 |
arrays, called Arrays (with a capital initial 'A'). They are |
| 29 |
accessed via macros for efficiency when you are sure that they are |
| 30 |
not being extended. Many other functions are in fact macros to |
| 31 |
give a cleaner programmer's interface (see the header file array.h |
| 32 |
for details). |
| 33 |
|
| 34 |
Basic operations: |
| 35 |
Array arrayCreate (int n, TYPE) |
| 36 |
|
| 37 |
n is the initial size. TYPE is a legitimate type. sizeof(TYPE) is |
| 38 |
taken (in a macro) to determine what size objects to have in the |
| 39 |
array. |
| 40 |
|
| 41 |
Array arrayHandleCreate(int n, TYPE, STORE_HANDLE handle) |
| 42 |
|
| 43 |
is analogous, but registers the resulting array to be destroyed by an call |
| 44 |
to handleFree(). See messubs.doc for details. |
| 45 |
|
| 46 |
Array arrayReCreate (Array a, int n, TYPE) |
| 47 |
|
| 48 |
If a exists already it is cleared and extended if necessary to |
| 49 |
length n. In this case the size of TYPE must correspond to the |
| 50 |
previous type size (they ought to be the same type if you have any |
| 51 |
sense!), else you messcrash(). If a does not exist then it is |
| 52 |
created as with arrayCreate. arrayReCreate() never deallocates |
| 53 |
memory, so repeated recreation of a static work array is more |
| 54 |
efficient than create/destruction. |
| 55 |
|
| 56 |
BOOL arrayDestroy (Array a) |
| 57 |
|
| 58 |
This is a macro that returns destroys a (releasing all its memory) |
| 59 |
and returns TRUE if a is non-zero, and returns FALSE if a == 0. |
| 60 |
|
| 61 |
int arrayMax (Array a) |
| 62 |
|
| 63 |
Returns the largest index addressed so far plus one. i.e. the |
| 64 |
number of elements. A (very) common use is as an upper bound to a |
| 65 |
loop, e.g.: |
| 66 |
|
| 67 |
for (i = 0 ; i < arrayMax(a) ; ++i) |
| 68 |
x += arr(a,i,float) ; |
| 69 |
|
| 70 |
arrayMax() is a macro that can in principle be assigned to. |
| 71 |
Unfortunately we do this quite a lot to clear arrays (e.g. |
| 72 |
arrayMax(a) = 0). This is considered very bad form. If you want |
| 73 |
to clear the array then call arrayRecreate(). Any other explicit |
| 74 |
manipulation of arrayMax is completely beyond the pale! |
| 75 |
|
| 76 |
TYPE array (Array a, int i, TYPE) |
| 77 |
TYPE* arrayp (Array a, int i, TYPE) |
| 78 |
TYPE arr (Array a, int i, TYPE) |
| 79 |
TYPE* arrp (Array a, int i, TYPE) |
| 80 |
|
| 81 |
These are the basic functions to access members of an Array. They |
| 82 |
can all be used as lvalues as well as rvalues, i.e. you can assign |
| 83 |
to them. arrayp() and arrp() give a pointer to the i'th element of |
| 84 |
a, while array() and arr() give the element itself. array() and |
| 85 |
arrayp() make subroutine calls that check whether i >= arrayMax(a), |
| 86 |
and if so extend the array if necessary and update arrayMax(a). |
| 87 |
arr() and arrp() are pure macros that do not check arrayMax(a) and |
| 88 |
should therefore only be used for accessing existing entries, not |
| 89 |
for creating new ones that might go beyond the previous limits. |
| 90 |
|
| 91 |
int arrayExists (Array a) |
| 92 |
|
| 93 |
This returns 0 if a is not an array, or a unique id if it is. If |
| 94 |
you really want to be sure that you have the same array now as some |
| 95 |
other time you should keep the id, and check it against the new |
| 96 |
value. The reason for this is that sometimes an array is destroyed |
| 97 |
and another created using the same memory, so there is a valid |
| 98 |
Array with pointer value "a", even though the one that you thought |
| 99 |
it was has been destroyed. This can lead to VERY subtle bugs. The |
| 100 |
most watertight solution would have been to use unique integers as |
| 101 |
the Array values; this was done for the Graph handle, but it has |
| 102 |
too high an efficiency cost for the Array package. |
| 103 |
|
| 104 |
int arrayStatus (int *memAlloc, int *memUsed) |
| 105 |
|
| 106 |
This returns the number of active arrays, and fills the arguments with |
| 107 |
the total amount of memory allocated, and the total amount of memory |
| 108 |
being used (i.e. the sum of the sizes of the memory blocks up to the |
| 109 |
current maximal entry in each array), both in bytes. |
| 110 |
|
| 111 |
int arrayReportMark (void) |
| 112 |
void arrayReport (int j) |
| 113 |
|
| 114 |
This allows more detailed monitoring. arrayReportMark gives the |
| 115 |
number of the most recently allocated array. arrayReport write out a |
| 116 |
line to stderr on each array created since j (the result of a previous |
| 117 |
call to arrayReportMark, or 0 for all arrays). |
| 118 |
|
| 119 |
Minor Array routines: |
| 120 |
Array arrayCopy (Array a) - gives a copy, including contents |
| 121 |
void arrayExtend (Array a, int n) |
| 122 |
- extends a to length at least n. Should |
| 123 |
not be used - use array() or arrayp() |
| 124 |
|
| 125 |
arrayForceFeed() is in array.h but considered too abusive by |
| 126 |
Richard, who is writing this. |
| 127 |
|
| 128 |
Sorted Array package |
| 129 |
-------------------- |
| 130 |
|
| 131 |
A number of additional routines are very useful for maintaining |
| 132 |
sorted arrays. Many of them make use of an order() function passed |
| 133 |
by the user, exactly as with Unix sort(). |
| 134 |
order(TYPE *x, TYPE *y) |
| 135 |
should return negative if x < y, 0 if x == y, positive if x > y. |
| 136 |
All the equality matches in these functions are byte-wise matches. |
| 137 |
|
| 138 |
void arraySort (Array a, int (*order)()) |
| 139 |
- Sorts a in ascending order of order(). |
| 140 |
Uses qsort(). Does not remove duplicates. |
| 141 |
BOOL arrayIsEntry (Array a, int i, void *s) |
| 142 |
- returns TRUE if arr(a,i,) matches *s, else FALSE |
| 143 |
BOOL arrayFind (Array a, void *s, int *ip, int (*order)()) |
| 144 |
- if *s matches any arr(a,i,) sets *ip = i and |
| 145 |
returns TRUE, else FALSE |
| 146 |
BOOL arrayInsert (Array a, void * s, int (*order)()) |
| 147 |
- s is a pointer to a potential entry. |
| 148 |
Returns FALSE if arrayFind (a, s, &junk, order) |
| 149 |
else inserts *s in order. |
| 150 |
BOOL arrayRemove (Array a, void * s, int (*order)()) |
| 151 |
- if arrayFind (a, s, &junk, order) removes it and |
| 152 |
returns TRUE, else returns FALSE |
| 153 |
void arrayCompress (Array a) |
| 154 |
- removes bytewise duplicate entries - assumes |
| 155 |
already sorted. |
| 156 |
|
| 157 |
Stack package |
| 158 |
------------- |
| 159 |
|
| 160 |
This gives standard stack functions on an arbitrary-size stack. |
| 161 |
There are also some extensions to handle arbitrary length text. It |
| 162 |
makes priveleged use of Arrays (i.e. uses internal non-documented |
| 163 |
properties of their implementation). |
| 164 |
|
| 165 |
Stacks can be used for pushing/popping. They also hold a cursor |
| 166 |
that can be used to step through the stack forwards. They can hold |
| 167 |
heterogeneous data types, since the package works in bytes. This |
| 168 |
is used in the graph package, and elsewhere. |
| 169 |
|
| 170 |
Creation/Destruction: |
| 171 |
|
| 172 |
Stack stackCreate (int n) |
| 173 |
Stack stackHandleCreate(int n, STORE_HANDLE handle) |
| 174 |
Stack stackReCreate (Stack s, int n) |
| 175 |
|
| 176 |
stackCreate() creates stack of n bytes initial size (n is just a |
| 177 |
guideline - it can be exceeded without error). stackReCreate() |
| 178 |
behaves analagously to arrayReCreate(). |
| 179 |
|
| 180 |
BOOL stackDestroy (Stack s) |
| 181 |
|
| 182 |
analagous to arrayDestroy(). |
| 183 |
|
| 184 |
BOOL stackExists (Stack s) |
| 185 |
|
| 186 |
Unlike for the arrayExists() this is just a BOOL. Oh well, I guess |
| 187 |
we never had any problems with this one. |
| 188 |
|
| 189 |
Basic operations: |
| 190 |
void push (Stack s, DATA x, TYPE) |
| 191 |
TYPE pop (Stack s, TYPE) |
| 192 |
BOOL stackEmpty (Stack s) |
| 193 |
|
| 194 |
These are all complex macros, that may call functions if necessary |
| 195 |
to extend the stack. In push() x must be of type TYPE. push() |
| 196 |
only refers to x once, but they all refer to s many times, so avoid |
| 197 |
side-effects in your instantiation of s. stackEmpty() is a macro that |
| 198 |
can be used when popping to check if there is anything left on the |
| 199 |
stack to pop. It is a fatal error to pop off an empty stack. |
| 200 |
|
| 201 |
For Efficiency Reasons, it is not permissable to push or pop |
| 202 |
a type double. Instead use; |
| 203 |
|
| 204 |
void pushDouble(Stack s, double x) |
| 205 |
double popDouble(Stack s) |
| 206 |
|
| 207 |
Text operations: |
| 208 |
void pushText (Stack s, char *text) |
| 209 |
char* popText (Stack s) |
| 210 |
void catText (Stack s, char *text) |
| 211 |
char* stackText (Stack s, int mark) |
| 212 |
|
| 213 |
pushText() pushes text onto a stack, and popText() takes it off. They |
| 214 |
correctly handle arbitrarily long text. catText() must follow |
| 215 |
pushText(), or another catText(). It concatenates the text like |
| 216 |
strcat(), but you don't have to worry about buffer overflow. The top |
| 217 |
of the stack is always left on a word boundary, to avoid alignment |
| 218 |
problems. stackText() gives a way to access the text in a stack if |
| 219 |
you do not want to destroy it using popText(). Normally mark is set |
| 220 |
to 0, starting from the start of the stack, but it could also be a |
| 221 |
return value from stackMark() (see below). stackTextMax() gives the |
| 222 |
byte length of text, assuming there is a single text object on the |
| 223 |
stack. |
| 224 |
|
| 225 |
There is an additional function |
| 226 |
void catBinary (Stack s, char* data, int size) |
| 227 |
analogous to catText that uses memcpy to add any type and amount of |
| 228 |
data. |
| 229 |
|
| 230 |
Functions to use the cursor: |
| 231 |
int stackMark (Stack s) |
| 232 |
void stackCursor (Stack s, int mark) |
| 233 |
TYPE stackNext (Stack s, TYPE) |
| 234 |
double stackDoubleNext(Stack s) |
| 235 |
char* stackNextText (Stack s) |
| 236 |
BOOL stackAtEnd (Stack s) |
| 237 |
|
| 238 |
stackMark() returns a cursor value. stackCursor() repositions the |
| 239 |
cursor to a previously determined mark. 0 is a valid cursor value for |
| 240 |
the start of the stack, i.e. it is valid to stackCursor (s, 0). |
| 241 |
stackNext() delivers the item at the current cursor location and |
| 242 |
increments the cursor. stackDoubleNext is the equivalent for doubles, which |
| 243 |
are not permissable with stackNext. stackNextText() delivers text put onto the |
| 244 |
stack using pushText(), and moves the cursor to the next item past it. |
| 245 |
stackAtEnd (Stack s) is a macro that returns TRUE if the cursor is at |
| 246 |
the end of the stack. It is a fatal error to go past the end of the |
| 247 |
stack. |
| 248 |
|
| 249 |
NB stackMark() gives the number of bytes from the base of the stack to |
| 250 |
its current top, so can be used as a stackMax() call to give the |
| 251 |
current size of the stack. |
| 252 |
|
| 253 |
Tokeniser: |
| 254 |
void stackTokeniseTextOn (Stack s, char *text, char *delimiters) |
| 255 |
|
| 256 |
This is potentially a nice tokeniser for text, but we don't use it |
| 257 |
much and it probably needs more work. Look at the code if you are |
| 258 |
interested. |
| 259 |
|
| 260 |
Minor stack routines: |
| 261 |
void stackClear (Stack s) - empties the stack |
| 262 |
void stackExtend (Stack s, int n) - see arrayExtend() |
| 263 |
|
| 264 |
stackTextForceFeed() is an ugly unsanctioned call. |
| 265 |
|
| 266 |
Associator package |
| 267 |
------------------ |
| 268 |
|
| 269 |
This is a package that implements hash tables to allow arbitrary |
| 270 |
pointers or integers to be associated with each other. The |
| 271 |
implementation keeps the hash table between 1/4 and 1/2 full. The |
| 272 |
hash function used is exceedingly simple: |
| 273 |
hash = ((unsigned int)xin >> 2) & a->mask ; |
| 274 |
The division by four (>> 2) is to make hashing pointers more |
| 275 |
efficient, but it means that consecutive integers hash badly. There |
| 276 |
is no text hashing capability yet. |
| 277 |
|
| 278 |
NB You can not associate anything to xin == 0. This is used as the |
| 279 |
marker for an empty bin. |
| 280 |
|
| 281 |
The basic routines to create/destroy etc. mimic those for Array/Stack: |
| 282 |
Associator assCreate (void) |
| 283 |
Associator assBigCreate (int n) -- lets you set the initial size |
| 284 |
Associator assReCreate (Associator a) |
| 285 |
BOOL assDestroy (Associator a) |
| 286 |
BOOL assExists (Associator a) |
| 287 |
|
| 288 |
The routines to add/find/subtract entries are: |
| 289 |
BOOL assInsert (Associator a, void* xin, void* xout) |
| 290 |
void assMultipleInsert(Associator a, void* xin, void* xout) |
| 291 |
BOOL assFind (Associator a, void* xin, void* *pout) |
| 292 |
BOOL assRemove (Associator a, void* xin) |
| 293 |
BOOL assPairRemove(Associator a, void *xin, void *xout) |
| 294 |
BOOL assFindNext (Associator a, void *xin, void* *pout) |
| 295 |
|
| 296 |
assInsert() fails if something has already been associated to xin. |
| 297 |
assMultipleInsert allows more than one association with xin. |
| 298 |
assPairRemove only removes an association with xin of it is to xout. |
| 299 |
assFindNext is used to get multiple associations to the same key. |
| 300 |
Start by calling assFind to initialise things, then call assfindNext until |
| 301 |
it returns FALSE. Note that the first call to AssFindNext gives the same result |
| 302 |
as the call to assFind. |
| 303 |
The other calls are self-explanatory. |
| 304 |
|
| 305 |
Utility routines are: |
| 306 |
void assDump (Associator a) |
| 307 |
void assClear (Associator a) |
| 308 |
BOOL assNext (Associator a, void* *pin, void* *pout) |
| 309 |
|
| 310 |
assDump() uses printf and is only for debugging. assClear() empties |
| 311 |
the associator. assNext() allows you to step through all the entries. |
| 312 |
Start with *pin == 0. Each call that returns TRUE gives a new in/out |
| 313 |
pair, until the end of the table is reached, when it returns FALSE. |
| 314 |
|
| 315 |
You can read a->n, which is the number of stored items, but do not set |
| 316 |
it! This should be a function call. |
| 317 |
|
| 318 |
Line breaking package |
| 319 |
--------------------- |
| 320 |
|
| 321 |
This is a package to break lines. Lines are broken at spaces if |
| 322 |
possible, else at width exactly if there are no spaces. Of course any |
| 323 |
'\n's contained in the text are respected as line breaks. |
| 324 |
|
| 325 |
There are several variants. One possibility is to first call |
| 326 |
int uLinesText (char *text, int width) |
| 327 |
then successively one of |
| 328 |
char *uNextLine (char *text) |
| 329 |
char *uPopLine (char *text) |
| 330 |
which get the lines in forward or reverse order, ending with a 0. |
| 331 |
|
| 332 |
Alternatively |
| 333 |
char **uBrokenLines (char *text, int width) |
| 334 |
gives a zero terminated array of lines, while |
| 335 |
char *uBrokenText (char *text, int width) |
| 336 |
gives a single string into which '\n's have been intercalated. |
| 337 |
|
| 338 |
<PRE> |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
</BODY> |
| 343 |
</HTML> |