<commit_before>#include <kotaka/privilege.h>
#include <kotaka/assert.h>

object root_category;

static void create()
{
	root_category = clone_object("~/obj/category");
}

static void destruct()
{
	if (root_category) {
		destruct_object(root_category);
	}
}

void reset()
{
	ACCESS_CHECK(PRIVILEGED());

	destruct_object(root_category);

	root_category = clone_object("~/obj/category");
}

void add_category(string category)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->insert_entry(category, 1);
}

void add_topic(string topic, varargs mixed content)
{
	ACCESS_CHECK(PRIVILEGED());
	ASSERT(content);

	root_category->insert_entry(topic, 0)->set_content(content);
}

void remove_category(string category)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->delete_entry(category, 1);
}

void remove_topic(string topic)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->delete_entry(topic, 0);
}

string *query_topics(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		return subnode->query_topics();
	} else {
		return nil;
	}
}

string *query_categories(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		return subnode->query_categories();
	} else {
		return nil;
	}
}

mapping query_index(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		return subnode->query_index();
	} else {
		return nil;
	}
}

int test_topic(string topic)
{
	string *parts;<commit_msg>Return index by value, plug tamper hole<commit_after>#include <kotaka/privilege.h>
#include <kotaka/assert.h>

object root_category;

static void create()
{
	root_category = clone_object("~/obj/category");
}

static void destruct()
{
	if (root_category) {
		destruct_object(root_category);
	}
}

void reset()
{
	ACCESS_CHECK(PRIVILEGED());

	destruct_object(root_category);

	root_category = clone_object("~/obj/category");
}

void add_category(string category)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->insert_entry(category, 1);
}

void add_topic(string topic, varargs mixed content)
{
	ACCESS_CHECK(PRIVILEGED());
	ASSERT(content);

	root_category->insert_entry(topic, 0)->set_content(content);
}

void remove_category(string category)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->delete_entry(category, 1);
}

void remove_topic(string topic)
{
	ACCESS_CHECK(PRIVILEGED());

	root_category->delete_entry(topic, 0);
}

string *query_topics(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		return subnode->query_topics();
	} else {
		return nil;
	}
}

string *query_categories(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		return subnode->query_categories();
	} else {
		return nil;
	}
}

mapping query_index(varargs string category)
{
	object subnode;

	if (!category || category == "") {
		subnode = root_category;
	} else {
		subnode = root_category->find_node(category);
	}

	if (subnode) {
		mapping index;

		string *ind;
		mapping *val;
		int sz, i;

		index = subnode->query_index()[..];

		ind = map_indices(index);
		val = map_values(index);

		for (sz = map_sizeof(index), i = 0; i < sz; i++) {
			index[ind[i]] = val[i][..];
		}

		return index;
	} else {
		return nil;
	}
}

int test_topic(string topic)
{
	string *parts;
{'fraction_non_alphanumeric': 0.143384290985767, 'fraction_numerical': 0.0023721665788086453, 'mean_word_length': 4.865533230293663, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 29, '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': '2741269', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1350, 'n_words': 268}