librnr  1.14.5
RoadNarrows Robotics Common Library 1
assoc.h File Reference

Simple associative map data and operator declarations. More...

#include <sys/types.h>
#include "rnr/rnrconfig.h"

Go to the source code of this file.

Classes

struct  Nvp_T
 
struct  AssocMapVoidPoint_T
 
struct  AssocMapVoidMapper_T
 

Macros

#define AMAP_NAME   Void
 
#define _AMAP_NAME   AMAP_NAME
 assoc. map name
 
#define AMAP_XTYPE   void *
 default assoc. map x data type More...
 
#define AMAP_YTYPE   void *
 default assoc. map y data type
 
#define _AMAP_XTYPE   AMAP_XTYPE
 assoc. map x data type
 
#define _AMAP_YTYPE   AMAP_YTYPE
 assoc. map y data type
 
#define _CONCAT_(x, y)   x ## y
 build concatenation operator More...
 
#define _CONCAT(x, y)   _CONCAT_(x, y)
 now concatenate
 
#define _AMAP_DEFPREFACE   _CONCAT(AssocMap, _AMAP_NAME)
 AssocMap (base or derived) definition preface.
 
#define _AMAP_DEF(name)   _CONCAT(_AMAP_DEFPREFACE, name)
 AssocMap (base or derived) definition name.
 
#define _RNR_ASSOC_H
 include base declaration only once More...
 

Typedefs

typedef int(* AssocMapVoidCmp_T) (void *t1, void *t2)
 base associative map comparator function type
 

Functions

int NvpName2Val (Nvp_T tbl[], size_t nTblEntries, const char *sName)
 Get the value associated with the name. More...
 
const char * NvpVal2Name (Nvp_T tbl[], size_t nTblEntries, int iVal)
 Get the name associated with the value. More...
 
void * AssocMapVoidXtoY (AssocMapVoidMapper_T *pMapper, void *px)
 Get the y value associated with the given x value. More...
 
void * AssocMapVoidYtoX (AssocMapVoidMapper_T *pMapper, void *py)
 Get the x value associated with the given y value. More...
 

Detailed Description

Simple associative map data and operator declarations.

These associative maps are composed of tables (vectors) of discrete (x, y) points where x and y may be of any data types.

The specific Nvp_T Name-Value Pair associative map point provides a char* <–> int mapping.

The more general associative mapping is provided by the AssocMap<x> declarations.

The general associative maps are configured to support a "poor man's" C version of class inheritance and templating.

The "base" set of void* data types and functions are AssocMapVoid<x> named definitions with data types returning void*. These definitions are defined in librnr.

A "derived" associative map can be defined using the following three #define's (usually in a header file). Follow these #defines with the inclusion of this header file (assoc.h):

AMAP_NAME
The associative map derived namespace name. All data types and function definitions are named: AssocMap<AMAP_NAME><x>
AMAP_XTYPE
The derived x data type.
AMAP_YTYPE
The the derived y data type.

Multiple assoc. maps derived types and the base type may be used in the same C source.

See also
example_assoc under "Related Pages" for an example.
Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/assoc.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file assoc.h.

Macro Definition Documentation

#define _CONCAT_ (   x,
 
)    x ## y

build concatenation operator

Useful string concatenation macros.

Definition at line 114 of file assoc.h.

#define _RNR_ASSOC_H

include base declaration only once

Undefine macros to allow redefinition.

Definition at line 352 of file assoc.h.

#define AMAP_NAME   Void

Define the associative namespace (must be literal).

If AMAP_NAME is defined then a new set of assoc. map data types and functions will be defined, prefaced by AssocMap<AMAP_NAME>.

If AMAP_NAME is not defined, then the base set prefaced by AssocMapVoid data types and functions will be used (already defined in librnr).base assoc. map name not a "derived" assoc. map

Definition at line 86 of file assoc.h.

#define AMAP_XTYPE   void *

default assoc. map x data type

Define the derived associative map x and y data types (must be literal).

AMAP_XTYPE defines the x data type. If not defined, then void* is used.

AMAP_YTYPE defines the y data type. If not defined, then void* is used.

Definition at line 101 of file assoc.h.

Function Documentation

void* AssocMapVoidXtoY ( AssocMapVoidMapper_T pMapper,
void *  px 
)

Get the y value associated with the given x value.

The table search terminates at the first match or at the end of the table.

Parameters
pMapperAssociative map mapper container.
pxPointer to X value in table to find.
Returns
The associated y value on success, or the default y value if no x value is found.

Definition at line 123 of file assoc.c.

References CHKPTR, AssocMapVoidMapper_T::m_opXCmp, AssocMapVoidMapper_T::m_pMapDft, AssocMapVoidMapper_T::m_tblAssocMap, AssocMapVoidMapper_T::m_tblSize, NULL, AssocMapVoidPoint_T::x, and AssocMapVoidPoint_T::y.

124 {
125  size_t i;
126 
127  CHKPTR(pMapper, NULL);
128  CHKPTR(pMapper->m_tblAssocMap, NULL);
129  CHKPTR(pMapper->m_pMapDft, NULL);
130 
131  if( pMapper->m_opXCmp == NULL )
132  {
133  return pMapper->m_pMapDft->y;
134  }
135 
136  for(i=0; i<pMapper->m_tblSize; ++i)
137  {
138  if( pMapper->m_opXCmp(pMapper->m_tblAssocMap[i].x, px) == 0 )
139  {
140  return pMapper->m_tblAssocMap[i].y;
141  }
142  }
143  return pMapper->m_pMapDft->y;
144 }
AssocMapVoidPoint_T * m_pMapDft
default x, y point
Definition: assoc.h:191
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_tblSize
number of entries in table
Definition: assoc.h:188
AssocMapVoidPoint_T * m_tblAssocMap
associative map table
Definition: assoc.h:187
void * y
with this y value (and vice versa).
Definition: assoc.h:172
void * x
the x value associates
Definition: assoc.h:171
AssocMapVoidCmp_T m_opXCmp
return 0 if x1 == x2
Definition: assoc.h:189
void* AssocMapVoidYtoX ( AssocMapVoidMapper_T pMapper,
void *  py 
)

Get the x value associated with the given y value.

The table search terminates at the first match or at the end of the table.

Parameters
pMapperAssociative map mapper container.
pyPointer to Y value in table to find.
Returns
The associated x value on success, or the default x value if no y value is found.

Definition at line 159 of file assoc.c.

References CHKPTR, AssocMapVoidMapper_T::m_opYCmp, AssocMapVoidMapper_T::m_pMapDft, AssocMapVoidMapper_T::m_tblAssocMap, AssocMapVoidMapper_T::m_tblSize, NULL, AssocMapVoidPoint_T::x, and AssocMapVoidPoint_T::y.

160 {
161  size_t i;
162 
163  CHKPTR(pMapper, NULL);
164  CHKPTR(pMapper->m_tblAssocMap, NULL);
165  CHKPTR(pMapper->m_pMapDft, NULL);
166 
167  if( pMapper->m_opYCmp == NULL )
168  {
169  return pMapper->m_pMapDft->x;
170  }
171 
172  for(i=0; i<pMapper->m_tblSize; ++i)
173  {
174  if( pMapper->m_opYCmp(pMapper->m_tblAssocMap[i].y, py) == 0 )
175  {
176  return pMapper->m_tblAssocMap[i].x;
177  }
178  }
179  return pMapper->m_pMapDft->x;
180 }
AssocMapVoidPoint_T * m_pMapDft
default x, y point
Definition: assoc.h:191
#define CHKPTR(p,...)
Checks validity of pointer.
Definition: log.h:651
#define NULL
null pointer
Definition: rnrconfig.h:199
size_t m_tblSize
number of entries in table
Definition: assoc.h:188
AssocMapVoidPoint_T * m_tblAssocMap
associative map table
Definition: assoc.h:187
void * y
with this y value (and vice versa).
Definition: assoc.h:172
void * x
the x value associates
Definition: assoc.h:171
AssocMapVoidCmp_T m_opYCmp
return 0 if y1 == y2
Definition: assoc.h:190
int NvpName2Val ( Nvp_T  tbl[],
size_t  nTblEntries,
const char *  sName 
)

Get the value associated with the name.

The table search terminates at the first match or at the end of nTblEntries entries.

Parameters
tbl[]Name-Value Pair table
nTblEntriesNumber of name-value pair table entries
sNameName to in table to find.
Returns
The associated value on success, or the first (default) value in the table if the no name match is found.

Definition at line 64 of file assoc.c.

References CHKEXPR_ULONG, and Nvp_T::m_iVal.

Referenced by OptsCvtArgLogLevel(), and test_assoc().

65 {
66  size_t i;
67 
68  CHKEXPR_ULONG(nTblEntries, (nTblEntries > 0), 0);
69 
70  for(i=0; i<nTblEntries; ++i)
71  {
72  if( !strcmp(tbl[i].m_sName, sName) )
73  {
74  return tbl[i].m_iVal;
75  }
76  }
77  return tbl[0].m_iVal;
78 }
int m_iVal
associated integer value
Definition: assoc.h:154
#define CHKEXPR_ULONG(val, expr,...)
check unsigned long integer
Definition: log.h:697
const char* NvpVal2Name ( Nvp_T  tbl[],
size_t  nTblEntries,
int  iVal 
)

Get the name associated with the value.

The table search terminates at the first match or at the end of nTblEntries entries.

Parameters
tbl[]Name-Value Pair table
nTblEntriesNumber of name-value pair table entries
iValValue in table to find.
Returns
The associated name on success, or the first (default) value in the table if the no value match is found.

Definition at line 94 of file assoc.c.

References CHKEXPR_ULONG, and Nvp_T::m_sName.

Referenced by AddNewBody(), PrintBody(), test_assoc(), and units_shortname().

95 {
96  size_t i;
97 
98  CHKEXPR_ULONG(nTblEntries, (nTblEntries > 0), 0);
99 
100  for(i=0; i<nTblEntries; ++i)
101  {
102  if( tbl[i].m_iVal == iVal )
103  {
104  return tbl[i].m_sName;
105  }
106  }
107  return tbl[0].m_sName;
108 }
const char * m_sName
null-terminated string name
Definition: assoc.h:153
#define CHKEXPR_ULONG(val, expr,...)
check unsigned long integer
Definition: log.h:697