LibCDS
tree_map.h File Reference

The ordered map to store key value pairs. More...

Go to the source code of this file.

Data Structures

struct  TreeMap
 The implementation for ordered map. More...
 

Typedefs

typedef struct _TreeMapData TreeMapData
 TreeMapData is the data type for the container private information. More...
 

Functions

int32_t TreeMapInit (TreeMap **ppObj)
 The constructor for TreeMap. More...
 
void TreeMapDeinit (TreeMap **ppObj)
 The destructor for TreeMap. More...
 
int32_t TreeMapPut (TreeMap *self, Pair *pPair)
 Insert a key value pair into the map. More...
 
int32_t TreeMapGet (TreeMap *self, Key key, Value *pValue)
 Retrieve the value corresponding to the designated key. More...
 
int32_t TreeMapFind (TreeMap *self, Key key)
 Check if the map contains the designated key. More...
 
int32_t TreeMapRemove (TreeMap *self, Key key)
 Delete the key value pair corresponding to the designated key. More...
 
int32_t TreeMapSize (TreeMap *self)
 Return the number of stored key value pairs. More...
 
int32_t TreeMapMinimum (TreeMap *self, Pair **ppPair)
 Retrieve the key value pair with the minimum order from the map. More...
 
int32_t TreeMapMaximum (TreeMap *self, Pair **ppPair)
 Retrieve the key value pair with the maximum order from the map. More...
 
int32_t TreeMapPredecessor (TreeMap *self, Key key, Pair **ppPair)
 Retrieve the key value pair which is the predecessor of the given key. More...
 
int32_t TreeMapSuccessor (TreeMap *self, Key key, Pair **ppPair)
 Retrieve the key value pair which is the successor of the given key. More...
 
int32_t TreeMapIterate (TreeMap *self, bool bReset, Pair **ppPair)
 Iterate through the map from the minimum order to the maximum order. More...
 
int32_t TreeMapReverseIterate (TreeMap *self, bool bReset, Pair **ppPair)
 Reversely iterate through the map from the minimum order to the maximum order. More...
 
int32_t TreeMapSetCompare (TreeMap *self, int32_t(*pFunc)(Key, Key))
 Set the custom key comparison method. More...
 
int32_t TreeMapSetDestroy (TreeMap *self, void(*pFunc)(Pair *))
 Set the custom key value pair resource clean method. More...
 

Detailed Description

The ordered map to store key value pairs.

Definition in file tree_map.h.

Typedef Documentation

typedef struct _TreeMapData TreeMapData

TreeMapData is the data type for the container private information.

Definition at line 11 of file tree_map.h.

Function Documentation

int32_t TreeMapInit ( TreeMap **  ppObj)

The constructor for TreeMap.

Parameters
ppObjThe double pointer to the to be constructed map
Return values
SUCC
ERR_NOMEMInsufficient memory for map construction
void TreeMapDeinit ( TreeMap **  ppObj)

The destructor for TreeMap.

If the custom resource clean method is set, it also runs the clean method for each pair.

Parameters
ppObjThe double pointer to the to be destructed map
int32_t TreeMapPut ( TreeMap self,
Pair *  pPair 
)

Insert a key value pair into the map.

This function inserts a key value pair into the map. If the order of the designated pair is the same with a certain one stored in the map, that pair will be replaced. Also, if the custom resource clean method is set, it runs the clean method for the replaced pair.

Parameters
selfThe pointer to TreeMap structure
pPairThe pointer to the designated pair
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for map extension
int32_t TreeMapGet ( TreeMap self,
Key  key,
Value *  pValue 
)

Retrieve the value corresponding to the designated key.

This function retrieves the value corresponding to the designated key from the map. If the key can be found, the value will be returned by the third parameter. Otherwise, the error code in returned and the third parameter is updated with NULL.

Parameters
selfThe pointer to TreeMap structure
keyThe designated key
pValueThe pointer to the returned value
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANo map entry can be found
ERR_GETInvalid parameter to store returned value
int32_t TreeMapFind ( TreeMap self,
Key  key 
)

Check if the map contains the designated key.

Parameters
selfThe pointer to TreeMap structure
keyThe designated key
Return values
SUCCThe key can be found
NOKEYThe key cannot be found
ERR_NOINITUninitialized container
int32_t TreeMapRemove ( TreeMap self,
Key  key 
)

Delete the key value pair corresponding to the designated key.

This function deletes the key value pair corresponding to the designated key. If the custom resource clean method is set, it runs the clean methods for the deleted pair.

Parameters
selfThe pointer to TreeMap structure
keyThe designated key
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANo map entry can be found
int32_t TreeMapSize ( TreeMap self)

Return the number of stored key value pairs.

Parameters
selfThe pointer to TreeMap structure
Returns
The number of stored pairs
Return values
ERR_NOINITUninitialized container
int32_t TreeMapMinimum ( TreeMap self,
Pair **  ppPair 
)

Retrieve the key value pair with the minimum order from the map.

Parameters
selfThe pointer to TreeMap structure
ppPairThe double pointer to the returned pair
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXEmpty map
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapMaximum ( TreeMap self,
Pair **  ppPair 
)

Retrieve the key value pair with the maximum order from the map.

Parameters
selfThe pointer to TreeMap structure
ppPairThe double pointer to the returned pair
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXEmpty map
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapPredecessor ( TreeMap self,
Key  key,
Pair **  ppPair 
)

Retrieve the key value pair which is the predecessor of the given key.

Parameters
selfThe pointer to TreeMap structure
keyThe designated key
ppPairThe double pointer to the returned pair
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANon-existent immediate predecessor
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapSuccessor ( TreeMap self,
Key  key,
Pair **  ppPair 
)

Retrieve the key value pair which is the successor of the given key.

Parameters
selfThe pointer to TreeMap structure
keyThe designated key
ppPairThe double pointer to the returned pair
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANon-existent immediate successor
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapIterate ( TreeMap self,
bool  bReset,
Pair **  ppPair 
)

Iterate through the map from the minimum order to the maximum order.

Before iterating through the map, it is necessary to pass:

  • bReset = true
  • pPair = NULL for iterator initialization.

After initialization, you can pass:

  • bReset = false
  • pPair = the pointer to get the returned pair at each iteration.
Parameters
selfThe pointer to TreeMap structure
bResetThe knob to restart the iteration
ppPairThe double pointer to the returned pair
Return values
SUCCIterator initialized successfully
CONTINUEIteration in progress
ENDIteration terminiated
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapReverseIterate ( TreeMap self,
bool  bReset,
Pair **  ppPair 
)

Reversely iterate through the map from the minimum order to the maximum order.

Before iterating through the map, it is necessary to pass:

  • bReset = true
  • pPair = NULL for iterator initialization.

After initialization, you can pass:

  • bReset = false
  • pPair = the pointer to get the returned pair at each iteration.
Parameters
selfThe pointer to TreeMap structure
bResetThe knob to restart the iteration
ppPairThe double pointer to the returned pair
Return values
SUCCIterator initialized successfully
CONTINUEIteration in progress
ENDIteration terminiated
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned pair
int32_t TreeMapSetCompare ( TreeMap self,
int32_t(*)(Key, Key)  pFunc 
)

Set the custom key comparison method.

Parameters
selfThe pointer to TreeMap structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container
int32_t TreeMapSetDestroy ( TreeMap self,
void(*)(Pair *)  pFunc 
)

Set the custom key value pair resource clean method.

Parameters
selfThe pointer to TreeMap structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container