Blame source/vdo/base/intMap.h

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