<commit_before>#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>

#include "dynmap.h"
#include "alloc.h"

typedef struct pair pair;

struct dynmap
{
	dynmap_cmp_f *cmp;
	struct pair
	{
		void *key, *value;
		pair *next;
	} *pairs, *last_pair;
	/* last_pair keeps the keys in the order they are added */
};

dynmap *
dynmap_new(dynmap_cmp_f cmp)
{
	dynmap *m = umalloc(sizeof *m);
	m->cmp = cmp;
	return m;
}

void
dynmap_free(dynmap *map)
{
	pair *p, *q;

	if(!map)
		return;

	for(p = map->pairs; p; q = p->next, free(p), p = q);

	free(map);
}

static pair *
dynmap_nochk_pair(dynmap *map, void *key)
{
	pair *i;

	assert(key && "null key");

	for(i = map->pairs; i; i = i->next)
		if(map->cmp ? !map->cmp(i->key, key) : i->key == key)
			return i;

	return NULL;
}

void *
dynmap_nochk_get(dynmap *map, void *key)
{
	pair *i;

	if(!map)
		return NULL;

	i = dynmap_nochk_pair(map, key);
	if(i)
		return i->value;

	return NULL;
}

void
dynmap_nochk_set(dynmap *map, void *key, void *val)
{
	pair *p;

	assert(key && val && "null key/val");

	p = dynmap_nochk_pair(map, key);

	if(p){
		p->value = val;
	}else{
		p = umalloc(sizeof *p);
		p->key   = key;
		p->value = val;

		if(map->last_pair)
			map->last_pair->next = p;
		else
			map->pairs = p;

		map->last_pair = p;<commit_msg>dynmap allows a null value<commit_after>#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>

#include "dynmap.h"
#include "alloc.h"

typedef struct pair pair;

struct dynmap
{
	dynmap_cmp_f *cmp;
	struct pair
	{
		void *key, *value;
		pair *next;
	} *pairs, *last_pair;
	/* last_pair keeps the keys in the order they are added */
};

dynmap *
dynmap_new(dynmap_cmp_f cmp)
{
	dynmap *m = umalloc(sizeof *m);
	m->cmp = cmp;
	return m;
}

void
dynmap_free(dynmap *map)
{
	pair *p, *q;

	if(!map)
		return;

	for(p = map->pairs; p; q = p->next, free(p), p = q);

	free(map);
}

static pair *
dynmap_nochk_pair(dynmap *map, void *key)
{
	pair *i;

	assert(key && "null key");

	for(i = map->pairs; i; i = i->next)
		if(map->cmp ? !map->cmp(i->key, key) : i->key == key)
			return i;

	return NULL;
}

void *
dynmap_nochk_get(dynmap *map, void *key)
{
	pair *i;

	if(!map)
		return NULL;

	i = dynmap_nochk_pair(map, key);
	if(i)
		return i->value;

	return NULL;
}

void
dynmap_nochk_set(dynmap *map, void *key, void *val)
{
	pair *p;

	assert(key && "null dynmap key");

	p = dynmap_nochk_pair(map, key);

	if(p){
		p->value = val;
	}else{
		p = umalloc(sizeof *p);
		p->key   = key;
		p->value = val;

		if(map->last_pair)
			map->last_pair->next = p;
		else
			map->pairs = p;

		map->last_pair = p;
{'fraction_non_alphanumeric': 0.17570165321030373, 'fraction_numerical': 0.0, 'mean_word_length': 3.4176570458404076, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356470', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1168, 'n_words': 289}