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

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

#include <memory.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "mapofint.h"
Include dependency graph for mapofint.c:

Go to the source code of this file.

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

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

Definition in file mapofint.c.

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().