<commit_before>#ifndef STINGRAYKIT_COLLECTION_ENUMERABLEWRAPPER_H
#define STINGRAYKIT_COLLECTION_ENUMERABLEWRAPPER_H

// Copyright (c) 2011 - 2019, GS Group, https://github.com/GSGroup
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
// provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#include <stingraykit/collection/IEnumerable.h>
#include <stingraykit/function/function.h>
#include <stingraykit/optional.h>

namespace stingray
{

	namespace Detail
	{
		template < typename SrcType, typename DestType >
		class EnumeratorWrapper : public IEnumerator<DestType>
		{
			typedef shared_ptr<IEnumerator<SrcType> >					SrcEnumeratorPtr;
			typedef typename AddConstLvalueReference<SrcType>::ValueT	ConstSrcTypeRef;
			typedef function<bool (ConstSrcTypeRef)>					FilterPredicate;
			typedef function<DestType (ConstSrcTypeRef)>				Caster;

		private:
			SrcEnumeratorPtr			_srcEnumerator;
			Caster						_caster;
			FilterPredicate				_filterPredicate;
			optional<SrcType>			_cache;

		public:
			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(&EnumeratorWrapper::DefaultCast), _filterPredicate(&EnumeratorWrapper::NoFilter)
			{ FindFirst(); }

			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator, const Caster& caster)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(caster), _filterPredicate(&EnumeratorWrapper::NoFilter)
			{ FindFirst(); }

			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator, const Caster& caster, const FilterPredicate& filterPredicate)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(caster), _filterPredicate(filterPredicate)
			{ FindFirst(); }

			virtual bool Valid() const
			{ return _srcEnumerator->Valid(); }

			virtual DestType Get() const
			{ return _cache ? _caster(*_cache) : _caster(_srcEnumerator->Get()); }

			virtual void Next()
			{
				if (!Valid())
					return;

				_cache.reset();
				do {
					_srcEnumerator->Next();
					if (!Valid())
						break;

					const SrcType& cache = _srcEnumerator->Get();
					if (_filterPredicate(cache))
						_cache.emplace(cache);
				} while (!_cache);
			}

		private:
			void FindFirst()
			{
				while (Valid() && !_cache)
				{
					const SrcType& cache = _srcEnumerator->Get();
					if (_filterPredicate(cache))
						_cache.emplace(cache);
					else
						_srcEnumerator->Next();
				}
			}

			static DestType DefaultCast(ConstSrcTypeRef src)	{ return DestType(src); }
			static bool NoFilter(ConstSrcTypeRef)				{ return true; }
		};

		template < typename SrcType >
		class EnumeratorWrapperProxy
		{
			typedef shared_ptr<IEnumerator<SrcType> >					SrcEnumeratorPtr;

		private:
			SrcEnumeratorPtr			_srcEnumerator;
<commit_msg>EnumerableWrapper: fix enumerator logic and deduplicate code<commit_after>#ifndef STINGRAYKIT_COLLECTION_ENUMERABLEWRAPPER_H
#define STINGRAYKIT_COLLECTION_ENUMERABLEWRAPPER_H

// Copyright (c) 2011 - 2019, GS Group, https://github.com/GSGroup
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
// provided that the above copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#include <stingraykit/collection/IEnumerable.h>
#include <stingraykit/function/function.h>
#include <stingraykit/optional.h>

namespace stingray
{

	namespace Detail
	{
		template < typename SrcType, typename DestType >
		class EnumeratorWrapper : public IEnumerator<DestType>
		{
			typedef shared_ptr<IEnumerator<SrcType> >					SrcEnumeratorPtr;
			typedef typename AddConstLvalueReference<SrcType>::ValueT	ConstSrcTypeRef;
			typedef function<bool (ConstSrcTypeRef)>					FilterPredicate;
			typedef function<DestType (ConstSrcTypeRef)>				Caster;

		private:
			SrcEnumeratorPtr			_srcEnumerator;
			Caster						_caster;
			FilterPredicate				_filterPredicate;
			optional<SrcType>			_cache;

		public:
			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(&EnumeratorWrapper::DefaultCast), _filterPredicate(&EnumeratorWrapper::NoFilter)
			{ FindNext(); }

			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator, const Caster& caster)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(caster), _filterPredicate(&EnumeratorWrapper::NoFilter)
			{ FindNext(); }

			EnumeratorWrapper(const SrcEnumeratorPtr& srcEnumerator, const Caster& caster, const FilterPredicate& filterPredicate)
				: _srcEnumerator(STINGRAYKIT_REQUIRE_NOT_NULL(srcEnumerator)), _caster(caster), _filterPredicate(filterPredicate)
			{ FindNext(); }

			virtual bool Valid() const
			{ return _cache.is_initialized(); }

			virtual DestType Get() const
			{
				STINGRAYKIT_CHECK(_cache, "Enumerator is not valid!");
				return _caster(*_cache);
			}

			virtual void Next()
			{
				STINGRAYKIT_CHECK(_cache, "Enumerator is not valid!");

				_cache.reset();
				_srcEnumerator->Next();

				FindNext();
			}

		private:
			void FindNext()
			{
				while (!_cache && _srcEnumerator->Valid())
				{
					const SrcType& cache = _srcEnumerator->Get();
					if (_filterPredicate(cache))
						_cache.emplace(cache);
					else
						_srcEnumerator->Next();
				}
			}

			static DestType DefaultCast(ConstSrcTypeRef src)	{ return DestType(src); }
			static bool NoFilter(ConstSrcTypeRef)				{ return true; }
		};

		template < typename SrcType >
		class EnumeratorWrapperProxy
		{
			typedef shared_ptr<IEnumerator<SrcType> >					SrcEnumeratorPtr;

		private:
			SrcEnumeratorPtr			_srcEnumerator;

{'fraction_non_alphanumeric': 0.08571428571428572, 'fraction_numerical': 0.002380952380952381, 'mean_word_length': 4.605504587155964, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '2755237', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2062, 'n_words': 535}