<commit_before>﻿/*
 * TinyReflectiveToolkit
 * Copyright (C) 2014  <NAME>
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using EnumerableExtensions;

namespace TinyReflectiveToolkit.Contracts
{
    /// <summary>
    /// This class exposes various special operations of the runtime as static methods.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class SpecialOperations
    {
        internal static MethodInfo IdentityMarkerMethodInfo
        {
            get { return typeof (SpecialOperations).GetMethods().Single(m => m.Name == "IdentityMarker"); }
        }

        internal static MethodInfo GetSpecialConversion(Type source, Type target)
        {
            var matches = typeof (SpecialOperations).GetMethods()
                .WithAttribute<SpecialConversionAttribute>()
                .Where(x => x.GetParameters().Count() == 1)
                .Where(x => x.GetParameters().First().ParameterType == source)
                .Where(x => x.ReturnType == target)
                .ToList();
            return matches.SingleOrDefault();
        }

        internal static MethodInfo GetSpecialOperator(Type operatorMarker, Type input1, Type input2, Type output, bool reverse)
        {
            var matches = typeof (SpecialOperations).GetMethods()
                .WithAttribute<SpecialOperatorAttribute>(x => x.Type == operatorMarker)
                .Where(x => x.GetParameters().Count() == 2)
                .Where(x => !reverse && x.GetParameters().First().ParameterType == input1
                    , x => reverse && x.GetParameters().First().ParameterType.IsAssignableFrom(input2)
                    )
                .Where(x => !reverse && x.GetParameters().Second().ParameterType.IsAssignableFrom(input2)
                    , x => reverse && x.GetParameters().Second().ParameterType == input1
                    )
                .Where(x => x.ReturnType == output)
                .ToList();
            return matches.FirstOrDefault();
        }
        
        /// <summary>
        /// Empty method, marking the need for a runtime-generated (non-generic) identity function.
        /// </summary>
        public static void IdentityMarker()
        {            
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int FloatToInt(float input)
        {
            return (int)input;
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int DoubleToInt(double input)
        {
            return (int) input;
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int ByteToInt(byte input)
        {
            return input;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, string str2)
        {
            return string.Concat(str1, str2);
        }
        
        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(int str1, string str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, int str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(object str1, string str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, object str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static int Add(int a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static float Add(int a, float b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static float Add(float a, int b)
        {
            return a + b;
        }
    }

    internal static class OtherWhere
    {
        public static IEnumerable<T> Where<T>(this IEnumerable<T> sequence, Func<T, bool> predicate1,
            Func<T, bool> predicate2)
        {
            return sequence.Where(x => predicate1.Invoke(x) || predicate2.Invoke(x));
        } 
    }
}<commit_msg>Added a few more special cases.<commit_after>﻿/*
 * TinyReflectiveToolkit
 * Copyright (C) 2014  <NAME>
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using EnumerableExtensions;

namespace TinyReflectiveToolkit.Contracts
{
    /// <summary>
    /// This class exposes various special operations of the runtime as static methods.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class SpecialOperations
    {
        internal static MethodInfo IdentityMarkerMethodInfo
        {
            get { return typeof (SpecialOperations).GetMethods().Single(m => m.Name == "IdentityMarker"); }
        }

        internal static MethodInfo GetSpecialConversion(Type source, Type target)
        {
            var matches = typeof (SpecialOperations).GetMethods()
                .WithAttribute<SpecialConversionAttribute>()
                .Where(x => x.GetParameters().Count() == 1)
                .Where(x => x.GetParameters().First().ParameterType == source)
                .Where(x => x.ReturnType == target)
                .ToList();
            return matches.SingleOrDefault();
        }

        internal static MethodInfo GetSpecialOperator(Type operatorMarker, Type input1, Type input2, Type output, bool reverse)
        {
            var matches = typeof (SpecialOperations).GetMethods()
                .WithAttribute<SpecialOperatorAttribute>(x => x.Type == operatorMarker)
                .Where(x => x.GetParameters().Count() == 2)
                .Where(x => !reverse && x.GetParameters().First().ParameterType == input1
                    , x => reverse && x.GetParameters().First().ParameterType.IsAssignableFrom(input2)
                    )
                .Where(x => !reverse && x.GetParameters().Second().ParameterType.IsAssignableFrom(input2)
                    , x => reverse && x.GetParameters().Second().ParameterType == input1
                    )
                .Where(x => x.ReturnType == output)
                .ToList();
            return matches.FirstOrDefault();
        }
        
        /// <summary>
        /// Empty method, marking the need for a runtime-generated (non-generic) identity function.
        /// </summary>
        public static void IdentityMarker()
        {            
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int FloatToInt(float input)
        {
            return (int)input;
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int DoubleToInt(double input)
        {
            return (int) input;
        }

        /// <summary>
        /// Special cast.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SpecialConversion]
        public static int ByteToInt(byte input)
        {
            return input;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, string str2)
        {
            return string.Concat(str1, str2);
        }
        
        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(int str1, string str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, int str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(object str1, string str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static string Concat(string str1, object str2)
        {
            return string.Concat(str1, str2);
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static int Add(int a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static float Add(int a, float b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static float Add(float a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static double Add(double a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static double Add(int a, double b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static int Add(int a, byte b)
        {
            return a + b;
        }

        /// <summary>
        /// Special operator.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        [SpecialOperator(typeof(AdditionAttribute))]
        public static int Add(byte a, int b)
        {
            return a + b;
        }
    }

    internal static class OtherWhere
    {
        public static IEnumerable<T> Where<T>(this IEnumerable<T> sequence, Func<T, bool> predicate1,
            Func<T, bool> predicate2)
        {
            return sequence.Where(x => predicate1.Invoke(x) || predicate2.Invoke(x));
        } 
    }
}
{'fraction_non_alphanumeric': 0.14581879100935363, 'fraction_numerical': 0.006980315510261063, 'mean_word_length': 1.6158480920211795, 'pattern_counts': {'":': 0, '<': 223, '<?xml version=': 0, '>': 247, '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': '2008208', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3680, 'n_words': 871}