HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/basic/hashmap.c

69,795 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 89517b99f724167eeb42f77e7647a8d9c851797f

Lines 2001–2264 of 2,264

        if (res_keys) {
                r = cachemem_maintain(&cache->keys, size);
                if (r < 0)
                        return r;
 
                sync_keys = r;
        } else
                cache->keys.active = false;
 
        if (res_values) {
                r = cachemem_maintain(&cache->values, size);
                if (r < 0)
                        return r;
 
                sync_values = r;
        } else
                cache->values.active = false;
 
        if (cache->hashmap->dirty) {
                if (cache->keys.active)
                        sync_keys = true;
                if (cache->values.active)
                        sync_values = true;
 
                cache->hashmap->dirty = false;
        }
 
        if (sync_keys || sync_values) {
                unsigned i, idx;
                Iterator iter;
 
                i = 0;
                HASHMAP_FOREACH_IDX(idx, cache->hashmap, iter) {
                        struct hashmap_base_entry *e;
 
                        e = bucket_at(cache->hashmap, idx);
 
                        if (sync_keys)
                                cache->keys.ptr[i] = e->key;
                        if (sync_values)
                                cache->values.ptr[i] = entry_value(cache->hashmap, e);
                        i++;
                }
        }
 
        if (res_keys)
                *res_keys = cache->keys.ptr;
        if (res_values)
                *res_values = cache->values.ptr;
        if (res_n_entries)
                *res_n_entries = size;
 
        return 0;
}
 
IteratedCache* iterated_cache_free(IteratedCache *cache) {
        if (cache) {
                free(cache->keys.ptr);
                free(cache->values.ptr);
        }
 
        return mfree(cache);
}
 
int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret) {
        _cleanup_free_ char *str = NULL;
        size_t separator_len, len = 0;
        const char *value;
        bool first;
 
        assert(ret);
 
        if (set_isempty(s)) {
                *ret = NULL;
                return 0;
        }
 
        separator_len = strlen_ptr(separator);
 
        if (separator_len == 0)
                wrap_with_separator = false;
 
        first = !wrap_with_separator;
 
        SET_FOREACH(value, s) {
                size_t l = strlen_ptr(value);
 
                if (l == 0)
                        continue;
 
                if (!GREEDY_REALLOC(str, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1))
                        return -ENOMEM;
 
                if (separator_len > 0 && !first) {
                        memcpy(str + len, separator, separator_len);
                        len += separator_len;
                }
 
                memcpy(str + len, value, l);
                len += l;
                first = false;
        }
 
        if (wrap_with_separator) {
                memcpy(str + len, separator, separator_len);
                len += separator_len;
        }
 
        str[len] = '\0';
 
        *ret = TAKE_PTR(str);
        return 0;
}
 
bool set_equal(Set *a, Set *b) {
        void *p;
 
        /* Checks whether each entry of 'a' is also in 'b' and vice versa, i.e. the two sets contain the same
         * entries */
 
        if (a == b)
                return true;
 
        if (set_isempty(a) && set_isempty(b))
                return true;
 
        if (set_size(a) != set_size(b)) /* Cheap check that hopefully catches a lot of inequality cases
                                         * already */
                return false;
 
        SET_FOREACH(p, a)
                if (!set_contains(b, p))
                        return false;
 
        /* If we have the same hashops, then we don't need to check things backwards given we compared the
         * size and that all of a is in b. */
        if (a->b.hash_ops == b->b.hash_ops)
                return true;
 
        SET_FOREACH(p, b)
                if (!set_contains(a, p))
                        return false;
 
        return true;
}
 
static bool set_fnmatch_one(Set *patterns, const char *needle) {
        const char *p;
 
        assert(needle);
 
        /* Any failure of fnmatch() is treated as equivalent to FNM_NOMATCH, i.e. as non-matching pattern */
 
        SET_FOREACH(p, patterns)
                if (fnmatch(p, needle, 0) == 0)
                        return true;
 
        return false;
}
 
bool set_fnmatch(Set *include_patterns, Set *exclude_patterns, const char *needle) {
        assert(needle);
 
        if (set_fnmatch_one(exclude_patterns, needle))
                return false;
 
        if (set_isempty(include_patterns))
                return true;
 
        return set_fnmatch_one(include_patterns, needle);
}
 
static int hashmap_entry_compare(
                struct hashmap_base_entry * const *a,
                struct hashmap_base_entry * const *b,
                compare_func_t compare) {
 
        assert(a && *a);
        assert(b && *b);
        assert(compare);
 
        return compare((*a)->key, (*b)->key);
}
 
static int _hashmap_dump_entries_sorted(
                HashmapBase *h,
                void ***ret,
                size_t *ret_n) {
        _cleanup_free_ void **entries = NULL;
        Iterator iter;
        unsigned idx;
        size_t n = 0;
 
        assert(ret);
        assert(ret_n);
 
        if (_hashmap_size(h) == 0) {
                *ret = NULL;
                *ret_n = 0;
                return 0;
        }
 
        /* We append one more element than needed so that the resulting array can be used as a strv. We
         * don't count this entry in the returned size. */
        entries = new(void*, _hashmap_size(h) + 1);
        if (!entries)
                return -ENOMEM;
 
        HASHMAP_FOREACH_IDX(idx, h, iter)
                entries[n++] = bucket_at(h, idx);
 
        assert(n == _hashmap_size(h));
        entries[n] = NULL;
 
        typesafe_qsort_r((struct hashmap_base_entry**) entries, n,
                         hashmap_entry_compare, h->hash_ops->compare);
 
        *ret = TAKE_PTR(entries);
        *ret_n = n;
        return 0;
}
 
int _hashmap_dump_keys_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
        _cleanup_free_ void **entries = NULL;
        size_t n;
        int r;
 
        assert(ret);
 
        r = _hashmap_dump_entries_sorted(h, &entries, &n);
        if (r < 0)
                return r;
 
        /* Reuse the array. */
        FOREACH_ARRAY(e, entries, n)
                *e = (void*) (*(struct hashmap_base_entry**) e)->key;
 
        *ret = TAKE_PTR(entries);
        if (ret_n)
                *ret_n = n;
        return 0;
}
 
int _hashmap_dump_sorted(HashmapBase *h, void ***ret, size_t *ret_n) {
        _cleanup_free_ void **entries = NULL;
        size_t n;
        int r;
 
        assert(ret);
 
        r = _hashmap_dump_entries_sorted(h, &entries, &n);
        if (r < 0)
                return r;
 
        /* Reuse the array. */
        FOREACH_ARRAY(e, entries, n)
                *e = entry_value(h, *(struct hashmap_base_entry**) e);
 
        *ret = TAKE_PTR(entries);
        if (ret_n)
                *ret_n = n;
        return 0;
}