librnr  1.14.5
RoadNarrows Robotics Common Library 1
assoc.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*!
3  * \file
4  *
5  * \brief Simple associvative map operator definitions.
6  *
7  * These associative maps are composed of tables (vectors) of discrete (x, y)
8  * points where the x and y may be of any data types.
9  *
10  * \pkgsynopsis
11  * RoadNarrows Robotics Common Library 1
12  *
13  * \pkgcomponent{Library}
14  * librnr
15  *
16  * \pkgfile{assoc.c}
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
21  *
22  * \license{MIT}
23  *
24  * \EulaBegin
25  * See the README and EULA files for any copyright and licensing information.
26  * \EulaEnd
27  */
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 #include <sys/types.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "rnr/rnrconfig.h"
35 #include "rnr/dliststr.h"
36 #include "rnr/log.h"
37 #include "rnr/new.h"
38 #include "rnr/assoc.h"
39 
40 
41 // ---------------------------------------------------------------------------
42 // Private Interface
43 // ---------------------------------------------------------------------------
44 
45 
46 // ---------------------------------------------------------------------------
47 // Public Interface
48 // ---------------------------------------------------------------------------
49 
50 /*!
51  * \brief Get the value associated with the name.
52  *
53  * The table search terminates at the first match or at the end of
54  * nTblEntries entries.
55  *
56  * \param tbl[] Name-Value Pair table
57  * \param nTblEntries Number of name-value pair table entries
58  * \param sName Name to in table to find.
59  *
60  * \return
61  * The associated value on success, or the first (default) value in the
62  * table if the no name match is found.
63  */
64 int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
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 }
79 
80 /*!
81  * \brief Get the name associated with the value.
82  *
83  * The table search terminates at the first match or at the end of
84  * nTblEntries entries.
85  *
86  * \param tbl[] Name-Value Pair table
87  * \param nTblEntries Number of name-value pair table entries
88  * \param iVal Value in table to find.
89  *
90  * \return
91  * The associated name on success, or the first (default) value in the
92  * table if the no value match is found.
93  */
94 const char *NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
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 }
109 
110 /*!
111  * \brief Get the y value associated with the given x value.
112  *
113  * The table search terminates at the first match or at the end of
114  * the table.
115  *
116  * \param pMapper Associative map mapper container.
117  * \param px Pointer to X value in table to find.
118  *
119  * \return
120  * The associated y value on success, or the default y value if no
121  * x value is found.
122  */
123 void *AssocMapVoidXtoY(AssocMapVoidMapper_T *pMapper, void *px)
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 }
145 
146 /*!
147  * \brief Get the x value associated with the given y value.
148  *
149  * The table search terminates at the first match or at the end of
150  * the table.
151  *
152  * \param pMapper Associative map mapper container.
153  * \param py Pointer to Y value in table to find.
154  *
155  * \return
156  * The associated x value on success, or the default x value if no
157  * y value is found.
158  */
159 void *AssocMapVoidYtoX(AssocMapVoidMapper_T *pMapper, void *py)
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 }
Simple associative map data and operator declarations.
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
AssocMapVoidPoint_T * m_pMapDft
default x, y point
Definition: assoc.h:191
Doubly linked list of character strings "inherited" from dlistvoid.
#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
const char * m_sName
null-terminated string name
Definition: assoc.h:153
Memory allocation and deallocation declarations.
AssocMapVoidPoint_T * m_tblAssocMap
associative map table
Definition: assoc.h:187
Definition: assoc.h:151
void * AssocMapVoidXtoY(AssocMapVoidMapper_T *pMapper, void *px)
Get the y value associated with the given x value.
Definition: assoc.c:123
int m_iVal
associated integer value
Definition: assoc.h:154
RoadNarrows Robotics common configuration file.
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
void * AssocMapVoidYtoX(AssocMapVoidMapper_T *pMapper, void *py)
Get the x value associated with the given y value.
Definition: assoc.c:159
AssocMapVoidCmp_T m_opXCmp
return 0 if x1 == x2
Definition: assoc.h:189
Logger declarations.
#define CHKEXPR_ULONG(val, expr,...)
check unsigned long integer
Definition: log.h:697
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.
Definition: assoc.c:64