Typeadapt-GetHash

Abstract interface (function table) to adapt a concrete user type to a container which needs to compute the hash value of a node or its associated key.

Summary
Typeadapt-GetHashAbstract interface (function table) to adapt a concrete user type to a container which needs to compute the hash value of a node or its associated key.
CopyrightThis program is free software.
Files
C-kern/api/ds/typeadapt/gethash.hHeader file Typeadapt-GetHash.
C-kern/ds/typeadapt/gethash.cImplementation file Typeadapt-GetHash impl.
Types
struct typeadapt_gethash_itExport typeadapt_gethash_it into global namespace.
Functions
test
unittest_ds_typeadapt_gethashTest typeadapt_gethash_it functionality.
typeadapt_gethash_itDeclares interface for computing a hash value from key / object.
hashobjectComputes the hash value of the key of an object.
hashkeyComputes the hash value of a key.
lifetime
typeadapt_gethash_FREEStatic initializer.
typeadapt_gethash_INITStatic initializer.
query
isequal_typeadaptgethashReturns true if two typeadapt_gethash_it objects are equal.
call-service
callhashobject_typeadaptgethashCalls function typeadapt_gethash_it.hashobject.
callhashkey_typeadaptgethashCalls function typeadapt_gethash_it.hashkey.
generic
genericcast_typeadaptgethashCasts parameter gethash into pointer to typeadapt_gethash_it.
typeadapt_gethash_DECLAREDeclares a derived interface from generic typeadapt_gethash_it.
typeadapt_gethash_EMBEDDeclares a derived interface from generic typeadapt_gethash_it.
inline implementation
Macros
genericcast_typeadaptgethashImplements typeadapt_gethash_it.genericcast_typeadaptgethash.
callhashobject_typeadaptgethashImplements typeadapt_gethash_it.callhashobject_typeadaptgethash.
callhashkey_typeadaptgethashImplements typeadapt_gethash_it.callhashkey_typeadaptgethash.
typeadapt_gethash_DECLAREImplements typeadapt_gethash_it.typeadapt_gethash_DECLARE.
typeadapt_gethash_EMBEDImplements typeadapt_gethash_it.typeadapt_gethash_EMBED.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/ds/typeadapt/gethash.h

Header file Typeadapt-GetHash.

C-kern/ds/typeadapt/gethash.c

Implementation file Typeadapt-GetHash impl.

Types

struct typeadapt_gethash_it

typedef struct typeadapt_gethash_it typeadapt_gethash_it

Export typeadapt_gethash_it into global namespace.

Functions

test

unittest_ds_typeadapt_gethash

int unittest_ds_typeadapt_gethash(void)

Test typeadapt_gethash_it functionality.

typeadapt_gethash_it

struct typeadapt_gethash_it

Declares interface for computing a hash value from key / object.  If you change this interface do not forget to adapt typeadapt_gethash_EMBED and genericcast_typeadaptgethash.

Summary
hashobjectComputes the hash value of the key of an object.
hashkeyComputes the hash value of a key.
lifetime
typeadapt_gethash_FREEStatic initializer.
typeadapt_gethash_INITStatic initializer.
query
isequal_typeadaptgethashReturns true if two typeadapt_gethash_it objects are equal.
call-service
callhashobject_typeadaptgethashCalls function typeadapt_gethash_it.hashobject.
callhashkey_typeadaptgethashCalls function typeadapt_gethash_it.hashkey.
generic
genericcast_typeadaptgethashCasts parameter gethash into pointer to typeadapt_gethash_it.
typeadapt_gethash_DECLAREDeclares a derived interface from generic typeadapt_gethash_it.
typeadapt_gethash_EMBEDDeclares a derived interface from generic typeadapt_gethash_it.

hashobject

size_t (
   *hashobject
) (struct typeadapt_t * typeadp, const struct typeadapt_object_t * node)

Computes the hash value of the key of an object.  The parameter is a pointer to the object which contains the key.

hashkey

size_t (*hashkey) (struct typeadapt_t * typeadp, const void * key)

Computes the hash value of a key.  This computation must correspond with hashobject.  The reason is that insert operations uses the object to compute the hash functions and find operations uses the key value fir its hash computation.

lifetime

typeadapt_gethash_FREE

#define typeadapt_gethash_FREE typeadapt_gethash_INIT(0,
)

Static initializer.  Sets all functions pointers of typeadapt_gethash_it to 0.

typeadapt_gethash_INIT

#define typeadapt_gethash_INIT(hashobject_f,
hashkey_f) { hashobject_f, hashkey_f }

Static initializer.  Sets all function pointers to the provided values.

Parameters

hashobject_fFunction pointer to function returning the hash value of an object.  See typeadapt_gethash_it.hashobject.
hashkey_fFunction pointer to function returning the hash value of a key.  See typeadapt_gethash_it.hashkey.

query

isequal_typeadaptgethash

bool isequal_typeadaptgethash(const typeadapt_gethash_it *lgethash,
const typeadapt_gethash_it *rgethash)

Returns true if two typeadapt_gethash_it objects are equal.

call-service

callhashobject_typeadaptgethash

size_t callhashobject_typeadaptgethash(typeadapt_gethash_it *gethash,
struct typeadapt_t *typeadp,
const struct typeadapt_object_t *node)

Calls function typeadapt_gethash_it.hashobject.  The first parameter is of type typeadapt_gethash_it the others are the same as in typeadapt_gethash_it.hashobject.  This function is implemented as macro and supports types derived from typeadapt_gethash_it - see use of typeadapt_gethash_DECLARE.

callhashkey_typeadaptgethash

size_t callhashkey_typeadaptgethash(typeadapt_gethash_it *gethash,
struct typeadapt_t *typeadp,
const void *key)

Calls function typeadapt_gethash_it.hashkey.  The first parameter is of type typeadapt_gethash_it the others are the same as in typeadapt_gethash_it.hashkey.  This function is implemented as macro and supports types derived from typeadapt_gethash_it - see use of typeadapt_gethash_DECLARE.

generic

genericcast_typeadaptgethash

typeadapt_gethash_it * genericcast_typeadaptgethash(void *gethash,
TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;

Casts parameter gethash into pointer to typeadapt_gethash_it.  The parameter gethash has to be of type “pointer to declared_it” where declared_it is the name used as first parameter in typeadapt_gethash_DECLARE.  The other parameter have to be the same as in typeadapt_gethash_DECLARE.

typeadapt_gethash_DECLARE

void typeadapt_gethash_DECLARE(TYPENAME declared_it,
TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;

Declares a derived interface from generic typeadapt_gethash_it.  It is structural compatible with typeadapt_gethash_it.  See typeadapt_gethash_it for a list of contained functions.

Parameter

declared_itThe name of the structure which is declared as the interface.  The name should have the suffix “_it”.
typeadapter_tThe adapter type which implements all interface functions.  The first parameter in every function is a pointer to this type.
object_tThe object type that typeadapt_gethash_it supports.
key_tThe key type that typeadapt_gethash_it supports.  Must be of size sizeof(void*).

typeadapt_gethash_EMBED

void typeadapt_gethash_EMBED(TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;

Declares a derived interface from generic typeadapt_gethash_it.  It is structural compatible with typeadapt_gethash_it.  See typeadapt_gethash_it for a list of contained functions.

Parameter

typeadapter_tThe adapter type which implements all interface functions.  The first parameter in every function is a pointer to this type.
object_tThe object type that typeadapt_gethash_it supports.

Macros

genericcast_typeadaptgethash

#define genericcast_typeadaptgethash(
   gethash,
   typeadapter_t,
   object_t,
   key_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_gethash_it, hashobject) == offsetof(typeof(*(gethash)), hashobject) && offsetof(typeadapt_gethash_it, hashkey) == offsetof(typeof(*(gethash)), hashkey), "ensure same structure") ; if (0) { size_t hash = 0 ; hash += (gethash)->hashobject((typeadapter_t*)0, (const object_t*)0) ; hash += (gethash)->hashkey((typeadapter_t*)0, (const key_t)0) ; } (typeadapt_gethash_it*) (gethash) ; }))

Implements typeadapt_gethash_it.genericcast_typeadaptgethash.

callhashobject_typeadaptgethash

#define callhashobject_typeadaptgethash(
   gethash,
   typeadp,
   node
) ((gethash)->hashobject((typeadp), (node)))

Implements typeadapt_gethash_it.callhashobject_typeadaptgethash.

callhashkey_typeadaptgethash

#define callhashkey_typeadaptgethash(
   gethash,
   typeadp,
   key
) ((gethash)->hashkey((typeadp), (key)))

Implements typeadapt_gethash_it.callhashkey_typeadaptgethash.

typeadapt_gethash_DECLARE

#define typeadapt_gethash_DECLARE(
   declared_it,
   typeadapter_t,
   object_t,
   key_t
) static inline void compiletimeassert##declared_it(void) { static_assert(sizeof(key_t)==sizeof(void*), "compatible with hashkey") ; } typedef struct declared_it declared_it ; struct declared_it { typeadapt_gethash_EMBED(typeadapter_t, object_t, key_t) ; }

Implements typeadapt_gethash_it.typeadapt_gethash_DECLARE.

typeadapt_gethash_EMBED

#define typeadapt_gethash_EMBED(
   typeadapter_t,
   object_t,
   key_t
) size_t (*hashobject) (typeadapter_t * typeadp, const object_t * node) ; size_t (*hashkey) (typeadapter_t * typeadp, const key_t key)

Implements typeadapt_gethash_it.typeadapt_gethash_EMBED.

Abstract interface (function table) to adapt a concrete user type to a container which needs to compute the hash value of a node or its associated key.
Implements Typeadapt-GetHash.
typedef struct typeadapt_gethash_it typeadapt_gethash_it
Export typeadapt_gethash_it into global namespace.
struct typeadapt_gethash_it
Declares interface for computing a hash value from key / object.
int unittest_ds_typeadapt_gethash(void)
Test typeadapt_gethash_it functionality.
size_t (
   *hashobject
) (struct typeadapt_t * typeadp, const struct typeadapt_object_t * node)
Computes the hash value of the key of an object.
size_t (*hashkey) (struct typeadapt_t * typeadp, const void * key)
Computes the hash value of a key.
#define typeadapt_gethash_FREE typeadapt_gethash_INIT(0,
)
Static initializer.
#define typeadapt_gethash_INIT(hashobject_f,
hashkey_f) { hashobject_f, hashkey_f }
Static initializer.
bool isequal_typeadaptgethash(const typeadapt_gethash_it *lgethash,
const typeadapt_gethash_it *rgethash)
Returns true if two typeadapt_gethash_it objects are equal.
size_t callhashobject_typeadaptgethash(typeadapt_gethash_it *gethash,
struct typeadapt_t *typeadp,
const struct typeadapt_object_t *node)
Calls function typeadapt_gethash_it.hashobject.
size_t callhashkey_typeadaptgethash(typeadapt_gethash_it *gethash,
struct typeadapt_t *typeadp,
const void *key)
Calls function typeadapt_gethash_it.hashkey.
typeadapt_gethash_it * genericcast_typeadaptgethash(void *gethash,
TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;
Casts parameter gethash into pointer to typeadapt_gethash_it.
void typeadapt_gethash_DECLARE(TYPENAME declared_it,
TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;
Declares a derived interface from generic typeadapt_gethash_it.
void typeadapt_gethash_EMBED(TYPENAME typeadapter_t,
TYPENAME object_t,
TYPENAME key_t) ;
Declares a derived interface from generic typeadapt_gethash_it.
#define genericcast_typeadaptgethash(
   gethash,
   typeadapter_t,
   object_t,
   key_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_gethash_it, hashobject) == offsetof(typeof(*(gethash)), hashobject) && offsetof(typeadapt_gethash_it, hashkey) == offsetof(typeof(*(gethash)), hashkey), "ensure same structure") ; if (0) { size_t hash = 0 ; hash += (gethash)->hashobject((typeadapter_t*)0, (const object_t*)0) ; hash += (gethash)->hashkey((typeadapter_t*)0, (const key_t)0) ; } (typeadapt_gethash_it*) (gethash) ; }))
Implements typeadapt_gethash_it.genericcast_typeadaptgethash.
#define callhashobject_typeadaptgethash(
   gethash,
   typeadp,
   node
) ((gethash)->hashobject((typeadp), (node)))
Implements typeadapt_gethash_it.callhashobject_typeadaptgethash.
#define callhashkey_typeadaptgethash(
   gethash,
   typeadp,
   key
) ((gethash)->hashkey((typeadp), (key)))
Implements typeadapt_gethash_it.callhashkey_typeadaptgethash.
#define typeadapt_gethash_DECLARE(
   declared_it,
   typeadapter_t,
   object_t,
   key_t
) static inline void compiletimeassert##declared_it(void) { static_assert(sizeof(key_t)==sizeof(void*), "compatible with hashkey") ; } typedef struct declared_it declared_it ; struct declared_it { typeadapt_gethash_EMBED(typeadapter_t, object_t, key_t) ; }
Implements typeadapt_gethash_it.typeadapt_gethash_DECLARE.
#define typeadapt_gethash_EMBED(
   typeadapter_t,
   object_t,
   key_t
) size_t (*hashobject) (typeadapter_t * typeadp, const object_t * node) ; size_t (*hashkey) (typeadapter_t * typeadp, const key_t key)
Implements typeadapt_gethash_it.typeadapt_gethash_EMBED.
Close