<commit_before>#include <stdio.h>
#include <stdlib.h>

#define ATOM_TABLE(atom) ((atom)[0])
#define TABLE_COUNT(table) ((table)[0])
#define SLOT_KEY(table, index) ((table)[1 + (index)*2])
#define SLOT_VALUE(table, index) ((table)[2 + (index)*2])

__dead2 void fail(const char *err) {
	fputs(err, stderr);
	fputc('\n', stderr);
	exit(1);
}

__dead2 void match_fail(void) {
	fputs("Match failure.\n", stderr);
	exit(1);
}

static int table_index(intptr_t *table, intptr_t count, intptr_t key) {
	int i;
	for (i = 0; i < count; i++)
		if (SLOT_KEY(table, i) == key)
			return i;
	return -1;
}

/* ENVS */

typedef intptr_t env_id;
typedef intptr_t stack_entry, env_entry;

static env_entry *resize_env_table(env_entry *table, intptr_t count) {
	intptr_t *new_table;
	new_table = realloc(table, (1 + count*2) * sizeof *table);
	if (!new_table) {
		free(table);
		fail("No memory to extend env table");
	}
	TABLE_COUNT(new_table) = count;
	return new_table;
}

static stack_entry *resize_env_stack(stack_entry *stack, intptr_t count) {
	stack_entry *new_stack;
	new_stack = realloc(stack, (1 + count) * sizeof *stack);
	if (!new_stack) {
		free(stack);
		fail("No memory to extend env stack");
	}
	TABLE_COUNT(new_stack) = count;
	return new_stack;
}

intptr_t _getenv(env_id env, env_entry *table) {
	int i;
	stack_entry *stack;

	if (!table)
		fail("Env not present (null context)");
	i = table_index(table, TABLE_COUNT(table), env);
	if (i == -1)
		fail("Env not present");
	stack = (stack_entry *) SLOT_VALUE(table, i);
	return stack[TABLE_COUNT(stack)];
}

int _haveenv(env_id env, env_entry *table) {
	return table && table_index(table, TABLE_COUNT(table), env) != -1;
}

env_entry *_pushenv(env_id env, env_entry *table, intptr_t val) {
	int i;
	intptr_t table_len, stack_len;
	stack_entry *stack, *new_stack;

	table_len = table ? TABLE_COUNT(table) : 0;
	i = table_index(table, table_len, env);
	if (i == -1) {
		i = table_len;
		table = resize_env_table(table, table_len+1);
		SLOT_KEY(table, i) = env;
		stack = NULL;
		stack_len = 1;
	}
	else {
		stack = (stack_entry *) SLOT_VALUE(table, i);
		stack_len = TABLE_COUNT(stack) + 1;
	}

	new_stack = resize_env_stack(stack, stack_len);
	/* TODO: move this thing into the resize_env_stack helper */
	if (new_stack != stack) {
		SLOT_VALUE(table, i) = (intptr_t) new_stack;
		stack = new_stack;
	}

	/* One-based due to length prefix */
	stack[stack_len] = val;

	return table;
}

env_entry *_popenv(env_id env, env_entry *table) {
	int i;
	intptr_t table_len, stack_len;
	stack_entry *stack, *new_stack;

	if (!table)
		fail("Empty env table?!");
	table_len = TABLE_COUNT(table);
	i = table_index(table, table_len, env);
	if (i == -1)
		fail("Env missing?!");

	stack = (stack_entry *) SLOT_VALUE(table, i);
	stack_len = TABLE_COUNT(stack) - 1;

	if (stack_len) {
		new_stack = resize_env_stack(stack, stack_len);
		if (new_stack != stack)
			SLOT_VALUE(table, i) = (intptr_t) new_stack;
	}
	else {
		SLOT_VALUE(table, i) = 0;
		free(stack);
		/* ought to remove this id from the env table altogether */
	}

	return table;
}

/* EXTRINSICS */

static intptr_t *resize_atom_table(intptr_t **atom, intptr_t* table,
                                   intptr_t count) {
	intptr_t *new_table;
	new_table = realloc(table, (1 + count*2) * sizeof *table);
	if (!new_table) {
		free(table);
		fail("No memory to extend extrinsic table");
	}
	if (new_table != table)<commit_msg>Streamline env stack resizing<commit_after>#include <stdio.h>
#include <stdlib.h>

#define ATOM_TABLE(atom) ((atom)[0])
#define TABLE_COUNT(table) ((table)[0])
#define SLOT_KEY(table, index) ((table)[1 + (index)*2])
#define SLOT_VALUE(table, index) ((table)[2 + (index)*2])

__dead2 void fail(const char *err) {
	fputs(err, stderr);
	fputc('\n', stderr);
	exit(1);
}

__dead2 void match_fail(void) {
	fputs("Match failure.\n", stderr);
	exit(1);
}

static int table_index(intptr_t *table, intptr_t count, intptr_t key) {
	int i;
	for (i = 0; i < count; i++)
		if (SLOT_KEY(table, i) == key)
			return i;
	return -1;
}

/* ENVS */

typedef intptr_t env_id;
typedef intptr_t stack_entry, env_entry;

static env_entry *resize_env_table(env_entry *table, intptr_t count) {
	intptr_t *new_table;
	new_table = realloc(table, (1 + count*2) * sizeof *table);
	if (!new_table) {
		free(table);
		fail("No memory to extend env table");
	}
	TABLE_COUNT(new_table) = count;
	return new_table;
}

static stack_entry *resize_env_stack(intptr_t *pstack, stack_entry *stack,
			intptr_t count) {
	stack_entry *new_stack;
	new_stack = realloc(stack, (1 + count) * sizeof *stack);
	if (!new_stack) {
		free(stack);
		fail("No memory to extend env stack");
	}
	TABLE_COUNT(new_stack) = count;
	if (new_stack != stack)
		*pstack = (intptr_t) new_stack;
	return new_stack;
}

intptr_t _getenv(env_id env, env_entry *table) {
	int i;
	stack_entry *stack;

	if (!table)
		fail("Env not present (null context)");
	i = table_index(table, TABLE_COUNT(table), env);
	if (i == -1)
		fail("Env not present");
	stack = (stack_entry *) SLOT_VALUE(table, i);
	return stack[TABLE_COUNT(stack)];
}

int _haveenv(env_id env, env_entry *table) {
	return table && table_index(table, TABLE_COUNT(table), env) != -1;
}

env_entry *_pushenv(env_id env, env_entry *table, intptr_t val) {
	int i;
	intptr_t table_len, stack_len;
	stack_entry *stack;

	table_len = table ? TABLE_COUNT(table) : 0;
	i = table_index(table, table_len, env);
	if (i == -1) {
		i = table_len;
		table = resize_env_table(table, table_len+1);
		SLOT_KEY(table, i) = env;
		stack = NULL;
		stack_len = 1;
	}
	else {
		stack = (stack_entry *) SLOT_VALUE(table, i);
		stack_len = TABLE_COUNT(stack) + 1;
	}

	stack = resize_env_stack(&SLOT_VALUE(table, i), stack, stack_len);

	/* One-based due to length prefix */
	stack[stack_len] = val;

	return table;
}

env_entry *_popenv(env_id env, env_entry *table) {
	int i;
	intptr_t table_len, stack_len;
	stack_entry *stack;

	if (!table)
		fail("Empty env table?!");
	table_len = TABLE_COUNT(table);
	i = table_index(table, table_len, env);
	if (i == -1)
		fail("Env missing?!");

	stack = (stack_entry *) SLOT_VALUE(table, i);
	stack_len = TABLE_COUNT(stack) - 1;

	if (stack_len) {
		stack = resize_env_stack(&SLOT_VALUE(table, i), stack,
				stack_len);
	}
	else {
		SLOT_VALUE(table, i) = 0;
		free(stack);
		/* ought to remove this id from the env table altogether */
	}

	return table;
}

/* EXTRINSICS */

static intptr_t *resize_atom_table(intptr_t **atom, intptr_t* table,
                                   intptr_t count) {
	intptr_t *new_table;
	new_table = realloc(table, (1 + count*2) * sizeof *table);
	if (!new_table) {
		free(table);
		fail("No memory to extend extrinsic table");
	}
	if (new_table != table)
{'fraction_non_alphanumeric': 0.14812611540749554, 'fraction_numerical': 0.008030933967876264, 'mean_word_length': 4.419016921837228, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5656320', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2655, 'n_words': 658}