<commit_before>#include "pch.h"
#include "EventManager.h"
#include "GameException.h"

using namespace PinnedDownClient::Core;

EventManager::EventManager()
{
}

void EventManager::AddListener(std::shared_ptr<IEventListener> const & listener, PinnedDownClient::Util::HashedString const & eventType)
{
	// Get the event type entry in the listeners map.
	std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::iterator iterator = this->listeners.find(eventType.getHash());
	std::list<std::shared_ptr<IEventListener>> eventListeners;

	if (iterator != this->listeners.end())
	{
		eventListeners = iterator->second;
	}
	else
	{
		// Add new entry to listeners map.
		eventListeners = std::list<std::shared_ptr<IEventListener>>();
		this->listeners.insert(std::pair<unsigned long, std::list<std::shared_ptr<IEventListener>>>(eventType.getHash(), eventListeners));
	}

	// Add listener.
	eventListeners.push_back(listener);
}

void EventManager::RemoveListener(std::shared_ptr<IEventListener> const & listener, PinnedDownClient::Util::HashedString const & eventType)
{
	// Find listener to remove.
	for (std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::iterator it = this->listeners.begin(); it != this->listeners.end(); it++)
	{
		unsigned long const eventId = it->first;
		std::list<std::shared_ptr<IEventListener>> eventListeners = it->second;

		for (std::list<std::shared_ptr<IEventListener>>::iterator it2 = eventListeners.begin(); it2 != eventListeners.end(); it2++)
		{
			if (*it2 == listener)
			{
				// Remove listener.
				eventListeners.erase(it2);
				return;
			}
		}
	}
}

void EventManager::QueueEvent(std::shared_ptr<Event> const & newEvent)
{
	// Queue event.
	this->newEvents.push_back(newEvent);
}

void EventManager::Tick()
{
	while (this->newEvents.size() > 0)
	{
		// Move events from new event queue to current event queue.
		std::shared_ptr<Event> newEvent = this->newEvents.front();
		this->newEvents.pop_front();
		this->currentEvents.push_back(newEvent);

		// Process all events.
		while (this->currentEvents.size() > 0)
		{
			// Get next event to process.
			std::shared_ptr<Event> currentEvent = this->currentEvents.front();
			this->currentEvents.pop_front();

			PinnedDownClient::Util::HashedString const & eventType = currentEvent->GetEventType();

			// Get listeners for the event.
			std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::const_iterator itListeners = this->listeners.find(eventType.getHash());

			if (itListeners != this->listeners.end())
			{
				std::list<std::shared_ptr<IEventListener>> const & eventListeners = itListeners->second;

				// Notify all listeners.
				for (std::list<std::shared_ptr<IEventListener>>::const_iterator it = eventListeners.begin(); it != eventListeners.end(); it++)
				{
					(*it)->OnEvent(*currentEvent);
				}
			}
		}
	}
}<commit_msg>FIXED: Core - Event listeners are correctly added if they're the first listener for an event.<commit_after>#include "pch.h"
#include "EventManager.h"
#include "GameException.h"

using namespace PinnedDownClient::Core;

EventManager::EventManager()
{
}

void EventManager::AddListener(std::shared_ptr<IEventListener> const & listener, PinnedDownClient::Util::HashedString const & eventType)
{
	// Get the event type entry in the listeners map.
	std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::iterator iterator = this->listeners.find(eventType.getHash());

	if (iterator != this->listeners.end())
	{
		std::list<std::shared_ptr<IEventListener>> eventListeners = iterator->second;

		// Add listener.
		eventListeners.push_back(listener);
	}
	else
	{
		// Add new entry to listeners map.
		std::list<std::shared_ptr<IEventListener>> eventListeners = std::list<std::shared_ptr<IEventListener>>();
		eventListeners.push_back(listener);
		this->listeners.insert(std::pair<unsigned long, std::list<std::shared_ptr<IEventListener>>>(eventType.getHash(), eventListeners));
	}
}

void EventManager::RemoveListener(std::shared_ptr<IEventListener> const & listener, PinnedDownClient::Util::HashedString const & eventType)
{
	// Find listener to remove.
	for (std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::iterator it = this->listeners.begin(); it != this->listeners.end(); it++)
	{
		unsigned long const eventId = it->first;
		std::list<std::shared_ptr<IEventListener>> eventListeners = it->second;

		for (std::list<std::shared_ptr<IEventListener>>::iterator it2 = eventListeners.begin(); it2 != eventListeners.end(); it2++)
		{
			if (*it2 == listener)
			{
				// Remove listener.
				eventListeners.erase(it2);
				return;
			}
		}
	}
}

void EventManager::QueueEvent(std::shared_ptr<Event> const & newEvent)
{
	// Queue event.
	this->newEvents.push_back(newEvent);
}

void EventManager::Tick()
{
	while (this->newEvents.size() > 0)
	{
		// Move events from new event queue to current event queue.
		std::shared_ptr<Event> newEvent = this->newEvents.front();
		this->newEvents.pop_front();
		this->currentEvents.push_back(newEvent);

		// Process all events.
		while (this->currentEvents.size() > 0)
		{
			// Get next event to process.
			std::shared_ptr<Event> currentEvent = this->currentEvents.front();
			this->currentEvents.pop_front();

			PinnedDownClient::Util::HashedString const & eventType = currentEvent->GetEventType();
			unsigned long eventHash = eventType.getHash();

			// Get listeners for the event.
			std::map<unsigned long, std::list<std::shared_ptr<IEventListener>>>::const_iterator itListeners = this->listeners.find(eventHash);

			if (itListeners != this->listeners.end())
			{
				std::list<std::shared_ptr<IEventListener>> const & eventListeners = itListeners->second;

				// Notify all listeners.
				for (std::list<std::shared_ptr<IEventListener>>::const_iterator it = eventListeners.begin(); it != eventListeners.end(); it++)
				{
					(*it)->OnEvent(*currentEvent);
				}
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.15570175438596492, 'fraction_numerical': 0.0023616734143049934, 'mean_word_length': 5.260823653643083, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 109, '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': '4891617', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1860, 'n_words': 359}