src/cx/map.h

Wed, 25 Dec 2024 19:33:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Dec 2024 19:33:04 +0100
changeset 1054
fb1076ead56f
parent 994
3603bdf4a78b
permissions
-rw-r--r--

add implementation for creating new json values

relates to #527

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * \file map.h
 * \brief Interface for map implementations.
 * \author Mike Becker
 * \author Olaf Wintermann
 * \copyright 2-Clause BSD License
 */

#ifndef UCX_MAP_H
#define UCX_MAP_H

#include "common.h"
#include "collection.h"
#include "string.h"
#include "hash_key.h"

#ifdef    __cplusplus
extern "C" {
#endif

/** Type for the UCX map. */
typedef struct cx_map_s CxMap;

/** Type for a map entry. */
typedef struct cx_map_entry_s CxMapEntry;

/** Type for map class definitions. */
typedef struct cx_map_class_s cx_map_class;

/** Structure for the UCX map. */
struct cx_map_s {
    /**
     * Base attributes.
     */
    CX_COLLECTION_BASE;
    /** The map class definition. */
    cx_map_class *cl;
};

/**
 * The type of iterator for a map.
 */
enum cx_map_iterator_type {
    /**
     * Iterates over key/value pairs.
     */
    CX_MAP_ITERATOR_PAIRS,
    /**
     * Iterates over keys only.
     */
    CX_MAP_ITERATOR_KEYS,
    /**
     * Iterates over values only.
     */
    CX_MAP_ITERATOR_VALUES
};

/**
 * The class definition for arbitrary maps.
 */
struct cx_map_class_s {
    /**
     * Deallocates the entire memory.
     */
    cx_attr_nonnull
    void (*deallocate)(struct cx_map_s *map);

    /**
     * Removes all elements.
     */
    cx_attr_nonnull
    void (*clear)(struct cx_map_s *map);

    /**
     * Add or overwrite an element.
     */
    cx_attr_nonnull
    int (*put)(
            CxMap *map,
            CxHashKey key,
            void *value
    );

    /**
     * Returns an element.
     */
    cx_attr_nonnull
    cx_attr_nodiscard
    void *(*get)(
            const CxMap *map,
            CxHashKey key
    );

    /**
     * Removes an element.
     *
     * Implementations SHALL check if \p targetbuf is set and copy the elements
     * to the buffer without invoking any destructor.
     * When \p targetbuf is not set, the destructors SHALL be invoked.
     *
     * The function SHALL return zero when the \p key was found and
     * non-zero, otherwise. 
     */
    cx_attr_nonnull_arg(1)
    cx_attr_access_w(3)
    int (*remove)(
            CxMap *map,
            CxHashKey key,
            void *targetbuf
    );

    /**
     * Creates an iterator for this map.
     */
    cx_attr_nonnull
    cx_attr_nodiscard
    CxIterator (*iterator)(const CxMap *map, enum cx_map_iterator_type type);
};

/**
 * A map entry.
 */
struct cx_map_entry_s {
    /**
     * A pointer to the key.
     */
    const CxHashKey *key;
    /**
     * A pointer to the value.
     */
    void *value;
};

/**
 * A shared instance of an empty map.
 *
 * Writing to that map is not allowed.
 */
extern CxMap *const cxEmptyMap;

/**
 * Advises the map to store copies of the objects (default mode of operation).
 *
 * Retrieving objects from this map will yield pointers to the copies stored
 * within this list.
 *
 * @param map the map
 * @see cxMapStorePointers()
 */
cx_attr_nonnull
static inline void cxMapStoreObjects(CxMap *map) {
    map->collection.store_pointer = false;
}

/**
 * Advises the map to only store pointers to the objects.
 *
 * Retrieving objects from this list will yield the original pointers stored.
 *
 * @note This function forcibly sets the element size to the size of a pointer.
 * Invoking this function on a non-empty map that already stores copies of
 * objects is undefined.
 *
 * @param map the map
 * @see cxMapStoreObjects()
 */
cx_attr_nonnull
static inline void cxMapStorePointers(CxMap *map) {
    map->collection.store_pointer = true;
    map->collection.elem_size = sizeof(void *);
}

/**
 * Returns true, if this map is storing pointers instead of the actual data.
 *
 * @param map
 * @return true, if this map is storing pointers
 * @see cxMapStorePointers()
 */
cx_attr_nonnull
static inline bool cxMapIsStoringPointers(const CxMap *map) {
    return map->collection.store_pointer;
}

/**
 * Deallocates the memory of the specified map.
 *
 * @param map the map to be freed
 */
static inline void cxMapFree(CxMap *map) {
    if (map == NULL) return;
    map->cl->deallocate(map);
}


/**
 * Clears a map by removing all elements.
 *
 * @param map the map to be cleared
 */
cx_attr_nonnull
static inline void cxMapClear(CxMap *map) {
    map->cl->clear(map);
}

/**
 * Returns the number of elements in this map.
 *
 * @param map the map
 * @return the number of stored elements
 */
cx_attr_nonnull
static inline size_t cxMapSize(const CxMap *map) {
    return map->collection.size;
}


// TODO: set-like map operations (union, intersect, difference)

/**
 * Creates a value iterator for a map.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored values
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline CxIterator cxMapIteratorValues(const CxMap *map) {
    return map->cl->iterator(map, CX_MAP_ITERATOR_VALUES);
}

/**
 * Creates a key iterator for a map.
 *
 * The elements of the iterator are keys of type CxHashKey.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored keys
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline CxIterator cxMapIteratorKeys(const CxMap *map) {
    return map->cl->iterator(map, CX_MAP_ITERATOR_KEYS);
}

/**
 * Creates an iterator for a map.
 *
 * The elements of the iterator are key/value pairs of type CxMapEntry.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored entries
 * @see cxMapIteratorKeys()
 * @see cxMapIteratorValues()
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline CxIterator cxMapIterator(const CxMap *map) {
    return map->cl->iterator(map, CX_MAP_ITERATOR_PAIRS);
}


/**
 * Creates a mutating iterator over the values of a map.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored values
 */
cx_attr_nonnull
cx_attr_nodiscard
CxIterator cxMapMutIteratorValues(CxMap *map);

/**
 * Creates a mutating iterator over the keys of a map.
 *
 * The elements of the iterator are keys of type CxHashKey.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored keys
 */
cx_attr_nonnull
cx_attr_nodiscard
CxIterator cxMapMutIteratorKeys(CxMap *map);

/**
 * Creates a mutating iterator for a map.
 *
 * The elements of the iterator are key/value pairs of type CxMapEntry.
 *
 * \note An iterator iterates over all elements successively. Therefore, the order
 * highly depends on the map implementation and may change arbitrarily when the contents change.
 *
 * @param map the map to create the iterator for
 * @return an iterator for the currently stored entries
 * @see cxMapMutIteratorKeys()
 * @see cxMapMutIteratorValues()
 */
cx_attr_nonnull
cx_attr_nodiscard
CxIterator cxMapMutIterator(CxMap *map);

#ifdef __cplusplus
} // end the extern "C" block here, because we want to start overloading

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cxMapPut(
        CxMap *map,
        CxHashKey const &key,
        void *value
) {
    return map->cl->put(map, key, value);
}


/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cxMapPut(
        CxMap *map,
        cxstring const &key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_cxstr(key), value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cxMapPut(
        CxMap *map,
        cxmutstr const &key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_cxstr(key), value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
cx_attr_cstr_arg(2)
static inline int cxMapPut(
        CxMap *map,
        const char *key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_str(key), value);
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cxMapGet(
        const CxMap *map,
        CxHashKey const &key
) {
    return map->cl->get(map, key);
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cxMapGet(
        const CxMap *map,
        cxstring const &key
) {
    return map->cl->get(map, cx_hash_key_cxstr(key));
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cxMapGet(
        const CxMap *map,
        cxmutstr const &key
) {
    return map->cl->get(map, cx_hash_key_cxstr(key));
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
cx_attr_cstr_arg(2)
static inline void *cxMapGet(
        const CxMap *map,
        const char *key
) {
    return map->cl->get(map, cx_hash_key_str(key));
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapRemoveAndGet()
 */
cx_attr_nonnull
static inline int cxMapRemove(
        CxMap *map,
        CxHashKey const &key
) {
    return map->cl->remove(map, key, nullptr);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapRemoveAndGet()
 */
cx_attr_nonnull
static inline int cxMapRemove(
        CxMap *map,
        cxstring const &key
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapRemoveAndGet()
 */
cx_attr_nonnull
static inline int cxMapRemove(
        CxMap *map,
        cxmutstr const &key
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), nullptr);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapRemoveAndGet()
 */
cx_attr_nonnull
cx_attr_cstr_arg(2)
static inline int cxMapRemove(
        CxMap *map,
        const char *key
) {
    return map->cl->remove(map, cx_hash_key_str(key), nullptr);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapStorePointers()
 * @see cxMapRemove()
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cxMapRemoveAndGet(
        CxMap *map,
        CxHashKey key,
        void *targetbuf
) {
    return map->cl->remove(map, key, targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapStorePointers()
 * @see cxMapRemove()
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cxMapRemoveAndGet(
        CxMap *map,
        cxstring key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapStorePointers()
 * @see cxMapRemove()
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cxMapRemoveAndGet(
        CxMap *map,
        cxmutstr key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapStorePointers()
 * @see cxMapRemove()
 */
cx_attr_nonnull
cx_attr_access_w(3)
cx_attr_cstr_arg(2)
static inline int cxMapRemoveAndGet(
        CxMap *map,
        const char *key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
}

#else // __cplusplus

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cx_map_put(
        CxMap *map,
        CxHashKey key,
        void *value
) {
    return map->cl->put(map, key, value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cx_map_put_cxstr(
        CxMap *map,
        cxstring key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_cxstr(key), value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
static inline int cx_map_put_mustr(
        CxMap *map,
        cxmutstr key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_cxstr(key), value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
cx_attr_nonnull
cx_attr_cstr_arg(2)
static inline int cx_map_put_str(
        CxMap *map,
        const char *key,
        void *value
) {
    return map->cl->put(map, cx_hash_key_str(key), value);
}

/**
 * Puts a key/value-pair into the map.
 *
 * @param map the map
 * @param key the key
 * @param value the value
 * @return 0 on success, non-zero value on failure
 */
#define cxMapPut(map, key, value) _Generic((key), \
    CxHashKey: cx_map_put,                        \
    cxstring: cx_map_put_cxstr,                   \
    cxmutstr: cx_map_put_mustr,                   \
    char*: cx_map_put_str,                        \
    const char*: cx_map_put_str)                  \
    (map, key, value)

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cx_map_get(
        const CxMap *map,
        CxHashKey key
) {
    return map->cl->get(map, key);
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cx_map_get_cxstr(
        const CxMap *map,
        cxstring key
) {
    return map->cl->get(map, cx_hash_key_cxstr(key));
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
static inline void *cx_map_get_mustr(
        const CxMap *map,
        cxmutstr key
) {
    return map->cl->get(map, cx_hash_key_cxstr(key));
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
cx_attr_nonnull
cx_attr_nodiscard
cx_attr_cstr_arg(2)
static inline void *cx_map_get_str(
        const CxMap *map,
        const char *key
) {
    return map->cl->get(map, cx_hash_key_str(key));
}

/**
 * Retrieves a value by using a key.
 *
 * @param map the map
 * @param key the key
 * @return the value
 */
#define cxMapGet(map, key) _Generic((key), \
    CxHashKey: cx_map_get,                 \
    cxstring: cx_map_get_cxstr,            \
    cxmutstr: cx_map_get_mustr,            \
    char*: cx_map_get_str,                 \
    const char*: cx_map_get_str)           \
    (map, key)

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
static inline int cx_map_remove(
        CxMap *map,
        CxHashKey key
) {
    return map->cl->remove(map, key, NULL);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
static inline int cx_map_remove_cxstr(
        CxMap *map,
        cxstring key
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
static inline int cx_map_remove_mustr(
        CxMap *map,
        cxmutstr key
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), NULL);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
cx_attr_cstr_arg(2)
static inline int cx_map_remove_str(
        CxMap *map,
        const char *key
) {
    return map->cl->remove(map, cx_hash_key_str(key), NULL);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * Always invokes the destructors functions, if any, on the removed element.
 *
 * @param map the map
 * @param key the key
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapRemoveAndGet()
 */
#define cxMapRemove(map, key) _Generic((key), \
    CxHashKey: cx_map_remove,                 \
    cxstring: cx_map_remove_cxstr,            \
    cxmutstr: cx_map_remove_mustr,            \
    char*: cx_map_remove_str,                 \
    const char*: cx_map_remove_str)           \
    (map, key)

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cx_map_remove_and_get(
        CxMap *map,
        CxHashKey key,
        void *targetbuf
) {
    return map->cl->remove(map, key, targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cx_map_remove_and_get_cxstr(
        CxMap *map,
        cxstring key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
cx_attr_access_w(3)
static inline int cx_map_remove_and_get_mustr(
        CxMap *map,
        cxmutstr key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_cxstr(key), targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 */
cx_attr_nonnull
cx_attr_access_w(3)
cx_attr_cstr_arg(2)
static inline int cx_map_remove_and_get_str(
        CxMap *map,
        const char *key,
        void *targetbuf
) {
    return map->cl->remove(map, cx_hash_key_str(key), targetbuf);
}

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * This function will copy the contents to the target buffer
 * which must be guaranteed to be large enough to hold the element.
 * The destructor functions, if any, will \em not be called.
 *
 * If this map is storing pointers, the element is the pointer itself
 * and not the object it points to.
 *
 * @param map the map
 * @param key the key
 * @param targetbuf the buffer where the element shall be copied to
 * @return zero on success, non-zero if the key was not found
 * 
 * @see cxMapStorePointers()
 * @see cxMapRemove()
 */
#define cxMapRemoveAndGet(map, key, targetbuf) _Generic((key), \
    CxHashKey: cx_map_remove_and_get,               \
    cxstring: cx_map_remove_and_get_cxstr,          \
    cxmutstr: cx_map_remove_and_get_mustr,          \
    char*: cx_map_remove_and_get_str,               \
    const char*: cx_map_remove_and_get_str)         \
    (map, key, targetbuf)

#endif // __cplusplus

#endif // UCX_MAP_H

mercurial