Skip to content

Commit 30250bd

Browse files
Added DataSize implementation (#31)
1 parent 9a0a844 commit 30250bd

15 files changed

+2687
-15
lines changed

OnixLabs.Core.UnitTests/Units/DataSizeTests.cs

Lines changed: 835 additions & 0 deletions
Large diffs are not rendered by default.

OnixLabs.Core/Numerics/GenericMath.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Numerics;
16+
17+
namespace OnixLabs.Core.Numerics;
18+
19+
/// <summary>
20+
/// Provides generic mathematical functions.
21+
/// </summary>
22+
internal static class GenericMath
23+
{
24+
/// <summary>
25+
/// Calculates the power of the specified value, raised by the specified exponent.
26+
/// </summary>
27+
/// <param name="value">The value to raise to the power of the specified exponent.</param>
28+
/// <param name="exponent">The specified exponent to raise the value by.</param>
29+
/// <typeparam name="T">The underlying <see cref="INumber{TSelf}"/> type.</typeparam>
30+
/// <returns>Returns the power of the specified value, raised by the specified exponent.</returns>
31+
public static T Pow<T>(T value, int exponent) where T : INumber<T>
32+
{
33+
if (exponent == 0) return T.One;
34+
if (exponent == 1 || value == T.One) return value;
35+
36+
T result = value;
37+
int count = int.Abs(exponent);
38+
39+
while (--count > 0) result *= value;
40+
41+
return exponent > 1 ? result : T.One / result;
42+
}
43+
}

OnixLabs.Core/OnixLabs.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@
5151
</ItemGroup>
5252

5353

54+
5455
</Project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Core.Units;
16+
17+
public readonly partial struct DataSize<T>
18+
{
19+
/// <summary>
20+
/// Computes the sum of the specified <see cref="DataSize{T}"/> values.
21+
/// </summary>
22+
/// <param name="left">The left-hand value to add to.</param>
23+
/// <param name="right">The right-hand value to add.</param>
24+
/// <returns>Returns the sum of the specified <see cref="DataSize{T}"/> values.</returns>
25+
public static DataSize<T> Add(DataSize<T> left, DataSize<T> right)
26+
{
27+
return new DataSize<T>(left.Bits + right.Bits);
28+
}
29+
30+
/// <summary>
31+
/// Computes the difference between the specified <see cref="DataSize{T}"/> values.
32+
/// </summary>
33+
/// <param name="left">The left-hand value to subtract from.</param>
34+
/// <param name="right">The right-hand value to subtract.</param>
35+
/// <returns>Returns the difference between the specified <see cref="DataSize{T}"/> values.</returns>
36+
public static DataSize<T> Subtract(DataSize<T> left, DataSize<T> right)
37+
{
38+
return new DataSize<T>(left.Bits - right.Bits);
39+
}
40+
41+
/// <summary>
42+
/// Computes the product of the specified <see cref="DataSize{T}"/> values.
43+
/// </summary>
44+
/// <param name="left">The left-hand value to multiply.</param>
45+
/// <param name="right">The right-hand value to multiply by.</param>
46+
/// <returns>Returns the product of the specified <see cref="DataSize{T}"/> values.</returns>
47+
public static DataSize<T> Multiply(DataSize<T> left, DataSize<T> right)
48+
{
49+
return new DataSize<T>(left.Bits * right.Bits);
50+
}
51+
52+
/// <summary>
53+
/// Computes the quotient of the specified <see cref="DataSize{T}"/> values.
54+
/// </summary>
55+
/// <param name="left">The left-hand value to divide.</param>
56+
/// <param name="right">The right-hand value to divide by.</param>
57+
/// <returns>Returns the quotient of the specified <see cref="DataSize{T}"/> values.</returns>
58+
public static DataSize<T> Divide(DataSize<T> left, DataSize<T> right)
59+
{
60+
return new DataSize<T>(left.Bits / right.Bits);
61+
}
62+
63+
/// <summary>
64+
/// Computes the sum of the specified <see cref="DataSize{T}"/> values.
65+
/// </summary>
66+
/// <param name="left">The left-hand value to add to.</param>
67+
/// <param name="right">The right-hand value to add.</param>
68+
/// <returns>Returns the sum of the specified <see cref="DataSize{T}"/> values.</returns>
69+
public static DataSize<T> operator +(DataSize<T> left, DataSize<T> right)
70+
{
71+
return Add(left, right);
72+
}
73+
74+
/// <summary>
75+
/// Computes the difference between the specified <see cref="DataSize{T}"/> values.
76+
/// </summary>
77+
/// <param name="left">The left-hand value to subtract from.</param>
78+
/// <param name="right">The right-hand value to subtract.</param>
79+
/// <returns>Returns the difference between the specified <see cref="DataSize{T}"/> values.</returns>
80+
public static DataSize<T> operator -(DataSize<T> left, DataSize<T> right)
81+
{
82+
return Subtract(left, right);
83+
}
84+
85+
/// <summary>
86+
/// Computes the product of the specified <see cref="DataSize{T}"/> values.
87+
/// </summary>
88+
/// <param name="left">The left-hand value to multiply.</param>
89+
/// <param name="right">The right-hand value to multiply by.</param>
90+
/// <returns>Returns the product of the specified <see cref="DataSize{T}"/> values.</returns>
91+
public static DataSize<T> operator *(DataSize<T> left, DataSize<T> right)
92+
{
93+
return Multiply(left, right);
94+
}
95+
96+
/// <summary>
97+
/// Computes the quotient of the specified <see cref="DataSize{T}"/> values.
98+
/// </summary>
99+
/// <param name="left">The left-hand value to divide.</param>
100+
/// <param name="right">The right-hand value to divide by.</param>
101+
/// <returns>Returns the quotient of the specified <see cref="DataSize{T}"/> values.</returns>
102+
public static DataSize<T> operator /(DataSize<T> left, DataSize<T> right)
103+
{
104+
return Divide(left, right);
105+
}
106+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace OnixLabs.Core.Units;
18+
19+
public readonly partial struct DataSize<T> : IComparable<DataSize<T>>, IComparable
20+
{
21+
/// <summary>
22+
/// Compares two <see cref="DataSize{T}"/> values and returns an integer that indicates
23+
/// whether the left-hand value is less than, equal to, or greater than the right-hand value.
24+
/// </summary>
25+
/// <param name="left">The left-hand value to compare.</param>
26+
/// <param name="right">The right-hand value to compare.</param>
27+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
28+
public static int Compare(DataSize<T> left, DataSize<T> right)
29+
{
30+
return left.Bits.CompareTo(right.Bits);
31+
}
32+
33+
/// <summary>
34+
/// Compares the current instance with another object of the same type and returns an integer that indicates
35+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
36+
/// other object.
37+
/// </summary>
38+
/// <param name="other">An object to compare with this instance.</param>
39+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
40+
public int CompareTo(DataSize<T> other)
41+
{
42+
return Compare(this, other);
43+
}
44+
45+
/// <summary>
46+
/// Compares the current instance with another object of the same type and returns an integer that indicates
47+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
48+
/// other object.
49+
/// </summary>
50+
/// <param name="obj">An object to compare with this instance.</param>
51+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
52+
public int CompareTo(object? obj)
53+
{
54+
return this.CompareObject(obj);
55+
}
56+
57+
/// <summary>
58+
/// Performs a greater than comparison check between two <see cref="DataSize{T}"/> values.
59+
/// </summary>
60+
/// <param name="left">The left-hand value to compare.</param>
61+
/// <param name="right">The right-hand value to compare.</param>
62+
/// <returns>Returns true if the left-hand operand is greater than right-hand operand; otherwise, false.</returns>
63+
public static bool operator >(DataSize<T> left, DataSize<T> right)
64+
{
65+
return Compare(left, right) is 1;
66+
}
67+
68+
/// <summary>
69+
/// Performs a greater than or equal comparison check between two <see cref="DataSize{T}"/> values.
70+
/// </summary>
71+
/// <param name="left">The left-hand value to compare.</param>
72+
/// <param name="right">The right-hand value to compare.</param>
73+
/// <returns>Returns true if the left-hand operand is greater than or equal to right-hand operand; otherwise, false.</returns>
74+
public static bool operator >=(DataSize<T> left, DataSize<T> right)
75+
{
76+
return Compare(left, right) is 1 or 0;
77+
}
78+
79+
/// <summary>
80+
/// Performs a less than comparison check between two <see cref="DataSize{T}"/> values.
81+
/// </summary>
82+
/// <param name="left">The left-hand value to compare.</param>
83+
/// <param name="right">The right-hand value to compare.</param>
84+
/// <returns>Returns true if the left-hand operand is less than right-hand operand; otherwise, false.</returns>
85+
public static bool operator <(DataSize<T> left, DataSize<T> right)
86+
{
87+
return Compare(left, right) is -1;
88+
}
89+
90+
/// <summary>
91+
/// Performs a less than or equal comparison check between two <see cref="DataSize{T}"/> values.
92+
/// </summary>
93+
/// <param name="left">The left-hand value to compare.</param>
94+
/// <param name="right">The right-hand value to compare.</param>
95+
/// <returns>Returns true if the left-hand operand is less than or equal to right-hand operand; otherwise, false.</returns>
96+
public static bool operator <=(DataSize<T> left, DataSize<T> right)
97+
{
98+
return Compare(left, right) is -1 or 0;
99+
}
100+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Core.Units;
16+
17+
public readonly partial struct DataSize<T>
18+
{
19+
/// <summary>
20+
/// Represents a zero-bit <see cref="DataSize{T}"/> value.
21+
/// </summary>
22+
public static DataSize<T> Zero => default;
23+
24+
/// <summary>
25+
/// Gets the number of bits per nibble.
26+
/// </summary>
27+
private static T BitsPerNibble => T.CreateChecked(4);
28+
29+
/// <summary>
30+
/// Gets the number of bits per byte.
31+
/// </summary>
32+
private static T BitsPerByte => T.CreateChecked(8);
33+
34+
/// <summary>
35+
/// Gets the number of bits per word.
36+
/// </summary>
37+
private static T BitsPerWord => T.CreateChecked(16);
38+
39+
/// <summary>
40+
/// Gets the number of bits per double-word.
41+
/// </summary>
42+
private static T BitsPerDoubleWord => T.CreateChecked(32);
43+
44+
/// <summary>
45+
/// Gets the number of bits per quad-word.
46+
/// </summary>
47+
private static T BitsPerQuadWord => T.CreateChecked(64);
48+
49+
/// <summary>
50+
/// Gets the value of a binary thousand.
51+
/// </summary>
52+
private static T BinaryThousand => T.CreateChecked(1024);
53+
54+
/// <summary>
55+
/// Gets the value of a decimal thousand.
56+
/// </summary>
57+
private static T DecimalThousand => T.CreateChecked(1000);
58+
59+
/// <summary>
60+
/// Gets the default format.
61+
/// </summary>
62+
private const string DefaultFormat = "b";
63+
}

0 commit comments

Comments
 (0)