<commit_before>#line 2 "togo/resource_manager.cpp"
/**
@copyright MIT license; see @ref index or the accompanying LICENSE file.
*/

#include <togo/config.hpp>
#include <togo/assert.hpp>
#include <togo/memory.hpp>
#include <togo/hash_map.hpp>
#include <togo/log.hpp>
#include <togo/resource_types.hpp>
#include <togo/resource_package.hpp>
#include <togo/resource_manager.hpp>

namespace togo {

namespace resource_manager {

static bool find_resource(
	ResourceManager& rm,
	ResourceType const type,
	ResourceNameHash const name_hash,
	ResourcePackage*& package,
	ResourcePackage::EntryNode*& node
) {
	// TODO: Tag filter
	ResourcePackage::EntryNode* it_node;
	for (
		auto* it = array::end(rm._packages) - 1;
		it >= array::begin(rm._packages);
		--it
	) {
		it_node = resource_package::find_resource(**it, type, name_hash);
		if (it_node) {
			package = *it;
			node = it_node;
			return true;
		}
	}
	return false;
}

} // namespace resource_manager

ResourceManager::~ResourceManager() {
	resource_manager::clear_packages(*this);
}

ResourceManager::ResourceManager(Allocator& allocator)
	: _handlers(allocator)
	, _resources(allocator)
	, _packages(allocator)
{
	hash_map::reserve(_handlers, 16);
}

void resource_manager::register_handler(
	ResourceManager& rm,
	ResourceHandler const& handler,
	void* type_data
) {
	TOGO_ASSERT(
		handler.func_load && handler.func_unload,
		"load_func and unload_func must be assigned in handler"
	);
	TOGO_ASSERT(
		!hash_map::has(rm._handlers, handler.type),
		"type has already been registered"
	);
	hash_map::push(rm._handlers, handler.type, {handler, type_data});
}

bool resource_manager::has_handler(
	ResourceManager const& rm,
	ResourceType type
) {
	return hash_map::has(rm._handlers, type);
}

hash64 resource_manager::add_package(<commit_msg>resource_manager: corrected register_handler() error message; tidy.<commit_after>#line 2 "togo/resource_manager.cpp"
/**
@copyright MIT license; see @ref index or the accompanying LICENSE file.
*/

#include <togo/config.hpp>
#include <togo/assert.hpp>
#include <togo/memory.hpp>
#include <togo/array.hpp>
#include <togo/hash_map.hpp>
#include <togo/log.hpp>
#include <togo/resource_types.hpp>
#include <togo/resource_package.hpp>
#include <togo/resource_manager.hpp>

namespace togo {

namespace resource_manager {

static bool find_resource(
	ResourceManager& rm,
	ResourceType const type,
	ResourceNameHash const name_hash,
	ResourcePackage*& package,
	ResourcePackage::EntryNode*& node
) {
	// TODO: Tag filter
	ResourcePackage::EntryNode* it_node;
	for (
		auto* it = array::end(rm._packages) - 1;
		it >= array::begin(rm._packages);
		--it
	) {
		it_node = resource_package::find_resource(**it, type, name_hash);
		if (it_node) {
			package = *it;
			node = it_node;
			return true;
		}
	}
	return false;
}

} // namespace resource_manager

ResourceManager::~ResourceManager() {
	resource_manager::clear_packages(*this);
}

ResourceManager::ResourceManager(Allocator& allocator)
	: _handlers(allocator)
	, _resources(allocator)
	, _packages(allocator)
{
	hash_map::reserve(_handlers, 16);
}

void resource_manager::register_handler(
	ResourceManager& rm,
	ResourceHandler const& handler,
	void* type_data
) {
	TOGO_ASSERT(
		handler.func_load && handler.func_unload,
		"func_load and func_unload must be assigned in handler"
	);
	TOGO_ASSERT(
		!hash_map::has(rm._handlers, handler.type),
		"type has already been registered"
	);
	hash_map::push(rm._handlers, handler.type, {handler, type_data});
}

bool resource_manager::has_handler(
	ResourceManager const& rm,
	ResourceType type
) {
	return hash_map::has(rm._handlers, type);
}

hash64 resource_manager::add_package(
{'fraction_non_alphanumeric': 0.12697547683923704, 'fraction_numerical': 0.0032697547683923707, 'mean_word_length': 5.638336347197106, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, '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': '7532046', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1258, 'n_words': 268}