<commit_before>#ifndef STINGRAY_TOOLKIT_MAPPER_H
#define STINGRAY_TOOLKIT_MAPPER_H


#include <stingray/toolkit/reference.h>


namespace stingray
{


	namespace Detail
	{
		template<typename T>
		struct Mappings
		{
			template<T Start_, T Center_, T End_>
			struct Range
			{
				static bool Fits(T val)
				{
					CompileTimeAssert<Start_ < End_> ERROR_start_after_end;
					return Start_ <= val && val < End_;
				}
				static T GetValue() { return Center_; }
			};

			template<T Preferred, T Other>
			struct BiValue
			{
				static bool Fits(T val)
				{
					CompileTimeAssert<Preferred != Other> ERROR_same_values;
					return val == Preferred || val == Other;
				}
				static T GetValue() { return Preferred; }
			};

			template<T Preferred, T Other1, T Other2>
			struct TriValue
			{
				static bool Fits(T val)	{ return val == Preferred || val == Other1 || val == Other2; }
				static T GetValue()		{ return Preferred; }
			};

			template<T Preferred, T Other1, T Other2, T Other3>
			struct QuadValue
			{
				static bool Fits(T val)	{ return val == Preferred || val == Other1 || val == Other2 || val == Other3; }
				static T GetValue()		{ return Preferred; }
			};

			template<T Value_>
			struct Value
			{
				static bool Fits(T val)	{ return val == Value_; }
				static T GetValue()		{ return Value_; }
			};

			struct Fail
			{
				static T GetValue()		{ TOOLKIT_THROW(std::runtime_error("Mapping failed!")); }
			};
		};

		template<typename SrcT, typename DstT, typename List, typename Default>
		struct Mapper
		{
			template<int N, bool IsMapBack>
			static bool DetailDoMap(typename If<IsMapBack, DstT, SrcT>::ValueT from, typename If<IsMapBack, SrcT, DstT>::ValueT& to)
			{
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 1 : 0)>::ValueT	MappingFrom;
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 0 : 1)>::ValueT	MappingTo;
				bool result = MappingFrom::Fits(from);
				if (result)
					to = MappingTo::GetValue();
				return !result; // need to negate since ForIf continues untill gets "false" and we need to stop when found fitting mapping
			}

			template<int N, bool IsMapBack>
			static bool DetailDoHasMapping(typename If<IsMapBack, DstT, SrcT>::ValueT from, bool& res)
			{
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 1 : 0)>::ValueT	MappingFrom;
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 0 : 1)>::ValueT	MappingTo;
				bool result = MappingFrom::Fits(from);
				if (result)
					res = !SameType<MappingTo, typename Mappings<typename If<IsMapBack, SrcT, DstT>::ValueT>::Fail>::Value;
				return !result; // need to negate since ForIf continues untill gets "false" and we need to stop when found fitting mapping
			}

			template<int N>
			struct MapFunctor
			{ static bool Call(SrcT from, DstT& to) { return DetailDoMap<N, false>(from, to); } };

			template<int N>
			struct UnmapFunctor
			{ static bool Call(DstT from, SrcT& to) { return DetailDoMap<N, true>(from, to); } };

			template<int N>
			struct HasMappingFunctor
			{ static bool Call(SrcT from, bool& res) { return DetailDoHasMapping<N, false>(from, res); } };

			template<int N>
			struct HasBackMappingFunctor
			{ static bool Call(DstT from, bool& res) { return DetailDoHasMapping<N, true>(from, res); } };

			static DstT Map(SrcT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 1>::ValueT DefaultValue;

				DstT result;
				if (ForIf<GetTypeListLength<List>::Value / 2, MapFunctor>::Do(val, ref(result)))
					return DefaultValue::GetValue();
				return result;
			}

			static SrcT Unmap(DstT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 0>::ValueT DefaultValue;

				SrcT result;
				if (ForIf<GetTypeListLength<List>::Value / 2, UnmapFunctor>::Do(val, ref(result)))
					return DefaultValue::GetValue();
				return result;
			}

			static bool HasMapping(SrcT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 1>::ValueT DefaultValue;

				bool result = false;
				if (ForIf<GetTypeListLength<List>::Value / 2, HasMappingFunctor>::Do(val, ref(result)))
					result = !SameType<DefaultValue, typename Mappings<DstT>::Fail>::Value;
				return result;
			}

			static bool HasBackMapping(DstT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 0>::ValueT DefaultValue;

				bool result = false;
				if (ForIf<GetTypeListLength<List>::Value / 2, HasBackMappingFunctor>::Do(val, ref(result)))
					result = !SameType<DefaultValue, typename Mappings<SrcT>::Fail>::Value;
				return result;
			}

			static void DetailCheckSizes()
			{
				CompileTimeAssert<GetTypeListLength<List>::Value % 2 == 0> ERROR_wrong_mappings_count;
				CompileTimeAssert<GetTypeListLength<Default>::Value == 2> ERROR_wrong_default_mapping;
			}
		};
	}

	template <typename Derived, typename SrcT, typename DstT>
	struct BaseValueMapper
	{
		typedef SrcT SrcType;
		typedef DstT DstType;
		typedef stingray::Detail::Mappings<SrcType> Src; // Short names are used for sake of mapping list readability: TypeList_2<Src::Value<123>, Dst::Range<1, 2, 3> >
		typedef stingray::Detail::Mappings<DstType> Dst;

		static DstType Map(SrcType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::Map(val);
		}

		static SrcType Unmap(DstType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::Unmap(val);
		}

		static bool HasMapping(SrcType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::HasMapping(val);
		}

		static bool HasBackMapping(DstType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::HasBackMapping(val);
		}
	private:
		static void Check()
		{
			typedef BaseValueMapper<Derived, SrcT, DstT> ThisT;
			CompileTimeAssert<Inherits<Derived, ThisT>::Value> ERR_DerivedDoesntInherit_BaseValueMapper;
		}
	};


	template <typename SrcT, typename DstT>
	struct IdentityMapper
	{
		typedef SrcT SrcType;
		typedef DstT DstType;
		static DstType Map(SrcType val)		{ return val; }
		static SrcType Unmap(DstType val)	{ return val; }
	};

}

#endif
<commit_msg>Added second template param to stingray::Detail::Mappings (BaseValueMapper helper) and got rid of SameType magic in HasMapping<commit_after>#ifndef STINGRAY_TOOLKIT_MAPPER_H
#define STINGRAY_TOOLKIT_MAPPER_H


#include <stingray/toolkit/reference.h>


namespace stingray
{


	namespace Detail
	{
		template<typename Src, typename Dst>
		struct MappingBase
		{
			static bool HasValue(Src from)	{ return true; }
		};

		template<typename Src, typename Dst>
		struct Mappings
		{
			template<Dst Start_, Dst Center_, Dst End_>
			struct Range : public MappingBase<Src, Dst>
			{
				static bool Fits(Dst val)
				{
					CompileTimeAssert<Start_ < End_> ERROR_start_after_end;
					return Start_ <= val && val < End_;
				}
				static Dst GetValue()	{ return Center_; }
			};

			template<Dst Preferred, Dst Other>
			struct BiValue : public MappingBase<Src, Dst>
			{
				static bool Fits(Dst val)
				{
					CompileTimeAssert<Preferred != Other> ERROR_same_values;
					return val == Preferred || val == Other;
				}
				static Dst GetValue() { return Preferred; }
			};

			template<Dst Preferred, Dst Other1, Dst Other2>
			struct TriValue : public MappingBase<Src, Dst>
			{
				static bool Fits(Dst val)		{ return val == Preferred || val == Other1 || val == Other2; }
				static Dst GetValue()	{ return Preferred; }
			};

			template<Dst Preferred, Dst Other1, Dst Other2, Dst Other3>
			struct QuadValue : public MappingBase<Src, Dst>
			{
				static bool Fits(Dst val)		{ return val == Preferred || val == Other1 || val == Other2 || val == Other3; }
				static Dst GetValue()	{ return Preferred; }
			};

			template<Dst Value_>
			struct Value : public MappingBase<Src, Dst>
			{
				static bool Fits(Dst val)		{ return val == Value_; }
				static Dst GetValue()	{ return Value_; }
			};

			struct Fail
			{
				static Dst GetValue()	{ TOOLKIT_THROW(std::runtime_error("Mapping failed!")); }
				static bool HasValue(Src from)	{ return false; }
			};
		};

		template<typename SrcT, typename DstT, typename List, typename Default>
		struct Mapper
		{
			template<int N, bool IsMapBack>
			static bool DetailDoMap(typename If<IsMapBack, DstT, SrcT>::ValueT from, typename If<IsMapBack, SrcT, DstT>::ValueT& to)
			{
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 1 : 0)>::ValueT	MappingFrom;
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 0 : 1)>::ValueT	MappingTo;
				bool result = MappingFrom::Fits(from);
				if (result)
					to = MappingTo::GetValue();
				return !result; // need to negate since ForIf continues untill gets "false" and we need to stop when found fitting mapping
			}

			template<int N, bool IsMapBack>
			static bool DetailDoHasMapping(typename If<IsMapBack, DstT, SrcT>::ValueT from, bool& res)
			{
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 1 : 0)>::ValueT	MappingFrom;
				typedef typename GetTypeListItem<List, N * 2 + (IsMapBack ? 0 : 1)>::ValueT	MappingTo;
				bool result = MappingFrom::Fits(from);
				if (result)
					res = MappingTo::HasValue(from);
				return !result; // need to negate since ForIf continues untill gets "false" and we need to stop when found fitting mapping
			}

			template<int N>
			struct MapFunctor
			{ static bool Call(SrcT from, DstT& to) { return DetailDoMap<N, false>(from, to); } };

			template<int N>
			struct UnmapFunctor
			{ static bool Call(DstT from, SrcT& to) { return DetailDoMap<N, true>(from, to); } };

			template<int N>
			struct HasMappingFunctor
			{ static bool Call(SrcT from, bool& res) { return DetailDoHasMapping<N, false>(from, res); } };

			template<int N>
			struct HasBackMappingFunctor
			{ static bool Call(DstT from, bool& res) { return DetailDoHasMapping<N, true>(from, res); } };

			static DstT Map(SrcT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 1>::ValueT DefaultValue;

				DstT result;
				if (ForIf<GetTypeListLength<List>::Value / 2, MapFunctor>::Do(val, ref(result)))
					return DefaultValue::GetValue();
				return result;
			}

			static SrcT Unmap(DstT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 0>::ValueT DefaultValue;

				SrcT result;
				if (ForIf<GetTypeListLength<List>::Value / 2, UnmapFunctor>::Do(val, ref(result)))
					return DefaultValue::GetValue();
				return result;
			}

			static bool HasMapping(SrcT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 1>::ValueT DefaultValue;

				bool result = false;
				if (ForIf<GetTypeListLength<List>::Value / 2, HasMappingFunctor>::Do(val, ref(result)))
					result = DefaultValue::HasValue(val);
				return result;
			}

			static bool HasBackMapping(DstT val)
			{
				DetailCheckSizes();
				typedef typename GetTypeListItem<Default, 0>::ValueT DefaultValue;

				bool result = false;
				if (ForIf<GetTypeListLength<List>::Value / 2, HasBackMappingFunctor>::Do(val, ref(result)))
					result = DefaultValue::HasValue(val);
				return result;
			}

			static void DetailCheckSizes()
			{
				CompileTimeAssert<GetTypeListLength<List>::Value % 2 == 0> ERROR_wrong_mappings_count;
				CompileTimeAssert<GetTypeListLength<Default>::Value == 2> ERROR_wrong_default_mapping;
			}
		};
	}

	template <typename Derived, typename SrcT, typename DstT>
	struct BaseValueMapper
	{
		typedef SrcT SrcType;
		typedef DstT DstType;
		typedef stingray::Detail::Mappings<DstType, SrcType> Src; // Short names are used for sake of mapping list readability: TypeList_2<Src::Value<123>, Dst::Range<1, 2, 3> >
		typedef stingray::Detail::Mappings<SrcType, DstType> Dst;

		static DstType Map(SrcType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::Map(val);
		}

		static SrcType Unmap(DstType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::Unmap(val);
		}

		static bool HasMapping(SrcType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::HasMapping(val);
		}

		static bool HasBackMapping(DstType val)
		{
			Check();
			typedef typename Derived::MappingsList MappingsList;
			typedef typename Derived::DefaultMapping DefaultMapping;

			return stingray::Detail::Mapper<SrcType, DstType, MappingsList, DefaultMapping>::HasBackMapping(val);
		}
	private:
		static void Check()
		{
			typedef BaseValueMapper<Derived, SrcT, DstT> ThisT;
			CompileTimeAssert<Inherits<Derived, ThisT>::Value> ERR_DerivedDoesntInherit_BaseValueMapper;
		}
	};


	template <typename SrcT, typename DstT>
	struct IdentityMapper
	{
		typedef SrcT SrcType;
		typedef DstT DstType;
		static DstType Map(SrcType val)		{ return val; }
		static SrcType Unmap(DstType val)	{ return val; }
	};

}

#endif

{'fraction_non_alphanumeric': 0.11028455871731908, 'fraction_numerical': 0.005777841976021956, 'mean_word_length': 4.1285185185185185, 'pattern_counts': {'":': 0, '<': 136, '<?xml version=': 0, '>': 130, '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': '1899578', 'n_tokens_mistral': 5509, 'n_tokens_neox': 4468, 'n_words': 1187}