Design Pattern Examples
Overview of object-oriented design patterns
mapofint.h File Reference

Declaration of the MapOfInt structure and supporting functions for managing a map of integers keyed by a string. More...

#include "uintarray.h"
Include dependency graph for mapofint.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  MapOfIntEntry
 Represents an association between a string and an integer. More...
 
struct  MapOfInt
 Represents a list of MapOfIntEntry objects that maps a string to an integer value. Call MapOfInt_Initialize() to initialize the MapOfInt structure and call MapOfInt_Clear() to release the memory of the structure. More...
 

Macros

#define __MAPOFINTS_H__
 

Functions

void MapOfInt_Initialize (MapOfInt *map)
 Initialize the given MapOfInt structure so it is ready for use.
 
void MapOfInt_Clear (MapOfInt *map)
 Clear the given MapOfInt object, releasing all memory associated with it. Leaves the object in an initialize state to be reused, if desired.
 
bool MapOfInt_Add (MapOfInt *map, const char *key, int value)
 Add a key/value association to the given MapOfInt object. The MapOfInt object takes ownership of the key and value arguments and will free them when MapOfInt_Clear() is called in the object.
 
int MapOfInt_Find (MapOfInt *map, const char *key)
 Find the specified key in the given MapOfInt object, returning an index into the object.
 

Detailed Description

Declaration of the MapOfInt structure and supporting functions for managing a map of integers keyed by a string.

Definition in file mapofint.h.

Macro Definition Documentation

◆ __MAPOFINTS_H__

#define __MAPOFINTS_H__

Definition at line 8 of file mapofint.h.

Function Documentation

◆ MapOfInt_Add()

bool MapOfInt_Add ( MapOfInt map,
const char *  key,
int  value 
)

Add a key/value association to the given MapOfInt object. The MapOfInt object takes ownership of the key and value arguments and will free them when MapOfInt_Clear() is called in the object.

Parameters
mapThe MapOfInt object to add to.
keyThe key value to be added. This must be allocated, directly or indirectly, with malloc() as it will be freed when the MapOfInt object is cleared.
valueThe integer being mapped to the key.
Returns
Returns true if the key/value was successfully added; otherwise, returns false, indicating an out of memory condition (or NULL argument).

Definition at line 40 of file mapofint.c.

References MapOfInt::entries, MapOfInt::entries_count, MapOfIntEntry::key, and MapOfIntEntry::value.

Referenced by Shop_AddItemToInventory().

◆ MapOfInt_Clear()

void MapOfInt_Clear ( MapOfInt map)

Clear the given MapOfInt object, releasing all memory associated with it. Leaves the object in an initialize state to be reused, if desired.

Parameters
mapThe MapOfInt object to clear.

Definition at line 28 of file mapofint.c.

References MapOfInt::entries, and MapOfInt_Initialize().

Referenced by Shop_Destroy().

◆ MapOfInt_Find()

int MapOfInt_Find ( MapOfInt map,
const char *  key 
)

Find the specified key in the given MapOfInt object, returning an index into the object.

Parameters
mapThe MapOfInt object to search.
keyThe key to search for.
Returns
Returns the index of the found item if successful; otherwise, returns -1, indicating the key was not found.

Definition at line 72 of file mapofint.c.

References MapOfInt::entries, MapOfInt::entries_count, and MapOfIntEntry::key.

Referenced by Shop_AddItemToInventory(), Shop_IsItemInStock(), and Shop_PickupOrder().

◆ MapOfInt_Initialize()

void MapOfInt_Initialize ( MapOfInt map)

Initialize the given MapOfInt structure so it is ready for use.

Alternatively, declare the MapOfInt instance like this:

MapOfInt map = { 0 };
Represents a list of MapOfIntEntry objects that maps a string to an integer value....
Definition: mapofint.h:27
Parameters
mapThe MapOfInt object to initialize.

Definition at line 16 of file mapofint.c.

References MapOfInt::entries, and MapOfInt::entries_count.

Referenced by MapOfInt_Clear(), and Shop_Create().