diff -r 6f63f5ed3cab -r be77fb2da242 src/ucx/map.h --- a/src/ucx/map.h Thu Dec 19 18:47:23 2019 +0100 +++ b/src/ucx/map.h Thu Dec 19 19:58:41 2019 +0100 @@ -124,7 +124,7 @@ /** Structure for an iterator over a UcxMap. */ struct UcxMapIterator { /** The map to iterate over. */ - UcxMap *map; + UcxMap const *map; /** The current map element. */ UcxMapElement *cur; @@ -211,7 +211,7 @@ * @param data additional data for the copy function * @return 0 on success or a non-zero value on memory allocation errors */ -int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data); +int ucx_map_copy(UcxMap const *from, UcxMap *to, copy_func fnc, void *data); /** * Clones the map and rehashes if necessary. @@ -227,7 +227,25 @@ * @return the cloned map * @see ucx_map_copy() */ -UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); +UcxMap *ucx_map_clone(UcxMap const *map, copy_func fnc, void *data); + +/** + * Clones the map and rehashes if necessary. + * + * Note: In contrast to ucx_map_rehash() the load factor is irrelevant. + * This function always ensures a new UcxMap.size of at least + * 2.5*UcxMap.count. + * + * @param allocator the allocator to use for the cloned map + * @param map the map to clone + * @param fnc the copy function to use or NULL if the new and + * the old map shall share the data pointers + * @param data additional data for the copy function + * @return the cloned map + * @see ucx_map_copy() + */ +UcxMap *ucx_map_clone_a(UcxAllocator *allocator, + UcxMap const *map, copy_func fnc, void *data); /** * Increases size of the hash map, if necessary. @@ -264,7 +282,7 @@ * @param key the key * @return the value */ -void* ucx_map_get(UcxMap *map, UcxKey key); +void* ucx_map_get(UcxMap const *map, UcxKey key); /** * Removes a key/value-pair from the map by using the key. @@ -406,7 +424,7 @@ * first element list * @see ucx_map_iter_next() */ -UcxMapIterator ucx_map_iterator(UcxMap *map); +UcxMapIterator ucx_map_iterator(UcxMap const *map); /** * Proceeds to the next element of the map (if any). @@ -426,6 +444,102 @@ */ int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value); +/** + * Returns the union of two maps. + * + * The union is a fresh map which is filled by two successive calls of + * ucx_map_copy() on the two input maps. + * + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new map containing the union + */ +UcxMap* ucx_map_union(const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + +/** + * Returns the union of two maps. + * + * The union is a fresh map which is filled by two successive calls of + * ucx_map_copy() on the two input maps. + * + * @param allocator the allocator that shall be used by the new map + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new map containing the union + */ +UcxMap* ucx_map_union_a(UcxAllocator *allocator, + const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + +/** + * Returns the intersection of two maps. + * + * The intersection is defined as a copy of the first map with every element + * removed that has no valid key in the second map. + * + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new map containing the intersection + */ +UcxMap* ucx_map_intersection(const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + +/** + * Returns the intersection of two maps. + * + * The intersection is defined as a copy of the first map with every element + * removed that has no valid key in the second map. + * + * @param allocator the allocator that shall be used by the new map + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new map containing the intersection + */ +UcxMap* ucx_map_intersection_a(UcxAllocator *allocator, + const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + +/** + * Returns the difference of two maps. + * + * The difference contains a copy of all elements of the first map + * for which the corresponding keys cannot be found in the second map. + * + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new list containing the difference + */ +UcxMap* ucx_map_difference(const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + +/** + * Returns the difference of two maps. + * + * The difference contains a copy of all elements of the first map + * for which the corresponding keys cannot be found in the second map. + * + * @param allocator the allocator that shall be used by the new map + * @param first the first source map + * @param second the second source map + * @param cpfnc a function to copy the elements + * @param cpdata additional data for the copy function + * @return a new list containing the difference + */ +UcxMap* ucx_map_difference_a(UcxAllocator *allocator, + const UcxMap *first, const UcxMap *second, + copy_func cpfnc, void* cpdata); + #ifdef __cplusplus }