In article
<0f2fcd7d-9fd0-47cd-8458-d517e7defc07@[EMAIL PROTECTED]
>
Michael.Zhang <zhangqiuyuan@[EMAIL PROTECTED]
> wrote:
[in a reply to Ben Bacarisse]
>I just want to make sure I got you right.You are suggesting:
>typedef struct Table{ ...} Table;
>Table * makeTable( void* size, void * data);
I can only speak for myself, not for Ben Bacarisse, but I rather
doubt that is what he is suggesting.
In the absence of additional details, here is how *I* might do
this. Note that I prefer not to use "typedef" -- which does not
actually define types -- at all, but there are some "pro-typedef"
reasons. See <http://web.torek.net/torek/c/types2.html>
for
details on this.
In this particular case, I might have a "table.h" file. In table.h,
one would find, e.g., the following (assuming the table is indexed
by simple "row,column" integer-pairs):
struct table;
struct table *table_create(void);
void *table_lookup(struct table *table, int row, int col);
int table_insert(struct table *table, int row, int col, void *data);
void *table_delete(struct table *table, int row, int col);
void table_iterate(struct table *table, void *aux,
void (*iter)(struct table *table, void *aux,
int row, int col, void *data));
void table_destroy(struct table *table, int free_all_data);
Note that there is no "{" after "struct table". This means that
the entire contents of a "struct table" are hidden. So where are
they? The answer is: in table.c (or perhaps in table-impl.h, which
is then #include-ed by table1.c and table2.c, if for some reason
"table.c" is not suitable to be used as a single file).
"Table clients", as it were, now have no idea what is *inside* a
table. They only know that they can "create" one, do "lookup"
operations, do "insert" operations, do "delete" operations, ask to
"iterate" a function (of their choosing) over all table entries,
and "destroy" a table (with, in this case, optional automatic calls
to free() on each table item, so that they need not write a "freeing
iterator").
There is still quite a bit exposed here. For instance, this makes
it clear that tables are indexed exclusively by integral (row,column)
pairs. We must also decide whether "iterate" is called for "empty"
entries (e.g., if there is a row 7, but only a column 3 in row 7
-- even though other rows have columns 0 through 5 -- does the
user's iterator get called with NULL entries for columns 0, 1, 2,
4, and 5 on row 7?). Nonetheless, the actual details of how the
tables are implemented are safely tucked away: clients know only
as much about tables as you choose to expose, while the "table
server" code (in table.c) sees all the details.
(Also, rather than "table", the above should probably called a
"sparse matrix" anyway. "Tables" might actually instead be hash
tables that implement what one uses associative arrays for in
languages like awk, for instance.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html


|