From 721e3ebf552b6041fa2228b5fdebc442975db1fa Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 11 Jul 2018 14:26:40 -0700 Subject: [PATCH] libcutils: remove some unused API. Bug: N/A Test: builds Change-Id: Iabe42353a708afde2611b5c642775849e2e45baa --- libcutils/hashmap.cpp | 106 +++-------------------------- libcutils/include/cutils/hashmap.h | 58 ++-------------- libcutils/str_parms.cpp | 8 +-- 3 files changed, 19 insertions(+), 153 deletions(-) diff --git a/libcutils/hashmap.cpp b/libcutils/hashmap.cpp index 10e3b2505..2a4a52e1a 100644 --- a/libcutils/hashmap.cpp +++ b/libcutils/hashmap.cpp @@ -36,7 +36,7 @@ struct Hashmap { size_t bucketCount; int (*hash)(void* key); bool (*equals)(void* keyA, void* keyB); - mutex_t lock; + mutex_t lock; size_t size; }; @@ -44,18 +44,18 @@ Hashmap* hashmapCreate(size_t initialCapacity, int (*hash)(void* key), bool (*equals)(void* keyA, void* keyB)) { assert(hash != NULL); assert(equals != NULL); - + Hashmap* map = static_cast(malloc(sizeof(Hashmap))); if (map == NULL) { return NULL; } - + // 0.75 load factor. size_t minimumBucketCount = initialCapacity * 4 / 3; map->bucketCount = 1; while (map->bucketCount <= minimumBucketCount) { // Bucket count must be power of 2. - map->bucketCount <<= 1; + map->bucketCount <<= 1; } map->buckets = static_cast(calloc(map->bucketCount, sizeof(Entry*))); @@ -63,14 +63,14 @@ Hashmap* hashmapCreate(size_t initialCapacity, free(map); return NULL; } - + map->size = 0; map->hash = hash; map->equals = equals; - + mutex_init(&map->lock); - + return map; } @@ -89,12 +89,8 @@ static inline int hashKey(Hashmap* map, void* key) { h ^= (((unsigned int) h) >> 14); h += (h << 4); h ^= (((unsigned int) h) >> 10); - - return h; -} -size_t hashmapSize(Hashmap* map) { - return map->size; + return h; } static inline size_t calculateIndex(size_t bucketCount, int hash) { @@ -111,7 +107,7 @@ static void expandIfNecessary(Hashmap* map) { // Abort expansion. return; } - + // Move over existing entries. size_t i; for (i = 0; i < map->bucketCount; i++) { @@ -240,54 +236,6 @@ void* hashmapGet(Hashmap* map, void* key) { return NULL; } -bool hashmapContainsKey(Hashmap* map, void* key) { - int hash = hashKey(map, key); - size_t index = calculateIndex(map->bucketCount, hash); - - Entry* entry = map->buckets[index]; - while (entry != NULL) { - if (equalKeys(entry->key, entry->hash, key, hash, map->equals)) { - return true; - } - entry = entry->next; - } - - return false; -} - -void* hashmapMemoize(Hashmap* map, void* key, - void* (*initialValue)(void* key, void* context), void* context) { - int hash = hashKey(map, key); - size_t index = calculateIndex(map->bucketCount, hash); - - Entry** p = &(map->buckets[index]); - while (true) { - Entry* current = *p; - - // Add a new entry. - if (current == NULL) { - *p = createEntry(key, hash, NULL); - if (*p == NULL) { - errno = ENOMEM; - return NULL; - } - void* value = initialValue(key, context); - (*p)->value = value; - map->size++; - expandIfNecessary(map); - return value; - } - - // Return existing value. - if (equalKeys(current->key, current->hash, key, hash, map->equals)) { - return current->value; - } - - // Move to next entry. - p = ¤t->next; - } -} - void* hashmapRemove(Hashmap* map, void* key) { int hash = hashKey(map, key); size_t index = calculateIndex(map->bucketCount, hash); @@ -310,9 +258,8 @@ void* hashmapRemove(Hashmap* map, void* key) { return NULL; } -void hashmapForEach(Hashmap* map, - bool (*callback)(void* key, void* value, void* context), - void* context) { +void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context), + void* context) { size_t i; for (i = 0; i < map->bucketCount; i++) { Entry* entry = map->buckets[i]; @@ -325,34 +272,3 @@ void hashmapForEach(Hashmap* map, } } } - -size_t hashmapCurrentCapacity(Hashmap* map) { - size_t bucketCount = map->bucketCount; - return bucketCount * 3 / 4; -} - -size_t hashmapCountCollisions(Hashmap* map) { - size_t collisions = 0; - size_t i; - for (i = 0; i < map->bucketCount; i++) { - Entry* entry = map->buckets[i]; - while (entry != NULL) { - if (entry->next != NULL) { - collisions++; - } - entry = entry->next; - } - } - return collisions; -} - -int hashmapIntHash(void* key) { - // Return the key value itself. - return *((int*) key); -} - -bool hashmapIntEquals(void* keyA, void* keyB) { - int a = *((int*) keyA); - int b = *((int*) keyB); - return a == b; -} diff --git a/libcutils/include/cutils/hashmap.h b/libcutils/include/cutils/hashmap.h index 5cb344c15..9cfd66942 100644 --- a/libcutils/include/cutils/hashmap.h +++ b/libcutils/include/cutils/hashmap.h @@ -16,6 +16,9 @@ /** * Hash map. + * + * Use std::map or std::unordered_map instead. + * https://en.cppreference.com/w/cpp/container */ #ifndef __HASHMAP_H @@ -67,39 +70,18 @@ void* hashmapPut(Hashmap* map, void* key, void* value); */ void* hashmapGet(Hashmap* map, void* key); -/** - * Returns true if the map contains an entry for the given key. - */ -bool hashmapContainsKey(Hashmap* map, void* key); - -/** - * Gets the value for a key. If a value is not found, this function gets a - * value and creates an entry using the given callback. - * - * If memory allocation fails, the callback is not called, this function - * returns NULL, and errno is set to ENOMEM. - */ -void* hashmapMemoize(Hashmap* map, void* key, - void* (*initialValue)(void* key, void* context), void* context); - /** * Removes an entry from the map. Returns the removed value or NULL if no * entry was present. */ void* hashmapRemove(Hashmap* map, void* key); -/** - * Gets the number of entries in this map. - */ -size_t hashmapSize(Hashmap* map); - /** * Invokes the given callback on each entry in the map. Stops iterating if * the callback returns false. */ -void hashmapForEach(Hashmap* map, - bool (*callback)(void* key, void* value, void* context), - void* context); +void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context), + void* context); /** * Concurrency support. @@ -115,36 +97,8 @@ void hashmapLock(Hashmap* map); */ void hashmapUnlock(Hashmap* map); -/** - * Key utilities. - */ - -/** - * Hashes int keys. 'key' is a pointer to int. - */ -int hashmapIntHash(void* key); - -/** - * Compares two int keys for equality. - */ -bool hashmapIntEquals(void* keyA, void* keyB); - -/** - * For debugging. - */ - -/** - * Gets current capacity. - */ -size_t hashmapCurrentCapacity(Hashmap* map); - -/** - * Counts the number of entry collisions. - */ -size_t hashmapCountCollisions(Hashmap* map); - #ifdef __cplusplus } #endif -#endif /* __HASHMAP_H */ +#endif /* __HASHMAP_H */ diff --git a/libcutils/str_parms.cpp b/libcutils/str_parms.cpp index f5a52a767..d818c519b 100644 --- a/libcutils/str_parms.cpp +++ b/libcutils/str_parms.cpp @@ -354,12 +354,8 @@ static bool combine_strings(void *key, void *value, void *context) char *str_parms_to_str(struct str_parms *str_parms) { char *str = NULL; - - if (hashmapSize(str_parms->map) > 0) - hashmapForEach(str_parms->map, combine_strings, &str); - else - str = strdup(""); - return str; + hashmapForEach(str_parms->map, combine_strings, &str); + return (str != NULL) ? str : strdup(""); } static bool dump_entry(void* key, void* value, void* /*context*/) {