|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
310c69 |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
310c69 |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
310c69 |
* of the License, or (at your option) any later version.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
310c69 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
310c69 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
310c69 |
* GNU General Public License for more details.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
310c69 |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
310c69 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
310c69 |
* 02110-1301, USA.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/base/intMap.h#1 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#ifndef INT_MAP_H
|
|
Packit Service |
310c69 |
#define INT_MAP_H
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "common.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* IntMap associates pointers (void * ) with integer keys
|
|
Packit Service |
310c69 |
* (uint64_t ). NULL pointer values are not
|
|
Packit Service |
310c69 |
* supported.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* The map is implemented as hash table, which should provide constant-time
|
|
Packit Service |
310c69 |
* insert, query, and remove operations, although the insert may occasionally
|
|
Packit Service |
310c69 |
* grow the table, which is linear in the number of entries in the map. The
|
|
Packit Service |
310c69 |
* table will grow as needed to hold new entries, but will not shrink as
|
|
Packit Service |
310c69 |
* entries are removed.
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
typedef struct intMap IntMap;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Allocate and initialize an IntMap.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] initialCapacity the number of entries the map should
|
|
Packit Service |
310c69 |
* initially be capable of holding (zero tells
|
|
Packit Service |
310c69 |
* the map to use its own small default)
|
|
Packit Service |
310c69 |
* @param [in] initialLoad the load factor of the map, expressed as an
|
|
Packit Service |
310c69 |
* integer percentage (typically in the range
|
|
Packit Service |
310c69 |
* 50 to 90, with zero telling the map to use
|
|
Packit Service |
310c69 |
* its own default)
|
|
Packit Service |
310c69 |
* @param [out] mapPtr a pointer to hold the new IntMap
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return UDS_SUCCESS or an error code
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
int makeIntMap(size_t initialCapacity,
|
|
Packit Service |
310c69 |
unsigned int initialLoad,
|
|
Packit Service |
310c69 |
IntMap **mapPtr)
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Free an IntMap and null out the reference to it. NOTE: The map does not own
|
|
Packit Service |
310c69 |
* the pointer values stored in the map and they are not freed by this call.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in,out] mapPtr the reference to the IntMap to free
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void freeIntMap(IntMap **mapPtr);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Get the number of entries stored in an IntMap.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param map the IntMap to query
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the number of entries in the map
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
size_t intMapSize(const IntMap *map);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Retrieve the value associated with a given key from the IntMap.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param map the IntMap to query
|
|
Packit Service |
310c69 |
* @param key the key to look up
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the value associated with the given key, or NULL
|
|
Packit Service |
310c69 |
* if the key is not mapped to any value
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void *intMapGet(IntMap *map, uint64_t key);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Try to associate a value (a pointer) with an integer in an IntMap. If the
|
|
Packit Service |
310c69 |
* map already contains a mapping for the provided key, the old value is
|
|
Packit Service |
310c69 |
* only replaced with the specified value if update is true. In either case
|
|
Packit Service |
310c69 |
* the old value is returned. If the map does not already contain a value for
|
|
Packit Service |
310c69 |
* the specified key, the new value is added regardless of the value of update.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param [in] map the IntMap to attempt to modify
|
|
Packit Service |
310c69 |
* @param [in] key the key with which to associate the new value
|
|
Packit Service |
310c69 |
* @param [in] newValue the value to be associated with the key
|
|
Packit Service |
310c69 |
* @param [in] update whether to overwrite an existing value
|
|
Packit Service |
310c69 |
* @param [out] oldValuePtr a pointer in which to store either the old value
|
|
Packit Service |
310c69 |
* (if the key was already mapped) or
|
|
Packit Service |
310c69 |
* NULL if the map did not contain the
|
|
Packit Service |
310c69 |
* key; NULL may be provided if the
|
|
Packit Service |
310c69 |
* caller does not need to know the old value
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return UDS_SUCCESS or an error code
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
int intMapPut(IntMap *map,
|
|
Packit Service |
310c69 |
uint64_t key,
|
|
Packit Service |
310c69 |
void *newValue,
|
|
Packit Service |
310c69 |
bool update,
|
|
Packit Service |
310c69 |
void **oldValuePtr)
|
|
Packit Service |
310c69 |
__attribute__((warn_unused_result));
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Remove the mapping for a given key from the IntMap.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @param map the IntMap from which to remove the mapping
|
|
Packit Service |
310c69 |
* @param key the key whose mapping is to be removed
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* @return the value that was associated with the key, or
|
|
Packit Service |
310c69 |
* NULL if it was not mapped
|
|
Packit Service |
310c69 |
**/
|
|
Packit Service |
310c69 |
void *intMapRemove(IntMap *map, uint64_t key);
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#endif /* INT_MAP_H */
|