// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

#include <stdbool.h>
#include <stddef.h>

typedef struct OrderedHashmap OrderedHashmap;

OrderedHashmap *ordered_hashmap_new(void);
void ordered_hashmap_free(OrderedHashmap *h);
void ordered_hashmap_freep(OrderedHashmap **hp);

#define _cleanup_hashmap_free_ __attribute__((cleanup(ordered_hashmap_freep)))

int ordered_hashmap_put(OrderedHashmap *h, const char *key, void *value);
void *ordered_hashmap_get(OrderedHashmap *h, const char *key);
bool ordered_hashmap_contains(OrderedHashmap *h, const char *key);
void *ordered_hashmap_remove(OrderedHashmap *h, const char *key);

size_t ordered_hashmap_size(OrderedHashmap *h);

typedef struct {
  OrderedHashmap *h;
  size_t idx;
} OrderedHashmapIter;
bool ordered_hashmap_iterate(OrderedHashmapIter *iter, const char **key,
                             void **value);

#define ORDERED_HASHMAP_FOREACH(k, v, map)			\
  for (OrderedHashmapIter _iter = {.h = (map), .idx = 0};	\
       ordered_hashmap_iterate(&_iter, &(k), &(v));)
