<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace Gablarski
{
	public class IdentifyingTypes
	{
		public IdentifyingTypes (Type userIdType, Type channelIdType)
		{
			if (userIdType == null)
				throw new ArgumentNullException ("userIdType");
			if (channelIdType == null)
				throw new ArgumentNullException ("channelIdType");

			this.UserIdType = userIdType;
			this.WriteUser = GetSerializationMethod (this.UserIdType);
			this.ReadUser = GetDeserializationMethod (this.UserIdType);

			this.ChannelIdType = channelIdType;
			this.WriteChannel = GetSerializationMethod (this.ChannelIdType);
			this.ReadChannel = GetDeserializationMethod (this.ChannelIdType);
		}

		public readonly Action<IValueWriter, object> WriteUser;
		public readonly Func<IValueReader, object> ReadUser;

		/// <summary>
		/// Gets the user identifying type.
		/// </summary>
		public Type UserIdType
		{
			get;
			private set;
		}

		public readonly Action<IValueWriter, object> WriteChannel;
		public readonly Func<IValueReader, object> ReadChannel;

		/// <summary>
		/// Gets the channel identifying type.
		/// </summary>
		public Type ChannelIdType
		{
			get;
			private set;
		}

		internal static Action<IValueWriter, object> GetSerializationMethod (Type idType)
		{
			if (typeof(Guid) == idType)
				return (w, o) => w.WriteBytes (((Guid)o).ToByteArray());

			var method = (from mi in typeof (IValueWriter).GetMethods()
							let param = mi.GetParameters()
							where param.Length == 1 && param[0].ParameterType == idType
							select mi).FirstOrDefault();

			if (method == null)
				throw new ArgumentException("No suitable serializer could be found.");

			var args = new[] { typeof (IValueWriter), typeof (object) };
			var dm = new DynamicMethod ("SerializeUser", typeof(void), args);
			var gen = dm.GetILGenerator();
			gen.Emit (OpCodes.Ldarg_0);
			gen.Emit (OpCodes.Ldarg_1);		
			if (idType.IsByRef)
				gen.Emit (OpCodes.Castclass, idType);
			else
				gen.Emit (OpCodes.Unbox_Any, idType);

			gen.Emit (OpCodes.Callvirt, method);
			gen.Emit (OpCodes.Ret);

			return (Action<IValueWriter, object>)dm.CreateDelegate (typeof (Action<IValueWriter, object>));
		}

		internal static Func<IValueReader, object> GetDeserializationMethod (Type idType)
		{
			if (typeof(Guid) == idType)
				return r => new Guid (r.ReadBytes());

			var method = typeof (IValueReader).GetMethods().Where (m => m.ReturnType == idType).FirstOrDefault();

			if (method == null)
				throw new ArgumentException("No suitable deserializer could be found.");

			var args = new[] { typeof (IValueReader) };
			var dm = new DynamicMethod ("SerializeUser", typeof(object), args);
			var gen = dm.GetILGenerator();
			gen.Emit (OpCodes.Ldarg_0);
			gen.Emit (OpCodes.Callvirt, method);
			gen.Emit (OpCodes.Box, idType);
			gen.Emit (OpCodes.Stloc_0);
			//gen.Emit (OpCodes.Br_S);
			gen.Emit (OpCodes.Ldloc_0);
			gen.Emit (OpCodes.Ret);

			return (Func<IValueReader, object>)dm.CreateDelegate (typeof (Func<IValueReader, object>));
		}
	}
}<commit_msg> * Fixed IdentifyingTypes serialization<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace Gablarski
{
	public class IdentifyingTypes
	{
		public IdentifyingTypes (Type userIdType, Type channelIdType)
		{
			if (userIdType == null)
				throw new ArgumentNullException ("userIdType");
			if (channelIdType == null)
				throw new ArgumentNullException ("channelIdType");

			this.UserIdType = userIdType;
			this.WriteUser = GetSerializationMethod (this.UserIdType);
			this.ReadUser = GetDeserializationMethod (this.UserIdType);

			this.ChannelIdType = channelIdType;
			this.WriteChannel = GetSerializationMethod (this.ChannelIdType);
			this.ReadChannel = GetDeserializationMethod (this.ChannelIdType);
		}

		public readonly Action<IValueWriter, object> WriteUser;
		public readonly Func<IValueReader, object> ReadUser;

		/// <summary>
		/// Gets the user identifying type.
		/// </summary>
		public Type UserIdType
		{
			get;
			private set;
		}

		public readonly Action<IValueWriter, object> WriteChannel;
		public readonly Func<IValueReader, object> ReadChannel;

		/// <summary>
		/// Gets the channel identifying type.
		/// </summary>
		public Type ChannelIdType
		{
			get;
			private set;
		}

		internal static Action<IValueWriter, object> GetSerializationMethod (Type idType)
		{
			if (typeof(Guid) == idType)
				return (w, o) => w.WriteBytes (((Guid)o).ToByteArray());

			var method = (from mi in typeof (IValueWriter).GetMethods()
							let param = mi.GetParameters()
							where param.Length == 1 && param[0].ParameterType == idType
							select mi).FirstOrDefault();

			if (method == null)
				throw new ArgumentException("No suitable serializer could be found.");

			var args = new[] { typeof (IValueWriter), typeof (object) };
			var dm = new DynamicMethod ("SerializeUser", typeof(void), args);
			var gen = dm.GetILGenerator();
			var value = gen.DeclareLocal (idType);

			Label call = gen.DefineLabel();
			Label def = gen.DefineLabel();

			// Check if value is null
			gen.Emit (OpCodes.Ldarg_1);
			gen.Emit (OpCodes.Brfalse, def);

			// Use the default value
			gen.MarkLabel (def);
			gen.Emit (OpCodes.Ldarg_0);
			
			if (idType.IsClass)
				gen.Emit (OpCodes.Ldnull);
			else
			{
				gen.Emit (OpCodes.Ldloca_S, value);
				gen.Emit (OpCodes.Initobj, idType);
				gen.Emit (OpCodes.Ldloca_S, value);
				gen.Emit (OpCodes.Ldobj, idType);
			}

			gen.Emit (OpCodes.Br, call);

			// Use the supplied value
			gen.Emit (OpCodes.Ldarg_0);
			gen.Emit (OpCodes.Ldarg_1);
			if (idType.IsByRef)
				gen.Emit (OpCodes.Castclass, idType);
			else
				gen.Emit (OpCodes.Unbox_Any, idType);

			// Call the serialization
			gen.MarkLabel (call);
			gen.Emit (OpCodes.Callvirt, method);
			gen.Emit (OpCodes.Ret);

			return (Action<IValueWriter, object>)dm.CreateDelegate (typeof (Action<IValueWriter, object>));
		}

		internal static Func<IValueReader, object> GetDeserializationMethod (Type idType)
		{
			if (typeof(Guid) == idType)
				return r => new Guid (r.ReadBytes());

			var method = typeof (IValueReader).GetMethods().Where (m => m.ReturnType == idType).FirstOrDefault();

			if (method == null)
				throw new ArgumentException("No suitable deserializer could be found.");

			var args = new[] { typeof (IValueReader) };
			var dm = new DynamicMethod ("SerializeUser", typeof(object), args);
			var gen = dm.GetILGenerator();
			gen.Emit (OpCodes.Ldarg_0);
			gen.Emit (OpCodes.Callvirt, method);
			gen.Emit (OpCodes.Box, idType);
			gen.Emit (OpCodes.Ret);

			return (Func<IValueReader, object>)dm.CreateDelegate (typeof (Func<IValueReader, object>));
		}
	}
}
{'fraction_non_alphanumeric': 0.12079876248066376, 'fraction_numerical': 0.0019687807621994093, 'mean_word_length': 3.292094146047073, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 37, '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': '2107936', 'n_tokens_mistral': 2594, 'n_tokens_neox': 2272, 'n_words': 560}