Typeadapt-Comparator

Abstract interface (function table) to adapt a concrete user type to a container which needs comparing nodes and nodes and keys.

Summary
Typeadapt-ComparatorAbstract interface (function table) to adapt a concrete user type to a container which needs comparing nodes and nodes and keys.
CopyrightThis program is free software.
Files
C-kern/api/ds/typeadapt/comparator.hHeader file Typeadapt-Comparator.
C-kern/ds/typeadapt/comparator.cImplementation file Typeadapt-Comparator impl.
Types
struct typeadapt_comparator_itExport typeadapt_comparator_it into global namespace.
Functions
test
unittest_ds_typeadapt_comparatorTest typeadapt_comparator_it functionality.
typeadapt_comparator_itDeclares interface for comparing two objects and key with object.
cmp_key_objectCompares key with an object.
cmp_objectCompares two objects.
lifetime
typeadapt_comparator_FREEStatic initializer.
typeadapt_comparator_INITStatic initializer.
query
call-service
callcmpkeyobj_typeadaptcomparatorCalls function typeadapt_comparator_it.cmp_key_object.
callcmpobj_typeadaptcomparatorCalls function typeadapt_comparator_it.cmp_object.
generic
genericcast_typeadaptcomparatorCasts parameter adpcmp into pointer to typeadapt_comparator_it.
typeadapt_comparator_DECLAREDeclares a derived interface from generic typeadapt_comparator_it.
typeadapt_comparator_EMBEDDeclares a derived interface from generic typeadapt_comparator_it.
inline implementation
Macros
genericcast_typeadaptcomparatorImplements typeadapt_comparator_it.genericcast_typeadaptcomparator.
Functions
callcmpkeyobj_typeadaptcomparatorImplements typeadapt_comparator_it.callcmpkeyobj_typeadaptcomparator.
callcmpobj_typeadaptcomparatorImplements typeadapt_comparator_it.callcmpobj_typeadaptcomparator.

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/comparator.h

C-kern/ds/typeadapt/comparator.c

Implementation file Typeadapt-Comparator impl.

Types

struct typeadapt_comparator_it

typedef struct typeadapt_comparator_it typeadapt_comparator_it

Export typeadapt_comparator_it into global namespace.

test

unittest_ds_typeadapt_comparator

int unittest_ds_typeadapt_comparator(void)

Test typeadapt_comparator_it functionality.

typeadapt_comparator_it

struct typeadapt_comparator_it

Declares interface for comparing two objects and key with object.  If you change this interface do not forget to adapt typeadapt_comparator_EMBED and genericcast_typeadaptcomparator.

cmp_key_object

int (
   *cmp_key_object
) (struct typeadapt_t * typeadp, const void * lkey, const struct typeadapt_object_t * robject)

Compares key with an object. lkey is the left operand and robject the right one.

Returns

-1lkey < robject
0lkey == robject
+1lkey > robject

cmp_object

int (
   *cmp_object
) (struct typeadapt_t * typeadp, const struct typeadapt_object_t * lobject, const struct typeadapt_object_t * robject)

Compares two objects. lobject is left operand and robject the right one.

Returns

-1lobject < robject
0lobject == robject
+1lobject > robject

lifetime

typeadapt_comparator_FREE

#define typeadapt_comparator_FREE { 0, 0 }

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

typeadapt_comparator_INIT

#define typeadapt_comparator_INIT(cmpkeyobj_f,
cmpobj_f) { cmpkeyobj_f, cmpobj_f }

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

Parameters

cmpkeyobj_fFunction pointer to function comparing key with object.  See typeadapt_comparator_it.cmp_key_object.
cmpobj_fFunction pointer to function comparing object with object.  See typeadapt_comparator_it.cmp_object.

query

call-service

callcmpkeyobj_typeadaptcomparator

int callcmpkeyobj_typeadaptcomparator(typeadapt_comparator_it *adpcmp,
struct typeadapt_t *typeadp,
const void *key,
const struct typeadapt_object_t *robject)

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

callcmpobj_typeadaptcomparator

int callcmpobj_typeadaptcomparator(typeadapt_comparator_it *adpcmp,
struct typeadapt_t *typeadp,
const struct typeadapt_object_t *lobject,
const struct typeadapt_object_t *robject)

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

generic

genericcast_typeadaptcomparator

typeadapt_comparator_it * genericcast_typeadaptcomparator(
   void *adpcmp,
   TYPENAME typeadapter_t,
   TYPENAME object_t,
   TYPENAME key_t
) ;

Casts parameter adpcmp into pointer to typeadapt_comparator_it.  The parameter adpcmp has to be of type “pointer to declared_it” where declared_it is the name used as first parameter in typeadapt_comparator_DECLARE.  The second and third parameter must be the same as in typeadapt_comparator_DECLARE.

typeadapt_comparator_DECLARE

#define typeadapt_comparator_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 cmp_key_object") ; } typedef struct declared_it declared_it ; struct declared_it { typeadapt_comparator_EMBED(typeadapter_t, object_t, key_t) ; }

Declares a derived interface from generic typeadapt_comparator_it.  It is structural compatible with typeadapt_comparator_it.  See typeadapt_comparator_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_comparator_it supports.
key_tThe key type that typeadapt_comparator_it supports.  Must be of size sizeof(void*).

typeadapt_comparator_EMBED

#define typeadapt_comparator_EMBED(
   typeadapter_t,
   object_t,
   key_t
) int (* cmp_key_object) (typeadapter_t * typeadp, const key_t lkey, const object_t * robject) ; int (* cmp_object) (typeadapter_t * typeadp, const object_t * lobject, const object_t * robject)

Declares a derived interface from generic typeadapt_comparator_it.  It is structural compatible with typeadapt_comparator_it.  See typeadapt_comparator_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_comparator_it supports.
key_tThe key type that typeadapt_comparator_it supports.  Must be of size sizeof(void*).

Macros

genericcast_typeadaptcomparator

#define genericcast_typeadaptcomparator(
   adpcmp,
   typeadapter_t,
   object_t,
   key_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_comparator_it, cmp_key_object) == offsetof(typeof(*(adpcmp)), cmp_key_object) && offsetof(typeadapt_comparator_it, cmp_object) == offsetof(typeof(*(adpcmp)), cmp_object), "ensure same structure") ; if (0) { int _err = (adpcmp)->cmp_key_object((typeadapter_t*)0, (const key_t)0, (const object_t*)0) ; _err += (adpcmp)->cmp_object((typeadapter_t*)0, (const object_t*)0, (const object_t*)0) ; (void) _err ; } (typeadapt_comparator_it*) (adpcmp) ; }))

Implements typeadapt_comparator_it.genericcast_typeadaptcomparator.

Functions

callcmpkeyobj_typeadaptcomparator

callcmpobj_typeadaptcomparator

Abstract interface (function table) to adapt a concrete user type to a container which needs comparing nodes and nodes and keys.
Implements Typeadapt-Comparator.
typedef struct typeadapt_comparator_it typeadapt_comparator_it
Export typeadapt_comparator_it into global namespace.
struct typeadapt_comparator_it
Declares interface for comparing two objects and key with object.
int unittest_ds_typeadapt_comparator(void)
Test typeadapt_comparator_it functionality.
int (
   *cmp_key_object
) (struct typeadapt_t * typeadp, const void * lkey, const struct typeadapt_object_t * robject)
Compares key with an object.
int (
   *cmp_object
) (struct typeadapt_t * typeadp, const struct typeadapt_object_t * lobject, const struct typeadapt_object_t * robject)
Compares two objects.
#define typeadapt_comparator_FREE { 0, 0 }
Static initializer.
#define typeadapt_comparator_INIT(cmpkeyobj_f,
cmpobj_f) { cmpkeyobj_f, cmpobj_f }
Static initializer.
int callcmpkeyobj_typeadaptcomparator(typeadapt_comparator_it *adpcmp,
struct typeadapt_t *typeadp,
const void *key,
const struct typeadapt_object_t *robject)
Calls function typeadapt_comparator_it.cmp_key_object.
int callcmpobj_typeadaptcomparator(typeadapt_comparator_it *adpcmp,
struct typeadapt_t *typeadp,
const struct typeadapt_object_t *lobject,
const struct typeadapt_object_t *robject)
Calls function typeadapt_comparator_it.cmp_object.
typeadapt_comparator_it * genericcast_typeadaptcomparator(
   void *adpcmp,
   TYPENAME typeadapter_t,
   TYPENAME object_t,
   TYPENAME key_t
) ;
Casts parameter adpcmp into pointer to typeadapt_comparator_it.
#define typeadapt_comparator_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 cmp_key_object") ; } typedef struct declared_it declared_it ; struct declared_it { typeadapt_comparator_EMBED(typeadapter_t, object_t, key_t) ; }
Declares a derived interface from generic typeadapt_comparator_it.
#define typeadapt_comparator_EMBED(
   typeadapter_t,
   object_t,
   key_t
) int (* cmp_key_object) (typeadapter_t * typeadp, const key_t lkey, const object_t * robject) ; int (* cmp_object) (typeadapter_t * typeadp, const object_t * lobject, const object_t * robject)
Declares a derived interface from generic typeadapt_comparator_it.
#define genericcast_typeadaptcomparator(
   adpcmp,
   typeadapter_t,
   object_t,
   key_t
) ( __extension__ ({ static_assert( offsetof(typeadapt_comparator_it, cmp_key_object) == offsetof(typeof(*(adpcmp)), cmp_key_object) && offsetof(typeadapt_comparator_it, cmp_object) == offsetof(typeof(*(adpcmp)), cmp_object), "ensure same structure") ; if (0) { int _err = (adpcmp)->cmp_key_object((typeadapter_t*)0, (const key_t)0, (const object_t*)0) ; _err += (adpcmp)->cmp_object((typeadapter_t*)0, (const object_t*)0, (const object_t*)0) ; (void) _err ; } (typeadapt_comparator_it*) (adpcmp) ; }))
Implements typeadapt_comparator_it.genericcast_typeadaptcomparator.
Close