-
Notifications
You must be signed in to change notification settings - Fork 1
/
SparseInteger.Conversions.cs
124 lines (111 loc) · 5.1 KB
/
SparseInteger.Conversions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
namespace Oeis.A002845
{
// Conversions to/from string and BigInteger, intended mostly for testing purposes.
// See doc comments on the other part in SparseInteger.cs.
public readonly partial struct SparseInteger
{
/// <summary>Returns a string representation of this value.</summary>
/// <remarks>
/// This method is intended mostly for testing purposes. Its implementation is delegated to
/// <see cref="BigInteger.ToString(IFormatProvider)"/> invoked with <see cref="CultureInfo.InvariantCulture"/>.
/// </remarks>
/// <exception cref="OverflowException">
/// This value is too large to be converted to <see cref="BigInteger"/>
/// </exception>
public override string ToString()
{
return ((BigInteger)this).ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts a string representation of a non-negative number to a value of type <see cref="SparseInteger"/>.
/// </summary>
/// <remarks>
/// This method is intended mostly for testing purposes. Its implementation is delegated to
/// <see cref="BigInteger.Parse(string, IFormatProvider)"/> invoked with
/// <see cref="CultureInfo.InvariantCulture"/> as the second argument.
/// </remarks>
/// <param name="s">The string representation to convert.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <c>null</c>.</exception>
/// <exception cref="FormatException"><paramref name="s"/> cannot be parsed as an integer.</exception>
/// <exception cref="OverflowException"><paramref name="s"/> represents a negative number.</exception>
public static SparseInteger Parse(string s)
{
return (SparseInteger) BigInteger.Parse(s, CultureInfo.InvariantCulture);
}
/// <summary>Converts <see cref="SparseInteger"/> to <see cref="string"/>.</summary>
/// <remarks>
/// This method is intended mostly for testing purposes. It is equivalent to <see cref="ToString"/> method.
/// </remarks>
/// <exception cref="OverflowException">
/// This value is too large to be converted to <see cref="BigInteger"/>.
/// </exception>
public static explicit operator string(SparseInteger x)
{
return x.ToString();
}
/// <summary>Converts <see cref="string"/> to <see cref="SparseInteger"/>.</summary>
/// <remarks>
/// This method is intended mostly for testing purposes. It is equivalent to <see cref="Parse"/> method.
/// </remarks>
/// <param name="s">A string to convert from.</param>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is <c>null</c>.</exception>
/// <exception cref="FormatException"><paramref name="s"/> cannot be parsed as an integer.</exception>
/// <exception cref="OverflowException"><paramref name="s"/> represents a negative number.</exception>
public static explicit operator SparseInteger(string s)
{
return Parse(s);
}
/// <summary>Converts <see cref="SparseInteger"/> to <see cref="BigInteger"/>.</summary>
/// <remarks>This method is intended mostly for testing purposes.</remarks>
/// <exception cref="OverflowException">
/// The position of the most significant bit exceeds <see cref="int.MaxValue"/>.
/// </exception>
public static explicit operator BigInteger(SparseInteger x)
{
if (x.IsSmall)
{
return x.value;
}
BigInteger result = 0;
foreach (var position in x.positions)
{
if (!position.IsSmall || position.value > int.MaxValue)
{
throw new OverflowException("The number is too large to be converted to BigInteger.");
}
result += BigInteger.Pow(2, (int) position.value);
}
return result;
}
/// <summary>Converts <see cref="BigInteger"/> to <see cref="SparseInteger"/>.</summary>
/// <remarks>This method is intended mostly for testing purposes.</remarks>
/// <exception cref="OverflowException"><paramref name="x"/> is a negative number.</exception>
public static explicit operator SparseInteger(BigInteger x)
{
if (x < 0)
{
throw new OverflowException("A negative number cannot be converted to SparseInteger.");
}
if (x <= ulong.MaxValue)
{
return (ulong) x;
}
var positions = new List<SparseInteger>();
ulong position = 0;
while (x > 0)
{
if (!x.IsEven)
{
positions.Add(position);
}
x >>= 1;
position++;
}
return new SparseInteger(positions.ToArray());
}
}
}