add empty map implementation - fixes #259

Sun, 21 May 2023 14:37:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 14:37:56 +0200
changeset 706
8c6edaccaef1
parent 705
0d5447230044
child 707
87eb4bdb2d0e

add empty map implementation - fixes #259

src/CMakeLists.txt file | annotate | diff | comparison | revisions
src/cx/map.h file | annotate | diff | comparison | revisions
src/map.c file | annotate | diff | comparison | revisions
tests/test_map.cpp file | annotate | diff | comparison | revisions
     1.1 --- a/src/CMakeLists.txt	Sun May 21 14:04:34 2023 +0200
     1.2 +++ b/src/CMakeLists.txt	Sun May 21 14:37:56 2023 +0200
     1.3 @@ -7,6 +7,7 @@
     1.4          linked_list.c
     1.5          tree.c
     1.6          buffer.c
     1.7 +        map.c
     1.8          hash_key.c
     1.9          hash_map.c
    1.10          basic_mempool.c
     2.1 --- a/src/cx/map.h	Sun May 21 14:04:34 2023 +0200
     2.2 +++ b/src/cx/map.h	Sun May 21 14:37:56 2023 +0200
     2.3 @@ -158,6 +158,8 @@
     2.4      void *value;
     2.5  };
     2.6  
     2.7 +extern CxMap *const cxEmptyMap;
     2.8 +
     2.9  /**
    2.10   * Advises the map to store copies of the objects (default mode of operation).
    2.11   *
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/map.c	Sun May 21 14:37:56 2023 +0200
     3.3 @@ -0,0 +1,90 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +
    3.32 +#include "cx/map.h"
    3.33 +
    3.34 +// <editor-fold desc="empty map implementation">
    3.35 +
    3.36 +static void cx_empty_map_noop(__attribute__((__unused__)) CxMap *map) {
    3.37 +    // this is a noop, but MUST be implemented
    3.38 +}
    3.39 +
    3.40 +static void *cx_empty_map_get(
    3.41 +        __attribute__((__unused__)) CxMap const *map,
    3.42 +        __attribute__((__unused__)) CxHashKey key
    3.43 +) {
    3.44 +    return NULL;
    3.45 +}
    3.46 +
    3.47 +static bool cx_empty_map_iter_valid(__attribute__((__unused__)) void const *iter) {
    3.48 +    return false;
    3.49 +}
    3.50 +
    3.51 +static CxIterator cx_empty_map_iterator(struct cx_map_s const *map) {
    3.52 +    CxIterator iter = {0};
    3.53 +    iter.src_handle = map;
    3.54 +    iter.base.valid = cx_empty_map_iter_valid;
    3.55 +    return iter;
    3.56 +}
    3.57 +
    3.58 +static CxMutIterator cx_empty_map_miterator(struct cx_map_s *map) {
    3.59 +    CxMutIterator iter = {0};
    3.60 +    iter.src_handle = map;
    3.61 +    iter.base.valid = cx_empty_map_iter_valid;
    3.62 +    return iter;
    3.63 +}
    3.64 +
    3.65 +static struct cx_map_class_s cx_empty_map_class = {
    3.66 +    cx_empty_map_noop,
    3.67 +    cx_empty_map_noop,
    3.68 +    NULL,
    3.69 +    cx_empty_map_get,
    3.70 +    NULL,
    3.71 +    cx_empty_map_iterator,
    3.72 +    cx_empty_map_iterator,
    3.73 +    cx_empty_map_iterator,
    3.74 +    cx_empty_map_miterator,
    3.75 +    cx_empty_map_miterator,
    3.76 +    cx_empty_map_miterator,
    3.77 +};
    3.78 +
    3.79 +CxMap cx_empty_map = {
    3.80 +        NULL,
    3.81 +        NULL,
    3.82 +        0,
    3.83 +        0,
    3.84 +        NULL,
    3.85 +        NULL,
    3.86 +        NULL,
    3.87 +        false,
    3.88 +        &cx_empty_map_class
    3.89 +};
    3.90 +
    3.91 +CxMap * const cxEmptyMap = &cx_empty_map;
    3.92 +
    3.93 +// </editor-fold>
     4.1 --- a/tests/test_map.cpp	Sun May 21 14:04:34 2023 +0200
     4.2 +++ b/tests/test_map.cpp	Sun May 21 14:37:56 2023 +0200
     4.3 @@ -472,3 +472,51 @@
     4.4      cxMapDestroy(map);
     4.5      EXPECT_TRUE(allocator.verify());
     4.6  }
     4.7 +
     4.8 +TEST(EmptyMap, Size) {
     4.9 +    auto map = cxEmptyMap;
    4.10 +
    4.11 +    EXPECT_EQ(map->size, 0);
    4.12 +}
    4.13 +
    4.14 +TEST(EmptyMap, Iterator) {
    4.15 +    auto map = cxEmptyMap;
    4.16 +
    4.17 +    auto it1 = cxMapIterator(map);
    4.18 +    auto it2 = cxMapIteratorValues(map);
    4.19 +    auto it3 = cxMapIteratorKeys(map);
    4.20 +    auto it4 = cxMapMutIterator(map);
    4.21 +    auto it5 = cxMapMutIteratorValues(map);
    4.22 +    auto it6 = cxMapMutIteratorKeys(map);
    4.23 +
    4.24 +    EXPECT_FALSE(cxIteratorValid(it1));
    4.25 +    EXPECT_FALSE(cxIteratorValid(it2));
    4.26 +    EXPECT_FALSE(cxIteratorValid(it3));
    4.27 +    EXPECT_FALSE(cxIteratorValid(it4));
    4.28 +    EXPECT_FALSE(cxIteratorValid(it5));
    4.29 +    EXPECT_FALSE(cxIteratorValid(it6));
    4.30 +
    4.31 +    int c = 0;
    4.32 +    cx_foreach(void*, data, it1) c++;
    4.33 +    cx_foreach(void*, data, it2) c++;
    4.34 +    cx_foreach(void*, data, it3) c++;
    4.35 +    cx_foreach(void*, data, it4) c++;
    4.36 +    cx_foreach(void*, data, it5) c++;
    4.37 +    cx_foreach(void*, data, it6) c++;
    4.38 +    EXPECT_EQ(c, 0);
    4.39 +}
    4.40 +
    4.41 +TEST(EmptyMap, NoOps) {
    4.42 +    auto map = cxEmptyMap;
    4.43 +
    4.44 +    ASSERT_NO_FATAL_FAILURE(cxMapClear(map));
    4.45 +    ASSERT_NO_FATAL_FAILURE(cxMapRehash(map));
    4.46 +    ASSERT_NO_FATAL_FAILURE(cxMapDestroy(map));
    4.47 +}
    4.48 +
    4.49 +TEST(EmptyMap, Get) {
    4.50 +    auto map = cxEmptyMap;
    4.51 +
    4.52 +    CxHashKey key = cx_hash_key_str("test");
    4.53 +    EXPECT_EQ(cxMapGet(map, key), nullptr);
    4.54 +}

mercurial