Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fc567a9b authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "libcutils: remove some unused API."

parents c457df7d 721e3ebf
Loading
Loading
Loading
Loading
+11 −95
Original line number Original line Diff line number Diff line
@@ -93,10 +93,6 @@ static inline int hashKey(Hashmap* map, void* key) {
    return h;
    return h;
}
}


size_t hashmapSize(Hashmap* map) {
    return map->size;
}

static inline size_t calculateIndex(size_t bucketCount, int hash) {
static inline size_t calculateIndex(size_t bucketCount, int hash) {
    return ((size_t) hash) & (bucketCount - 1);
    return ((size_t) hash) & (bucketCount - 1);
}
}
@@ -240,54 +236,6 @@ void* hashmapGet(Hashmap* map, void* key) {
    return NULL;
    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 = &current->next;
    }
}

void* hashmapRemove(Hashmap* map, void* key) {
void* hashmapRemove(Hashmap* map, void* key) {
    int hash = hashKey(map, key);
    int hash = hashKey(map, key);
    size_t index = calculateIndex(map->bucketCount, hash);
    size_t index = calculateIndex(map->bucketCount, hash);
@@ -310,8 +258,7 @@ void* hashmapRemove(Hashmap* map, void* key) {
    return NULL;
    return NULL;
}
}


void hashmapForEach(Hashmap* map, 
void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context),
        bool (*callback)(void* key, void* value, void* context),
                    void* context) {
                    void* context) {
    size_t i;
    size_t i;
    for (i = 0; i < map->bucketCount; i++) {
    for (i = 0; i < map->bucketCount; 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;
}
+6 −52
Original line number Original line Diff line number Diff line
@@ -16,6 +16,9 @@


/**
/**
 * Hash map.
 * Hash map.
 *
 * Use std::map or std::unordered_map instead.
 * https://en.cppreference.com/w/cpp/container
 */
 */


#ifndef __HASHMAP_H
#ifndef __HASHMAP_H
@@ -67,38 +70,17 @@ void* hashmapPut(Hashmap* map, void* key, void* value);
 */
 */
void* hashmapGet(Hashmap* map, void* key);
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
 * Removes an entry from the map. Returns the removed value or NULL if no
 * entry was present.
 * entry was present.
 */
 */
void* hashmapRemove(Hashmap* map, void* key);
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
 * Invokes the given callback on each entry in the map. Stops iterating if
 * the callback returns false.
 * the callback returns false.
 */
 */
void hashmapForEach(Hashmap* map, 
void hashmapForEach(Hashmap* map, bool (*callback)(void* key, void* value, void* context),
        bool (*callback)(void* key, void* value, void* context),
                    void* context);
                    void* context);


/**
/**
@@ -115,34 +97,6 @@ void hashmapLock(Hashmap* map);
 */
 */
void hashmapUnlock(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
#ifdef __cplusplus
}
}
#endif
#endif
+2 −6
Original line number Original line Diff line number Diff line
@@ -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_parms_to_str(struct str_parms *str_parms)
{
{
    char *str = NULL;
    char *str = NULL;

    if (hashmapSize(str_parms->map) > 0)
    hashmapForEach(str_parms->map, combine_strings, &str);
    hashmapForEach(str_parms->map, combine_strings, &str);
    else
    return (str != NULL) ? str : strdup("");
        str = strdup("");
    return str;
}
}


static bool dump_entry(void* key, void* value, void* /*context*/) {
static bool dump_entry(void* key, void* value, void* /*context*/) {