├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── TypeNum.sln ├── azure-pipelines.yml ├── samples └── Tensors │ ├── MutableTensor.cs │ ├── Program.cs │ ├── SafeIndexing.cs │ ├── Tensor.cs │ └── Tensors.csproj ├── src ├── FixedArray.cs ├── INumeral.cs ├── N0.cs ├── N1.cs ├── PowersOfTwo.cs ├── Sum.cs ├── Twice.cs └── TypeNum.csproj └── tests ├── FixedArrayTests.cs ├── NumeralTests.cs ├── PowersOfTwoTests.cs └── TypeNum.Tests.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TypeNum 2 | Type-level integers for C# 3 | 4 | [![NuGet](https://img.shields.io/nuget/v/TypeNum.svg)](https://www.nuget.org/packages/TypeNum/) 5 | [![Build Status](https://losttech.visualstudio.com/TypeNum/_apis/build/status/losttech.TypeNum?branchName=master)](https://losttech.visualstudio.com/TypeNum/_build/latest?definitionId=29&branchName=master) 6 | 7 | # Example 8 | ```csharp 9 | class Tensor 10 | where NCols : struct, INumeral 11 | where NRows : struct, INumeral 12 | { 13 | public static int ColumnCount { get; } = NCols.Num; 14 | public static int RowCount { get; } = NRows.Num; 15 | 16 | internal readonly float[,] values = new float[ColumnCount, RowCount]; 17 | 18 | public void Add(Tensor tensor) 19 | { 20 | for (int col = 0; col < ColumnCount; col++) 21 | for (int row = 0; row < RowCount; row++) 22 | this.values[col, row] += tensor.values[col, row]; 23 | } 24 | } 25 | ``` 26 | 27 | ## Shorthands 28 | For non-power-of-two numerals it is useful to add shorthands. 29 | Unfortunately declaring one is rather verbose in C#: 30 | ```csharp 31 | using N39 = TypeNum.Sum, 34 | TypeNum.N2>, 35 | TypeNum.N1>; 36 | 37 | ... 38 | 39 | Debug.Assert(N39.Num == 39); 40 | var thirtyNine = new Tensor(); 41 | ``` 42 | 43 | # Consistency 44 | Technically, `Sum` and `Sum` represent the same numeral. 45 | However, there is no way to express it in C# type system. To alleviate that 46 | TypeNum tries to enforce one and only way to represent specific number. 47 | 48 | To take advantage of that enforcement, avoid declaring your own implementations 49 | of the `Numeral` interface, and stick to `N*`, `Twice`, and `Sum` 50 | classes. They enforce several rules at run time (and, potentially, [compile 51 | time in the future](https://github.com/losttech/TypeNum/issues/1)): 52 | 53 | ## Twice 54 | In `Twice` `T` can only be one of the following: 55 | 56 | * the largest predefined type numeral (currently `N4096`) 57 | * another `Twice<*>` 58 | 59 | So `Twice` can only be used to represent very large numerals 60 | 61 | ## Sum 62 | The following rules are enforced on parameters `T1` and `T2` of `Sum`: 63 | 64 | * `T1.Num` must be strictly greater (>) than `T2.Num` (swap them, if yours are opposite) 65 | * `T2` can never be another `Sum<*,*>`. Replace any `Sum>` with 66 | `Sum, C>` 67 | * if `T1` itself is a `Sum`, then in `Sum, C>` `A` and `B` must 68 | each be strictly greater (>) than `C` 69 | 70 | Following these rules will ensure, that equal type numerals are always 71 | represented by identical types. 72 | 73 | # F# Example 74 | In F# (unlike C#) operators can introduce new generic type parameters, 75 | which enables defining statically-typed matrix multiplication as an operator: 76 | 77 | ```fsharp 78 | type Matrix<'rows, 'cols>() = 79 | static member ( * )(left: Matrix<'rows, 'cols>, right: Matrix<'cols, 'ncols>) = 80 | Matrix<'rows, 'ncols>() 81 | 82 | module private Test = 83 | open TypeNum 84 | 85 | type N3 = Sum 86 | type N7 = Sum 87 | type N5 = Sum 88 | 89 | let a = Matrix() 90 | let b = Matrix() 91 | let product = a * b 92 | 93 | ``` -------------------------------------------------------------------------------- /TypeNum.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28711.60 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeNum", "src\TypeNum.csproj", "{C64FBD4B-20A5-48B0-865B-0F708A74C457}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeNum.Tests", "tests\TypeNum.Tests.csproj", "{DAB4E441-7F95-4870-848C-925D1F1245F8}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Repo", "Repo", "{44BFD517-B192-42D8-B16F-373AE20DABE6}" 11 | ProjectSection(SolutionItems) = preProject 12 | .gitattributes = .gitattributes 13 | .gitignore = .gitignore 14 | azure-pipelines.yml = azure-pipelines.yml 15 | LICENSE = LICENSE 16 | README.md = README.md 17 | EndProjectSection 18 | EndProject 19 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{058BAD21-D662-4697-B2F5-AE49D50D2D90}" 20 | EndProject 21 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensors", "samples\Tensors\Tensors.csproj", "{700CD753-222E-4F6C-BECD-230E49A56F23}" 22 | EndProject 23 | Global 24 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 25 | Debug|Any CPU = Debug|Any CPU 26 | Release|Any CPU = Release|Any CPU 27 | EndGlobalSection 28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 29 | {C64FBD4B-20A5-48B0-865B-0F708A74C457}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {C64FBD4B-20A5-48B0-865B-0F708A74C457}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {C64FBD4B-20A5-48B0-865B-0F708A74C457}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {C64FBD4B-20A5-48B0-865B-0F708A74C457}.Release|Any CPU.Build.0 = Release|Any CPU 33 | {DAB4E441-7F95-4870-848C-925D1F1245F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 34 | {DAB4E441-7F95-4870-848C-925D1F1245F8}.Debug|Any CPU.Build.0 = Debug|Any CPU 35 | {DAB4E441-7F95-4870-848C-925D1F1245F8}.Release|Any CPU.ActiveCfg = Release|Any CPU 36 | {DAB4E441-7F95-4870-848C-925D1F1245F8}.Release|Any CPU.Build.0 = Release|Any CPU 37 | {700CD753-222E-4F6C-BECD-230E49A56F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 38 | {700CD753-222E-4F6C-BECD-230E49A56F23}.Debug|Any CPU.Build.0 = Debug|Any CPU 39 | {700CD753-222E-4F6C-BECD-230E49A56F23}.Release|Any CPU.ActiveCfg = Release|Any CPU 40 | {700CD753-222E-4F6C-BECD-230E49A56F23}.Release|Any CPU.Build.0 = Release|Any CPU 41 | EndGlobalSection 42 | GlobalSection(SolutionProperties) = preSolution 43 | HideSolutionNode = FALSE 44 | EndGlobalSection 45 | GlobalSection(NestedProjects) = preSolution 46 | {700CD753-222E-4F6C-BECD-230E49A56F23} = {058BAD21-D662-4697-B2F5-AE49D50D2D90} 47 | EndGlobalSection 48 | GlobalSection(ExtensibilityGlobals) = postSolution 49 | SolutionGuid = {AF6A4D21-03BB-4811-86F3-3802AD5B526C} 50 | EndGlobalSection 51 | EndGlobal 52 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | # ASP.NET Core 2 | # Build and test ASP.NET Core projects targeting .NET Core. 3 | # Add steps that run tests, create a NuGet package, deploy, and more: 4 | # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core 5 | 6 | trigger: 7 | - master 8 | 9 | pool: 10 | vmImage: 'ubuntu-latest' 11 | 12 | variables: 13 | buildConfiguration: 'Release' 14 | 15 | steps: 16 | - task: UseDotNet@2 17 | displayName: 'Install .NET Core 3.x SDK' 18 | inputs: 19 | packageType: 'runtime' 20 | version: '3.x' 21 | 22 | - task: UseDotNet@2 23 | displayName: 'Use .NET 6.0 SDK' 24 | inputs: 25 | packageType: 'sdk' 26 | version: '6.x' 27 | includePreviewVersions: true 28 | 29 | - script: dotnet build --configuration $(buildConfiguration) 30 | displayName: 'Build $(buildConfiguration)' 31 | 32 | - task: DotNetCoreCLI@2 33 | displayName: 'Test' 34 | inputs: 35 | command: 'test' 36 | arguments: '--configuration $(buildConfiguration)' 37 | 38 | - task: DotNetCoreCLI@2 39 | displayName: 'Pack' 40 | inputs: 41 | command: 'pack' 42 | packagesToPack: 'src/**/*.csproj' 43 | versioningScheme: 'byPrereleaseNumber' 44 | majorVersion: '0' 45 | minorVersion: '2' 46 | patchVersion: '0' 47 | 48 | - task: NuGetCommand@2 49 | displayName: 'Push to Nuget.org' 50 | inputs: 51 | command: 'push' 52 | packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg' 53 | nuGetFeedType: 'external' 54 | publishFeedCredentials: 'TypeNum @ Nuget.org' -------------------------------------------------------------------------------- /samples/Tensors/MutableTensor.cs: -------------------------------------------------------------------------------- 1 | namespace Tensors 2 | { 3 | using TypeNum; 4 | 5 | class MutableTensor: Tensor 6 | where NCols : unmanaged, INumeral 7 | where NRows : unmanaged, INumeral 8 | { 9 | public void Add(MutableTensor tensor) 10 | { 11 | for (int col = 0; col < ColumnCount; col++) 12 | for (int row = 0; row < RowCount; row++) 13 | this.values[col, row] += tensor.values[col, row]; 14 | } 15 | 16 | public float this[int col, int row] { 17 | get => this.values[col, row]; 18 | set => this.values[col, row] = value; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /samples/Tensors/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Tensors 2 | { 3 | using System.Diagnostics; 4 | using TypeNum; 5 | using N39 = TypeNum.Sum, 8 | TypeNum.N2>, 9 | TypeNum.N1>; 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | var diag1 = new MutableTensor { 15 | [0, 0] = 1, 16 | [1, 1] = 1, 17 | }; 18 | var all1 = new MutableTensor { 19 | [0,0] = 1, 20 | [0,1] = 1, 21 | [1,0] = 1, 22 | [1,1] = 1, 23 | }; 24 | var sum = new MutableTensor(); 25 | sum.Add(diag1); 26 | sum.Add(all1); 27 | 28 | var mul = new MutableTensor(); 29 | sum.Mul(all1, result: mul); 30 | // OK 31 | 32 | var mulByFail = new MutableTensor>(); 33 | // sum.Mul(mulByFail, result: mul); 34 | // error CS0411: The type arguments for method 35 | // 'Tensor.Mul(Tensor, MutableTensor)' 36 | // cannot be inferred from the usage. Try specifying the type arguments explicitly. 37 | 38 | Debug.Assert(N39.Num == 39); 39 | var thirtyNine = new Tensor(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /samples/Tensors/SafeIndexing.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using static TypeNum.FixedArray; 5 | 6 | namespace TypeNum { 7 | class SafeIndexing where Shape: unmanaged, INumeral { 8 | object Get(object key) => throw new NotImplementedException(); 9 | void Set(object key, object value) => throw new NotImplementedException(); 10 | 11 | T this[FixedArray indexes] { 12 | get => (T)this.Get(indexes); 13 | set => this.Set(indexes, value); 14 | } 15 | 16 | static void Sample() { 17 | var a = new SafeIndexing, float>(); 18 | a[A(1, 2)] = 1.5f; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /samples/Tensors/Tensor.cs: -------------------------------------------------------------------------------- 1 | namespace Tensors 2 | { 3 | using System; 4 | using TypeNum; 5 | 6 | class Tensor 7 | where NCols : unmanaged, INumeral 8 | where NRows : unmanaged, INumeral 9 | { 10 | public static int ColumnCount { get; } = NCols.Num; 11 | public static int RowCount { get; } = NRows.Num; 12 | 13 | internal readonly float[,] values = new float[ColumnCount, RowCount]; 14 | 15 | public void Mul(Tensor by, MutableTensor result) 16 | where NOtherRows : unmanaged, INumeral 17 | { 18 | if (by == null) throw new ArgumentNullException(nameof(by)); 19 | if (result == null) throw new ArgumentNullException(nameof(result)); 20 | 21 | for (int resultCol = 0; resultCol < ColumnCount; resultCol++) 22 | for (int resultRow = 0; resultRow < MutableTensor.RowCount; resultRow++) 23 | for (int row = 0; row < RowCount; row++) 24 | result.values[resultCol, resultRow] += this.values[resultCol, row] * by.values[row, resultRow]; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /samples/Tensors/Tensors.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | 11.0 6 | net7.0 7 | 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/FixedArray.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using System.Runtime.CompilerServices; 6 | using System.Runtime.InteropServices; 7 | 8 | [StructLayout(LayoutKind.Sequential)] 9 | public struct FixedArray : IList 10 | where TSize : unmanaged, INumeral 11 | where T : unmanaged { 12 | public static int ElementCount { 13 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 14 | get; 15 | } 16 | 17 | #pragma warning disable IDE0044 // Add readonly modifier. The field is modified via pointers. 18 | TSize value; 19 | #pragma warning restore IDE0044 // Add readonly modifier 20 | 21 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 22 | static void CheckIndex(int index) { 23 | if (index < 0 || index >= ElementCount) 24 | throw new IndexOutOfRangeException(); 25 | } 26 | 27 | public unsafe T this[int index] { 28 | get { 29 | CheckIndex(index); 30 | return this.UnsafeRef(index); 31 | } 32 | set { 33 | CheckIndex(index); 34 | this.UnsafeRef(index) = value; 35 | } 36 | } 37 | 38 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 39 | public unsafe ref T UnsafeRef(int index) { 40 | fixed (TSize* self = &this.value) { 41 | var data = (T*)self; 42 | return ref data[index]; 43 | } 44 | } 45 | 46 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 47 | public unsafe T UnsafeGet(int index) { 48 | fixed (TSize* self = &this.value) { 49 | var data = (T*)self; 50 | return data[index]; 51 | } 52 | } 53 | 54 | public unsafe void UnsafeSet(int index, T value) { 55 | fixed (TSize* self = &this.value) { 56 | var data = (T*)self; 57 | data[index] = value; 58 | } 59 | } 60 | 61 | public int Count { 62 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 63 | get => ElementCount; 64 | } 65 | public bool IsReadOnly => false; 66 | 67 | public bool Contains(T item) => this.IndexOf(item) >= 0; 68 | 69 | public unsafe void CopyTo(T[] array, int targetIndex) { 70 | if (array == null) throw new ArgumentNullException(nameof(array)); 71 | if (targetIndex < 0 || (long)targetIndex + ElementCount > array.Length) 72 | throw new ArgumentOutOfRangeException(paramName: nameof(targetIndex)); 73 | fixed (T* dest = array) 74 | fixed (TSize* self = &this.value) 75 | Buffer.MemoryCopy(source: self, dest + targetIndex, array.Length - targetIndex, ElementCount); 76 | } 77 | 78 | public IEnumerator GetEnumerator() { 79 | for (int i = 0; i < ElementCount; i++) 80 | yield return this[i]; 81 | } 82 | public unsafe int IndexOf(T item) { 83 | fixed (TSize* self = &this.value) { 84 | var data = (T*)self; 85 | for (int i = 0; i < ElementCount; i++) 86 | if (object.Equals(data[i], item)) 87 | return i; 88 | } 89 | 90 | return -1; 91 | } 92 | 93 | IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); 94 | 95 | #region Unsupported 96 | public void Add(T item) => throw new NotSupportedException(); 97 | public void Clear() => throw new NotSupportedException(); 98 | public void Insert(int index, T item) => throw new NotSupportedException(); 99 | public bool Remove(T item) => throw new NotSupportedException(); 100 | public void RemoveAt(int index) => throw new NotSupportedException(); 101 | #endregion 102 | 103 | static FixedArray() { 104 | ElementCount = TSize.Num; 105 | } 106 | } 107 | 108 | public static class FixedArray where T : unmanaged { 109 | public static Type GetType(int elementCount) { 110 | if (elementCount < 0) throw new ArgumentOutOfRangeException(nameof(elementCount)); 111 | var numeral = Numeral.GetNType(elementCount); 112 | return typeof(FixedArray<,>).MakeGenericType(numeral, typeof(T)); 113 | } 114 | } 115 | 116 | public static class FixedArray { 117 | public static FixedArray, T> A(T item0) where T : unmanaged 118 | => new() { [0] = item0 }; 119 | public static FixedArray, T> A(T item0, T item1) where T : unmanaged 120 | => new() { [0] = item0, [1] = item1 }; 121 | public static FixedArray, N1>, T> A(T item0, T item1, T item2) where T : unmanaged 122 | => new() { [0] = item0, [1] = item1, [2] = item2 }; 123 | public static FixedArray, T> A(T item0, T item1, T item2, T item3) where T : unmanaged 124 | => new() { [0] = item0, [1] = item1, [2] = item2, [3] = item3 }; 125 | 126 | public static unsafe IList ToFixedArray(this ICollection collection) where T : unmanaged { 127 | if (collection is null) throw new ArgumentNullException(nameof(collection)); 128 | 129 | int len = collection.Count; 130 | var type = FixedArray.GetType(len); 131 | var result = (IList)Activator.CreateInstance(type)!; 132 | 133 | GCHandle box; 134 | try { 135 | box = GCHandle.Alloc(result, GCHandleType.Pinned); 136 | } catch (ArgumentException) { 137 | // workaround for bool and char not being pinnable 138 | using IEnumerator enumerator = collection.GetEnumerator(); 139 | for (int i = 0; i < len; i++) { 140 | if (!enumerator.MoveNext()) throw new ArgumentException("Bad collection enumerator"); 141 | result[i] = enumerator.Current; 142 | } 143 | return result; 144 | } 145 | var data = (T*)box.AddrOfPinnedObject(); 146 | 147 | try { 148 | using IEnumerator enumerator = collection.GetEnumerator(); 149 | for (int i = 0; i < len; i++) { 150 | if (!enumerator.MoveNext()) throw new ArgumentException("Bad collection enumerator"); 151 | data[i] = enumerator.Current; 152 | } 153 | } finally { 154 | box.Free(); 155 | } 156 | 157 | return result; 158 | } 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /src/INumeral.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Reflection; 4 | 5 | public interface INumeral { 6 | static abstract int Num { get; } 7 | } 8 | 9 | public interface INumeral : INumeral { 10 | } 11 | 12 | public static class Numeral where T : unmanaged { 13 | public static Type GetNType(int n) { 14 | if (n < 0) throw new ArgumentOutOfRangeException(nameof(n)); 15 | if (n > PowersOfTwo.Max) throw new NotImplementedException(); 16 | 17 | Type result = (n & 1) == 0 ? typeof(N0) : typeof(N1); 18 | int pwr = 2; 19 | while (n >= pwr) { 20 | if ((n & pwr) == pwr) { 21 | var powerOfTwo = PowersOfTwo.Get(pwr); 22 | result = result == typeof(N0) 23 | ? powerOfTwo 24 | : typeof(Sum<,,>).MakeGenericType(typeof(T), powerOfTwo, result); 25 | } 26 | 27 | pwr <<= 1; 28 | } 29 | return result; 30 | } 31 | } 32 | 33 | public static class Numeral { 34 | static int GetNum() where T : INumeral => T.Num; 35 | static readonly MethodInfo getNumGeneric = typeof(Numeral).GetMethod(nameof(GetNum), BindingFlags.Static | BindingFlags.NonPublic)!; 36 | public static int GetNum(Type numeral) { 37 | var getNum = getNumGeneric.MakeGenericMethod(numeral); 38 | return (int)getNum.Invoke(null, null)!; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/N0.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | public struct N0 : INumeral { public static int Num => 0; } 5 | [StructLayout(LayoutKind.Sequential)] 6 | public struct N0 : INumeral where T : unmanaged { 7 | public static int Num { 8 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 9 | get => 0; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/N1.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | public struct N1 : INumeral { public static int Num => 1; } 6 | [StructLayout(LayoutKind.Sequential)] 7 | public struct N1 : INumeral where T: unmanaged { 8 | public static int Num { 9 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 10 | get => 1; 11 | } 12 | 13 | public T Value; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/PowersOfTwo.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Runtime.CompilerServices; 5 | using System.Runtime.InteropServices; 6 | 7 | public struct N2 : INumeral { public static int Num => 2; } 8 | [StructLayout(LayoutKind.Sequential)] 9 | public struct N2 : INumeral where T : unmanaged { 10 | Twice, T> data; 11 | public static int Num { 12 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 13 | get => 2; 14 | } 15 | } 16 | 17 | public struct N4 : INumeral { public static int Num => 4; } 18 | [StructLayout(LayoutKind.Sequential)] 19 | public struct N4 : INumeral where T : unmanaged { 20 | Twice, T> data; 21 | public static int Num { 22 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 23 | get => 4; 24 | } 25 | } 26 | 27 | public struct N8 : INumeral { public static int Num => 8; } 28 | [StructLayout(LayoutKind.Sequential)] 29 | public struct N8 : INumeral where T : unmanaged { 30 | Twice, T> data; 31 | public static int Num { 32 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 33 | get => 8; 34 | } 35 | } 36 | 37 | public struct N16 : INumeral { public static int Num => 16; } 38 | [StructLayout(LayoutKind.Sequential)] 39 | public struct N16 : INumeral where T : unmanaged { 40 | Twice, T> data; 41 | public static int Num { 42 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 43 | get => 16; 44 | } 45 | } 46 | 47 | public struct N32 : INumeral { public static int Num => 32; } 48 | [StructLayout(LayoutKind.Sequential)] 49 | public struct N32 : INumeral where T : unmanaged { 50 | Twice, T> data; 51 | public static int Num { 52 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 53 | get => 32; 54 | } 55 | } 56 | 57 | public struct N64 : INumeral { public static int Num => 64; } 58 | [StructLayout(LayoutKind.Sequential)] 59 | public struct N64 : INumeral where T : unmanaged { 60 | Twice, T> data; 61 | public static int Num { 62 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 63 | get => 64; 64 | } 65 | } 66 | 67 | public struct N128 : INumeral { public static int Num => 128; } 68 | [StructLayout(LayoutKind.Sequential)] 69 | public struct N128 : INumeral where T : unmanaged { 70 | Twice, T> data; 71 | public static int Num { 72 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 73 | get => 128; 74 | } 75 | } 76 | 77 | public struct N256 : INumeral { public static int Num => 256; } 78 | [StructLayout(LayoutKind.Sequential)] 79 | public struct N256 : INumeral where T : unmanaged { 80 | Twice, T> data; 81 | public static int Num { 82 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 83 | get => 256; 84 | } 85 | } 86 | 87 | public struct N512 : INumeral { public static int Num => 512; } 88 | [StructLayout(LayoutKind.Sequential)] 89 | public struct N512 : INumeral where T : unmanaged { 90 | Twice, T> data; 91 | public static int Num { 92 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 93 | get => 512; 94 | } 95 | } 96 | 97 | public struct N1024 : INumeral { public static int Num => 1024; } 98 | [StructLayout(LayoutKind.Sequential)] 99 | public struct N1024 : INumeral where T : unmanaged { 100 | Twice, T> data; 101 | public static int Num { 102 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 103 | get => 1024; 104 | } 105 | } 106 | 107 | public struct N2048 : INumeral { public static int Num => 2048; } 108 | [StructLayout(LayoutKind.Sequential)] 109 | public struct N2048 : INumeral where T : unmanaged { 110 | Twice, T> data; 111 | public static int Num { 112 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 113 | get => 2048; 114 | } 115 | } 116 | 117 | public struct N4096 : INumeral { public static int Num => 4096; } 118 | [StructLayout(LayoutKind.Sequential)] 119 | public struct N4096 : INumeral where T : unmanaged { 120 | Twice, T> data; 121 | public static int Num { 122 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 123 | get => 4096; 124 | } 125 | } 126 | 127 | public static class PowersOfTwo { 128 | public static int Max { get; } = 4096; 129 | } 130 | 131 | public static class PowersOfTwo where T : unmanaged { 132 | static readonly Dictionary types = new(); 133 | public static Type Get(int powerOfTwo) { 134 | if (powerOfTwo < 0) throw new ArgumentOutOfRangeException(nameof(powerOfTwo)); 135 | if (powerOfTwo > PowersOfTwo.Max) throw new NotImplementedException("Too large"); 136 | return types.TryGetValue(powerOfTwo, out var type) 137 | ? type 138 | : throw new ArgumentException("Not a power of 2"); 139 | } 140 | 141 | static PowersOfTwo() { 142 | for (int i = 2; i <= PowersOfTwo.Max; i <<= 1) { 143 | types[i] = Type.GetType(nameof(TypeNum) + ".N" + i.ToString(System.Globalization.CultureInfo.InvariantCulture) + "`1")! 144 | .MakeGenericType(typeof(T)); 145 | } 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/Sum.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | 6 | public readonly struct Sum : INumeral where T1 : struct, INumeral where T2 : struct, INumeral 7 | { 8 | public static int Num { get; } = checked(T1.Num + T2.Num); 9 | 10 | static Sum() 11 | { 12 | int n2 = T2.Num; 13 | int n1 = T1.Num; 14 | if (n1 <= n2) 15 | throw new ArgumentException("Consistency: T1 must always be > T2. Swap them." 16 | + $"\n{typeof(Sum).Name}: {typeof(T1).Name}={n1},{typeof(T2).Name}={n2}"); 17 | if (n1 == 0 || n2 == 0) 18 | throw new ArgumentException("Consistency: T1 and T2 must always be > 0." 19 | + $"\n{typeof(Sum).Name}: {typeof(T1).Name}={n1},{typeof(T2).Name}={n2}"); 20 | var t1 = typeof(T1); 21 | var t2 = typeof(T2); 22 | if (t2.IsConstructedGenericType && t2.GetGenericTypeDefinition() == typeof(Sum<,>)) 23 | throw new ArgumentException("Consistency: T2 must not be another Sum. Instead use Sum, Y>"); 24 | if (t1.IsConstructedGenericType && t1.GetGenericTypeDefinition() == typeof(Sum<,>)) 25 | { 26 | foreach (var part in t1.GenericTypeArguments) { 27 | if (Numeral.GetNum(part) <= n2) 28 | throw new ArgumentException("Consistency: in Sum, C> A and B must each be > C"); 29 | } 30 | } 31 | } 32 | } 33 | 34 | [StructLayout(LayoutKind.Sequential)] 35 | public struct Sum: INumeral 36 | where T1 : struct, INumeral 37 | where T2 : struct, INumeral 38 | { 39 | public T1 Item1; 40 | public T2 Item2; 41 | 42 | public static int Num { 43 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 44 | get => T1.Num + T2.Num; 45 | } 46 | 47 | static Sum() { 48 | Sum.Num.GetHashCode(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Twice.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | 6 | public readonly struct Twice: INumeral 7 | where T: struct, INumeral 8 | { 9 | public static int Num { get; } = checked(T.Num * 2); 10 | 11 | static Twice() 12 | { 13 | if (typeof(T) != typeof(N4096) 14 | && (!typeof(T).IsConstructedGenericType || typeof(T).GetGenericTypeDefinition() != typeof(Twice<>))) 15 | throw new ArgumentException("Consistency: T must be either PowersOfTwo.Max or Twice"); 16 | } 17 | } 18 | 19 | [StructLayout(LayoutKind.Sequential)] 20 | public struct Twice : INumeral 21 | where TNum: unmanaged, INumeral 22 | { 23 | public TNum Item1; 24 | public TNum Item2; 25 | 26 | public static int Num { 27 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 28 | get => TNum.Num * 2; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/TypeNum.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | 11.0 6 | enable 7 | true 8 | 9 | Lost Tech 10 | Type-level integers for C#/.NET 11 | README.md 12 | LICENSE 13 | https://github.com/losttech/TypeNum 14 | https://github.com/losttech/TypeNum.git 15 | git 16 | 0.3.0 17 | 18 | 19 | true 20 | true 21 | true 22 | snupkg 23 | 24 | 25 | 26 | 27 | all 28 | runtime; build; native; contentfiles; analyzers; buildtransitive 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | True 37 | 38 | 39 | 40 | True 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /tests/FixedArrayTests.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using Xunit; 4 | using ArrayOfTwoInts = FixedArray, int>, int>; 5 | 6 | public class FixedArrayTests { 7 | [Fact] 8 | public void ElementCount() { 9 | int actualCount = ArrayOfTwoInts.ElementCount; 10 | 11 | Assert.Equal(expected: 2, actual: actualCount); 12 | } 13 | 14 | [Fact] 15 | public void Indexing() { 16 | var array = new ArrayOfTwoInts { 17 | [0] = 1, 18 | [1] = 2 19 | }; 20 | 21 | Assert.Equal(expected: 1, array[0]); 22 | Assert.Equal(expected: 2, array[1]); 23 | } 24 | 25 | [Fact] 26 | public void OutOfBoundsIndexThrows() { 27 | var array = new ArrayOfTwoInts(); 28 | Assert.Throws(() => array[-1]); 29 | Assert.Throws(() => array[2]); 30 | } 31 | 32 | [Fact] 33 | public void Contains() { 34 | var array = new ArrayOfTwoInts { 35 | [0] = 1, 36 | [1] = 2, 37 | }; 38 | 39 | Assert.Contains(2, array); 40 | Assert.DoesNotContain(3, array); 41 | } 42 | 43 | [Fact] 44 | public void ToFixedArray() { 45 | int[] netArray = new[] { 1, 2, 3, 4, 5 }; 46 | var array = netArray.ToFixedArray(); 47 | Assert.Equal(netArray, array); 48 | } 49 | 50 | [Fact] 51 | public void ToFixedBoolArray() { 52 | bool[] netArrray = new[] { false, true, true }; 53 | var array = netArrray.ToFixedArray(); 54 | Assert.Equal(netArrray, array); 55 | } 56 | 57 | [Fact] 58 | public void ToFixedArray_TooLarge() { 59 | int[] netArray = new int[PowersOfTwo.Max + 1]; 60 | Assert.Throws(() => netArray.ToFixedArray()); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/NumeralTests.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | 4 | using Xunit; 5 | public class NumeralTests { 6 | [Fact] 7 | public void GetNumeralType() { 8 | var n5int = Numeral.GetNType(5); 9 | Assert.Equal(typeof(Sum, N1>), n5int); 10 | } 11 | 12 | [Fact] 13 | public void Sum0_Prohibited() { 14 | Assert.Throws(() => Sum, N0>.Num.GetHashCode()); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/PowersOfTwoTests.cs: -------------------------------------------------------------------------------- 1 | namespace TypeNum { 2 | using System; 3 | using System.Linq; 4 | using Xunit; 5 | using static System.FormattableString; 6 | public class PowersOfTwoTests { 7 | [Fact] 8 | public void NumsAreCorrect() { 9 | int num = N4096.Num; 10 | 11 | Assert.Equal(expected: PowersOfTwo.Max, num); 12 | } 13 | 14 | [Fact] 15 | public unsafe void SizesAreCorrect() { 16 | int size = sizeof(N4096); 17 | 18 | Assert.Equal(expected: PowersOfTwo.Max, size); 19 | } 20 | 21 | [Fact] 22 | public void PureNumeralsAreCorrect() { 23 | string ns = typeof(N0).Namespace; 24 | var numerals = Enumerable.Range(1, int.MaxValue) 25 | .Select(i => 1 << i) 26 | .Prepend(0) 27 | .TakeWhile(n => n <= PowersOfTwo.Max); 28 | foreach(int n in numerals) 29 | { 30 | string typeName = Invariant($"{ns}.N{n}"); 31 | var numeral = typeof(N0).Assembly.GetType(typeName); 32 | int num = Numeral.GetNum(numeral); 33 | Assert.Equal(num, n); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/TypeNum.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net7.0 5 | 11.0 6 | true 7 | TypeNum 8 | 9 | false 10 | 11 | 12 | 13 | 14 | 15 | 16 | all 17 | runtime; build; native; contentfiles; analyzers; buildtransitive 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | --------------------------------------------------------------------------------