<commit_before>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2006, 2010, 2012, 2013, 2014, 2015  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kotaka/assert.h>
#include <kotaka/checkarg.h>
#include <kotaka/privilege.h>

private object *archetypes;

private object first_instance;

private mapping prev_instance;
private mapping next_instance;

static void create()
{
	archetypes = ({ });
	prev_instance = ([ ]);
	next_instance = ([ ]);
}

nomask void clean_archetypes()
{
	archetypes -= ({ nil });
}

nomask int is_archetype_of(object test)
{
	int index;
	int sz;
	object *arch;

	arch = test->query_archetypes();
	sz = sizeof(arch);

	if (sizeof(arch & ({ this_object() }))) {
		return 1;
	}

	for(index = 0; index < sz; index++) {
		if (is_archetype_of(arch[index])) {
			return 1;
		}
	}

	return 0;
}

nomask object *query_archetypes()
{
	clean_archetypes();

	return archetypes[..];
}

atomic nomask void set_archetypes(object *new_archs)
{
	int i;
	int sz;

	object *removed, *added, *arch;
	object *check, this;

	CHECKARG(sizeof(new_archs & ({ nil })) == 0, 1, "set_archetypes");

	arch = query_archetypes();

	removed = arch - new_archs;
	added = new_archs - arch;

	removed -= ({ nil });
	added -= ({ nil });

	sz = sizeof(added);

	for (i = 0; i < sz; i++) {
		object arch;

		arch = added[i];

		if (is_archetype_of(arch)) {
			error("Circular reference");
		}
	}

	archetypes = new_archs;

	this = this_object();

	sz = sizeof(removed);

	for (i = 0; i < sz; i++) {
		removed[i]->thing_remove_instance(this);
	}

	sz = sizeof(added);

	for (i = 0; i < sz; i++) {
		added[i]->thing_add_instance(this);
	}
}

atomic nomask void clear_archetypes()
{
	int sz, i;
	object this;

	sz = sizeof(archetypes -= ({ nil }) );

	this = this_object();

	for (i = 0; i < sz; i++) {
		archetypes[i]->thing_remove_instance(this);
	}

	archetypes = ({ });
}

atomic nomask void add_archetype(object new_arch)
{
	CHECKARG(new_arch, 1, "add_archetype");

	if (is_archetype_of(new_arch)) {
		error("Circular reference");
	}
<commit_msg>Optimize a few loops in thing library<commit_after>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2006, 2010, 2012, 2013, 2014, 2015  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kotaka/assert.h>
#include <kotaka/checkarg.h>
#include <kotaka/privilege.h>

private object *archetypes;

private object first_instance;

private mapping prev_instance;
private mapping next_instance;

static void create()
{
	archetypes = ({ });
	prev_instance = ([ ]);
	next_instance = ([ ]);
}

nomask void clean_archetypes()
{
	archetypes -= ({ nil });
}

nomask int is_archetype_of(object test)
{
	int index;
	int sz;
	object *arch;

	arch = test->query_archetypes();
	sz = sizeof(arch);

	if (sizeof(arch & ({ this_object() }))) {
		return 1;
	}

	for(index = 0; index < sz; index++) {
		if (is_archetype_of(arch[index])) {
			return 1;
		}
	}

	return 0;
}

nomask object *query_archetypes()
{
	clean_archetypes();

	return archetypes[..];
}

atomic nomask void set_archetypes(object *new_archs)
{
	int sz;

	object *removed, *added, *arch;
	object *check, this;

	CHECKARG(sizeof(new_archs & ({ nil })) == 0, 1, "set_archetypes");

	arch = query_archetypes();

	removed = arch - new_archs;
	added = new_archs - arch;

	removed -= ({ nil });
	added -= ({ nil });

	for (sz = sizeof(added); --sz >= 0; ) {
		object arch;

		arch = added[sz];

		if (is_archetype_of(arch)) {
			error("Circular reference");
		}
	}

	archetypes = new_archs;

	this = this_object();

	for (sz = sizeof(removed); --sz >= 0; ) {
		removed[sz]->thing_remove_instance(this);
	}

	for (sz = sizeof(added); --sz >= 0; ) {
		added[sz]->thing_add_instance(this);
	}
}

atomic nomask void clear_archetypes()
{
	int sz;
	object this;

	this = this_object();

	for (sz = sizeof(archetypes -= ({ nil }) ); sz >= 0; ) {
		archetypes[sz]->thing_remove_instance(this);
	}

	archetypes = ({ });
}

atomic nomask void add_archetype(object new_arch)
{
	CHECKARG(new_arch, 1, "add_archetype");

	if (is_archetype_of(new_arch)) {
		error("Circular reference");
	}

{'fraction_non_alphanumeric': 0.13770371726789965, 'fraction_numerical': 0.01318439846182018, 'mean_word_length': 3.855111111111111, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2356438', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1903, 'n_words': 579}