hashmap: remove now-unused offmap and strmap

This commit is contained in:
Edward Thomson
2024-10-01 01:36:49 +01:00
parent 8c55bbeb9e
commit 8d81bb57d6
8 changed files with 0 additions and 658 deletions

View File

@@ -10,7 +10,6 @@
#include "common.h"
#include "attr_file.h"
#include "strmap.h"
#define GIT_ATTR_CONFIG "core.attributesfile"
#define GIT_IGNORE_CONFIG "core.excludesfile"

View File

@@ -1,101 +0,0 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "offmap.h"
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"
__KHASH_TYPE(off, off64_t, void *)
__KHASH_IMPL(off, static kh_inline, off64_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal)
int git_offmap_new(git_offmap **out)
{
*out = kh_init(off);
GIT_ERROR_CHECK_ALLOC(*out);
return 0;
}
void git_offmap_free(git_offmap *map)
{
kh_destroy(off, map);
}
void git_offmap_clear(git_offmap *map)
{
kh_clear(off, map);
}
size_t git_offmap_size(git_offmap *map)
{
return kh_size(map);
}
void *git_offmap_get(git_offmap *map, const off64_t key)
{
size_t idx = kh_get(off, map, key);
if (idx == kh_end(map) || !kh_exist(map, idx))
return NULL;
return kh_val(map, idx);
}
int git_offmap_set(git_offmap *map, const off64_t key, void *value)
{
size_t idx;
int rval;
idx = kh_put(off, map, key, &rval);
if (rval < 0)
return -1;
if (rval == 0)
kh_key(map, idx) = key;
kh_val(map, idx) = value;
return 0;
}
int git_offmap_delete(git_offmap *map, const off64_t key)
{
khiter_t idx = kh_get(off, map, key);
if (idx == kh_end(map))
return GIT_ENOTFOUND;
kh_del(off, map, idx);
return 0;
}
int git_offmap_exists(git_offmap *map, const off64_t key)
{
return kh_get(off, map, key) != kh_end(map);
}
int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key)
{
size_t i = *iter;
while (i < map->n_buckets && !kh_exist(map, i))
i++;
if (i >= map->n_buckets)
return GIT_ITEROVER;
if (key)
*key = kh_key(map, i);
if (value)
*value = kh_value(map, i);
*iter = ++i;
return 0;
}

View File

@@ -1,133 +0,0 @@
/*
* Copyright (C) 2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_offmap_h__
#define INCLUDE_offmap_h__
#include "common.h"
#include "git2/types.h"
/** A map with `off64_t`s as key. */
typedef struct kh_off_s git_offmap;
/**
* Allocate a new `off64_t` map.
*
* @param out Pointer to the map that shall be allocated.
* @return 0 on success, an error code if allocation has failed.
*/
int git_offmap_new(git_offmap **out);
/**
* Free memory associated with the map.
*
* Note that this function will _not_ free values added to this
* map.
*
* @param map Pointer to the map that is to be free'd. May be
* `NULL`.
*/
void git_offmap_free(git_offmap *map);
/**
* Clear all entries from the map.
*
* This function will remove all entries from the associated map.
* Memory associated with it will not be released, though.
*
* @param map Pointer to the map that shall be cleared. May be
* `NULL`.
*/
void git_offmap_clear(git_offmap *map);
/**
* Return the number of elements in the map.
*
* @parameter map map containing the elements
* @return number of elements in the map
*/
size_t git_offmap_size(git_offmap *map);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for
* @return value associated with the given key or NULL if the key was not found
*/
void *git_offmap_get(git_offmap *map, const off64_t key);
/**
* Set the entry for key to value.
*
* If the map has no corresponding entry for the given key, a new
* entry will be created with the given value. If an entry exists
* already, its value will be updated to match the given value.
*
* @param map map to create new entry in
* @param key key to set
* @param value value to associate the key with; may be NULL
* @return zero if the key was successfully set, a negative error
* code otherwise
*/
int git_offmap_set(git_offmap *map, const off64_t key, void *value);
/**
* Delete an entry from the map.
*
* Delete the given key and its value from the map. If no such
* key exists, this will do nothing.
*
* @param map map to delete key in
* @param key key to delete
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
* such key was found, a negative code in case of an
* error
*/
int git_offmap_delete(git_offmap *map, const off64_t key);
/**
* Check whether a key exists in the given map.
*
* @param map map to query for the key
* @param key key to search for
* @return 0 if the key has not been found, 1 otherwise
*/
int git_offmap_exists(git_offmap *map, const off64_t key);
/**
* Iterate over entries of the map.
*
* This functions allows to iterate over all key-value entries of
* the map. The current position is stored in the `iter` variable
* and should be initialized to `0` before the first call to this
* function.
*
* @param map map to iterate over
* @param value pointer to the variable where to store the current
* value. May be NULL.
* @param iter iterator storing the current position. Initialize
* with zero previous to the first call.
* @param key pointer to the variable where to store the current
* key. May be NULL.
* @return `0` if the next entry was correctly retrieved.
* GIT_ITEROVER if no entries are left. A negative error
* code otherwise.
*/
int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key);
#define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
code; \
} }
#define git_offmap_foreach_value(h, vvar, code) { size_t __i = 0; \
while (git_offmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \
} }
#endif

View File

@@ -12,7 +12,6 @@
#include "git2/oid.h"
#include "git2/refs.h"
#include "git2/refdb.h"
#include "strmap.h"
#include "str.h"
#include "oid.h"

View File

@@ -34,7 +34,6 @@
#include "submodule.h"
#include "worktree.h"
#include "path.h"
#include "strmap.h"
#ifdef GIT_WIN32
# include "win32/w32_util.h"

View File

@@ -1,100 +0,0 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "strmap.h"
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"
__KHASH_TYPE(str, const char *, void *)
__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
int git_strmap_new(git_strmap **out)
{
*out = kh_init(str);
GIT_ERROR_CHECK_ALLOC(*out);
return 0;
}
void git_strmap_free(git_strmap *map)
{
kh_destroy(str, map);
}
void git_strmap_clear(git_strmap *map)
{
kh_clear(str, map);
}
size_t git_strmap_size(git_strmap *map)
{
return kh_size(map);
}
void *git_strmap_get(git_strmap *map, const char *key)
{
size_t idx = kh_get(str, map, key);
if (idx == kh_end(map) || !kh_exist(map, idx))
return NULL;
return kh_val(map, idx);
}
int git_strmap_set(git_strmap *map, const char *key, void *value)
{
size_t idx;
int rval;
idx = kh_put(str, map, key, &rval);
if (rval < 0)
return -1;
if (rval == 0)
kh_key(map, idx) = key;
kh_val(map, idx) = value;
return 0;
}
int git_strmap_delete(git_strmap *map, const char *key)
{
khiter_t idx = kh_get(str, map, key);
if (idx == kh_end(map))
return GIT_ENOTFOUND;
kh_del(str, map, idx);
return 0;
}
int git_strmap_exists(git_strmap *map, const char *key)
{
return kh_get(str, map, key) != kh_end(map);
}
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key)
{
size_t i = *iter;
while (i < map->n_buckets && !kh_exist(map, i))
i++;
if (i >= map->n_buckets)
return GIT_ITEROVER;
if (key)
*key = kh_key(map, i);
if (value)
*value = kh_val(map, i);
*iter = ++i;
return 0;
}

View File

@@ -1,131 +0,0 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_strmap_h__
#define INCLUDE_strmap_h__
#include "git2_util.h"
/** A map with C strings as key. */
typedef struct kh_str_s git_strmap;
/**
* Allocate a new string map.
*
* @param out Pointer to the map that shall be allocated.
* @return 0 on success, an error code if allocation has failed.
*/
int git_strmap_new(git_strmap **out);
/**
* Free memory associated with the map.
*
* Note that this function will _not_ free keys or values added
* to this map.
*
* @param map Pointer to the map that is to be free'd. May be
* `NULL`.
*/
void git_strmap_free(git_strmap *map);
/**
* Clear all entries from the map.
*
* This function will remove all entries from the associated map.
* Memory associated with it will not be released, though.
*
* @param map Pointer to the map that shall be cleared. May be
* `NULL`.
*/
void git_strmap_clear(git_strmap *map);
/**
* Return the number of elements in the map.
*
* @parameter map map containing the elements
* @return number of elements in the map
*/
size_t git_strmap_size(git_strmap *map);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for
* @return value associated with the given key or NULL if the key was not found
*/
void *git_strmap_get(git_strmap *map, const char *key);
/**
* Set the entry for key to value.
*
* If the map has no corresponding entry for the given key, a new
* entry will be created with the given value. If an entry exists
* already, its value will be updated to match the given value.
*
* @param map map to create new entry in
* @param key key to set
* @param value value to associate the key with; may be NULL
* @return zero if the key was successfully set, a negative error
* code otherwise
*/
int git_strmap_set(git_strmap *map, const char *key, void *value);
/**
* Delete an entry from the map.
*
* Delete the given key and its value from the map. If no such
* key exists, this will do nothing.
*
* @param map map to delete key in
* @param key key to delete
* @return `0` if the key has been deleted, GIT_ENOTFOUND if no
* such key was found, a negative code in case of an
* error
*/
int git_strmap_delete(git_strmap *map, const char *key);
/**
* Check whether a key exists in the given map.
*
* @param map map to query for the key
* @param key key to search for
* @return 0 if the key has not been found, 1 otherwise
*/
int git_strmap_exists(git_strmap *map, const char *key);
/**
* Iterate over entries of the map.
*
* This functions allows to iterate over all key-value entries of
* the map. The current position is stored in the `iter` variable
* and should be initialized to `0` before the first call to this
* function.
*
* @param map map to iterate over
* @param value pointer to the variable where to store the current
* value. May be NULL.
* @param iter iterator storing the current position. Initialize
* with zero previous to the first call.
* @param key pointer to the variable where to store the current
* key. May be NULL.
* @return `0` if the next entry was correctly retrieved.
* GIT_ITEROVER if no entries are left. A negative error
* code otherwise.
*/
int git_strmap_iterate(void **value, git_strmap *map, size_t *iter, const char **key);
#define git_strmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
while (git_strmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
code; \
} }
#define git_strmap_foreach_value(h, vvar, code) { size_t __i = 0; \
while (git_strmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \
} }
#endif

View File

@@ -1,190 +0,0 @@
#include "clar_libgit2.h"
#include "strmap.h"
static git_strmap *g_table;
void test_strmap__initialize(void)
{
cl_git_pass(git_strmap_new(&g_table));
cl_assert(g_table != NULL);
}
void test_strmap__cleanup(void)
{
git_strmap_free(g_table);
}
void test_strmap__0(void)
{
cl_assert(git_strmap_size(g_table) == 0);
}
static void insert_strings(git_strmap *table, size_t count)
{
size_t i, j, over;
char *str;
for (i = 0; i < count; ++i) {
str = malloc(10);
for (j = 0; j < 10; ++j)
str[j] = 'a' + (i % 26);
str[9] = '\0';
/* if > 26, then encode larger value in first letters */
for (j = 0, over = i / 26; over > 0; j++, over = over / 26)
str[j] = 'A' + (over % 26);
cl_git_pass(git_strmap_set(table, str, str));
}
cl_assert_equal_i(git_strmap_size(table), count);
}
void test_strmap__inserted_strings_can_be_retrieved(void)
{
char *str;
int i;
insert_strings(g_table, 20);
cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
cl_assert(git_strmap_exists(g_table, "ggggggggg"));
cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert(i == 20);
}
void test_strmap__deleted_entry_cannot_be_retrieved(void)
{
char *str;
int i;
insert_strings(g_table, 20);
cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
str = git_strmap_get(g_table, "bbbbbbbbb");
cl_assert_equal_s(str, "bbbbbbbbb");
cl_git_pass(git_strmap_delete(g_table, "bbbbbbbbb"));
free(str);
cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert_equal_i(i, 19);
}
void test_strmap__inserting_many_keys_succeeds(void)
{
char *str;
int i;
insert_strings(g_table, 10000);
i = 0;
git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert_equal_i(i, 10000);
}
void test_strmap__get_succeeds_with_existing_entries(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++)
cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
cl_assert_equal_s(git_strmap_get(g_table, "gobble"), "elbbog");
}
void test_strmap__get_returns_null_on_nonexisting_key(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++)
cl_git_pass(git_strmap_set(g_table, keys[i], values[i]));
cl_assert_equal_p(git_strmap_get(g_table, "other"), NULL);
}
void test_strmap__set_persists_key(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
}
void test_strmap__set_persists_multpile_keys(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_git_pass(git_strmap_set(g_table, "bar", "rab"));
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
}
void test_strmap__set_updates_existing_key(void)
{
cl_git_pass(git_strmap_set(g_table, "foo", "oof"));
cl_git_pass(git_strmap_set(g_table, "bar", "rab"));
cl_git_pass(git_strmap_set(g_table, "gobble", "elbbog"));
cl_assert_equal_i(git_strmap_size(g_table), 3);
cl_git_pass(git_strmap_set(g_table, "foo", "other"));
cl_assert_equal_i(git_strmap_size(g_table), 3);
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "other");
}
void test_strmap__iteration(void)
{
struct {
char *key;
char *value;
int seen;
} entries[] = {
{ "foo", "oof" },
{ "bar", "rab" },
{ "gobble", "elbbog" },
};
const char *key, *value;
size_t i, n;
for (i = 0; i < ARRAY_SIZE(entries); i++)
cl_git_pass(git_strmap_set(g_table, entries[i].key, entries[i].value));
i = 0, n = 0;
while (git_strmap_iterate((void **) &value, g_table, &i, &key) == 0) {
size_t j;
for (j = 0; j < ARRAY_SIZE(entries); j++) {
if (strcmp(entries[j].key, key))
continue;
cl_assert_equal_i(entries[j].seen, 0);
cl_assert_equal_s(entries[j].value, value);
entries[j].seen++;
break;
}
n++;
}
for (i = 0; i < ARRAY_SIZE(entries); i++)
cl_assert_equal_i(entries[i].seen, 1);
cl_assert_equal_i(n, ARRAY_SIZE(entries));
}
void test_strmap__iterating_empty_map_stops_immediately(void)
{
size_t i = 0;
cl_git_fail_with(git_strmap_iterate(NULL, g_table, &i, NULL), GIT_ITEROVER);
}