src/ucx/cx/map.h

Sat, 18 Mar 2023 12:06:18 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 18 Mar 2023 12:06:18 +0100
changeset 478
4dccc47c282e
parent 438
22eca559aded
child 490
d218607f5a7e
permissions
-rw-r--r--

prevent crash in default_propfind_do triggered by bad webdav config

/*
 * 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
 * \version 3.0
 * \copyright 2-Clause BSD License
 */

#ifndef UCX_MAP_H
#define UCX_MAP_H

#include "common.h"
#include "allocator.h"
#include "iterator.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 {
    /** The map class definition. */
    cx_map_class *cl;
    /** An allocator that is used for the map elements. */
    CxAllocator *allocator;
    /** The number of elements currently stored. */
    size_t size;
    // TODO: elemsize + a flag if values shall be copied to the map
};

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

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

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

    /**
     * Returns an element.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    void *(*get)(
            CxMap const *map,
            CxHashKey key
    );

    /**
     * Removes an element.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    void *(*remove)(
            CxMap *map,
            CxHashKey key
    );

    /**
     * Iterator over the key/value pairs.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxIterator (*iterator)(CxMap const *map);

    /**
     * Iterator over the keys.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxIterator (*iterator_keys)(CxMap const *map);

    /**
     * Iterator over the values.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxIterator (*iterator_values)(CxMap const *map);

    /**
     * Mutating iterator over the key/value pairs.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxMutIterator (*mut_iterator)(CxMap *map);

    /**
     * Mutating iterator over the keys.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxMutIterator (*mut_iterator_keys)(CxMap *map);

    /**
     * Mutating iterator over the values.
     */
    __attribute__((__nonnull__, __warn_unused_result__))
    CxMutIterator (*mut_iterator_values)(CxMap *map);
};

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


/**
 * Deallocates the memory of the specified map.
 *
 * @param map the map to be destroyed
 */
__attribute__((__nonnull__))
static inline void cxMapDestroy(CxMap *map) {
    // TODO: likely to add auto-free feature for contents in the future
    map->cl->destructor(map);
}


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

/**
 * 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
 */
__attribute__((__nonnull__))
static inline int cxMapPut(
        CxMap *map,
        CxHashKey key,
        void *value
) {
    return map->cl->put(map, key, value);
}

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

/**
 * Removes a key/value-pair from the map by using the key.
 *
 * @param map the map
 * @param key the key
 * @return the removed value
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline void *cxMapRemove(
        CxMap *map,
        CxHashKey key
) {
    return map->cl->remove(map, key);
}

// 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
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIteratorValues(CxMap *map) {
    return map->cl->iterator_values(map);
}

/**
 * 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
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIteratorKeys(CxMap *map) {
    return map->cl->iterator_keys(map);
}

/**
 * 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()
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxMapIterator(CxMap *map) {
    return map->cl->iterator(map);
}


/**
 * 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
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxMutIterator cxMapMutIteratorValues(CxMap *map) {
    return map->cl->mut_iterator_values(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
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxMutIterator cxMapMutIteratorKeys(CxMap *map) {
    return map->cl->mut_iterator_keys(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()
 */
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxMutIterator cxMapMutIterator(CxMap *map) {
    return map->cl->mut_iterator(map);
}

#ifdef    __cplusplus
}
#endif

#endif // UCX_MAP_H

mercurial