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

Example of a librnr simple associative map operations. More...

#include <stdio.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "rnr/assoc.h"

Go to the source code of this file.

Classes

struct  Factoids_T
 Some solar body facts. More...
 

Macros

#define AMAP_NAME   Planet
 associate map namespace
 
#define AMAP_XTYPE   BodyId_T
 x data type
 
#define AMAP_YTYPE   Factoids_T*
 make the magic More...
 

Enumerations

enum  BodyId_T {
  unknown,
  Sun,
  Mercury,
  Venus,
  Earth,
  Moon,
  Mars,
  Phobos,
  Deimos,
  Ceres,
  Jupiter,
  Io,
  Europa,
  Ganymede,
  Callisto,
  Saturn,
  Titan,
  Uranus,
  Titania,
  Neptune,
  Triton,
  Pluto,
  Charon,
  Eris,
  NumBodies,
  unknown,
  Sun,
  Mercury,
  Venus,
  Earth,
  Moon,
  Mars,
  Phobos,
  Deimos,
  Ceres,
  Jupiter,
  Io,
  Europa,
  Ganymede,
  Callisto,
  Saturn,
  Titan,
  Uranus,
  Titania,
  Neptune,
  Triton,
  Pluto,
  Charon,
  Eris,
  NumBodies
}
 Major and minor solar body ids (partial, of course).
 

Functions

static void AddNewBody (BodyId_T eThisBodyId, double diam, char *units_diam, double dist, char *units_dist, double period, char *units_period, BodyId_T eOrbitsId)
 Add a new body to our db, normalizing units. More...
 
void PrintBody (Factoids_T *pOid)
 Print solar body facts. More...
 
int CmpIds (const BodyId_T x1, const BodyId_T x2)
 X comparator callback. More...
 
int CmpOids (const Factoids_T *y1, const Factoids_T *y2)
 Y comparator callback. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

Nvp_T SolarBodyNames []
 Solar body name-id pairs (partial). More...
 
AssocMapPlanetPoint_T SolarBodyFacts [NumBodies]
 the solar body factoids
 
size_t BodyCount = 0
 number of bodies in facts table above.
 

Detailed Description

Example of a librnr simple associative map operations.

LastChangedDate
2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010)
Rev
307
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file example_assoc.c.

Macro Definition Documentation

#define AMAP_YTYPE   Factoids_T*

make the magic

y data type

Definition at line 92 of file example_assoc.c.

Function Documentation

static void AddNewBody ( BodyId_T  eThisBodyId,
double  diam,
char *  units_diam,
double  dist,
char *  units_dist,
double  period,
char *  units_period,
BodyId_T  eOrbitsId 
)
static

Add a new body to our db, normalizing units.

Parameters
eThisBodyIdSolar body id.
diamBody diameter.
units_diamDiameter units.
distAverage orbital distance from the Sun.
units_distDistance units.
periodAverage orbital period about parent body.
units_periodPeriod units.
eOrbitsIdParent body.

Definition at line 140 of file example_assoc.c.

References arraysize, BodyCount, Factoids_T::m_eOrbits, Factoids_T::m_fDiam, Factoids_T::m_fOrbitalAvgDist, Factoids_T::m_fOribtalPeriod, Factoids_T::m_sName, NEW, NULL, NvpVal2Name(), and SolarBodyFacts.

Referenced by main().

145 {
146  Factoids_T *pOid = NEW(Factoids_T);
147  const char *sName;
148 
149  sName = NvpVal2Name(SolarBodyNames,
151  (int)eThisBodyId);
152 
153  if( sName == NULL )
154  {
155  fprintf(stderr, "Error: %d: unknown body type", eThisBodyId);
156  return;
157  }
158 
159  // normalize diameter to km units
160  if( !strcmp(units_diam, "miles") )
161  {
162  diam *= 1.609344;
163  }
164 
165  // normalize distance to km units
166  if( !strcmp(units_dist, "miles") )
167  {
168  dist *= 1.609344;
169  }
170 
171  // normalize period to days
172  if( !strcmp(units_period, "years") )
173  {
174  period *= 365.25;
175  }
176 
177  // initialize factoid
178  pOid->m_sName = sName;
179  pOid->m_fDiam = diam;
180  pOid->m_fOrbitalAvgDist = dist;
181  pOid->m_fOribtalPeriod = period;
182  pOid->m_eOrbits = eOrbitsId;
183 
184  // insert into table
185  SolarBodyFacts[BodyCount].x = eThisBodyId;
186  SolarBodyFacts[BodyCount].y = pOid;
187 
188  BodyCount++;
189 }
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
Nvp_T SolarBodyNames[]
Solar body name-id pairs (partial).
Definition: example_assoc.c:98
double m_fDiam
diameter of body (km)
Definition: example_assoc.c:81
BodyId_T m_eOrbits
body this body orbits around
Definition: example_assoc.c:84
#define NULL
null pointer
Definition: rnrconfig.h:199
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
#define NEW(T)
Allocate new type.
Definition: new.h:49
size_t BodyCount
number of bodies in facts table above.
const char * m_sName
name of body
Definition: example_assoc.c:80
AssocMapPlanetPoint_T SolarBodyFacts[NumBodies]
the solar body factoids
double m_fOrbitalAvgDist
average orbital distance (km)
Definition: example_assoc.c:82
Some solar body facts.
Definition: example_assoc.c:78
double m_fOribtalPeriod
orbital revolution period (days)
Definition: example_assoc.c:83
int CmpIds ( const BodyId_T  x1,
const BodyId_T  x2 
)

X comparator callback.

Parameters
x1Solar body id x1.
x2Solar body id x2.
Returns
< 0, 0, or > 0 if body x1 is less than, equal to, or greater than x2, respectively.

Definition at line 216 of file example_assoc.c.

Referenced by main().

217 {
218  return (int)(x1 - x2);
219 }
int CmpOids ( const Factoids_T y1,
const Factoids_T y2 
)

Y comparator callback.

Parameters
y1Solar body factoids y1.
y2Solar body factoids y2.
Returns
< 0, 0, or > 0 if y1 is less than, equal to, or greater than y2, respectively.

Definition at line 230 of file example_assoc.c.

References Factoids_T::m_sName.

Referenced by main().

231 {
232  return strcmp(y1->m_sName, y2->m_sName);
233 }
const char * m_sName
name of body
Definition: example_assoc.c:80
int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 244 of file example_assoc.c.

References AddNewBody(), BodyCount, CmpIds(), CmpOids(), Factoids_T::m_sName, NULL, PrintBody(), and SolarBodyFacts.

Referenced by shm_mutex_unlock().

245 {
246  AssocMapPlanetMapper_T map; // associative x<-->y mapper
247  AssocMapPlanetPoint_T dft = {unknown, NULL}; // default "body"
248  Factoids_T *pOid; // working factoid
249  Factoids_T findoid; // search factoid
250 
251  // add some factioids
252  AddNewBody(Mercury, 4878.0, "km", 46.0E6, "km", 0.24, "years", Sun);
253  AddNewBody(Moon, 3476.0, "km", 384400.0, "km", 27.322, "days", Earth);
254  AddNewBody(Deimos, 8.0, "km", 23460.0, "km", 1.263, "days", Mars);
255  AddNewBody(Phobos, 28.0, "km", 9270.0, "km", 0.319, "days", Mars);
256  AddNewBody(Callisto, 4800.0, "km", 1883000.0, "km", 16.689, "days", Jupiter);
257  AddNewBody(Europa, 3126.0, "km", 670900.0, "km", 3.551, "days", Jupiter);
258  AddNewBody(Jupiter, 88736.0, "miles", 817.0E6, "km", 12.0, "years", Sun);
259  AddNewBody(Earth, 7926.0, "miles", 91.0E6, "miles", 1.0, "years", Sun);
260 
261  // initialize mapper
262  map.m_tblAssocMap = SolarBodyFacts;
263  map.m_tblSize = BodyCount;
264  map.m_opXCmp = CmpIds;
265  map.m_opYCmp = CmpOids;
266  map.m_pMapDft = &dft;
267 
268  //
269  // print some data
270  //
271 
272  if( (pOid = AssocMapPlanetXtoY(&map, Moon)) != NULL )
273  {
274  PrintBody(pOid);
275  printf("\n");
276  }
277  if( (pOid = AssocMapPlanetXtoY(&map, Mercury)) != NULL )
278  {
279  PrintBody(pOid);
280  printf("\n");
281  }
282  if( (pOid = AssocMapPlanetXtoY(&map, Earth)) != NULL )
283  {
284  PrintBody(pOid);
285  printf("\n");
286  }
287  if( (pOid = AssocMapPlanetXtoY(&map, Callisto)) != NULL )
288  {
289  PrintBody(pOid);
290  printf("\n");
291  }
292  if( (pOid = AssocMapPlanetXtoY(&map, Eris)) != NULL ) // negative
293  {
294  PrintBody(pOid);
295  printf("\n");
296  }
297 
298  // what was ceres' id? negative
299  findoid.m_sName = "Ceres"; // note: only searches on name in this example
300  printf("ceres id: %u\n", AssocMapPlanetYtoX(&map, &findoid));
301 
302  // an Jupiter's id?
303  findoid.m_sName = "Jupiter";
304  printf("jupiter id: %u\n", AssocMapPlanetYtoX(&map, &findoid));
305 
306  return 0;
307 }
#define NULL
null pointer
Definition: rnrconfig.h:199
int CmpIds(const BodyId_T x1, const BodyId_T x2)
X comparator callback.
size_t BodyCount
number of bodies in facts table above.
void PrintBody(Factoids_T *pOid)
Print solar body facts.
int CmpOids(const Factoids_T *y1, const Factoids_T *y2)
Y comparator callback.
const char * m_sName
name of body
Definition: example_assoc.c:80
AssocMapPlanetPoint_T SolarBodyFacts[NumBodies]
the solar body factoids
Some solar body facts.
Definition: example_assoc.c:78
static void AddNewBody(BodyId_T eThisBodyId, double diam, char *units_diam, double dist, char *units_dist, double period, char *units_period, BodyId_T eOrbitsId)
Add a new body to our db, normalizing units.
void PrintBody ( Factoids_T pOid)

Print solar body facts.

Parameters
pOidPointer to facts.

Definition at line 196 of file example_assoc.c.

References arraysize, Factoids_T::m_eOrbits, Factoids_T::m_fDiam, Factoids_T::m_fOrbitalAvgDist, Factoids_T::m_fOribtalPeriod, Factoids_T::m_sName, and NvpVal2Name().

Referenced by main().

197 {
198  printf("name: %s\n", pOid->m_sName);
199  printf("diameter: %e km\n", pOid->m_fDiam);
200  printf("distance: %e km\n", pOid->m_fOrbitalAvgDist);
201  printf("period: %f days\n", pOid->m_fOribtalPeriod);
202  printf("orbits: %s\n", NvpVal2Name(SolarBodyNames,
204  (int)pOid->m_eOrbits));
205 }
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
Nvp_T SolarBodyNames[]
Solar body name-id pairs (partial).
Definition: example_assoc.c:98
double m_fDiam
diameter of body (km)
Definition: example_assoc.c:81
BodyId_T m_eOrbits
body this body orbits around
Definition: example_assoc.c:84
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
const char * m_sName
name of body
Definition: example_assoc.c:80
double m_fOrbitalAvgDist
average orbital distance (km)
Definition: example_assoc.c:82
double m_fOribtalPeriod
orbital revolution period (days)
Definition: example_assoc.c:83

Variable Documentation

Nvp_T SolarBodyNames[]
Initial value:
=
{
{"unknown", unknown},
{"Sun", Sun},
{"Mercury", Mercury},
{"Venus", Venus},
{"Earth", Earth},
{"Moon", Moon},
{"Mars", Mars},
{"Phobos", Phobos},
{"Deimos", Deimos},
{"Jupiter", Jupiter},
{"Io", Io},
{"Europa", Europa},
{"Ganymede", Ganymede},
{"Callisto", Callisto},
{"Saturn", Saturn},
{"Uranus", Uranus},
{"Neptune", Neptune},
{"Ceres", Ceres},
{"Pluto", Pluto}
}

Solar body name-id pairs (partial).

Definition at line 98 of file example_assoc.c.