map

Overview

Module that defines the macros to generate map types.

Type and Definitions

float const C_MaxMapLoad = 0.85f;

Defines the recommended maxLoad for a map.

cMapInfo

struct cMapInfo
{
   int64_t count;
   int64_t size;
   float maxLoad;
};
typedef struct cMapInfo cMapInfo;

cMapInfo stores the properties of the map. The count value tells how much elements are stored in the map. The size shows how much memory is allocated, the maxLoad says how much percent of the size can be used without to grow.

cMapItr

struct cMapItr
{
   int64_t _v;
};
typedef struct cMapItr cMapItr;

Type to iterate over the entries in a map.

Code Generation Macros

create

MAKE_MAP_C_

#define MAKE_MAP_C_(                                                           \
   FuncSuffix, MapType, RowType, Meta                                          \
)

The generated function has the following common signature:

MapType* make_FuncSuffix( int64_t size, float maxLoad );

NEW_VEC_C_

#define NEW_MAP_C_(                                                            \
   FuncSuffix, VecType                                                         \
)

The generated function has the following common signature:

MapType* new_FuncSuffix();

manage

INFO_OF_Map_C_

#define INFO_OF_MAP_C_(                                                        \
   FuncSuffix, MapType                                                         \
)

Generates a function that returns a reference to the info from the vector. The generated function has the following common signature:

cMapInfo const* info_of_FuncSuffix( MapType* map );

RESIZE_MAP_C_

#define RESIZE_MAP_C_(                                                         \
   FuncSuffix, MapType, RowType, HashFunc, DoRef                               \
)

Generates a function to resize the map size. The generated function has the following common signature:

bool resize_FuncSuffix( MapType* map, int64_t size );

SET_MAX_LOAD_OF_MAP_C_

#define SET_MAX_LOAD_OF_MAP_C_(                                                \
   FuncSuffix, MapType                                                         \
)

Generates a function to change the max load factor of the map. The generated function has the following common signature:

bool set_max_load_of_FuncSuffix( MapType* map, float load );

NEXT_IN_MAP_C_

#define NEXT_IN_MAP_C_(                                                        \
   FuncSuffix, MapType, RowType, KeyType, DoRefKey, ValType, DoRefVal          \
)

Generates a function that gets a pointer to the next key and value in the map after the iterator position. The generated function has the following common signatures:

cMapItr next_in_FuncSuffix( MapType const* map,
                            cMapItr itr,
                            KeyType const* key[static 1],
                            ValType const* val[static 1] );

The function gives only read access to the value. The returned iterator is invalid if no next value exist.

NEXT_VAR_IN_MAP_C_

#define NEXT_VAR_IN_MAP_C_(                                                    \
   FuncSuffix, MapType, RowType, KeyType, DoRefKey, ValType, DoRefVal          \
)

Generates a function that gets a pointer to the next key and value in the map after the iterator position. The generated function has the following common signatures:

cMapItr next_var_in_FuncSuffix( MapType const* map,
                                cMapItr itr,
                                KeyType const* key[static 1],
                                ValType* val[static 1] );

The function gives only read access to the value. The returned iterator is invalid if no next value exist.

intl

INTL_ERASE_IN_MAP_C_

#define INTL_ERASE_IN_MAP_C_(                                                  \
   FuncSuffix, MapType, RowType                                                \
)

Generates a internal function to erase a map entry. The generated function has the folllowing common signature:

bool intl_erase_in_FuncSuffix( MapType* map, int64_t index, RowType* oldRow );

INTL_INDEX_IN_MAP_C_

#define INTL_INDEX_IN_MAP_C_(                                                  \
   FuncSuffix, MapType, RowType, KeyType, CmpFunc, DoRef                       \
)

Generates a internal function that returns the index of a map entry. The generated function has the following common signature:

int64_t intl_index_in_FuncSuffix( MapType* map, uint64_t hash, KeyType key );

INTL_SET_ON_MAP_C_

#define INTL_SET_ON_MAP_C_(                                                    \
   FuncSuffix, MapType, RowType, CmpFunc, DoRef                                \
)

Generates a internal function The generated function has the following common signature:

bool intl_set_on_FuncSuffix( MapType* map,
                             uint64_t hash,
                             RowType* row,
                             RowType* out );

type

MAP_STRUCTS_C_

#define MAP_STRUCTS_C_(                                                        \
   MapType, RowType, KeyType, ValType                                          \
)

Generates the structs that all functions in this module use.

struct RowType                                                                 \
{                                                                              \
   int8_t d;                                                                   \
   KeyType k;                                                                  \
   ValType v;                                                                  \
};                                                                             \
typedef struct RowType RowType;                                                \
                                                                               \
struct MapType                                                                 \
{                                                                              \
   RowType* array;                                                             \
   int64_t size;                                                               \
   int64_t count;                                                              \
   uint8_t shift;                                                              \
   float maxLoadFactor;                                                        \
};                                                                             \
typedef struct MapType MapType;

SHARED_MAP_INTL_DECL_C_

#define SHARED_MAP_INTL_DECL_C_(                                               \
   MapType, RowType, KeyType, FuncSuffix                                       \
)

Generates the declaration for the internal functions.

OBJ_OBJ_MAP_DEF_C_

#define OBJ_OBJ_MAP_DEF_C_(                                                    \
   MapType, KeyType, ValType, FuncSuffix, Meta                                 \
)

Generates the definition of a map to store objects from type KeyType and ValType for a .h file.

struct MapType;
typedef struct MapType MapType;
extern cMeta const Meta;
/**************************************/
MapType* make_FuncSuffix( int64_t size, float maxLoad );
MapType* new_FuncSuffix();
/**************************************/
cMapInfo const* info_of_FuncSuffix( MapType const* map );
bool resize_FuncSuffix( MapType* map, int64_t size );
bool set_max_load_of_FuncSuffix( MapType* map, float maxLoad );
/**************************************/
bool next_in_FuncSuffix( MapType const* map, cMapItr itr[static 1] );
bool next_var_in_FuncSuffix( MapType* map, cVarMapItr itr[static 1] );
/**************************************/
ValType* get_from_FuncSuffix( MapType const* map, KeyType const* key );
bool in_FuncSuffix( MapType const* map, KeyType const* key );
bool remove_from_FuncSuffix( MapType* map, KeyType const* key );
bool set_on_FuncSuffix( MapType* map, KeyType* key, ValType* val );

OBJ_OBJ_MAP_IMPL_C_

#define OBJ_OBJ_MAP_IMPL_C_(                                                   \
   Static, MapType, RowType, KeyType, ValType,                                 \
   FuncSuffix, Meta, HashFunc, CmpFunc                                         \
)

Generates the implementation of a map to store objects from type KeyType and ValType for a .c file. The Static value can be used to define the visibility of the functions.

struct MapType;
typedef struct MapType MapType;
struct RowType;
typedef struct RowType RowType;
extern cMeta const Meta;
/**************************************/
MapType* make_FuncSuffix( int64_t size, float maxLoad );
MapType* new_FuncSuffix();
/**************************************/
cMapInfo const* info_of_FuncSuffix( MapType const* map );
bool resize_FuncSuffix( MapType* map, int64_t size );
bool set_max_load_of_FuncSuffix( MapType* map, float maxLoad );
/**************************************/
bool next_in_FuncSuffix( MapType const* map, cMapItr itr[static 1] );
bool next_var_in_FuncSuffix( MapType* map, cVarMapItr itr[static 1] );
/**************************************/
ValType* get_from_FuncSuffix( MapType const* map, KeyType const* key );
bool in_FuncSuffix( MapType const* map, KeyType const* key );
bool remove_from_FuncSuffix( MapType* map, KeyType const* key );
bool set_on_FuncSuffix( MapType* map, KeyType* key, ValType* val );
/**************************************/
bool intl_erase_in_FuncSuffix( MapType* map, int64_t index, RowType* oldRow );
int64_t intl_index_in_FuncSuffix( MapType* map, uint64_t hash, KeyType key );
bool intl_set_on_FuncSuffix( MapType* map,
                             uint64_t hash,
                             RowType* row,
                             RowType* out );

VAL_VAL_MAP_DEF_C_

#define VAL_VAL_MAP_DEF_C_(                                                    \
   MapType, KeyType, ValType, FuncSuffix, Meta                                 \
)

Generates the definition of a map to store values from type KeyType and ValType for a .h file.

struct MapType;
typedef struct MapType MapType;
extern cMeta const Meta;
/**************************************/
MapType* make_FuncSuffix( int64_t size, float maxLoad );
MapType* new_FuncSuffix();
/**************************************/
cMapInfo const* info_of_FuncSuffix( MapType const* map );
bool resize_FuncSuffix( MapType* map, int64_t size );
bool set_max_load_of_FuncSuffix( MapType* map, float maxLoad );
/**************************************/
bool next_in_FuncSuffix( MapType const* map, cMapItr itr[static 1] );
bool next_var_in_FuncSuffix( MapType* map, cVarMapItr itr[static 1] );
/**************************************/
ValType const* get_from_FuncSuffix( MapType const* map, KeyType key );
ValType* get_var_from_FuncSuffix( MapType* map, KeyType key );
bool in_FuncSuffix( MapType const* map, KeyType key );
bool remove_from_FuncSuffix( MapType* map, KeyType key );
bool set_on_FuncSuffix( MapType* map, KeyType key, ValType val );

VAL_VAL_MAP_IMPL_C_

#define VAL_VAL_MAP_IMPL_C_(                                                   \
   Static, MapType, RowType, KeyType, ValType,                                 \
   FuncSuffix, Meta, HashFunc, CmpFunc, DoRef                                  \
)

Generates the implementation of a map to store values from type KeyType and ValType for a .c file. The Static value can be used to define the visibility of the functions.

struct MapType;
typedef struct MapType MapType;
struct RowType;
typedef struct RowType RowType;
extern cMeta const Meta;
/**************************************/
MapType* make_FuncSuffix( int64_t size, float maxLoad );
MapType* new_FuncSuffix();
/**************************************/
cMapInfo const* info_of_FuncSuffix( MapType const* map );
bool resize_FuncSuffix( MapType* map, int64_t size );
bool set_max_load_of_FuncSuffix( MapType* map, float maxLoad );
/**************************************/
bool next_in_FuncSuffix( MapType const* map, cMapItr itr[static 1] );
bool next_var_in_FuncSuffix( MapType* map, cVarMapItr itr[static 1] );
/**************************************/
ValType const* get_from_FuncSuffix( MapType const* map, KeyType key );
ValType* get_var_from_FuncSuffix( MapType* map, KeyType key );
bool in_FuncSuffix( MapType const* map, KeyType key );
bool remove_from_FuncSuffix( MapType* map, KeyType key );
bool set_on_FuncSuffix( MapType* map, KeyType key, ValType val );
/**************************************/
bool intl_erase_in_FuncSuffix( MapType* map, int64_t index, RowType* oldRow );
int64_t intl_index_in_FuncSuffix( MapType* map, uint64_t hash, KeyType key );
bool intl_set_on_FuncSuffix( MapType* map,
                             uint64_t hash,
                             RowType* row,
                             RowType* out );

Functions

util

calculate_load_c

float calculate_load_c( int64_t cap, int64_t count );

Calculates the load factor.

fibonacci_hash_index_c

int64_t fibonacci_hash_index_c( uint64_t hash, uint8_t shift );

Fibonacci Hash function.

combine_hashes_c

uint64_t combine_hashes_c( cUint64Slice hashes );

Combines single hash values to one hash value.

info

map_cap_c

int64_t map_cap_c( cMapInfo const info[static 1] );

Returns the capacity of a map.

map_load_c

float map_load_c( cMapInfo const info[static 1] );

Returns the load factor of a map.

map_is_empty_c

bool map_is_empty_c( cMapInfo const info[static 1] );

Returns true if a map is empty, otherwise false.

itr

iterate_map_c_

#define iterate_map_c_( Itr, KeyPtr, ValPtr, Map, Func )

Macro function to define a for statement to iterate over all elements in a map.

Example
#include "clingo/lang/expect.h"
#include "clingo/container/CInt64Map.h"

int main( void )
{
   init_tap_c_();

   CInt64Map* map = retain_c( new_int64_map_c() );
   {
      set_on_int64_map_c( map, 1000, 1 );
      set_on_int64_map_c( map,  100, 2 );
      set_on_int64_map_c( map,   10, 3 );
      set_on_int64_map_c( map,    1, 4 );
   }

   int64_t res = 0;
   int64_t const* key;
   int64_t const* val;
   iterate_map_c_( itr, key, val, map, next_in_int64_map_c )
   {
      res += *key * *val;
   }    
   expect_c_( res == 1234 );

   release_c( map );

   return finish_tap_c_();
}

start_map_itr_c

cMapItr start_map_itr_c();

Function that returns a cMapItr value to get the first value in the map.

map_itr_is_valid_c

bool map_itr_is_valid_c( cMapItr itr );

Function to check if the iterator is valid.