This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ConcurrentBatchAdder : IIntegerAdder | |
| { | |
| private const int degreeOfParallelism = 4; | |
| public int Add(int[] values) | |
| { | |
| var countPerSegment = values.Length / degreeOfParallelism + 1; | |
| int[,] segments = SplitIntoSegments(values, countPerSegment); | |
| var sums = new int[degreeOfParallelism]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ParallelAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| var queue = new ConcurrentQueue<int>(values); | |
| int overallSum = 0; | |
| Action addFromQueue = () => | |
| { | |
| int localSum = 0; | |
| while (queue.TryDequeue(out int localValue)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class QueuingAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| var queue = new Queue<int>(values); | |
| var sum = 0; | |
| while (queue.TryDequeue(out int value)) | |
| { | |
| sum += value; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class ExtensionMethods | |
| { | |
| public static bool TryDequeue<T>(this Queue<T> queue, out T result) | |
| { | |
| result = default; | |
| if (queue == null || !queue.Any()) | |
| { | |
| return false; | |
| } | |
| result = queue.Dequeue(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal unsafe class PointerHeadTailAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| fixed (int* head = values) | |
| { | |
| return AddImplementation(head, values.Length); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqHeadTailAdder : IIntegerAdder | |
| { | |
| private const int maxSupportedLength = 10000; | |
| public int Add(int[] values) | |
| { | |
| if (values.Length > maxSupportedLength) | |
| { | |
| throw new NotSupportedException($"Max supported length is {maxSupportedLength}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ArrayHeadTailAdder : IIntegerAdder | |
| { | |
| //This will fail with large arrays | |
| public int Add(int[] values) | |
| { | |
| var (head, tail) = GetHeadAndTail(values); | |
| return AddImplentation(head, tail); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let addInts ints = | |
| let rec loop list acc = | |
| match list with | |
| | head :: tail -> loop tail (acc + head) | |
| | [] -> acc | |
| loop ints 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqSumAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) => values.Sum(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqAggregateAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) => values.Aggregate((sum, val) => sum += val); | |
| } |
NewerOlder