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

Simple associvative map operator definitions. More...

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/dliststr.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/assoc.h"

Go to the source code of this file.

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 associvative map operator definitions.

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

Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
assoc.c
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.c.

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