|
6 | 6 | using Open.Collections;
|
7 | 7 | using System;
|
8 | 8 | using System.Collections.Generic;
|
| 9 | +using System.Diagnostics.Contracts; |
9 | 10 | using System.Linq;
|
10 |
| -using System.Threading; |
11 | 11 |
|
12 | 12 | namespace Open.Arithmetic
|
13 | 13 | {
|
14 | 14 |
|
15 | 15 | public static class Triangular
|
16 | 16 | {
|
| 17 | + public static readonly short MaxInt16 = Reverse(short.MaxValue); |
| 18 | + public static readonly ushort MaxUInt16 = Reverse(ushort.MaxValue); |
| 19 | + public static readonly int MaxInt32 = Reverse(int.MaxValue); |
| 20 | + public static readonly uint MaxUInt32 = Reverse(uint.MaxValue); |
| 21 | + public static readonly long MaxInt64 = Reverse(long.MaxValue); |
| 22 | + public static readonly ulong MaxUInt64 = Reverse(ulong.MaxValue); |
17 | 23 |
|
18 |
| - public static uint Forward(uint n) |
| 24 | + /// <summary> |
| 25 | + /// Returns the total count of triangular numbers bound by n. |
| 26 | + /// </summary> |
| 27 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 28 | + /// <exception cref="ArgumentOutOfRangeException">Result will exceed maximum value for a 32 bit integer.</exception> |
| 29 | + public static int ForwardInt(int n) |
19 | 30 | {
|
| 31 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 32 | + if (n > MaxInt32) throw new ArgumentOutOfRangeException(nameof(n), n, "Result will exceed maximum value for a 32 bit integer."); |
| 33 | + Contract.EndContractBlock(); |
| 34 | + |
| 35 | + return (int)Forward(n); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Returns the total count of triangular numbers bound by n. |
| 40 | + /// </summary> |
| 41 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 42 | + /// <exception cref="ArgumentOutOfRangeException">Result will exceed maximum value for an unsigned 32 bit integer.</exception> |
| 43 | + public static uint ForwardInt(uint n) |
| 44 | + { |
| 45 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 46 | + if (n > MaxUInt32) throw new ArgumentOutOfRangeException(nameof(n), n, "Result will exceed maximum value for an unsigned 32 bit integer."); |
| 47 | + Contract.EndContractBlock(); |
| 48 | + |
| 49 | + return (uint)Forward(n); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Returns the total count of triangular numbers bound by n. |
| 54 | + /// </summary> |
| 55 | + public static ulong Forward(long n) |
| 56 | + { |
| 57 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 58 | + Contract.EndContractBlock(); |
| 59 | + |
| 60 | + return Forward((ulong)n); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Returns the total count of triangular numbers bound by n. |
| 65 | + /// </summary> |
| 66 | + /// <exception cref="ArgumentOutOfRangeException">Result will exceed maximum value for an unsigned 64 bit integer.</exception> |
| 67 | + public static ulong Forward(ulong n) |
| 68 | + { |
| 69 | + if (n > MaxUInt64) throw new ArgumentOutOfRangeException(nameof(n), n, "Result will exceed maximum value for an unsigned 64 bit integer."); |
| 70 | + Contract.EndContractBlock(); |
| 71 | + |
20 | 72 | return n * (n + 1) / 2;
|
21 | 73 | }
|
22 | 74 |
|
| 75 | + static double ReverseInternal(double n) |
| 76 | + => (Math.Sqrt(8 * n + 1) - 1) / 2; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Returns the source value (possible decimal) from an index in a triangular set. |
| 80 | + /// </summary> |
| 81 | + /// <exception cref="ArgumentOutOfRangeException">Cannot be NaN.</exception> |
| 82 | + /// <exception cref="ArgumentOutOfRangeException">Must be finite.</exception> |
| 83 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 84 | + public static double Reverse(double n) |
| 85 | + { |
| 86 | + if (double.IsNaN(n)) throw new ArgumentOutOfRangeException(nameof(n), n, "Cannot be NaN."); |
| 87 | + if (double.IsInfinity(n)) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be finite."); |
| 88 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 89 | + Contract.EndContractBlock(); |
| 90 | + |
| 91 | + return ReverseInternal(n); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Returns the source index from an index in a triangular set. |
| 96 | + /// </summary> |
| 97 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 98 | + public static ulong Reverse(ulong n) |
| 99 | + => (ulong)ReverseInternal(n); |
23 | 100 |
|
| 101 | + /// <summary> |
| 102 | + /// Returns the source index from an index in a triangular set. |
| 103 | + /// </summary> |
24 | 104 | public static uint Reverse(uint n)
|
| 105 | + => (uint)ReverseInternal(n); |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Returns the source index from an index in a triangular set. |
| 109 | + /// </summary> |
| 110 | + public static short Reverse(short n) |
| 111 | + => (short)ReverseInternal(n); |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Returns the source index from an index in a triangular set. |
| 115 | + /// </summary> |
| 116 | + public static ushort Reverse(ushort n) |
| 117 | + => (ushort)ReverseInternal(n); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Returns the source index from an index in a triangular set. |
| 121 | + /// </summary> |
| 122 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 123 | + public static long Reverse(long n) |
25 | 124 | {
|
26 |
| - return (uint)(Math.Sqrt(8 * n + 1) - 1) / 2; |
| 125 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 126 | + Contract.EndContractBlock(); |
| 127 | + |
| 128 | + return (long)ReverseInternal(n); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Returns the source index from an index in a triangular set. |
| 133 | + /// </summary> |
| 134 | + /// <exception cref="ArgumentOutOfRangeException">Must be at least zero.</exception> |
| 135 | + public static int Reverse(int n) |
| 136 | + { |
| 137 | + if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), n, "Must be at least zero."); |
| 138 | + Contract.EndContractBlock(); |
| 139 | + |
| 140 | + return (int)ReverseInternal(n); |
27 | 141 | }
|
28 | 142 |
|
29 | 143 | public static class Disperse
|
30 | 144 | {
|
31 | 145 |
|
32 | 146 | /**
|
33 |
| - * Increases the number an element based on it's index. |
| 147 | + * Increases the repeat of an element based on its index. |
| 148 | + * 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ... |
34 | 149 | * @param source
|
35 | 150 | * @returns {Enumerable<T>}
|
36 | 151 | */
|
37 | 152 | public static IEnumerable<T> Increasing<T>(IEnumerable<T> source)
|
38 |
| - { |
39 |
| - int i = 0; |
40 |
| - return source.SelectMany( |
41 |
| - c => Enumerable.Repeat(c, Interlocked.Increment(ref i))); |
42 |
| - } |
| 153 | + => source.SelectMany((c, i) => Enumerable.Repeat(c, i)); |
43 | 154 |
|
44 | 155 | /**
|
45 |
| - * Increases the count of each element for each index. |
| 156 | + * Starting with the first item, increases the size of the loop of items until there is no more. |
| 157 | + * The quantity/total of an item is lessened as the loop spreads the items out as it progresses. |
| 158 | + * Given an entire set, the first item is represented the most and the last item is represented the least. |
| 159 | + * 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, ... |
46 | 160 | * @param source
|
47 | 161 | * @returns {Enumerable<T>}
|
48 | 162 | */
|
49 | 163 | public static IEnumerable<T> Decreasing<T>(IEnumerable<T> source)
|
50 | 164 | {
|
51 | 165 | var s = source.Memoize();
|
52 |
| - int i = 0; |
53 |
| - return s.SelectMany( |
54 |
| - c => s.Take(Interlocked.Increment(ref i))); |
| 166 | + return s.SelectMany((c, i) => s.Take(i)); |
55 | 167 | }
|
| 168 | + |
| 169 | + /** |
| 170 | + * Effectively the same as Disperse.Increasing but in reverse. |
| 171 | + * Given a fininte set, calling Disperse.Increasing(sourse).Reverse() could produce the desired result as well. |
| 172 | + * 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5 |
| 173 | + * @param source |
| 174 | + * @returns {Enumerable<T>} |
| 175 | + */ |
| 176 | + public static IEnumerable<T> Descending<T>(IReadOnlyList<T> source) |
| 177 | + => source.SelectMany((c, i) => Enumerable.Repeat(c, source.Count - i)); |
56 | 178 | }
|
57 | 179 | }
|
58 | 180 |
|
|
0 commit comments