<commit_before>#ifndef STINGRAY_TOOLKIT_BASICHISTORY_H
#define STINGRAY_TOOLKIT_BASICHISTORY_H


#include <algorithm>
#include <deque>
#include <functional>


namespace stingray
{

	template < typename T >
	class BasicHistory
	{
		typedef std::deque<T>		HistoryDeque;
		typedef std::deque<int>		LastHistoryIndices;

		HistoryDeque				_history;
		LastHistoryIndices			_lastIndices;
		size_t						_maxHistorySize;
		size_t						_lastHistoryIndicesCount;
		Mutex						_mutex;

	public:
		BasicHistory(size_t maxHistorySize, size_t lastHistoryIndicesCount = 2)
			: _maxHistorySize(maxHistorySize), _lastHistoryIndicesCount(lastHistoryIndicesCount)
		{ }

		void Add(const T& val)
		{
			MutexLock l(_mutex);

			if (!_lastIndices.empty())
			{
				int li = *_lastIndices.rbegin();
				if (li >= 0 && (int)_history.size() > li && _history[li] == val)
					return;
			}

			_history.push_back(val);
			int index_delta = 0;
			for (;_history.size() > _maxHistorySize; ++index_delta)
				_history.pop_front();

			for (LastHistoryIndices::iterator it = _lastIndices.begin(); it != _lastIndices.end(); ++it)
				*it -= index_delta;
			_lastIndices.erase(std::remove_if(_lastIndices.begin(), _lastIndices.end(), bind(std::less<size_t>(), _1, 0)), _lastIndices.end());

			AddLastIndex(_history.size() - 1);
		}

		bool ToggleLast(T& outVal) // TODO: replace with more generic accessors
		{
			MutexLock l(_mutex);

			if (_lastIndices.size() < 2)
				return false;

			int prev = _lastIndices[_lastIndices.size() - 2];
			if (prev < 0 || (int)_history.size() <= prev)
				return false;

			AddLastIndex(prev);
			outVal = _history[prev];
			return true;
		}

	private:
		void AddLastIndex(size_t index)
		{
			_lastIndices.push_back(index);
			while (_lastIndices.size() > _lastHistoryIndicesCount)
				_lastIndices.pop_front();
		}
	};
	
}


#endif<commit_msg>Added some logging to BasicHistory<commit_after>#ifndef STINGRAY_TOOLKIT_BASICHISTORY_H
#define STINGRAY_TOOLKIT_BASICHISTORY_H


#include <algorithm>
#include <deque>
#include <functional>


namespace stingray
{

	template < typename T >
	class BasicHistory
	{
		typedef std::deque<T>		HistoryDeque;
		typedef std::deque<int>		LastHistoryIndices;

		HistoryDeque				_history;
		LastHistoryIndices			_lastIndices;
		size_t						_maxHistorySize;
		size_t						_lastHistoryIndicesCount;
		Mutex						_mutex;

	public:
		BasicHistory(size_t maxHistorySize, size_t lastHistoryIndicesCount = 2)
			: _maxHistorySize(maxHistorySize), _lastHistoryIndicesCount(lastHistoryIndicesCount)
		{ }

		void Add(const T& val)
		{
			MutexLock l(_mutex);

			//LogState("Before Add(" + ToString(val) + ")");

			if (!_lastIndices.empty())
			{
				int li = *_lastIndices.rbegin();
				if (li >= 0 && (int)_history.size() > li && _history[li] == val)
					return;
			}

			_history.push_back(val);
			int index_delta = 0;
			for (;_history.size() > _maxHistorySize; ++index_delta)
				_history.pop_front();

			for (LastHistoryIndices::iterator it = _lastIndices.begin(); it != _lastIndices.end(); ++it)
				*it -= index_delta;
			_lastIndices.erase(std::remove_if(_lastIndices.begin(), _lastIndices.end(), bind(std::less<size_t>(), _1, 0)), _lastIndices.end());

			AddLastIndex(_history.size() - 1);

			//LogState("After Add(" + ToString(val) + ")");
		}

		bool ToggleLast(T& outVal) // TODO: replace with more generic accessors
		{
			MutexLock l(_mutex);

			//LogState("Before ToggleLast");

			if (_lastIndices.size() < 2)
				return false;

			int prev = _lastIndices[_lastIndices.size() - 2];
			if (prev < 0 || (int)_history.size() <= prev)
				return false;

			AddLastIndex(prev);
			outVal = _history[prev];
			//Logger::Info() << "[BasicHistory] " << "outVal: " << outVal;

			//LogState("After ToggleLast");

			return true;
		}

	private:
		void AddLastIndex(size_t index)
		{
			_lastIndices.push_back(index);
			while (_lastIndices.size() > _lastHistoryIndicesCount)
				_lastIndices.pop_front();
		}

		void LogState(const std::string& msg) const
		{ Logger::Info() << "[BasicHistory] " << msg << ": { history: " << _history << ", lastIndices: " << _lastIndices << " }"; }
	};
	
}


#endif
{'fraction_non_alphanumeric': 0.1390296886314265, 'fraction_numerical': 0.004344677769732078, 'mean_word_length': 3.9927710843373494, 'pattern_counts': {'":': 1, '<': 43, '<?xml version=': 0, '>': 25, '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': '5471931', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1465, 'n_words': 285}