├── Banner.png
├── CODE-OF-CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE.md
├── README.md
├── SECURITY.md
├── docs
├── .gitignore
├── Docs.sln
└── Jodosoft.Libraries.Docs
│ ├── ContentLayout.content
│ ├── Jodosoft.Libraries.Docs.shfbproj
│ ├── Rename-Html-Folder.ps1
│ ├── content
│ └── welcome.aml
│ └── icons
│ ├── Icon.png
│ └── favicon.ico
└── src
├── .editorconfig
├── .gitignore
├── AssemblyInfo.cs
├── Benchmarks.props
├── Directory.Build.props
├── Jodosoft.Benchmarking
├── Benchmark.cs
├── BenchmarkAttribute.cs
├── BenchmarkBuilder.cs
├── BenchmarkResult.cs
├── BenchmarkWriter.cs
├── Count.cs
├── Counter.cs
├── Jodosoft.Benchmarking.csproj
└── Program.cs
├── Jodosoft.Collections.Benchmarks
└── Jodosoft.Collections.Benchmarks.csproj
├── Jodosoft.Collections.Tests
└── Jodosoft.Collections.Tests.csproj
├── Jodosoft.Collections
├── Array2.cs
├── DictionaryExtensions.cs
├── DictionaryLookup.cs
├── IReadOnlyArray2.cs
├── IReadOnlyLookup.cs
├── Jodosoft.Collections.csproj
├── Lookup.cs
├── LookupExtensions.cs
├── README.md
├── SortedDictionaryLookup.cs
└── TAGS.md
├── Jodosoft.Geometry.Benchmarks
├── GeometryBenchmarks.cs
└── Jodosoft.Geometry.Benchmarks.csproj
├── Jodosoft.Geometry.Tests
├── AARectangleNIntegralTestBase.cs
├── AARectangleNTestBase.cs
├── AARectangleNTests.cs
├── AngleNTestBase.cs
├── AngleNTests.cs
├── CircleNTestBase.cs
├── CircleNTests.cs
├── Jodosoft.Geometry.Tests.csproj
├── RectangleNTestBase.cs
├── RectangleNTests.cs
├── TriangleNTestBase.cs
└── TriangleNTests.cs
├── Jodosoft.Geometry
├── AARectangleN.cs
├── AARectangleNExtensions.cs
├── AngleN.cs
├── AngleNExtensions.cs
├── CircleN.cs
├── CompatibilitySuppressions.xml
├── Jodosoft.Geometry.csproj
├── README.md
├── RectangleN.cs
├── RectangleNExtensions.cs
├── TAGS.md
├── TriangleN.cs
└── VectorExtensions.cs
├── Jodosoft.Numerics.Benchmarks
├── Jodosoft.Numerics.Benchmarks.csproj
├── NumericsBenchmarks.cs
└── Properties
│ └── launchSettings.json
├── Jodosoft.Numerics.Tests
├── ByteMTests.cs
├── ByteNTests.cs
├── CheckedNumericConversionTestBase.cs
├── CheckedNumericTestBase.cs
├── ClampedTests.cs
├── ConvertNTests.cs
├── DecimalMTests.cs
├── DecimalNTests.cs
├── DoubleMTests.cs
├── DoubleNTests.cs
├── Fix64MTests.cs
├── Fix64Tests.cs
├── FluentAssertionsExtensions.cs
├── Int16MTests.cs
├── Int16NTests.cs
├── Int32MTests.cs
├── Int32NTests.cs
├── Int64MTests.cs
├── Int64NTests.cs
├── Jodosoft.Numerics.Tests.csproj
├── NumericBitConverterTestBase.cs
├── NumericCastTestBase.cs
├── NumericConversionConsistencyTestBase.cs
├── NumericConvertTestBase.cs
├── NumericFixedPointTestBase.cs
├── NumericFloatingPointTestBase.cs
├── NumericInfinityTestBase.cs
├── NumericIntegralTestBase.cs
├── NumericMathErrorNaNTestBase.cs
├── NumericMathTestBase.cs
├── NumericNaNTestBase.cs
├── NumericNonFloatingPointTestBase.cs
├── NumericNonInfinityTestBase.cs
├── NumericNonNaNTestBase.cs
├── NumericRandomTestBase.cs
├── NumericRealTestBase.cs
├── NumericSignedTestBase.cs
├── NumericStringConvertTestBase.cs
├── NumericTestBase.cs
├── NumericUnsignedTestBase.cs
├── NumericWrapperIntegralTestBase.cs
├── NumericWrapperTestBase.cs
├── RandomExtensions.cs
├── SByteMTests.cs
├── SByteNTests.cs
├── ScaledMathTests.cs
├── Showcase.cs
├── SingleMTests.cs
├── SingleNTests.cs
├── TestBounds.cs
├── TestUtilities.cs
├── UFix64MTests.cs
├── UFix64Tests.cs
├── UInt16MTests.cs
├── UInt16NTests.cs
├── UInt32MTests.cs
├── UInt32NTests.cs
├── UInt64MTests.cs
├── UInt64NTests.cs
├── UnitNTest.cs
├── UnitNTestBase.cs
├── Vector2NTestBase.cs
├── Vector2NTests.cs
├── Vector3NTestBase.cs
└── Vector3NTests.cs
├── Jodosoft.Numerics
├── BitConverterN.cs
├── BitOperations.cs
├── ByteN.cs
├── Clamped
│ ├── ByteM.cs
│ ├── Clamped.cs
│ ├── ConversionExtensions.cs
│ ├── DecimalM.cs
│ ├── DoubleM.cs
│ ├── Fix64M.cs
│ ├── Int16M.cs
│ ├── Int32M.cs
│ ├── Int64M.cs
│ ├── SByteM.cs
│ ├── SingleM.cs
│ ├── UFix64M.cs
│ ├── UInt16M.cs
│ ├── UInt32M.cs
│ └── UInt64M.cs
├── CompatibilitySuppressions.xml
├── Conversion.cs
├── ConvertN.cs
├── DecimalN.cs
├── DoubleN.cs
├── EnumerableExtensions.cs
├── Fix64.cs
├── Generation.cs
├── IConvert.cs
├── IConvertExtended.cs
├── IMath.cs
├── INumeric.cs
├── INumericBitConverter.cs
├── INumericExtended.cs
├── INumericRandom.cs
├── INumericStatic.cs
├── Int16N.cs
├── Int32N.cs
├── Int64N.cs
├── Jodosoft.Numerics.csproj
├── MathN.cs
├── Numeric.cs
├── NumericExtensions.cs
├── README.md
├── RandomExtensions.cs
├── SByteN.cs
├── Scaled.cs
├── SingleN.cs
├── TAGS.md
├── UFix64.cs
├── UInt16N.cs
├── UInt32N.cs
├── UInt64N.cs
├── UnitN.cs
├── Vector2N.cs
├── Vector3N.cs
└── VectorExtensions.cs
├── Jodosoft.Primitives.Benchmarks
└── Jodosoft.Primitives.Benchmarks.csproj
├── Jodosoft.Primitives.Tests
├── BinaryIOTestBase.cs
├── DynamicInvokeTests.cs
├── FuncExtensionsTests.cs
├── Jodosoft.Primitives.Tests.csproj
└── RandomExtensionsTests.cs
├── Jodosoft.Primitives
├── BinaryConvert.cs
├── BinaryReaderExtensions.cs
├── BinaryWriterExtensions.cs
├── BitOperations.cs
├── Compatibility
│ ├── BitConverterShim.cs
│ ├── DoubleShim.cs
│ ├── HashCodeShim.cs
│ ├── MathFShim.cs
│ ├── MathShim.cs
│ ├── SingleShim.cs
│ └── ValueTupleShim.cs
├── ConvertibleExtensions.cs
├── DefaultProvider.cs
├── DynamicInvoke.cs
├── FuncExtensions.cs
├── IBinaryIO.cs
├── IProvider.cs
├── IVariantRandom.cs
├── Jodosoft.Primitives.csproj
├── README.md
├── RandomExtensions.cs
├── StringUtilities.cs
├── TAGS.md
└── Variants.cs
├── Jodosoft.Testing.NewtonsoftJson
├── Jodosoft.Testing.NewtonsoftJson.csproj
└── JsonConvertTestBase.cs
├── Jodosoft.Testing.Tests
├── AssertSameTests.cs
└── Jodosoft.Testing.Tests.csproj
├── Jodosoft.Testing
├── AssertSame.cs
├── AssertionMethodAttribute.cs
├── FixtureExtensions.cs
├── FormattableTestBase.cs
├── GlobalFixtureBase.cs
├── Jodosoft.Testing.csproj
├── ObjectTestBase.cs
├── SerializableTestBase.cs
└── TestLocal.cs
├── Libraries.sln
├── PackageIcon.png
├── Packages.props
├── PackagesNonBreaking.props
├── TestAssemblyInfo.cs
└── Tests.props
/Banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jodosoft/Libraries/7c1e73eacaace8374bd8e6b99f50817da22708ba/Banner.png
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Contributing
2 | ====
3 |
4 | Contributions are welcome at https://github.com/Jodosoft/Libraries (the home of this repository). Contributors are requested to adhere to the [code of conduct](CODE-OF-CONDUCT.md).
5 |
6 | Please consult the [.NET Runtime guide](https://github.com/dotnet/runtime/blob/main/CONTRIBUTING.md) for a good model of how to contribute.
7 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2023 Joe Lawry-Short
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | Vulnerabilities should be reported privately to support@jodosoft.com.
4 |
--------------------------------------------------------------------------------
/docs/.gitignore:
--------------------------------------------------------------------------------
1 | bin/
2 | obj/
3 | .vs/
--------------------------------------------------------------------------------
/docs/Docs.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.2.32519.379
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{7CF6DF6D-3B04-46F8-A40B-537D21BCA0B4}") = "Jodosoft.Libraries.Docs", "Jodosoft.Libraries.Docs\Jodosoft.Libraries.Docs.shfbproj", "{22A055ED-ABAB-4BDD-8989-6EB738411445}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D824DA2D-77E1-46FB-8E51-D49AE7A7B9D8}"
9 | ProjectSection(SolutionItems) = preProject
10 | .gitignore = .gitignore
11 | CNAME = CNAME
12 | EndProjectSection
13 | EndProject
14 | Global
15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 | Debug|Any CPU = Debug|Any CPU
17 | Release|Any CPU = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
20 | {22A055ED-ABAB-4BDD-8989-6EB738411445}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {22A055ED-ABAB-4BDD-8989-6EB738411445}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {22A055ED-ABAB-4BDD-8989-6EB738411445}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {22A055ED-ABAB-4BDD-8989-6EB738411445}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {0F607B33-F092-419F-82D9-C7D2879B3ED8}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/docs/Jodosoft.Libraries.Docs/ContentLayout.content:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/docs/Jodosoft.Libraries.Docs/Rename-Html-Folder.ps1:
--------------------------------------------------------------------------------
1 | ((Get-Content -path .\bin\docs\index.html -Raw) -replace 'html/','api/') | Set-Content -Path .\bin\docs\index.html
2 | ((Get-Content -path .\bin\docs\PageNotFound.html -Raw) -replace 'html/','api/') | Set-Content -Path .\bin\docs\PageNotFound.html
3 | ((Get-Content -path .\bin\docs\search.html -Raw) -replace 'html/','api/') | Set-Content -Path .\bin\docs\search.html
4 | ((Get-Content -path .\bin\docs\fti\FTI_Files.json -Raw) -replace 'html/','api/') | Set-Content -Path .\bin\docs\fti\FTI_Files.json
5 | Rename-Item -Path .\bin\docs\html -NewName "api"
--------------------------------------------------------------------------------
/docs/Jodosoft.Libraries.Docs/content/welcome.aml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Simple, reliable .NET libraries covering numbers, geometry and data structures.
6 |
7 |
8 |
9 | Getting started
10 |
11 |
12 | For a general information, see the repository page on GitHub at
13 | https://github.com/Jodosoft/Libraries/
14 | https://github.com/Jodosoft/Libraries/
15 | .
16 |
17 |
18 | For API reference documentation, use the the table of contents to navigate each namespace.
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/docs/Jodosoft.Libraries.Docs/icons/Icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jodosoft/Libraries/7c1e73eacaace8374bd8e6b99f50817da22708ba/docs/Jodosoft.Libraries.Docs/icons/Icon.png
--------------------------------------------------------------------------------
/docs/Jodosoft.Libraries.Docs/icons/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jodosoft/Libraries/7c1e73eacaace8374bd8e6b99f50817da22708ba/docs/Jodosoft.Libraries.Docs/icons/favicon.ico
--------------------------------------------------------------------------------
/src/.gitignore:
--------------------------------------------------------------------------------
1 | bin/
2 | obj/
3 | .vs/
4 | *opencover.xml
5 | *coverage.xml
6 | *cobertura.xml
7 | *.trx
8 | TestResults/
9 | output/
10 | publish/
--------------------------------------------------------------------------------
/src/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics.CodeAnalysis;
22 | using System.Runtime.CompilerServices;
23 |
24 | [assembly: InternalsVisibleTo("Jodosoft.Benchmarking.Tests")]
25 | [assembly: InternalsVisibleTo("Jodosoft.Geometry.Benchmarks")]
26 | [assembly: InternalsVisibleTo("Jodosoft.Geometry.Tests")]
27 | [assembly: InternalsVisibleTo("Jodosoft.Collections.Benchmarks")]
28 | [assembly: InternalsVisibleTo("Jodosoft.Collections.Tests")]
29 | [assembly: InternalsVisibleTo("Jodosoft.Numerics.Benchmarks")]
30 | [assembly: InternalsVisibleTo("Jodosoft.Numerics.Tests")]
31 | [assembly: InternalsVisibleTo("Jodosoft.Testing.Tests")]
32 |
33 | [assembly: SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", MessageId = "csharpsquid", Justification = "False positive on csharpsquid suppressions")]
34 |
35 | [assembly: CLSCompliant(true)]
36 |
--------------------------------------------------------------------------------
/src/Benchmarks.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | net5.0
4 | Exe
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 | true
6 | latest
7 | true
8 | 4
9 | true
10 |
11 |
12 |
13 | HAS_VALUE_TUPLES;HAS_SPANS;HAS_DEFAULT_INTERFACE_METHODS;$(AdditionalConstants)
14 |
15 |
16 |
17 | HAS_VALUE_TUPLES;HAS_SPANS;HAS_DEFAULT_INTERFACE_METHODS;$(AdditionalConstants)
18 |
19 |
20 |
21 | HAS_VALUE_TUPLES;HAS_SPANS;HAS_DEFAULT_INTERFACE_METHODS;$(AdditionalConstants)
22 |
23 |
24 |
25 | HAS_VALUE_TUPLES;HAS_SPANS;HAS_DEFAULT_INTERFACE_METHODS;$(AdditionalConstants)
26 |
27 |
28 |
29 | HAS_VALUE_TUPLES;$(AdditionalConstants)
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/Benchmark.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics;
22 |
23 | namespace Jodosoft.Benchmarking
24 | {
25 | public class Benchmark : Benchmark
26 | {
27 | public Func DataFactory { get; }
28 | public Func Subject1 { get; }
29 | public Func Subject2 { get; }
30 |
31 | public Benchmark(string name, Func dataFactory, Func subject1, Func subject2) : base(name)
32 | {
33 | DataFactory = dataFactory ?? throw new ArgumentNullException(nameof(dataFactory));
34 | Subject2 = subject2 ?? throw new ArgumentNullException(nameof(subject2));
35 | Subject1 = subject1 ?? throw new ArgumentNullException(nameof(subject1));
36 | }
37 |
38 | public override BenchmarkResult Execute(TimeSpan duration)
39 | {
40 | (Count Subject1, Count Subject2) measurements = Counter.Measure(DataFactory, Subject1, Subject2, duration);
41 |
42 | return new BenchmarkResult(Name, measurements.Subject1, measurements.Subject2);
43 | }
44 | }
45 |
46 | public abstract class Benchmark
47 | {
48 | public string Name { get; }
49 |
50 | protected Benchmark(string name)
51 | {
52 | Name = name ?? throw new ArgumentNullException(nameof(name));
53 | }
54 |
55 | public abstract BenchmarkResult Execute(TimeSpan duration);
56 |
57 | public static BenchmarkBuilder.WithNameAndData Using(Func factory)
58 | {
59 | return new BenchmarkBuilder.WithNameAndData(new StackTrace().GetFrame(1).GetMethod().Name, factory);
60 | }
61 |
62 | public static BenchmarkBuilder.WithNameAndData UsingData(T value)
63 | {
64 | return new BenchmarkBuilder.WithNameAndData(new StackTrace().GetFrame(1).GetMethod().Name, () => value);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/BenchmarkAttribute.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics.CodeAnalysis;
22 |
23 | namespace Jodosoft.Benchmarking
24 | {
25 | [ExcludeFromCodeCoverage]
26 | [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
27 | public sealed class BenchmarkAttribute : Attribute
28 | {
29 | public string Name { get; set; }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/BenchmarkBuilder.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 |
22 | namespace Jodosoft.Benchmarking
23 | {
24 | public static class BenchmarkBuilder
25 | {
26 | public sealed class WithNameAndData
27 | {
28 | public string Name { get; }
29 | public Func DataFactory { get; }
30 |
31 | public WithNameAndData(string name, Func dataFactory)
32 | {
33 | Name = name ?? throw new ArgumentNullException(nameof(name));
34 | DataFactory = dataFactory ?? throw new ArgumentNullException(nameof(dataFactory));
35 | }
36 |
37 | public WithNameDataAndSubject1 Measure(Func subject1)
38 | {
39 | return new WithNameDataAndSubject1(Name, DataFactory, subject1);
40 | }
41 | }
42 |
43 | public class WithNameDataAndSubject1
44 | {
45 | public string Name { get; }
46 | public Func DataFactory { get; }
47 | public Func Subject1 { get; }
48 |
49 | public WithNameDataAndSubject1(string name, Func dataFactory, Func subject1)
50 | {
51 | Name = name ?? throw new ArgumentNullException(nameof(name));
52 | DataFactory = dataFactory ?? throw new ArgumentNullException(nameof(dataFactory));
53 | Subject1 = subject1 ?? throw new ArgumentNullException(nameof(subject1));
54 | }
55 |
56 | public Benchmark Versus(Func subject2)
57 | {
58 | return new Benchmark(Name, DataFactory, Subject1, subject2);
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/BenchmarkResult.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace Jodosoft.Benchmarking
24 | {
25 | public sealed class BenchmarkResult : IEquatable
26 | {
27 | public string Name { get; }
28 | public Count Subject1 { get; }
29 | public Count Subject2 { get; }
30 |
31 | public BenchmarkResult(string name, Count subject1, Count subject2)
32 | {
33 | if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Must not be null, empty or whitespace.", nameof(name));
34 | Name = name;
35 | Subject1 = subject1;
36 | Subject2 = subject2;
37 | }
38 |
39 | public override bool Equals(object obj) => Equals(obj as BenchmarkResult);
40 | public bool Equals(BenchmarkResult other) => other != null && Name == other.Name && Subject2.Equals(other.Subject2) && Subject1.Equals(other.Subject1);
41 | public override int GetHashCode() => HashCode.Combine(Name, Subject2, Subject1);
42 |
43 | public static bool operator ==(BenchmarkResult left, BenchmarkResult right) => EqualityComparer.Default.Equals(left, right);
44 | public static bool operator !=(BenchmarkResult left, BenchmarkResult right) => !(left == right);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/Count.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics.CodeAnalysis;
22 |
23 | namespace Jodosoft.Benchmarking
24 | {
25 | [ExcludeFromCodeCoverage]
26 | public sealed class Count : IEquatable
27 | {
28 | public long Executions { get; }
29 | public TimeSpan TotalTime { get; }
30 |
31 | public Count(long executions, TimeSpan totalTime)
32 | {
33 | if (executions <= 0) throw new ArgumentOutOfRangeException(nameof(executions), executions, "Must be positive.");
34 | if (totalTime <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(totalTime), totalTime, "Must be positive.");
35 |
36 | Executions = executions;
37 | TotalTime = totalTime;
38 | }
39 |
40 | public TimeSpan GetAverageTime() => new TimeSpan(TotalTime.Ticks / Executions);
41 |
42 | public double GetExecutionsPerSecond() => Executions / TotalTime.TotalSeconds;
43 |
44 | public override bool Equals(object obj) => Equals(obj as Count);
45 |
46 | public bool Equals(Count other) => other != null && Executions == other.Executions && TotalTime.Equals(other.TotalTime);
47 |
48 | public override int GetHashCode() => HashCode.Combine(Executions, TotalTime);
49 |
50 | public static bool operator ==(Count left, Count right) => left.Equals(right);
51 |
52 | public static bool operator !=(Count left, Count right) => !(left == right);
53 |
54 | public static Count operator +(Count count1, Count count2)
55 | => new Count(count1.Executions + count2.Executions, count1.TotalTime + count2.TotalTime);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/Counter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics;
22 | using System.Diagnostics.CodeAnalysis;
23 | using System.Runtime.CompilerServices;
24 |
25 | namespace Jodosoft.Benchmarking
26 | {
27 | [ExcludeFromCodeCoverage]
28 | public static class Counter
29 | {
30 | private static readonly TimeSpan TrialDuration = TimeSpan.FromSeconds(1);
31 |
32 | public static (Count Subject1, Count Subject2) Measure(
33 | Func inputFactory,
34 | Func subject1,
35 | Func subject2,
36 | TimeSpan overallDuration)
37 | {
38 | Stopwatch overallStopwatch = Stopwatch.StartNew();
39 |
40 | T input = inputFactory();
41 | Count count1 = Measure(subject1, input, TrialDuration);
42 | Count count2 = Measure(subject2, input, TrialDuration);
43 |
44 | while (overallStopwatch.Elapsed < overallDuration)
45 | {
46 | input = inputFactory();
47 | count1 = Add(count1, Measure(subject1, input, TrialDuration));
48 | count2 = Add(count2, Measure(subject2, input, TrialDuration));
49 | }
50 |
51 | return (count1, count2);
52 | }
53 |
54 | [MethodImpl(MethodImplOptions.NoOptimization)]
55 | public static Count Measure(Func subject, T input, TimeSpan duration)
56 | {
57 | object checkObj = new object();
58 | Stopwatch stopwatch = new Stopwatch();
59 | double maxTicks = duration.TotalSeconds * Stopwatch.Frequency;
60 | long iterations = 0;
61 | object obj;
62 | stopwatch.Start();
63 | do
64 | {
65 | obj = subject(input);
66 | iterations++;
67 | } while (stopwatch.ElapsedTicks < maxTicks);
68 | stopwatch.Stop();
69 | if (ReferenceEquals(obj, checkObj)) throw new InvalidOperationException();
70 | return new Count(iterations, stopwatch.Elapsed);
71 | }
72 |
73 | private static Count Add(Count measurement1, Count measurement2)
74 | {
75 | return new Count(measurement1.Executions + measurement2.Executions, measurement1.TotalTime + measurement2.TotalTime);
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/Jodosoft.Benchmarking/Jodosoft.Benchmarking.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net5.0
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections.Benchmarks/Jodosoft.Collections.Benchmarks.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections.Tests/Jodosoft.Collections.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/Array2.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 |
22 | namespace Jodosoft.Collections
23 | {
24 | public static class Array2
25 | {
26 | public static Array2 Empty() => Array2.Empty;
27 | }
28 |
29 | public sealed class Array2 : IReadOnlyArray2
30 | {
31 | public static readonly Array2 Empty = new Array2(0, 0);
32 |
33 | private readonly T[] _array;
34 |
35 | public int Length => _array.Length;
36 | public int LengthX { get; }
37 | public int LengthY { get; }
38 |
39 | public T this[int x, int y]
40 | {
41 | get => _array[(y * LengthX) + x];
42 | set => _array[(y * LengthX) + x] = value;
43 | }
44 |
45 | public Array2(int lengthX, int lengthY)
46 | {
47 | _array = new T[lengthX * lengthY];
48 | LengthX = lengthX;
49 | LengthY = lengthY;
50 | }
51 |
52 | #if HAS_SPANS
53 | public ReadOnlySpan AsSpan() => _array.AsSpan();
54 | public ReadOnlyMemory AsMemory() => _array.AsMemory();
55 | #endif
56 |
57 | public T[] ToArray()
58 | {
59 | T[]? copy = new T[_array.Length];
60 | Array.Copy(_array, copy, _array.Length);
61 | return copy;
62 | }
63 |
64 | public override bool Equals(object? obj) => _array.Equals(obj);
65 | public override int GetHashCode() => _array.GetHashCode();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/DictionaryExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System.Collections.Generic;
21 |
22 | namespace Jodosoft.Collections
23 | {
24 | public static class DictionaryExtensions
25 | {
26 | public static DictionaryLookup ToLookup(this Dictionary dictionary) where TKey : notnull
27 | => new DictionaryLookup(dictionary);
28 |
29 | public static SortedDictionaryLookup ToLookup(this SortedDictionary dictionary) where TKey : notnull
30 | => new SortedDictionaryLookup(dictionary);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/DictionaryLookup.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System.Collections.Generic;
21 |
22 | namespace Jodosoft.Collections
23 | {
24 | public sealed class DictionaryLookup : IReadOnlyLookup where TKey : notnull
25 | {
26 | private readonly Dictionary _dictionary;
27 |
28 | public DictionaryLookup(Dictionary dictionary)
29 | {
30 | _dictionary = dictionary;
31 | }
32 |
33 | public TValue this[TKey key] => _dictionary[key];
34 | public IReadOnlyCollection Keys => _dictionary.Keys;
35 | public IReadOnlyCollection Values => _dictionary.Values;
36 | public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/IReadOnlyArray2.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | namespace Jodosoft.Collections
21 | {
22 | public interface IReadOnlyArray2
23 | {
24 | T this[int x, int y] { get; }
25 |
26 | int Length { get; }
27 | int LengthX { get; }
28 | int LengthY { get; }
29 |
30 | T[] ToArray();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/IReadOnlyLookup.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System.Collections.Generic;
21 |
22 | namespace Jodosoft.Collections
23 | {
24 | public interface IReadOnlyLookup
25 | {
26 | TValue this[TKey key] { get; }
27 | IReadOnlyCollection Keys { get; }
28 | IReadOnlyCollection Values { get; }
29 | bool ContainsKey(TKey key);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/Jodosoft.Collections.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/Lookup.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System.Collections.Generic;
21 |
22 | namespace Jodosoft.Collections
23 | {
24 | public static class Lookup
25 | {
26 | public static IReadOnlyLookup Empty() where TKey : notnull
27 | => new Dictionary(0).ToLookup();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/LookupExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 | using System.Linq;
23 |
24 | namespace Jodosoft.Collections
25 | {
26 | public static class LookupExtensions
27 | {
28 | public static bool TryGetValue(this IReadOnlyLookup instance, TKey key, out TValue value)
29 | {
30 | if (instance.ContainsKey(key))
31 | {
32 | value = instance[key];
33 | return true;
34 | }
35 | value = (new TValue[1])[0];
36 | return false;
37 | }
38 |
39 | public static IReadOnlyLookup ToReadOnlyLookup(this IReadOnlyDictionary dictionary) where TKey : notnull
40 | => new DictionaryLookup(dictionary.ToDictionary(x => x.Key, x => x.Value));
41 |
42 | public static IReadOnlyLookup ToReadOnlyLookup(this IEnumerable source, Func keySelector) where TKey : notnull
43 | => new DictionaryLookup(source.ToDictionary(keySelector));
44 |
45 | public static IReadOnlyLookup ToReadOnlyLookup(this IEnumerable source, Func keySelector, Func elementSelector) where TKey : notnull
46 | => new DictionaryLookup(source.ToDictionary(keySelector, elementSelector));
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/README.md:
--------------------------------------------------------------------------------
1 | Extended collection classes, utilities and abstractions.
2 |
3 | See: https://github.com/Jodosoft/Libraries/#collections
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/SortedDictionaryLookup.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace Jodosoft.Collections
24 | {
25 | public sealed class SortedDictionaryLookup : IReadOnlyLookup where TKey : notnull
26 | {
27 | private readonly SortedDictionary _dictionary;
28 |
29 | public SortedDictionaryLookup(SortedDictionary dictionary)
30 | {
31 | _dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary));
32 | }
33 |
34 | public TValue this[TKey key] => _dictionary[key];
35 | public IReadOnlyCollection Keys => _dictionary.Keys;
36 | public IReadOnlyCollection Values => _dictionary.Values;
37 | public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Jodosoft.Collections/TAGS.md:
--------------------------------------------------------------------------------
1 | collection, collections, generic, jodo, jodosoft
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Benchmarks/GeometryBenchmarks.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using System.Diagnostics.CodeAnalysis;
22 | using Jodosoft.Benchmarking;
23 | using Jodosoft.Numerics;
24 | using Jodosoft.Primitives;
25 |
26 | namespace Jodosoft.Geometry.Benchmarks
27 | {
28 | [ExcludeFromCodeCoverage]
29 | public static class GeometryBenchmarks
30 | {
31 | private static readonly Random Random = new Random();
32 |
33 | [Benchmark]
34 | public static Benchmark AARectangleNGetArea_Versus_SingleMultiplication()
35 | {
36 | return Benchmark
37 | .Using(() =>
38 | {
39 | float singleWidth = Random.NextSingle(Variants.LowMagnitude);
40 | float singleHeight = Random.NextSingle(Variants.LowMagnitude);
41 | AARectangleN aaRectangle = new AARectangleN(
42 | Random.NextSingle(Variants.LowMagnitude), Random.NextSingle(Variants.LowMagnitude), singleWidth, singleHeight);
43 | return (singleWidth, singleHeight, aaRectangle);
44 | })
45 | .Measure(x => x.aaRectangle.GetArea())
46 | .Versus(x => x.singleWidth * x.singleHeight);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Benchmarks/Jodosoft.Geometry.Benchmarks.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/AARectangleNIntegralTestBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using FluentAssertions;
21 | using Jodosoft.Numerics;
22 | using Jodosoft.Testing;
23 | using NUnit.Framework;
24 |
25 | namespace Jodosoft.Geometry.Tests
26 | {
27 | public abstract class AARectangleNIntegralTestBase : GlobalFixtureBase where TNumeric : struct, INumeric
28 | {
29 | [SetUp]
30 | public void SetUp() => Assert.That(Numeric.IsIntegral());
31 |
32 | [Test, Repeat(RandomVariations)]
33 | public void FromBottomLeft_UnitSquare_CorrectVertices()
34 | {
35 | //arrange
36 | AARectangleN subject = AARectangleN.FromBottomLeft(
37 | new Vector2N(Numeric.Zero(), Numeric.Zero()),
38 | new Vector2N(Numeric.One(), Numeric.One()));
39 |
40 | //act
41 | Vector2N bottomLeftResult = subject.GetBottomLeft();
42 | Vector2N topRightResult = subject.GetTopRight();
43 |
44 | //assert
45 | bottomLeftResult.Should().Be(new Vector2N(Numeric.Zero(), Numeric.Zero()));
46 | topRightResult.Should().Be(new Vector2N(Numeric.One(), Numeric.One()));
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/AARectangleNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 | using Jodosoft.Primitives.Tests;
22 | using Jodosoft.Testing;
23 |
24 | namespace Jodosoft.Geometry.Tests
25 | {
26 | public static class AARectangleNTests
27 | {
28 | public sealed class AARectangleIntegralFixedPointTests : AARectangleNTestBase { }
29 | public sealed class AARectangleIntegralFloatingPointTests : AARectangleNTestBase { }
30 | public sealed class AARectangleIntegralUnsignedIntegralTests : AARectangleNIntegralTestBase { }
31 | public sealed class FixedPointBinaryIOTests : BinaryIOTestBase> { }
32 | public sealed class FixedPointFormattableTests : FormattableTestBase> { }
33 | public sealed class FixedPointObjectTests : ObjectTestBase> { }
34 | public sealed class FixedPointSerializableTests : SerializableTestBase> { }
35 | public sealed class FloatingPointBinaryIOTests : BinaryIOTestBase> { }
36 | public sealed class FloatingPointFormattableTests : FormattableTestBase> { }
37 | public sealed class FloatingPointObjectTests : ObjectTestBase> { }
38 | public sealed class FloatingPointSerializableTests : SerializableTestBase> { }
39 | public sealed class UnsignedIntegralBinaryIOTests : BinaryIOTestBase> { }
40 | public sealed class UnsignedIntegralFormattableTests : FormattableTestBase> { }
41 | public sealed class UnsignedIntegralObjectTests : ObjectTestBase> { }
42 | public sealed class UnsignedIntegralSerializableTests : SerializableTestBase> { }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/AngleNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 | using Jodosoft.Primitives.Tests;
22 | using Jodosoft.Testing;
23 |
24 | namespace Jodosoft.Geometry.Tests
25 | {
26 | public static class AngleNTests
27 | {
28 | public sealed class FixedPointBinaryIOTests : BinaryIOTestBase> { }
29 | public sealed class FixedPointFormattableTests : FormattableTestBase> { }
30 | public sealed class FixedPointGeneralTests : AngleNTestBase { }
31 | public sealed class FixedPointObjectTests : ObjectTestBase> { }
32 | public sealed class FixedPointSerializableFixedPointTests : SerializableTestBase> { }
33 | public sealed class FloatingPointBinaryIOTests : BinaryIOTestBase> { }
34 | public sealed class FloatingPointFormattableTests : FormattableTestBase> { }
35 | public sealed class FloatingPointGeneralTests : AngleNTestBase { }
36 | public sealed class FloatingPointObjectTests : ObjectTestBase> { }
37 | public sealed class FloatingPointSerializableTests : SerializableTestBase> { }
38 | public sealed class UnsignedIntegralBinaryIOTests : BinaryIOTestBase> { }
39 | public sealed class UnsignedIntegralFormattableTests : FormattableTestBase> { }
40 | public sealed class UnsignedIntegralGeneralTests : AngleNTestBase { }
41 | public sealed class UnsignedIntegralObjectTests : ObjectTestBase> { }
42 | public sealed class UnsignedIntegralSerializableTests : SerializableTestBase> { }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/CircleNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 | using Jodosoft.Primitives.Tests;
22 | using Jodosoft.Testing;
23 |
24 | namespace Jodosoft.Geometry.Tests
25 | {
26 | public static class CircleNTests
27 | {
28 | public sealed class FixedPointBinaryIOTests : BinaryIOTestBase> { }
29 | public sealed class FixedPointFormattableTests : FormattableTestBase> { }
30 | public sealed class FixedPointGeneralTests : CircleNTestBase { }
31 | public sealed class FixedPointObjectTests : ObjectTestBase> { }
32 | public sealed class FixedPointSerializableTests : SerializableTestBase> { }
33 | public sealed class FloatingPointBinaryIOTests : BinaryIOTestBase> { }
34 | public sealed class FloatingPointFormattableTests : FormattableTestBase> { }
35 | public sealed class FloatingPointGeneralTests : CircleNTestBase { }
36 | public sealed class FloatingPointObjectTests : ObjectTestBase> { }
37 | public sealed class FloatingPointSerializableTests : SerializableTestBase> { }
38 | public sealed class UnsignedIntegralBinaryIOTests : BinaryIOTestBase> { }
39 | public sealed class UnsignedIntegralFormattableTests : FormattableTestBase> { }
40 | public sealed class UnsignedIntegralGeneralTests : CircleNTestBase { }
41 | public sealed class UnsignedIntegralObjectTests : ObjectTestBase> { }
42 | public sealed class UnsignedIntegralSerializableTests : SerializableTestBase> { }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/Jodosoft.Geometry.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/RectangleNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 | using Jodosoft.Primitives.Tests;
22 | using Jodosoft.Testing;
23 |
24 | namespace Jodosoft.Geometry.Tests
25 | {
26 | public static class RectangleNTests
27 | {
28 | public sealed class FixedPointBinaryIOTests : BinaryIOTestBase> { }
29 | public sealed class FixedPointFormattableTests : FormattableTestBase> { }
30 | public sealed class FixedPointObjectTests : ObjectTestBase> { }
31 | public sealed class FixedPointRectangleTests : RectangleNTestBase { }
32 | public sealed class FixedPointSerializableTests : SerializableTestBase> { }
33 | public sealed class FloatingPointBinaryIOTests : BinaryIOTestBase> { }
34 | public sealed class FloatingPointFormattableTests : FormattableTestBase> { }
35 | public sealed class FloatingPointObjectTests : ObjectTestBase> { }
36 | public sealed class FloatingPointRectangleTests : RectangleNTestBase { }
37 | public sealed class FloatingPointSerializableTests : SerializableTestBase> { }
38 | public sealed class UnsignedIntegralBinaryIOTests : BinaryIOTestBase> { }
39 | public sealed class UnsignedIntegralFormattableTests : FormattableTestBase> { }
40 | public sealed class UnsignedIntegralObjectTests : ObjectTestBase> { }
41 | public sealed class UnsignedIntegralRectangleTests : RectangleNTestBase { }
42 | public sealed class UnsignedIntegralSerializableTests : SerializableTestBase> { }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/TriangleNTestBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System.Diagnostics.CodeAnalysis;
21 | using Jodosoft.Numerics;
22 | using Jodosoft.Primitives;
23 | using Jodosoft.Testing;
24 | using NUnit.Framework;
25 |
26 | namespace Jodosoft.Geometry.Tests
27 | {
28 |
29 | [SuppressMessage("csharpsquid", "S1940:Boolean checks should not be inverted.", Justification = "Intentional for verifying operator consistency.")]
30 | public abstract class TriangleNTestBase
31 | : GlobalFixtureBase where TNumeric : struct, INumeric
32 | {
33 | [Test, Repeat(RandomVariations)]
34 | public void EqualsMethods_RandomValues_SameOutcome()
35 | {
36 | //arrange
37 | TriangleN input1 = Random.NextVariant>();
38 | TriangleN input2 = Random.Choose(input1, Random.NextVariant>());
39 |
40 | //act
41 | //assert
42 | AssertSame.Outcome(
43 | () => input1.Equals(input2),
44 | () => input1.Equals((object)input2),
45 | () => input1 == input2,
46 | () => !(input1 != input2));
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry.Tests/TriangleNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 | using Jodosoft.Primitives.Tests;
22 | using Jodosoft.Testing;
23 |
24 | namespace Jodosoft.Geometry.Tests
25 | {
26 | public static class TriangleNTests
27 | {
28 | public sealed class FixedPointBinaryIOTests : BinaryIOTestBase> { }
29 | public sealed class FixedPointFormattableTests : FormattableTestBase> { }
30 | public sealed class FixedPointGeneralTests : TriangleNTestBase { }
31 | public sealed class FixedPointObjectTests : ObjectTestBase> { }
32 | public sealed class FixedPointSerializableTests : SerializableTestBase> { }
33 | public sealed class FloatingPointBinaryIOTests : BinaryIOTestBase> { }
34 | public sealed class FloatingPointFormattableTests : FormattableTestBase> { }
35 | public sealed class FloatingPointGeneralTests : TriangleNTestBase { }
36 | public sealed class FloatingPointObjectTests : ObjectTestBase> { }
37 | public sealed class FloatingPointSerializableTests : SerializableTestBase> { }
38 | public sealed class UnsignedIntegralBinaryIOTests : BinaryIOTestBase> { }
39 | public sealed class UnsignedIntegralFormattableTests : FormattableTestBase> { }
40 | public sealed class UnsignedIntegralGeneralTests : TriangleNTestBase { }
41 | public sealed class UnsignedIntegralObjectTests : ObjectTestBase> { }
42 | public sealed class UnsignedIntegralSerializableTests : SerializableTestBase> { }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/AngleNExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 |
22 | namespace Jodosoft.Geometry
23 | {
24 | public static class AngleNExtensions
25 | {
26 | public static TNumeric GetRadians(this AngleN angle) where TNumeric : struct, INumeric
27 | => AngleN.DegreesToRadians(angle.Degrees);
28 |
29 | public static TNumeric Cos(this AngleN angle) where TNumeric : struct, INumeric
30 | => MathN.Cos(angle.GetRadians());
31 |
32 | public static TNumeric Cosh(this AngleN angle) where TNumeric : struct, INumeric
33 | => MathN.Cosh(angle.GetRadians());
34 |
35 | public static TNumeric Sin(this AngleN angle) where TNumeric : struct, INumeric
36 | => MathN.Sin(angle.GetRadians());
37 |
38 | public static TNumeric Sinh(this AngleN angle) where TNumeric : struct, INumeric
39 | => MathN.Sinh(angle.GetRadians());
40 |
41 | public static TNumeric Tan(this AngleN angle) where TNumeric : struct, INumeric
42 | => MathN.Tan(angle.GetRadians());
43 |
44 | public static TNumeric Tanh(this AngleN angle) where TNumeric : struct, INumeric
45 | => MathN.Tanh(angle.GetRadians());
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/CompatibilitySuppressions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | CP0002
5 | M:Jodosoft.Geometry.AARectangleN`1.op_Implicit(Jodosoft.Geometry.AARectangleN{`0})~System.ValueTuple{`0,`0,`0,`0}
6 | lib/netstandard2.0/Jodosoft.Geometry.dll
7 | lib/net461/Jodosoft.Geometry.dll
8 |
9 |
10 | CP0002
11 | M:Jodosoft.Geometry.AARectangleN`1.op_Implicit(Jodosoft.Geometry.AARectangleN{`0})~System.ValueTuple{Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0}}
12 | lib/netstandard2.0/Jodosoft.Geometry.dll
13 | lib/net461/Jodosoft.Geometry.dll
14 |
15 |
16 | CP0002
17 | M:Jodosoft.Geometry.AARectangleN`1.op_Implicit(System.ValueTuple{`0,`0,`0,`0})~Jodosoft.Geometry.AARectangleN{`0}
18 | lib/netstandard2.0/Jodosoft.Geometry.dll
19 | lib/net461/Jodosoft.Geometry.dll
20 |
21 |
22 | CP0002
23 | M:Jodosoft.Geometry.AARectangleN`1.op_Implicit(System.ValueTuple{Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0}})~Jodosoft.Geometry.AARectangleN{`0}
24 | lib/netstandard2.0/Jodosoft.Geometry.dll
25 | lib/net461/Jodosoft.Geometry.dll
26 |
27 |
28 | CP0002
29 | M:Jodosoft.Geometry.TriangleN`1.op_Implicit(Jodosoft.Geometry.TriangleN{`0})~System.ValueTuple{Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0}}
30 | lib/netstandard2.0/Jodosoft.Geometry.dll
31 | lib/net461/Jodosoft.Geometry.dll
32 |
33 |
34 | CP0002
35 | M:Jodosoft.Geometry.TriangleN`1.op_Implicit(System.ValueTuple{Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0},Jodosoft.Numerics.Vector2N{`0}})~Jodosoft.Geometry.TriangleN{`0}
36 | lib/netstandard2.0/Jodosoft.Geometry.dll
37 | lib/net461/Jodosoft.Geometry.dll
38 |
39 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/Jodosoft.Geometry.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/README.md:
--------------------------------------------------------------------------------
1 | Geometric structures and utilities that support generic number types.
2 |
3 | See: https://github.com/Jodosoft/Libraries/#geometry
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/TAGS.md:
--------------------------------------------------------------------------------
1 | geometry, geometric, angle, angles, shape, shapes, 2d, jodo, jodosoft
--------------------------------------------------------------------------------
/src/Jodosoft.Geometry/VectorExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using Jodosoft.Numerics;
21 |
22 | namespace Jodosoft.Geometry
23 | {
24 | public static class VectorExtensions
25 | {
26 | public static Vector2N RotateAround(this Vector2N vector, Vector2N pivot, AngleN angle) where TNumeric : struct, INumeric
27 | {
28 | if (angle == AngleN.Zero()) return vector;
29 |
30 | AngleN newAngle = -angle;
31 | Vector2N difference = vector - pivot;
32 | return pivot + new Vector2N(
33 | difference.X.Multiply(newAngle.Cos()).Subtract(difference.Y.Multiply(newAngle.Sin())),
34 | difference.X.Multiply(newAngle.Sin()).Add(difference.Y.Multiply(newAngle.Cos())));
35 | }
36 |
37 | public static TNumeric GetLengthSquared(this Vector2N vector) where TNumeric : struct, INumeric
38 | => vector.X.Multiply(vector.X).Add(vector.Y.Multiply(vector.Y));
39 |
40 | public static TNumeric GetLength(this Vector2N vector) where TNumeric : struct, INumeric
41 | => MathN.Sqrt(vector.GetLengthSquared());
42 |
43 | public static TNumeric DistanceFrom(this Vector2N vector, Vector2N point) where TNumeric : struct, INumeric
44 | => MathN.Sqrt(vector.X.Subtract(point.X).Square().Add(vector.Y.Subtract(point.Y).Square()));
45 |
46 | public static Vector2N Translate(this Vector2N vector, Vector2N delta) where TNumeric : struct, INumeric
47 | => new Vector2N(vector.X.Add(delta.X), vector.Y.Add(delta.Y));
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Jodosoft.Numerics.Benchmarks/Jodosoft.Numerics.Benchmarks.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/Jodosoft.Numerics.Benchmarks/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Jodosoft.Numerics.Benchmarks": {
4 | "commandName": "Project",
5 | "commandLineArgs": "-fd"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/src/Jodosoft.Numerics.Tests/ByteNTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2023 Joe Lawry-Short
2 | // Permission is hereby granted, free of charge, to any person obtaining a copy
3 | // of this software and associated documentation files (the "Software"), to
4 | // deal in the Software without restriction, including without limitation the
5 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 | // sell copies of the Software, and to permit persons to whom the Software is
7 | // furnished to do so, subject to the following conditions:
8 | //
9 | // The above copyright notice and this permission notice shall be included in
10 | // all copies or substantial portions of the Software.
11 | //
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 | // IN THE SOFTWARE.
19 |
20 | using System;
21 | using FluentAssertions;
22 | using Jodosoft.Primitives;
23 | using Jodosoft.Primitives.Tests;
24 | using Jodosoft.Testing;
25 | using Jodosoft.Testing.NewtonsoftJson;
26 | using NUnit.Framework;
27 |
28 | namespace Jodosoft.Numerics.Tests
29 | {
30 | public sealed class ByteNTests : GlobalFixtureBase
31 | {
32 | public sealed class BinaryIOTests : BinaryIOTestBase { }
33 | public sealed class FormattableTests : FormattableTestBase { }
34 | public sealed class JsonConvertTests : JsonConvertTestBase { }
35 | public sealed class NumericBitConverterTests : NumericBitConverterTestBase { }
36 | public sealed class NumericCastTests : NumericCastTestBase { }
37 | public sealed class NumericConversionConsistencyTests : NumericConversionConsistencyTestBase { }
38 | public sealed class NumericConvertTests : NumericConvertTestBase { }
39 | public sealed class NumericIntegralTests : NumericIntegralTestBase { }
40 | public sealed class NumericMathTests : NumericMathTestBase { }
41 | public sealed class NumericNonFloatingPointTests : NumericNonFloatingPointTestBase { }
42 | public sealed class NumericNonInfinityTests : NumericNonInfinityTestBase { }
43 | public sealed class NumericNonNaNTests : NumericNonNaNTestBase { }
44 | public sealed class NumericRandomTestBase : NumericRandomTestBase { }
45 | public sealed class NumericStringConvertTests : NumericStringConvertTestBase { }
46 | public sealed class NumericTests : NumericTestBase { }
47 | public sealed class NumericUnsignedTests : NumericUnsignedTestBase { }
48 | public sealed class ObjectTests : ObjectTestBase