Returning a pointer to a function

T

Thomas Brunko

Hy,
I'm working on a hash table and came upon a small problem. For a better
explanation I've included an extract of the header file for the hash module:

typedef struct TABLE * TABLE_P;
typedef void* (*APPLY_SINGLE_F)( void * data );
typedef void* (*APPLY_PAIR_F)( void * key, void * value );

/* Allocate memory for the table and initialise it */
TABLE_P table_create();

/* Release memory of the table */
void table_release( TABLE_P *t);

.
. Some functions for table manipulation omited.
.

/* For all (key->value) pairs execute apply(key,value). Returns first
* value with (NULL != apply(key,value)) */
void* table_apply( TABLE_P t, APPLY_PAIR_F apply );

Now I would like to write a function transform:
APPLY_PAIR_F transform( APPLY_SINGLE_F apply );
This function builds from its parameter 'apply' a function that can be used in
the call to 'table_apply'. The result of transform should be a function, that
applies the 'apply' function to the 'value' of a (key->value) pair like this:
void * result_of_transform( void * key, void * value ){
return apply( value );
}

Of course it would be easier to write a function like
void * table_apply_value( TABE_P table, APPLY_SINGLE_F apply );
which applies 'apply' to the values of the table. But I don't want to extend
the interface of my tables if it's not absolutely necessary.

Any suggestions?

Thanks,
Thomas.
 
C

CBFalconer

Thomas said:
.... snip ...

Now I would like to write a function transform:
APPLY_PAIR_F transform( APPLY_SINGLE_F apply );
This function builds from its parameter 'apply' a function that
can be used in the call to 'table_apply'. The result of transform
should be a function, that applies the 'apply' function to the
'value' of a (key->value) pair like this:

void * result_of_transform( void * key, void * value ){
return apply( value );
}

Of course it would be easier to write a function like
void * table_apply_value( TABE_P table, APPLY_SINGLE_F apply );
which applies 'apply' to the values of the table. But I don't want
to extend the interface of my tables if it's not absolutely
necessary.

Any suggestions?

Take a look at the implementation of hshwalk in my hashlib.zip
package. This allows the user to build just such an apply
operation, and many other things.

<http://cbfalconer.home.att.net/download/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top