<commit_before>﻿using System;

namespace ConvNetSharp.Volume
{
    public abstract class VolumeStorage<T> where T : struct, IEquatable<T>, IFormattable
    {
        protected VolumeStorage(Shape shape)
        {
            this.Shape = new Shape(shape);
        }

        public Shape Shape { get; set; }

        public abstract void Clear();

        public abstract void CopyFrom(VolumeStorage<T> source);

        public abstract T Get(int[] coordinates);

        public abstract T Get(int w, int h, int c, int n);

        public abstract T Get(int w, int h, int c);

        public abstract T Get(int w, int h);

        public abstract T Get(int i);

        public void Map(Func<T, T, T> f, VolumeStorage<T> other, VolumeStorage<T> result)
        {
            for (var i = 0; i < this.Shape.TotalLength; i++)
            {
                result.Set(i, f(Get(i), other.Get(i)));
            }
        }

        public void Map(Func<T, T> f, VolumeStorage<T> result)
        {
            for (var i = 0; i < this.Shape.TotalLength; i++)
            {
                result.Set(i, f(Get(i)));
            }
        }

        /// <summary>
        /// Implement broadcast
        /// </summary>
        public void MapEx(Func<T, T, T> f, VolumeStorage<T> other, VolumeStorage<T> result)
        {
            var big = this;
            var small = other;

            if (small.Shape.TotalLength > big.Shape.TotalLength)
            {
                big = other;
                small = this;
            }
            else if (small.Shape.TotalLength == big.Shape.TotalLength)
            {
                if (!small.Shape.Equals(big.Shape))
                {
                    throw new ArgumentException("Volumes have the same total number of dimensions but have different shapes");
                }
            }

            var w = big.Shape.GetDimension(0);
            var h = big.Shape.GetDimension(1);<commit_msg>Should improve CPU perf when no broadcast is required<commit_after>﻿using System;

namespace ConvNetSharp.Volume
{
    public abstract class VolumeStorage<T> where T : struct, IEquatable<T>, IFormattable
    {
        protected VolumeStorage(Shape shape)
        {
            this.Shape = new Shape(shape);
        }

        public Shape Shape { get; set; }

        public abstract void Clear();

        public abstract void CopyFrom(VolumeStorage<T> source);

        public abstract T Get(int[] coordinates);

        public abstract T Get(int w, int h, int c, int n);

        public abstract T Get(int w, int h, int c);

        public abstract T Get(int w, int h);

        public abstract T Get(int i);

        public void Map(Func<T, T, T> f, VolumeStorage<T> other, VolumeStorage<T> result)
        {
            for (var i = 0; i < this.Shape.TotalLength; i++)
            {
                result.Set(i, f(Get(i), other.Get(i)));
            }
        }

        public void Map(Func<T, T> f, VolumeStorage<T> result)
        {
            for (var i = 0; i < this.Shape.TotalLength; i++)
            {
                result.Set(i, f(Get(i)));
            }
        }

        /// <summary>
        /// Implement broadcast
        /// </summary>
        public void MapEx(Func<T, T, T> f, VolumeStorage<T> other, VolumeStorage<T> result)
        {
            var big = this;
            var small = other;

            if (small.Shape.TotalLength > big.Shape.TotalLength)
            {
                big = other;
                small = this;
            }
            else if (small.Shape.TotalLength == big.Shape.TotalLength)
            {
                if (!small.Shape.Equals(big.Shape))
                {
                    throw new ArgumentException("Volumes have the same total number of dimensions but have different shapes");
                }

                // No broadcast to do here -> we switch to non-broacast implem
                this.Map(f, other, result);
                return;
            }

            var w = big.Shape.GetDimension(0);
            var h = big.Shape.GetDimension(1);
{'fraction_non_alphanumeric': 0.10598626104023552, 'fraction_numerical': 0.001962708537782139, 'mean_word_length': 1.537025513378967, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 32, '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': '1873357', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1094, 'n_words': 339}