├── .gitignore ├── .ocamlinit ├── CHANGES.md ├── COPYING ├── COPYING.LESSER ├── COPYING.LINKING ├── README.md ├── _tags ├── dune-project ├── jbuild-workspace.dev ├── lib ├── bitword.ml ├── bitword.mli ├── bitword_radixmap.ml ├── bitword_radixmap.mli ├── bitword_radixmap_sig.mli ├── dune ├── ip_radixmap.ml ├── ip_radixmap.mli ├── ip_radixmap_sig.mli ├── ip_radixset.ml ├── ip_radixset.mli └── ip_radixset_sig.mli ├── radixmap-ipaddr.opam ├── radixmap.opam └── tests ├── dune ├── test_bitword.ml ├── test_bitword_radixmap.ml ├── test_ip_radixmap.ml ├── test_ip_radixset.ml └── testkit.ml /.gitignore: -------------------------------------------------------------------------------- 1 | .merlin 2 | /_build 3 | /radixmap.install 4 | /radixmap-ipaddr.install 5 | -------------------------------------------------------------------------------- /.ocamlinit: -------------------------------------------------------------------------------- 1 | #use "topfind" 2 | #directory "_build/lib" 3 | #require "ipaddr" 4 | #load "radixmap.cma" 5 | #load "radixmap-ipaddr.cma" 6 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | There is no release yet. 4 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /COPYING.LESSER: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /COPYING.LINKING: -------------------------------------------------------------------------------- 1 | LGPL-3.0 LINKING EXCEPTION 2 | 3 | As a special exception to the GNU Lesser General Public License 4 | version 3 ("LGPL3"), the copyright holders of this Library give you 5 | permission to convey to a third party a Combined Work that links 6 | statically or dynamically to this Library without providing any 7 | Minimal Corresponding Source or Minimal Application Code as set out in 8 | 4d or providing the installation information set out in section 4e, 9 | provided that you comply with the other provisions of LGPL3 and 10 | provided that you meet, for the Application the terms and conditions 11 | of the license(s) which apply to the Application. 12 | 13 | Except as stated in this special exception, the provisions of LGPL3 14 | will continue to comply in full to this Library. If you modify this 15 | Library, you may apply this exception to your version of this Library, 16 | but you are not obliged to do so. If you do not wish to do so, delete 17 | this exception statement from your version. This exception does not 18 | (and cannot) modify any license terms which apply to the Application, 19 | with which you must still comply. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # radixmap - Map à la Binary Radix Tree 2 | 3 | This OCaml library provides a pure map-like data structure which efficiently 4 | represent piecewise constant functions over domains which can be binary 5 | subdivided. Suitable domains include bit strings, byte strings, and IP 6 | addresses. It is most efficient where the maps tend to be piecewise 7 | constant over prefixes of the key type, with optional nested exceptions 8 | which themselves have the same structure. 9 | 10 | This package has an optional dependency on 11 | [ipaddr](https://github.com/mirage/ocaml-ipaddr) and provides a related set 12 | implementation `Ip_radixset` if present. 13 | 14 | The core map, `Bitword_radixmap`, works on finite size `Bitword` sub-paths, 15 | whereas longer paths are encoded in the tree itself. This makes the maps 16 | compact for common cases. A higher level map or set can be implemented in 17 | terms of `Bitword_radixmap` if keys may exceed 24 bits. 18 | -------------------------------------------------------------------------------- /_tags: -------------------------------------------------------------------------------- 1 | true: bin_annot 2 | "lib": include 3 | 4 | : inline(4) 5 | : package(ipaddr) 6 | : package(ipaddr) 7 | : package(unix) 8 | -------------------------------------------------------------------------------- /dune-project: -------------------------------------------------------------------------------- 1 | (lang dune 1.3) 2 | -------------------------------------------------------------------------------- /jbuild-workspace.dev: -------------------------------------------------------------------------------- 1 | (context ((switch 4.04.2))) 2 | (context ((switch 4.03.0))) 3 | (context ((switch 4.02.3))) 4 | -------------------------------------------------------------------------------- /lib/bitword.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | type t = int 19 | 20 | (* Representation *) 21 | 22 | (* TODO: Need a compile-time constant for efficient switch to 56 bit width for 23 | * 64 bit native compilation. *) 24 | let length_width = 5 25 | let max_length = 24 26 | let floor_log2_halfwidth = 16 27 | 28 | let bit0 = 1 lsl length_width 29 | let length_mask = bit0 - 1 30 | let bitmask = (1 lsl max_length - 1) lsl length_width 31 | 32 | (* Helpers *) 33 | 34 | (* Lifted from prime_int.ml. *) 35 | let rec floor_log2_loop j n l = 36 | if j = 0 then l else 37 | if n lsr j = 0 38 | then floor_log2_loop (j / 2) n l 39 | else floor_log2_loop (j / 2) (n lsr j) (l + j) 40 | let floor_log2 n = floor_log2_loop floor_log2_halfwidth n 0 41 | 42 | (* Inspection and Slicing *) 43 | 44 | let length p = p land length_mask 45 | 46 | let bits p = p lsr length_width 47 | 48 | (* Comparison *) 49 | 50 | let equal (v : int) (w : int) = v = w 51 | 52 | let compare p q = 53 | let m, n = length p, length q in 54 | let p' = p lsl (max_length - m) land bitmask in 55 | let q' = q lsl (max_length - n) land bitmask in 56 | if p' < q' then -1 else 57 | if p' > q' then 1 else 58 | if m < n then -1 else if n > m then 1 else 0 59 | 60 | let common_prefix_length p q = 61 | let m, n = length p, length q in 62 | if m < n 63 | then length_width - 1 + m - floor_log2 (p lxor q lsr (n - m) lor length_mask) 64 | else length_width - 1 + n - floor_log2 (q lxor p lsr (m - n) lor length_mask) 65 | 66 | (* Construction *) 67 | 68 | let is_empty p = p = 0 69 | let is_full p = length p = max_length 70 | 71 | let empty = 0 72 | let c0 = 1 73 | let c1 = 1 lor bit0 74 | let c00 = 2 75 | let c01 = 2 lor bit0 76 | let c10 = 2 lor 2 lsl length_width 77 | let c11 = 2 lor 3 lsl length_width 78 | 79 | let make_unsafe n x = x lsl length_width lor n 80 | let make n x = 81 | if n < 0 || n > max_length || x lsr n <> 0 then invalid_arg "Bitword.make"; 82 | x lsl length_width lor n 83 | 84 | let const_unsafe n x = (if x then (1 lsl n - 1) lsl length_width else 0) lor n 85 | let const n x = 86 | if n < 0 || n > max_length then invalid_arg "Bitword.const" else 87 | const_unsafe n x 88 | 89 | let random_uniform n = 90 | if n < 0 || n > max_length then invalid_arg "Bitword.random_uniform"; 91 | let rec loop m acc = 92 | if m <= 0 then acc else 93 | loop (m - 30) (acc lsl 30 lor Random.bits ()) in 94 | (loop n 0 lsl length_width) land (1 lsl n - 1) lor n 95 | 96 | (* Endian-Dependent Operations *) 97 | 98 | module type ENDIAN_API = sig 99 | val init : int -> (int -> bool) -> t 100 | val get : t -> int -> bool 101 | 102 | val prefix : int -> t -> t 103 | val prefix_unsafe : int -> t -> t 104 | val suffix : int -> t -> t 105 | val suffix_unsafe : int -> t -> t 106 | 107 | val cut : int -> t -> t * t 108 | 109 | val push_exn : bool -> t -> t 110 | val push_c0_exn : t -> t 111 | val push_c0_unsafe : t -> t 112 | val push_c1_exn : t -> t 113 | val push_c1_unsafe : t -> t 114 | val drop_exn : t -> t 115 | val drop_unsafe : t -> t 116 | val pop_exn : t -> bool * t 117 | val pop_unsafe : t -> bool * t 118 | end 119 | 120 | module Be = struct 121 | 122 | let init n f = 123 | let rec loop i acc = 124 | if i = n then acc lsl length_width lor n else 125 | loop (i + 1) (acc lsl 1 lor (if f i then 1 else 0)) in 126 | if n < 0 || n > max_length then invalid_arg "Bitword.Be.init" else 127 | loop 0 0 128 | 129 | let get p i = p lsr (length p - 1 - i) land bit0 <> 0 130 | 131 | let prefix_unsafe k p = 132 | let n = length p in 133 | (p lsr (n - k) land bitmask) lor k 134 | 135 | let prefix k p = 136 | let n = length p in 137 | if k < 0 || k > n then invalid_arg "Bitword.Be.prefix" else 138 | (p lsr (n - k) land bitmask) lor k 139 | 140 | let suffix_unsafe k p = 141 | let n = length p in 142 | p land (1 lsl (n - k) - 1) lsl length_width lor (n - k) 143 | 144 | let suffix k p = 145 | let n = length p in 146 | if k < 0 || k > n then invalid_arg "Bitword.Be.suffix" else 147 | p land (1 lsl (n - k) - 1) lsl length_width lor (n - k) 148 | 149 | let cut k p = 150 | let n = length p in 151 | if k < 0 || k > n then invalid_arg "Bitword.Be.cut" else 152 | (prefix_unsafe k p, suffix_unsafe k p) 153 | 154 | let push_exn x p = 155 | let n = length p in 156 | if n >= max_length then invalid_arg "Bitword.Be.push_exn" else 157 | p lor (if x then bit0 lsl n else 0) + 1 158 | 159 | let push_c0_unsafe p = p + 1 160 | let push_c0_exn p = 161 | if length p >= max_length then invalid_arg "Bitword.Be.push_c0_exn" else 162 | push_c0_unsafe p 163 | 164 | let push_c1_unsafe p = p lor bit0 lsl length p + 1 165 | let push_c1_exn p = 166 | if length p >= max_length then invalid_arg "Bitword.Be.push_c1_exn" else 167 | push_c1_unsafe p 168 | 169 | let drop_unsafe p = 170 | let n = length p in 171 | p land (bit0 lsl (n - 1) - 1) - 1 172 | 173 | let drop_exn p = 174 | let n = length p in 175 | if n = 0 then invalid_arg "Bitword.Be.drop_exn" else 176 | p land (bit0 lsl (n - 1) - 1) - 1 177 | 178 | let pop_unsafe p = 179 | let n = length p in 180 | let bit = bit0 lsl (n - 1) in 181 | (p land bit <> 0, p land (lnot bit) - 1) 182 | 183 | let pop_exn p = 184 | let n = length p in 185 | if n = 0 then invalid_arg "Bitword.Be.pop_exn" else 186 | let bit = bit0 lsl (n - 1) in 187 | (p land bit <> 0, p land (lnot bit) - 1) 188 | end 189 | 190 | module Le = struct 191 | 192 | let init n f = 193 | let rec loop i acc = 194 | if i = n then acc lsl length_width lor n else 195 | loop (i + 1) (if f i then acc lor 1 lsl i else acc) in 196 | if n < 0 || n > max_length then invalid_arg "Bitword.Le.init" else 197 | loop 0 0 198 | 199 | let get p i = p lsr i land bit0 <> 0 200 | 201 | let prefix_unsafe k p = 202 | let n = length p in 203 | p lsr k land bitmask lor (n - k) 204 | 205 | let prefix k p = 206 | let n = length p in 207 | if k < 0 || k > n then invalid_arg "Bitword.Le.prefix" else 208 | p lsr k land bitmask lor (n - k) 209 | 210 | let suffix_unsafe k p = 211 | p land (1 lsl k - 1) lsl length_width lor k 212 | 213 | let suffix k p = 214 | let n = length p in 215 | if k < 0 || k > n then invalid_arg "Bitword.Le.suffix" else 216 | suffix_unsafe k p 217 | 218 | let cut k p = 219 | let n = length p in 220 | if k < 0 || k > n then invalid_arg "Bitword.Le.cut" else 221 | (p lsr k land bitmask lor (n - k), suffix_unsafe k p) 222 | 223 | let push_exn x p = 224 | let n = length p in 225 | if n >= max_length then invalid_arg "Bitword.Le.push_exn" else 226 | (p land bitmask) lsl 1 lor (if x then bit0 else 0) lor (n + 1) 227 | 228 | let push_c0_unsafe p = (p land bitmask) lsl 1 lor (length p + 1) 229 | let push_c0_exn p = 230 | if length p >= max_length then invalid_arg "Bitword.Le.push_c0_exn" else 231 | push_c0_unsafe p 232 | 233 | let push_c1_unsafe p = (p land bitmask) lsl 1 lor bit0 lor (length p + 1) 234 | let push_c1_exn p = 235 | if length p >= max_length then invalid_arg "Bitword.Le.push_c1_exn" else 236 | push_c1_unsafe p 237 | 238 | let drop_unsafe p = p lsr 1 land bitmask lor (length p - 1) 239 | let drop_exn p = 240 | if length p = 0 then invalid_arg "Bitword.Le.drop_exn" else 241 | drop_unsafe p 242 | 243 | let pop_unsafe p = (p land bit0 <> 0, drop_unsafe p) 244 | let pop_exn p = 245 | if length p = 0 then invalid_arg "Bitword.Le.pop_exn" else 246 | pop_unsafe p 247 | end 248 | 249 | (* Slice and Concatenate *) 250 | 251 | let cat_exn p q = 252 | let m = length p in 253 | let n = length q in 254 | if n + m > max_length then invalid_arg "Bitword.cat_exn" else 255 | (p land bitmask) lsl n lor q + m 256 | 257 | let cat_rem p q = 258 | let m = length p in 259 | let n = length q in 260 | let k = min m (max_length - n) in 261 | let p' = (p lsr k land bitmask) + (m - k) in 262 | let q' = ((p land bitmask) lsl n land bitmask) lor q + k in 263 | (p', q') 264 | 265 | (* Conversion and Pretty-Printing *) 266 | 267 | let pp_set fmtr w = 268 | let rec loop i u = 269 | if u <> 0 then begin 270 | if u land 1 <> 0 then begin 271 | Format.pp_print_int fmtr i; 272 | Format.pp_print_string fmtr ", " 273 | end; 274 | loop (i + 1) (u lsr 1) 275 | end in 276 | Format.pp_print_char fmtr '{'; 277 | loop 0 (w lsr length_width); 278 | Format.pp_print_char fmtr '}' 279 | 280 | let to_base2_string p = 281 | let n = length p in 282 | String.init n (fun i -> if Le.get p (n - i - 1) then '1' else '0') 283 | 284 | let pp_base2 fmtr p = Format.pp_print_string fmtr (to_base2_string p) 285 | 286 | let of_base2_string_exn s = 287 | let n = String.length s in 288 | if n > max_length then invalid_arg "Bitword.of_base2_string_exn"; 289 | Le.init n 290 | (fun i -> 291 | (match s.[i] with 292 | | '0' -> false 293 | | '1' -> true 294 | | _ -> invalid_arg "Bitword.of_base2_string_exn")) 295 | -------------------------------------------------------------------------------- /lib/bitword.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Limited-length bit vectors represented by a unboxed integers. 19 | 20 | This module provides bit vectors up to a maximum length, limited by what can 21 | be stored in an OCaml [int]. The maximum length is architecture-dependent 22 | but guaranteed to be a multiple of 8 and at least 24. 23 | *) 24 | 25 | type t = private int 26 | 27 | (** {2 Inspection and Slicing} *) 28 | 29 | val max_length : int 30 | 31 | val length : t -> int 32 | 33 | val bits : t -> int 34 | 35 | (** {2 Comparison} *) 36 | 37 | val equal : t -> t -> bool 38 | 39 | val compare : t -> t -> int 40 | 41 | val common_prefix_length : t -> t -> int 42 | 43 | (** {2 Construction} *) 44 | 45 | val is_empty : t -> bool 46 | val is_full : t -> bool 47 | 48 | val empty : t 49 | val c0 : t 50 | val c1 : t 51 | val c00 : t 52 | val c01 : t 53 | val c10 : t 54 | val c11 : t 55 | 56 | val const : int -> bool -> t 57 | val const_unsafe : int -> bool -> t 58 | 59 | val make : int -> int -> t 60 | val make_unsafe : int -> int -> t 61 | 62 | val cat_exn : t -> t -> t 63 | val cat_rem : t -> t -> t * t 64 | 65 | val random_uniform : int -> t 66 | 67 | (** {2 Endian-Dependent Operations} *) 68 | 69 | module type ENDIAN_API = sig 70 | val init : int -> (int -> bool) -> t 71 | val get : t -> int -> bool 72 | 73 | (** {2 Parts} *) 74 | 75 | val prefix : int -> t -> t 76 | val prefix_unsafe : int -> t -> t 77 | val suffix : int -> t -> t 78 | val suffix_unsafe : int -> t -> t 79 | val cut : int -> t -> t * t 80 | 81 | (** {2 Queue-Like Operations} 82 | 83 | These functions operate around index 0. That is, for big-endian it 84 | changes the prefix bit and for low-endian the suffix bit. *) 85 | 86 | val push_exn : bool -> t -> t 87 | val push_c0_exn : t -> t 88 | val push_c0_unsafe : t -> t 89 | val push_c1_exn : t -> t 90 | val push_c1_unsafe : t -> t 91 | val drop_exn : t -> t 92 | val drop_unsafe : t -> t 93 | val pop_exn : t -> bool * t 94 | val pop_unsafe : t -> bool * t 95 | end 96 | 97 | module Be : ENDIAN_API 98 | module Le : ENDIAN_API 99 | 100 | (** {2 Parsing and Pretty-Printing} *) 101 | 102 | val pp_set : Format.formatter -> t -> unit 103 | val pp_base2 : Format.formatter -> t -> unit 104 | 105 | val to_base2_string : t -> string 106 | 107 | val of_base2_string_exn : string -> t 108 | -------------------------------------------------------------------------------- /lib/bitword_radixmap.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | open Bitword_radixmap_sig 19 | 20 | let cat_bitword p pN ps = 21 | let p', pN' = Bitword.cat_rem p pN in 22 | if Bitword.is_empty p' then (pN', ps) else (pN', (p' :: ps)) 23 | 24 | module Poly = struct 25 | type path = Bitword.t 26 | type ('a, 'id) t = 27 | | Const of 'a 28 | | Appose of ('a, 'id) t * ('a, 'id) t 29 | | Unzoom of 'a * Bitword.t * ('a, 'id) t 30 | 31 | let rec max_depth = function 32 | | Const _ -> 0 33 | | Appose (h0, h1) -> 1 + max (max_depth h0) (max_depth h1) 34 | | Unzoom (_, p, h) -> Bitword.length p + max_depth h 35 | 36 | let recurse ~const ~appose ~unzoom = function 37 | | Const x -> const x 38 | | Appose (h0, h1) -> appose h0 h1 39 | | Unzoom (x, p, h) -> unzoom x p h 40 | 41 | let rec cata ~const ~appose ~unzoom = function 42 | | Const x -> 43 | const x 44 | | Appose (h0, h1) -> 45 | appose (cata ~const ~appose ~unzoom h0) (cata ~const ~appose ~unzoom h1) 46 | | Unzoom (x, p, h) -> 47 | unzoom x p (cata ~const ~appose ~unzoom h) 48 | 49 | let const x = Const x 50 | let is_const = function Const _ -> true | _ -> false 51 | let value = function Const x -> Some x | _ -> None 52 | 53 | let rec zoom pZ h = 54 | if Bitword.is_empty pZ then h else 55 | (match h with 56 | | Const _ -> h 57 | | Appose (h0, h1) -> 58 | let kZ, pZ' = Bitword.Be.pop_exn pZ in 59 | zoom pZ' (if kZ then h1 else h0) 60 | | Unzoom (xH, pN, Const xN) 61 | when Bitword.equal (Bitword.Le.drop_exn pN) pZ -> 62 | if Bitword.Le.get pN 0 63 | then Appose (Const xH, Const xN) 64 | else Appose (Const xN, Const xH) 65 | | Unzoom (xH, pN, hN) -> 66 | let nZ = Bitword.length pZ in 67 | let nN = Bitword.length pN in 68 | if nZ > nN then 69 | let pC, pZ' = Bitword.Be.cut nN pZ in 70 | if Bitword.equal pC pN then zoom pZ' hN else (Const xH) 71 | else if nZ = nN then 72 | if Bitword.equal pZ pN then hN else (Const xH) 73 | else 74 | let pC, pN' = Bitword.Be.cut nZ pN in 75 | if Bitword.equal pC pZ then Unzoom (xH, pN', hN) else (Const xH)) 76 | 77 | let zoom_string n p h = 78 | let rec loop i h = 79 | if i + 24 <= n then 80 | let bits = Char.code p.[i / 8] lsl 16 lor 81 | Char.code p.[i / 8 + 1] lsl 8 lor 82 | Char.code p.[i / 8 + 2] in 83 | loop (i + 24) (zoom (Bitword.make 24 bits) h) 84 | else if i + 8 <= n then 85 | let bits = Char.code p.[i / 8] in 86 | loop (i + 8) (zoom (Bitword.make 8 bits) h) 87 | else if i < n then 88 | let bits = Char.code p.[i / 8] lsr (8 - n + i) in 89 | zoom (Bitword.make (n - i) bits) h 90 | else 91 | h in 92 | loop 0 h 93 | 94 | let rec pp f fmtr m = 95 | Format.pp_print_char fmtr '{'; 96 | (match m with 97 | | Const x -> 98 | Format.pp_print_string fmtr "_ ↦ "; 99 | f fmtr x 100 | | Appose (m0, m1) -> 101 | Format.pp_print_string fmtr "0 ↦ "; 102 | pp f fmtr m0; 103 | Format.pp_print_string fmtr ", 1 ↦ "; 104 | pp f fmtr m1; 105 | | Unzoom (x, p, mN) -> 106 | Bitword.pp_base2 fmtr p; 107 | Format.pp_print_string fmtr " ↦ "; 108 | pp f fmtr mN; 109 | Format.pp_print_string fmtr ", _ ↦ "; 110 | f fmtr x); 111 | Format.pp_print_char fmtr '}' 112 | end 113 | 114 | module Make (Cod : EQUAL) = struct 115 | open Poly 116 | 117 | type path = Bitword.t 118 | type cod = Cod.t 119 | type t_id 120 | type t = (cod, t_id) Poly.t 121 | 122 | let const = Poly.const 123 | let is_const = Poly.is_const 124 | let value = Poly.value 125 | let zoom = Poly.zoom 126 | let zoom_string = Poly.zoom_string 127 | let recurse = Poly.recurse 128 | let cata = Poly.cata 129 | 130 | let rec valid = function 131 | | Const _ -> true 132 | | Appose (Const x0, Const x1) -> not (Cod.equal x0 x1) 133 | | Appose (Const _, _) | Appose (_, Const _) -> false 134 | | Appose (h0, h1) -> valid h0 && valid h1 135 | | Unzoom (xH, _, Appose (Const x0, Const x1)) -> 136 | not (Cod.equal xH x0 || Cod.equal xH x1 || Cod.equal x0 x1) 137 | | Unzoom (xH, p, Const xN) -> 138 | not (Bitword.is_empty p) && not (Cod.equal xH xN) 139 | | Unzoom (xH, p, (Unzoom (xH', p', _) as hN)) when Cod.equal xH xH' -> 140 | not (Bitword.is_empty p) && Bitword.is_full p' && valid hN 141 | | Unzoom (_, p, hN) -> 142 | not (Bitword.is_empty p) && valid hN 143 | 144 | let rec equal hA hB = 145 | (match hA, hB with 146 | | Const xA, Const xB -> Cod.equal xA xB 147 | | Const _, _ | _, Const _ -> false 148 | | Appose (h0A, h1A), Appose (h0B, h1B) -> equal h0A h0B && equal h1A h1B 149 | | Appose _, _ | _, Appose _ -> false 150 | | Unzoom (xA, pA, hAN), Unzoom (xB, pB, hBN) -> 151 | Cod.equal xA xB && Bitword.equal pA pB && equal hAN hBN) 152 | 153 | let rec unzoom xA pA h = 154 | if Bitword.is_empty pA then h else 155 | (match h with 156 | | Const x when Cod.equal x xA -> h 157 | | Const _ when Bitword.length pA = 1 -> 158 | if Bitword.Be.get pA 0 159 | then Appose (Const xA, h) 160 | else Appose (h, Const xA) 161 | | Appose (h0, Const x1) when Cod.equal x1 xA -> 162 | unzoom xA pA (Unzoom (xA, Bitword.c0, h0)) 163 | | Appose (Const x0, h1) when Cod.equal x0 xA -> 164 | unzoom xA pA (Unzoom (xA, Bitword.c1, h1)) 165 | | Unzoom (xH, pN, hN) when Cod.equal xH xA -> 166 | let pA', pN' = Bitword.cat_rem pA pN in 167 | if Bitword.is_empty pA' 168 | then Unzoom (xH, pN', hN) 169 | else Unzoom (xH, pA', Unzoom (xH, pN', hN)) 170 | | Const _ | Appose _ | Unzoom _ -> Unzoom (xA, pA, h)) 171 | 172 | let rec unzoom_string xA l s h = 173 | if l = 0 then h else 174 | let k, bits = 175 | if l >= 24 then 176 | let bits = Char.code s.[l / 8 - 3] lsl 16 177 | lor Char.code s.[l / 8 - 2] lsl 8 178 | lor Char.code s.[l / 8 - 1] in 179 | if l mod 8 = 0 then (24, bits) else 180 | let bits = bits lsl (l mod 8) land 0xffffff 181 | lor Char.code s.[l / 8] lsr (8 - l mod 8) in 182 | (24, bits) 183 | else if l >= 16 then 184 | let bits = Char.code s.[l / 8 - 2] lsl 8 185 | lor Char.code s.[l / 8 - 1] in 186 | if l mod 8 = 0 then (16, bits) else 187 | let bits = bits lsl (l mod 8) land 0xffff 188 | lor Char.code s.[l / 8] lsr (8 - l mod 8) in 189 | (16, bits) 190 | else if l >= 8 then 191 | let bits = Char.code s.[l / 8 - 1] in 192 | if l mod 8 = 0 then (8, bits) else 193 | let bits = bits lsl (l mod 8) land 0xff 194 | lor Char.code s.[l / 8] lsr (8 - l mod 8) in 195 | (8, bits) 196 | else 197 | (l, Char.code s.[0] lsr (8 - l)) in 198 | unzoom_string xA (l - k) s (unzoom xA (Bitword.make k bits) h) 199 | 200 | let appose h0 h1 = 201 | (match h0, h1 with 202 | | Const x0, Const x1 -> 203 | if Cod.equal x0 x1 then Const x0 else Appose (h0, h1) 204 | | Unzoom (xH, pN, hN), Const xC when Cod.equal xC xH -> 205 | if Bitword.is_full pN 206 | then Unzoom (xH, Bitword.c0, h0) 207 | else Unzoom (xH, Bitword.Be.push_c0_exn pN, hN) 208 | | Const xC, Unzoom (xH, pN, hN) when Cod.equal xC xH -> 209 | if Bitword.is_full pN 210 | then Unzoom (xH, Bitword.c1, h1) 211 | else Unzoom (xH, Bitword.Be.push_c1_exn pN, hN) 212 | | Appose ((Const x00 as h00), (Const x01 as h01)), Const x1 -> 213 | if Cod.equal x1 x01 then Unzoom (x1, Bitword.c00, h00) else 214 | if Cod.equal x1 x00 then Unzoom (x1, Bitword.c01, h01) else 215 | Unzoom (x1, Bitword.c0, h0) 216 | | Const x0, Appose ((Const x10 as h10), (Const x11 as h11)) -> 217 | if Cod.equal x0 x10 then Unzoom (x0, Bitword.c11, h11) else 218 | if Cod.equal x0 x11 then Unzoom (x0, Bitword.c10, h10) else 219 | Unzoom (x0, Bitword.c1, h1) 220 | | _, Const x1 -> Unzoom (x1, Bitword.c0, h0) 221 | | Const x0, _ -> Unzoom (x0, Bitword.c1, h1) 222 | | (Appose _ | Unzoom _), (Appose _ | Unzoom _) -> Appose (h0, h1)) 223 | 224 | let catai_bytes ?index_buffer_size ~make_index ~const ~appose ~unzoom h = 225 | let pbuf = 226 | Bytes.make 227 | (match index_buffer_size with 228 | | None -> ((max_depth h + 7) / 8) 229 | | Some n -> n) '\x00' in 230 | let rec loop plen zlen = function 231 | | Const x -> 232 | const (make_index plen pbuf) x 233 | | Appose (h0, h1) -> 234 | let byte = Char.code (Bytes.get pbuf (plen / 8)) in 235 | let bit = 0x80 lsr (plen mod 8) in 236 | let acc0 = loop (plen + 1) 0 h0 in 237 | Bytes.set pbuf (plen / 8) (Char.chr (byte lor bit)); 238 | let acc1 = loop (plen + 1) 0 h1 in 239 | Bytes.set pbuf (plen / 8) (Char.chr byte); 240 | appose (make_index plen pbuf) acc0 acc1 241 | | Unzoom (x, p, h) -> 242 | let bp = Bitword.bits p in 243 | let lp = Bitword.length p in 244 | let first_byte = Char.code (Bytes.get pbuf (plen / 8)) in 245 | if lp <= 8 - plen mod 8 then begin 246 | let first_bits = bp lsl (8 - plen mod 8 - lp) in 247 | Bytes.set pbuf (plen / 8) (Char.chr (first_byte lor first_bits)); 248 | let acc = 249 | (match h with 250 | | Unzoom (x', _, _) when Cod.equal x' x -> 251 | loop (plen + lp) (zlen + lp) h 252 | | _ -> 253 | loop (plen + lp) 0 h 254 | |> unzoom x (zlen + lp) (make_index (plen + lp) pbuf)) in 255 | Bytes.set pbuf (plen / 8) (Char.chr first_byte); 256 | acc 257 | end else begin 258 | let first_bits = bp lsr (lp - 8 + plen mod 8) in 259 | Bytes.set pbuf (plen / 8) (Char.chr (first_byte lor first_bits)); 260 | let rec ext_pbuf l j = 261 | if l >= 8 then begin 262 | Bytes.set pbuf j (Char.chr (bp lsr (l - 8) land 0xff)); 263 | ext_pbuf (l - 8) (j + 1) 264 | end else if l > 0 then begin 265 | Bytes.set pbuf j (Char.chr (bp lsl (8 - l) land 0xff)) 266 | end in 267 | let lp' = lp - 8 + plen mod 8 in 268 | ext_pbuf lp' (plen / 8 + 1); 269 | let acc = 270 | (match h with 271 | | Unzoom (x', _, _) when Cod.equal x' x -> 272 | loop (plen + lp) (zlen + lp) h 273 | | _ -> 274 | loop (plen + lp) 0 h 275 | |> unzoom x (zlen + lp) (make_index (plen + lp) pbuf)) in 276 | Bytes.fill pbuf (plen / 8 + 1) ((lp' + 7) / 8) '\x00'; 277 | Bytes.set pbuf (plen / 8) (Char.chr first_byte); 278 | acc 279 | end in 280 | loop 0 0 h 281 | 282 | let rec modify pA f h = 283 | if Bitword.is_empty pA then f h else 284 | (match h with 285 | | Const xH -> 286 | (match f h with 287 | | Const xN when Cod.equal xH xN -> h 288 | | hN -> unzoom xH pA hN) 289 | | Appose (h0, h1) -> 290 | let k, pA' = Bitword.Be.pop_exn pA in 291 | if k then appose h0 (modify pA' f h1) 292 | else appose (modify pA' f h0) h1 293 | | Unzoom (xH, pN, hN) -> 294 | let nA = Bitword.length pA in 295 | let nN = Bitword.length pN in 296 | let nC = Bitword.common_prefix_length pN pA in 297 | if nC = nA then 298 | let pN' = Bitword.Be.suffix nC pN in 299 | unzoom xH pA (f (unzoom xH pN' hN)) 300 | else if nC = nN then 301 | let pA' = Bitword.Be.suffix nC pA in 302 | unzoom xH pN (modify pA' f hN) 303 | else 304 | let pC = Bitword.Be.prefix nC pN in 305 | let kA = Bitword.Be.get pA nC in 306 | let pA' = Bitword.Be.suffix (nC + 1) pA in 307 | let pN' = Bitword.Be.suffix (nC + 1) pN in 308 | let hA' = unzoom xH pA' (f (Const xH)) in 309 | let hN' = unzoom xH pN' hN in 310 | unzoom xH pC (if kA then appose hN' hA' else appose hA' hN')) 311 | 312 | let modify_string n p f h = 313 | let rec loop i h = 314 | if i + 24 <= n then 315 | let bits = Char.code p.[i / 8] lsl 16 lor 316 | Char.code p.[i / 8 + 1] lsl 8 lor 317 | Char.code p.[i / 8 + 2] in 318 | modify (Bitword.make 24 bits) (loop (i + 24)) h 319 | else if i + 8 <= n then 320 | let bits = Char.code p.[i / 8] in 321 | modify (Bitword.make 8 bits) (loop (i + 8)) h 322 | else if i < n then 323 | let bits = Char.code p.[i / 8] lsr (8 - n + i) in 324 | modify (Bitword.make (n - i) bits) f h 325 | else 326 | f h in 327 | loop 0 h 328 | 329 | let rec map f = function 330 | | Const x -> Const (f x) 331 | | Appose (h0, h1) -> appose (map f h0) (map f h1) 332 | | Unzoom (x, p, h) -> unzoom (f x) p (map f h) 333 | 334 | let mapi f h = 335 | let rec loop p ps = function 336 | | Const x -> 337 | Const (f (p :: ps) x) 338 | | Appose (h0, h1) -> 339 | if Bitword.is_full p then 340 | appose (loop Bitword.c0 (p :: ps) h0) 341 | (loop Bitword.c1 (p :: ps) h1) 342 | else 343 | appose (loop (Bitword.Be.push_c0_exn p) ps h0) 344 | (loop (Bitword.Be.push_c1_exn p) ps h1) 345 | | Unzoom (x, pN, hN) -> 346 | let p', ps' = cat_bitword p pN ps in 347 | unzoom (f (p :: ps) x) p (loop p' ps' hN) in 348 | (match h with 349 | | Const x -> Const (f [] x) 350 | | Appose (h0, h1) -> appose (loop Bitword.c0 [] h0) (loop Bitword.c1 [] h1) 351 | | Unzoom (x, pN, hN) -> unzoom (f [] x) pN (loop pN [] hN)) 352 | 353 | let rec merge f hA hB = 354 | (match hA, hB with 355 | | Const xA, Const xB -> 356 | Const (f xA xB) 357 | | Const xA, Appose (h0B, h1B) -> 358 | let g xB = f xA xB in appose (map g h0B) (map g h1B) 359 | | Appose (h0A, h1A), Const xB -> 360 | let g xA = f xA xB in appose (map g h0A) (map g h1A) 361 | | Const xA, Unzoom (xB, pB, hNB) -> 362 | unzoom (f xA xB) pB (map (fun x -> f xA x) hNB) 363 | | Unzoom (xA, pA, hNA), Const xB -> 364 | unzoom (f xA xB) pA (map (fun x -> f x xB) hNA) 365 | | Appose (h0A, h1A), Appose (h0B, h1B) -> 366 | appose (merge f h0A h0B) (merge f h1A h1B) 367 | | Appose (h0A, h1A), Unzoom (xB, pB, hNB) -> 368 | let kB, pB' = Bitword.Be.pop_exn pB in 369 | let hB' = if Bitword.is_empty pB' then hNB else Unzoom (xB, pB', hNB) in 370 | if kB then appose (map (fun x -> f x xB) h0A) (merge f h1A hB') 371 | else appose (merge f h0A hB') (map (fun x -> f x xB) h1A) 372 | | Unzoom (xA, pA, hNA), Appose (h0B, h1B) -> 373 | let kA, pA' = Bitword.Be.pop_exn pA in 374 | let hA' = if Bitword.is_empty pA' then hNA else Unzoom (xA, pA', hNA) in 375 | if kA then appose (map (fun x -> f xA x) h0B) (merge f hA' h1B) 376 | else appose (merge f hA' h0B) (map (fun x -> f xA x) h1B) 377 | | Unzoom (xA, pA, hNA), Unzoom (xB, pB, hNB) -> 378 | let xAB = f xA xB in 379 | let nA = Bitword.length pA in 380 | let nB = Bitword.length pB in 381 | let nC = Bitword.common_prefix_length pA pB in 382 | if nC = nA then 383 | if nC = nB then unzoom xAB pA (merge f hNA hNB) else 384 | let hB' = Unzoom (xB, Bitword.Be.suffix nC pB, hNB) in 385 | unzoom xAB pA (merge f hNA hB') 386 | else if nC = nB then 387 | let hA' = Unzoom (xA, Bitword.Be.suffix nC pA, hNA) in 388 | unzoom xAB pB (merge f hA' hNB) 389 | else 390 | let pC = Bitword.Be.prefix nC pA in 391 | let kA = Bitword.Be.get pA nC in 392 | let pA' = Bitword.Be.suffix (nC + 1) pA in 393 | let pB' = Bitword.Be.suffix (nC + 1) pB in 394 | let hA' = unzoom xAB pA' (map (fun x -> f x xB) hNA) in 395 | let hB' = unzoom xAB pB' (map (fun x -> f xA x) hNB) in 396 | unzoom xAB pC (if kA then appose hB' hA' else appose hA' hB')) 397 | end 398 | -------------------------------------------------------------------------------- /lib/bitword_radixmap.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Core radix map using bitword keys. 19 | 20 | This is the implementation by with the others are based. It is somewhat 21 | low-level in that keys are transferred in chunks of a limited maximum size. 22 | The underlying is scale invaniant, and keys of arbitrary length can be used 23 | by iteratively zooming or combining subtrees. 24 | 25 | The map represent a complete function; to represent the more common kind of 26 | map, an option type may be used for the codomain. *) 27 | 28 | open Bitword_radixmap_sig 29 | 30 | module Poly : sig 31 | type path = Bitword.t 32 | type ('a, 'id) t 33 | 34 | val const : 'a -> ('a, 'id) t 35 | val is_const : ('a, _) t -> bool 36 | val value : ('a, _) t -> 'a option 37 | val zoom : path -> ('a, 'id) t -> ('a, 'id) t 38 | val zoom_string : int -> string -> ('a, 'id) t -> ('a, 'id) t 39 | 40 | val recurse : 41 | const: ('a -> 'b) -> 42 | appose: (('a, 'id) t -> ('a, 'id) t -> 'b) -> 43 | unzoom: ('a -> path -> ('a, 'id) t -> 'b) -> 44 | ('a, 'id) t -> 'b 45 | 46 | val cata : 47 | const: ('a -> 'b) -> 48 | appose: ('b -> 'b -> 'b) -> 49 | unzoom: ('a -> path -> 'b -> 'b) -> 50 | ('a, 'id) t -> 'b 51 | 52 | val pp : (Format.formatter -> 'a -> unit) -> 53 | Format.formatter -> ('a, _) t -> unit 54 | end 55 | 56 | module Make (Cod : EQUAL) : S 57 | with type cod = Cod.t 58 | and type ('a, 'id) poly := ('a, 'id) Poly.t 59 | -------------------------------------------------------------------------------- /lib/bitword_radixmap_sig.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Signature for {!Bitword_radixmap}. *) 19 | 20 | module type EQUAL = sig 21 | type t 22 | val equal : t -> t -> bool 23 | end 24 | 25 | module type S = sig 26 | type path = Bitword.t 27 | type cod 28 | type ('a, 'id) poly 29 | type t_id 30 | type t = (cod, t_id) poly 31 | 32 | val equal : t -> t -> bool 33 | 34 | val const : cod -> t 35 | val is_const : t -> bool 36 | val value : t -> cod option 37 | val appose : t -> t -> t 38 | val unzoom : cod -> path -> t -> t 39 | val unzoom_string : cod -> int -> string -> t -> t 40 | val zoom : path -> t -> t 41 | val zoom_string : int -> string -> t -> t 42 | 43 | val modify : path -> (t -> t) -> t -> t 44 | val modify_string : int -> string -> (t -> t) -> t -> t 45 | val map : ('a -> cod) -> ('a, _) poly -> t 46 | val mapi : (path list -> 'a -> cod) -> ('a, _) poly -> t 47 | val merge : ('a -> 'b -> cod) -> ('a, _) poly -> ('b, _) poly -> t 48 | 49 | val recurse : 50 | const: (cod -> 'b) -> 51 | appose: (t -> t -> 'b) -> 52 | unzoom: (cod -> path -> t -> 'b) -> 53 | t -> 'b 54 | 55 | val cata : 56 | const: (cod -> 'b) -> 57 | appose: ('b -> 'b -> 'b) -> 58 | unzoom: (cod -> path -> 'b -> 'b) -> 59 | t -> 'b 60 | 61 | val catai_bytes : 62 | ?index_buffer_size: int -> make_index: (int -> Bytes.t -> 'i) -> 63 | const: ('i -> cod -> 'b) -> 64 | appose: ('i -> 'b -> 'b -> 'b) -> 65 | unzoom: (cod -> int -> 'i -> 'b -> 'b) -> 66 | t -> 'b 67 | 68 | val valid : t -> bool 69 | end 70 | -------------------------------------------------------------------------------- /lib/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name radixmap) 3 | (public_name radixmap) 4 | (wrapped false) 5 | (modules 6 | Bitword 7 | Bitword_radixmap 8 | Bitword_radixmap_sig) 9 | (modules_without_implementation 10 | Bitword_radixmap_sig)) 11 | 12 | (library 13 | (name radixmap_ipaddr) 14 | (public_name radixmap-ipaddr) 15 | (wrapped false) 16 | (modules 17 | Ip_radixset 18 | Ip_radixset_sig 19 | Ip_radixmap 20 | Ip_radixmap_sig) 21 | (modules_without_implementation 22 | Ip_radixset_sig 23 | Ip_radixmap_sig) 24 | (libraries ipaddr radixmap)) 25 | -------------------------------------------------------------------------------- /lib/ip_radixmap.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module Poly_v4 = struct 19 | type ('a, 'id) t = ('a option, 'id) Bitword_radixmap.Poly.t 20 | end 21 | module Poly_v6 = Poly_v4 22 | 23 | module type BASE = sig 24 | type address 25 | type network 26 | type ('a, 'id) poly 27 | 28 | val max_length : int 29 | val bytes_of_address : address -> string 30 | val bytes_of_network : network -> string 31 | val length_of_network : network -> int 32 | 33 | val make_index : int -> Bytes.t -> network 34 | end 35 | 36 | module V4_base = struct 37 | type address = Ipaddr.V4.t 38 | type network = Ipaddr.V4.Prefix.t 39 | type ('a, 'id) poly = ('a, 'id) Poly_v4.t 40 | 41 | let max_length = 32 42 | let bytes_of_address addr = Ipaddr.V4.to_bytes addr 43 | let bytes_of_network net = Ipaddr.V4.to_bytes (Ipaddr.V4.Prefix.network net) 44 | let length_of_network net = Ipaddr.V4.Prefix.bits net 45 | 46 | let make_index plen pbuf = 47 | Ipaddr.V4.Prefix.make plen (Ipaddr.V4.of_bytes_exn (Bytes.to_string pbuf)) 48 | end 49 | 50 | module V6_base = struct 51 | type address = Ipaddr.V6.t 52 | type network = Ipaddr.V6.Prefix.t 53 | type ('a, 'id) poly = ('a, 'id) Poly_v6.t 54 | 55 | let max_length = 128 56 | let bytes_of_address addr = Ipaddr.V6.to_bytes addr 57 | let bytes_of_network net = Ipaddr.V6.to_bytes (Ipaddr.V6.Prefix.network net) 58 | let length_of_network net = Ipaddr.V6.Prefix.bits net 59 | 60 | let make_index plen pbuf = 61 | Ipaddr.V6.Prefix.make plen (Ipaddr.V6.of_bytes_exn (Bytes.to_string pbuf)) 62 | end 63 | 64 | module Make (Base : BASE) (Cod : Bitword_radixmap_sig.EQUAL) = struct 65 | include Base 66 | 67 | module Cod_option = struct 68 | type t = Cod.t option 69 | let equal (x : t) (y : t) = 70 | (match x, y with 71 | | None, None -> true 72 | | Some x, Some y -> Cod.equal x y 73 | | None, Some _ | Some _, None -> false) 74 | end 75 | module M = Bitword_radixmap.Make (Cod_option) 76 | 77 | type cod = Cod.t 78 | type t_id = M.t_id 79 | type t = M.t 80 | 81 | let empty = M.const None 82 | let is_empty m = M.value m = Some None 83 | 84 | let equal = M.equal 85 | 86 | let get_address m addr = 87 | match 88 | m |> M.zoom_string max_length (bytes_of_address addr) 89 | |> M.value 90 | with 91 | | None -> None 92 | | Some x -> x 93 | 94 | let get_network m netw = 95 | match 96 | m |> M.zoom_string (length_of_network netw) (bytes_of_network netw) 97 | |> M.value 98 | with 99 | | None -> None 100 | | Some x -> x 101 | 102 | let add_address addr x m = 103 | M.modify_string max_length (bytes_of_address addr) 104 | (fun _ -> M.const (Some x)) m 105 | 106 | let add_network netw x m = 107 | M.modify_string (length_of_network netw) (bytes_of_network netw) 108 | (fun _ -> M.const (Some x)) m 109 | 110 | let remove_address addr m = 111 | M.modify_string max_length (bytes_of_address addr) 112 | (fun _ -> M.const None) m 113 | 114 | let remove_network netw m = 115 | M.modify_string (length_of_network netw) (bytes_of_network netw) 116 | (fun _ -> M.const None) m 117 | 118 | let inter_network netw m = 119 | let l = length_of_network netw in 120 | let s = bytes_of_network netw in 121 | m |> M.zoom_string l s |> M.unzoom_string None l s 122 | 123 | let map f = M.map (function None -> None | Some x -> Some (f x)) 124 | let filter f = M.map (function Some x as xopt when f x -> xopt | _ -> None) 125 | 126 | let catai ~const ~appose ~unzoom m = 127 | M.catai_bytes 128 | ~index_buffer_size:(max_length / 8) ~make_index 129 | ~const ~appose ~unzoom m 130 | 131 | let merge = M.merge 132 | end 133 | 134 | module Make_v4 = Make (V4_base) 135 | module Make_v6 = Make (V6_base) 136 | -------------------------------------------------------------------------------- /lib/ip_radixmap.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Radix map over IP networks. *) 19 | 20 | module Poly_v4 : sig 21 | type ('a, 'id) t 22 | end 23 | 24 | module Poly_v6 : sig 25 | type ('a, 'id) t 26 | end 27 | 28 | module Make_v4 (Cod : Bitword_radixmap_sig.EQUAL) : Ip_radixmap_sig.S 29 | with type address = Ipaddr.V4.t 30 | and type network = Ipaddr.V4.Prefix.t 31 | and type cod = Cod.t 32 | and type ('a, 'id) poly = ('a, 'id) Poly_v4.t 33 | 34 | module Make_v6 (Cod : Bitword_radixmap_sig.EQUAL) : Ip_radixmap_sig.S 35 | with type address = Ipaddr.V6.t 36 | and type network = Ipaddr.V6.Prefix.t 37 | and type cod = Cod.t 38 | and type ('a, 'id) poly = ('a, 'id) Poly_v6.t 39 | -------------------------------------------------------------------------------- /lib/ip_radixmap_sig.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Signature for {!Ip_radixmap}. *) 19 | 20 | module type S = sig 21 | type address 22 | type network 23 | type cod 24 | type ('a, 'id) poly 25 | type t_id 26 | type t = (cod, t_id) poly 27 | 28 | val empty : t 29 | val is_empty : t -> bool 30 | 31 | val equal : t -> t -> bool 32 | 33 | val get_address : t -> address -> cod option 34 | val get_network : t -> network -> cod option 35 | 36 | val add_address : address -> cod -> t -> t 37 | val add_network : network -> cod -> t -> t 38 | val remove_address : address -> t -> t 39 | val remove_network : network -> t -> t 40 | val inter_network : network -> t -> t 41 | 42 | val map : ('a -> cod) -> ('a, _) poly -> t 43 | val filter : (cod -> bool) -> (cod, _) poly -> t 44 | 45 | val merge : ('a option -> 'b option -> cod option) -> 46 | ('a, _) poly -> ('b, _) poly -> t 47 | 48 | val catai : 49 | const: (network -> cod option -> 'a) -> 50 | appose: (network -> 'a -> 'a -> 'a) -> 51 | unzoom: (cod option -> int -> network -> 'a -> 'a) -> 52 | t -> 'a 53 | end 54 | -------------------------------------------------------------------------------- /lib/ip_radixset.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module type BASE = sig 19 | type t 20 | type address 21 | type network 22 | 23 | val max_length : int 24 | val bytes_of_address : address -> string 25 | val bytes_of_network : network -> string 26 | val length_of_network : network -> int 27 | 28 | val is_empty : t -> bool 29 | val is_full : t -> bool 30 | val of_network : network -> t 31 | val contains_address : t -> address -> bool 32 | val zoom_network : network -> t -> t 33 | val make_index : int -> Bytes.t -> network 34 | end 35 | 36 | module Bool_equal = struct 37 | type t = bool 38 | let equal (x : t) (y : t) = x = y 39 | end 40 | 41 | module M = Bitword_radixmap.Make (Bool_equal) 42 | open M 43 | 44 | module V4_base = struct 45 | type t = M.t 46 | type address = Ipaddr.V4.t 47 | type network = Ipaddr.V4.Prefix.t 48 | 49 | let max_length = 32 50 | let bytes_of_address addr = Ipaddr.V4.to_bytes addr 51 | let bytes_of_network net = Ipaddr.V4.to_bytes (Ipaddr.V4.Prefix.network net) 52 | let length_of_network net = Ipaddr.V4.Prefix.bits net 53 | 54 | let is_empty s = value s = Some false 55 | let is_full s = value s = Some true 56 | 57 | let of_network pfx = 58 | let l = Ipaddr.V4.Prefix.bits pfx in 59 | let netH, netL = Ipaddr.V4.Prefix.network pfx |> Ipaddr.V4.to_int16 in 60 | if l <= 16 then 61 | const true |> unzoom false (Bitword.make l (netH lsr (16 - l))) 62 | else 63 | const true |> unzoom false (Bitword.make (l - 16) (netL lsr (32 - l))) 64 | |> unzoom false (Bitword.make 16 netH) 65 | 66 | let contains_address s addr = 67 | let aH, aL = Ipaddr.V4.to_int16 addr in 68 | s |> zoom (Bitword.make 16 aH) 69 | |> zoom (Bitword.make 16 aL) 70 | |> is_full 71 | 72 | let zoom_network pfx s = 73 | let l = Ipaddr.V4.Prefix.bits pfx in 74 | let aH, aL = Ipaddr.V4.Prefix.network pfx |> Ipaddr.V4.to_int16 in 75 | if l <= 16 then 76 | s |> zoom (Bitword.make l (aH lsr (16 - l))) 77 | else 78 | s |> zoom (Bitword.make 16 aH) 79 | |> zoom (Bitword.make (l - 16) (aL lsr (32 - l))) 80 | 81 | let make_index plen pbuf = 82 | Ipaddr.V4.Prefix.make plen (Ipaddr.V4.of_bytes_exn (Bytes.to_string pbuf)) 83 | end 84 | 85 | module V6_base = struct 86 | type t = M.t 87 | type address = Ipaddr.V6.t 88 | type network = Ipaddr.V6.Prefix.t 89 | 90 | let max_length = 128 91 | let bytes_of_address addr = Ipaddr.V6.to_bytes addr 92 | let bytes_of_network net = Ipaddr.V6.to_bytes (Ipaddr.V6.Prefix.network net) 93 | let length_of_network net = Ipaddr.V6.Prefix.bits net 94 | 95 | let is_empty s = value s = Some false 96 | let is_full s = value s = Some true 97 | 98 | let of_network pfx = 99 | let l = Ipaddr.V6.Prefix.bits pfx in 100 | let net = Ipaddr.V6.Prefix.network pfx |> Ipaddr.V6.to_bytes in 101 | let rec loop i acc = 102 | if i < 0 then acc else 103 | loop (i - 1) (acc |> unzoom false (Bitword.make 8 (Char.code net.[i]))) in 104 | let lr = l mod 8 in 105 | if lr = 0 then 106 | loop (l / 8 - 1) (const true) 107 | else 108 | let w = Bitword.make lr (Char.code net.[l / 8] lsr (8 - lr)) in 109 | loop (l / 8 - 1) (const true |> unzoom false w) 110 | 111 | let contains_address s addr = 112 | let a0, a1, a2, a3, a4, a5, a6, a7 = Ipaddr.V6.to_int16 addr in 113 | s |> zoom (Bitword.make 16 a7) |> zoom (Bitword.make 16 a6) 114 | |> zoom (Bitword.make 16 a5) |> zoom (Bitword.make 16 a4) 115 | |> zoom (Bitword.make 16 a3) |> zoom (Bitword.make 16 a2) 116 | |> zoom (Bitword.make 16 a1) |> zoom (Bitword.make 16 a0) 117 | |> is_full 118 | 119 | let zoom_network pfx s = 120 | let l = Ipaddr.V6.Prefix.bits pfx in 121 | let a = Ipaddr.V6.to_bytes (Ipaddr.V6.Prefix.network pfx) in 122 | let rec loop k acc = 123 | if k = l then acc else 124 | if k + 8 > l then 125 | let lr = l mod 8 in 126 | acc |> zoom (Bitword.make lr (Char.code a.[k / 8] lsr (8 - lr))) 127 | else 128 | loop (k + 8) (acc |> zoom (Bitword.make 8 (Char.code a.[k / 8]))) in 129 | loop 0 s 130 | 131 | let make_index plen pbuf = 132 | Ipaddr.V6.Prefix.make plen (Ipaddr.V6.of_bytes_exn (Bytes.to_string pbuf)) 133 | end 134 | 135 | module Vn_base = struct 136 | type t = M.t 137 | type address = Ipaddr.t 138 | type network = Ipaddr.Prefix.t 139 | 140 | let max_length = 128 141 | 142 | let bytes_of_address addr = 143 | (match Ipaddr.to_v4 addr with 144 | | Some addr_v4 -> Ipaddr.V4.to_bytes addr_v4 145 | | None -> Ipaddr.V6.to_bytes (Ipaddr.to_v6 addr)) 146 | let bytes_of_network net = bytes_of_address (Ipaddr.Prefix.network net) 147 | 148 | let length_of_network net = 149 | (match Ipaddr.Prefix.to_v4 net with 150 | | Some net_v4 -> Ipaddr.V4.Prefix.bits net_v4 151 | | None -> Ipaddr.V6.Prefix.bits (Ipaddr.Prefix.to_v6 net)) 152 | 153 | let is_empty s = value s = Some false 154 | let is_full s = value s = Some true 155 | 156 | let of_network pfx = 157 | let l, net = 158 | (match Ipaddr.Prefix.to_v4 pfx with 159 | | Some pfx_v4 -> 160 | (Ipaddr.V4.Prefix.bits pfx_v4, 161 | Ipaddr.V4.to_bytes (Ipaddr.V4.Prefix.network pfx_v4)) 162 | | None -> 163 | let pfx_v6 = Ipaddr.Prefix.to_v6 pfx in 164 | (Ipaddr.V6.Prefix.bits pfx_v6, 165 | Ipaddr.V6.to_bytes (Ipaddr.V6.Prefix.network pfx_v6))) in 166 | let rec loop i acc = 167 | if i < 0 then acc else 168 | loop (i - 1) (acc |> unzoom false (Bitword.make 8 (Char.code net.[i]))) in 169 | let lr = l mod 8 in 170 | if lr = 0 then 171 | loop (l / 8 - 1) (const true) 172 | else 173 | let w = Bitword.make lr (Char.code net.[l / 8] lsr (8 - lr)) in 174 | loop (l / 8 - 1) (const true |> unzoom false w) 175 | 176 | let contains_address s addr = 177 | (match Ipaddr.to_v4 addr with 178 | | Some addr -> V4_base.contains_address s addr 179 | | None -> V6_base.contains_address s (Ipaddr.to_v6 addr)) 180 | 181 | let zoom_network pfx s = 182 | (match Ipaddr.Prefix.to_v4 pfx with 183 | | Some pfx -> V4_base.zoom_network pfx s 184 | | None -> V6_base.zoom_network (Ipaddr.Prefix.to_v6 pfx) s) 185 | 186 | let make_index plen pbuf = 187 | Ipaddr.V6.Prefix.make plen (Ipaddr.V6.of_bytes_exn pbuf) 188 | end 189 | 190 | module Make (Base : BASE) = struct 191 | include Base 192 | 193 | let empty = const false 194 | let full = const true 195 | 196 | let equal = M.equal 197 | 198 | let contains_network s pfx = is_full (zoom_network pfx s) 199 | let overlaps_network s pfx = not (is_empty (zoom_network pfx s)) 200 | 201 | let flip_address addr x s = 202 | let b = bytes_of_address addr in 203 | let rec loop i = 204 | if i = max_length / 8 then (fun _ -> const x) else 205 | let w = Bitword.make 8 (Char.code b.[i]) in 206 | modify w (loop (i + 1)) in 207 | loop 0 s 208 | 209 | let flip_network net x s = 210 | let l = length_of_network net in 211 | let b = bytes_of_network net in 212 | let rec loop k = 213 | if k = l then (fun _ -> const x) else 214 | if k + 8 > l then 215 | let w = Bitword.make (l - k) (Char.code b.[k / 8] lsr (8 - l + k)) in 216 | modify w (fun _ -> const x) 217 | else 218 | let w = Bitword.make 8 (Char.code b.[k / 8]) in 219 | modify w (loop (k + 8)) in 220 | loop 0 s 221 | 222 | let add_address addr s = flip_address addr true s 223 | let remove_address addr s = flip_address addr false s 224 | let add_network net s = flip_network net true s 225 | let remove_network net s = flip_network net false s 226 | 227 | let inter_network netw s = 228 | let l = length_of_network netw in 229 | let b = bytes_of_network netw in 230 | s |> M.zoom_string l b |> M.unzoom_string false l b 231 | 232 | let union sA sB = merge (||) sA sB 233 | let inter sA sB = merge (&&) sA sB 234 | let compl sX sU = merge (fun x y -> not x && y) sX sU 235 | 236 | let cata ~const ~appose ~unzoom s = 237 | catai_bytes 238 | ~index_buffer_size:(max_length / 8) ~make_index 239 | ~const ~appose ~unzoom s 240 | 241 | let rec is_network s = 242 | recurse 243 | ~const:(fun _ -> false) 244 | ~appose:(fun _ _ -> false) 245 | ~unzoom:(fun x _p s' -> not x && is_network s') s 246 | 247 | let valid = M.valid 248 | end 249 | 250 | module V4 = Make (V4_base) 251 | module V6 = Make (V6_base) 252 | module Vn = Make (Vn_base) 253 | -------------------------------------------------------------------------------- /lib/ip_radixset.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Radix set over IP networks. *) 19 | 20 | open Ip_radixset_sig 21 | 22 | module V4 : S with type address = Ipaddr.V4.t 23 | and type network = Ipaddr.V4.Prefix.t 24 | module V6 : S with type address = Ipaddr.V6.t 25 | and type network = Ipaddr.V6.Prefix.t 26 | module Vn : S with type address = Ipaddr.t 27 | and type network = Ipaddr.Prefix.t 28 | -------------------------------------------------------------------------------- /lib/ip_radixset_sig.mli: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | (** Signature for {!Ip_radixset}. *) 19 | 20 | module type S = sig 21 | type t 22 | type address 23 | type network 24 | 25 | val empty : t 26 | val is_empty : t -> bool 27 | val full : t 28 | val is_full : t -> bool 29 | 30 | val equal : t -> t -> bool 31 | 32 | val of_network : network -> t 33 | val is_network : t -> bool 34 | 35 | val contains_address : t -> address -> bool 36 | val contains_network : t -> network -> bool 37 | val overlaps_network : t -> network -> bool 38 | 39 | val add_address : address -> t -> t 40 | val remove_address : address -> t -> t 41 | val add_network : network -> t -> t 42 | val remove_network : network -> t -> t 43 | val inter_network : network -> t -> t 44 | 45 | val union : t -> t -> t 46 | val inter : t -> t -> t 47 | val compl : t -> t -> t 48 | 49 | val cata : 50 | const: (network -> bool -> 'a) -> 51 | appose: (network -> 'a -> 'a -> 'a) -> 52 | unzoom: (bool -> int -> network -> 'a -> 'a) -> 53 | t -> 'a 54 | 55 | (**/**) 56 | val valid : t -> bool 57 | end 58 | -------------------------------------------------------------------------------- /radixmap-ipaddr.opam: -------------------------------------------------------------------------------- 1 | opam-version: "2.0" 2 | name: "radixmap-ipaddr" 3 | maintainer: "paurkedal@gmail.com" 4 | authors: "Petter A. Urkedal" 5 | license: "LGPL-3.0-or-later WITH LGPL-3.0-linking-exception" 6 | homepage: "https://github.com/paurkedal/ocaml-radixmap/" 7 | bug-reports: "https://github.com/paurkedal/ocaml-radixmap/issues" 8 | depends: [ 9 | "dune" {>= "1.3"} 10 | "ocaml" {>= "4.02.3"} 11 | "ipaddr" 12 | "radixmap" 13 | ] 14 | build: [ 15 | ["dune" "build" "-p" name "-j" jobs] 16 | ["dune" "runtest" "-p" name "-j" jobs] {with-test} 17 | ] 18 | dev-repo: "git+https://github.com/paurkedal/ocaml-radixmap.git" 19 | synopsis: "Sets and maps over IP networks based on radixmap" 20 | -------------------------------------------------------------------------------- /radixmap.opam: -------------------------------------------------------------------------------- 1 | opam-version: "2.0" 2 | name: "radixmap" 3 | maintainer: "paurkedal@gmail.com" 4 | authors: "Petter A. Urkedal" 5 | license: "LGPL-3.0-or-later WITH LGPL-3.0-linking-exception" 6 | homepage: "https://github.com/paurkedal/ocaml-radixmap/" 7 | bug-reports: "https://github.com/paurkedal/ocaml-radixmap/issues" 8 | depends: [ 9 | "dune" {>= "1.3"} 10 | "ocaml" {>= "4.02.3"} 11 | ] 12 | build: [ 13 | ["dune" "build" "-p" name "-j" jobs] 14 | ["dune" "runtest" "-p" name "-j" jobs] {with-test} 15 | ] 16 | dev-repo: "git+https://github.com/paurkedal/ocaml-radixmap.git" 17 | synopsis: "Map à la Binary Radix Tree" 18 | -------------------------------------------------------------------------------- /tests/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name testkit) 3 | (wrapped false) 4 | (modules Testkit) 5 | (libraries unix)) 6 | 7 | (test 8 | (name test_bitword) 9 | (modules Test_bitword) 10 | (libraries radixmap testkit)) 11 | 12 | (test 13 | (name test_bitword_radixmap) 14 | (modules Test_bitword_radixmap) 15 | (libraries radixmap testkit)) 16 | 17 | (test 18 | (name test_ip_radixset) 19 | (modules Test_ip_radixset) 20 | (libraries radixmap_ipaddr testkit)) 21 | 22 | (test 23 | (name test_ip_radixmap) 24 | (modules Test_ip_radixmap) 25 | (libraries radixmap_ipaddr testkit)) 26 | -------------------------------------------------------------------------------- /tests/test_bitword.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module Test_endian (Xe : sig include Bitword.ENDIAN_API val is_be : bool end) = 19 | struct 20 | let test_one w = 21 | let n = Bitword.length w in 22 | assert (w = Xe.init n (Xe.get w)); 23 | for i = 0 to n do 24 | let pfx = Xe.prefix i w in 25 | let sfx = Xe.suffix i w in 26 | let pfx', sfx' = Xe.cut i w in 27 | assert (Bitword.length pfx = (if Xe.is_be then i else n - i)); 28 | assert (Bitword.length sfx = (if Xe.is_be then n - i else i)); 29 | assert (pfx = pfx'); 30 | assert (sfx = sfx'); 31 | assert (Bitword.cat_exn pfx sfx = w) 32 | done; 33 | let w' = Xe.drop_exn w in 34 | assert (Xe.drop_unsafe w = w'); 35 | let b, v = Xe.pop_exn w in 36 | let b', v' = Xe.pop_unsafe w in 37 | assert (b = b'); 38 | assert (v = v'); 39 | assert (v = w'); 40 | assert (Xe.push_exn b v = w); 41 | let push_cb_exn, push_cb_unsafe = 42 | if b then Xe.(push_c1_exn, push_c1_unsafe) 43 | else Xe.(push_c0_exn, push_c0_unsafe) in 44 | assert (push_cb_exn v = w); 45 | assert (push_cb_unsafe v = w) 46 | 47 | let test () = 48 | for n = 1 to Bitword.max_length do 49 | for _ = 1 to Pervasives.(min 10000 (1 lsl n)) do 50 | test_one (Bitword.random_uniform n) 51 | done 52 | done 53 | end 54 | 55 | module Test_be = Test_endian (struct include Bitword.Be let is_be = true end) 56 | module Test_le = Test_endian (struct include Bitword.Le let is_be = false end) 57 | 58 | let () = 59 | Testkit.init "test_bitword"; 60 | 61 | assert (Bitword.max_length mod 8 = 0); 62 | assert (Bitword.max_length >= 24); 63 | assert (Bitword.length Bitword.c0 = 1); 64 | assert (Bitword.length Bitword.c1 = 1); 65 | assert (Bitword.Le.get Bitword.c0 0 = false); 66 | assert (Bitword.Le.get Bitword.c1 0 = true); 67 | assert (Bitword.bits Bitword.c0 = 0); 68 | assert (Bitword.bits Bitword.c1 = 1); 69 | assert (Bitword.equal Bitword.c0 Bitword.c0); 70 | assert (Bitword.equal Bitword.c1 Bitword.c1); 71 | assert (not (Bitword.equal Bitword.c0 Bitword.c1)); 72 | assert (not (Bitword.equal Bitword.c1 Bitword.c0)); 73 | assert (Bitword.compare Bitword.c0 Bitword.c0 = 0); 74 | assert (Bitword.compare Bitword.c1 Bitword.c1 = 0); 75 | assert (Bitword.compare Bitword.c0 Bitword.c1 = -1); 76 | assert (Bitword.compare Bitword.c1 Bitword.c0 = 1); 77 | assert (Bitword.empty <> Bitword.c0); 78 | assert (Bitword.empty <> Bitword.c1); 79 | 80 | assert (Bitword.const 0 false = Bitword.empty); 81 | assert (Bitword.const 0 true = Bitword.empty); 82 | assert (Bitword.const 1 false = Bitword.c0); 83 | assert (Bitword.const 1 true = Bitword.c1); 84 | assert (Bitword.const_unsafe 0 false = Bitword.empty); 85 | assert (Bitword.const_unsafe 0 true = Bitword.empty); 86 | assert (Bitword.const_unsafe 1 false = Bitword.c0); 87 | assert (Bitword.const_unsafe 1 true = Bitword.c1); 88 | 89 | for n = 0 to Bitword.max_length do 90 | let w0 = Bitword.const n false in 91 | let w1 = Bitword.const n true in 92 | assert (Bitword.length w0 = n); 93 | assert (Bitword.length w1 = n); 94 | assert (w0 = Bitword.Le.init n (fun _ -> false)); 95 | assert (w1 = Bitword.Le.init n (fun _ -> true)); 96 | assert (w0 = Bitword.make n 0); 97 | assert (w1 = Bitword.make n Pervasives.(1 lsl n - 1)); 98 | for _ = 0 to Pervasives.(min 10000 (1 lsl n)) do 99 | let w = Bitword.random_uniform n in 100 | assert (Bitword.length w = n); 101 | assert (w = Bitword.make n (Bitword.bits w)); 102 | 103 | (* common prefix *) 104 | let v = Bitword.random_uniform (Random.int (n + 1)) in 105 | let l = Bitword.common_prefix_length w v in 106 | assert (Bitword.Be.prefix l w = Bitword.Be.prefix l v); 107 | assert (l = Bitword.length v || Bitword.Be.get v l <> Bitword.Be.get w l); 108 | 109 | (* cat_rem *) 110 | let v = Bitword.random_uniform (Random.int (Bitword.max_length + 1)) in 111 | let v', w' = Bitword.cat_rem v w in 112 | assert Bitword.(length v' = 0 || length w' = max_length); 113 | assert Bitword.(length v' + length w' = length v + length w); 114 | let nw = Bitword.length w in 115 | let nv = Bitword.length v in 116 | for i = 0 to nw - 1 do 117 | assert (Bitword.Le.get w i = Bitword.Le.get w' i) 118 | done; 119 | let n' = min nv (Bitword.max_length - nw) in 120 | for i = 0 to n' - 1 do 121 | assert (Bitword.Le.get v i = Bitword.Le.get w' (nw + i)) 122 | done; 123 | for i = n' to nv - 1 do 124 | assert (Bitword.Le.get v i = Bitword.Le.get v' (i - n')) 125 | done 126 | done 127 | done; 128 | 129 | Test_be.test (); 130 | Test_le.test () 131 | -------------------------------------------------------------------------------- /tests/test_bitword_radixmap.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module P = Bitword_radixmap.Poly 19 | module M = Bitword_radixmap.Make 20 | (struct 21 | type t = int 22 | let equal (x : int) (y : int) = x = y 23 | end) 24 | 25 | module Bitwords_map = Bitword_radixmap.Make 26 | (struct 27 | type t = Bitword.t list 28 | let equal (x : t) (y : t) = x = y 29 | end) 30 | 31 | let pp fmtr = P.pp Format.pp_print_int fmtr 32 | 33 | let size = 100 34 | 35 | module Path = struct 36 | type t = string 37 | 38 | let make l buf = (l, Bytes.sub_string buf 0 ((l + 7) / 8)) 39 | 40 | let empty = (0, "") 41 | 42 | let length (l, _) = l 43 | 44 | let get (l, s) k = 45 | assert (0 <= k && k < l); 46 | Char.code s.[k / 8] lsr (7 - k mod 8) land 1 <> 0 47 | 48 | let prefix l' (l, s) = 49 | assert (l' <= l); 50 | let s' = 51 | String.init ((l' + 7) / 8) 52 | (fun i -> 53 | if 8 * i + 8 <= l' then s.[i] else 54 | Char.chr (Char.code s.[i] land 0xff lsl (8 - l' mod 8) land 0xff)) in 55 | (l', s') 56 | 57 | let pp ppf (l, s) = Format.fprintf ppf "(%d,%S)" l s 58 | end 59 | 60 | let rec random_bitwords_map ws = 61 | (match Random.int 7 with 62 | | 0 | 2 -> 63 | Bitwords_map.appose (random_bitwords_map (Bitword.c0 :: ws)) 64 | (random_bitwords_map (Bitword.c1 :: ws)) 65 | | 1 | 3 -> 66 | let w = Bitword.random_uniform (Random.int Bitword.max_length + 1) in 67 | Bitwords_map.unzoom ws w (random_bitwords_map (w :: ws)) 68 | | _ -> 69 | Bitwords_map.const ws) 70 | 71 | let check_const_path p ws = 72 | let rec loop i = function 73 | | [] -> () 74 | | w :: ws -> 75 | let l = Bitword.length w in 76 | for k = 0 to l - 1 do 77 | assert (Bitword.Be.get w k = Path.get p (i + k)) 78 | done; 79 | loop (i + l) ws in 80 | loop 0 (List.rev ws) 81 | 82 | let check_unzoom_path p ws = 83 | let rec loop i = function 84 | | [] -> () 85 | | w :: ws -> 86 | let lw = Bitword.length w in 87 | let lr = Path.length p - i in 88 | assert (lr + 1 >= lw); 89 | let l = min lw lr in 90 | if l > 0 then begin 91 | for k = 0 to l - 1 do 92 | assert (Bitword.Be.get w k = Path.get p (i + k)) 93 | done; 94 | loop (i + l) ws 95 | end in 96 | loop 0 (List.rev ws) 97 | 98 | let test_catai_bytes () = 99 | let make_index plen pbuf = Path.make plen pbuf in 100 | let const p ws = check_const_path p ws; p in 101 | let appose p p0 p1 = 102 | assert (Path.length p0 = Path.length p + 1); 103 | assert (Path.length p1 = Path.length p + 1); 104 | assert (Path.get p0 (Path.length p) = false); 105 | assert (Path.get p1 (Path.length p) = true); 106 | p in 107 | let unzoom ws depth pN pN' = 108 | assert (pN = pN'); 109 | let p = Path.prefix (Path.length pN - depth) pN in 110 | check_unzoom_path p ws; 111 | p in 112 | for _ = 1 to 10000 do 113 | let m = random_bitwords_map [] in 114 | let p = Bitwords_map.catai_bytes ~make_index ~const ~appose ~unzoom m in 115 | assert (p = Path.empty); 116 | done 117 | 118 | let (=%) mA mB = 119 | assert (M.valid mA); 120 | assert (M.valid mB); 121 | M.equal mA mB 122 | 123 | let (<>%) mA mB = not (mA =% mB) 124 | 125 | let rec modify_random m = 126 | M.modify (Bitword.random_uniform (Random.int 24)) 127 | (fun m' -> 128 | if Random.bool () then modify_random m' else 129 | M.const (Random.int size)) 130 | m 131 | 132 | let random_map () = 133 | let rec loop n acc = 134 | if n = 0 then acc else 135 | loop (n - 1) (modify_random acc) in 136 | loop (Random.int size) (M.const (Random.int size)) 137 | 138 | let random_string_path () = 139 | let l = Random.int 70 in 140 | let f i = 141 | if i < l / 8 then Char.chr (Random.int 256) else 142 | Char.chr (Random.int (1 lsl (l mod 8)) lsl (8 - l mod 8)) in 143 | let s = String.init ((l + 7) / 8) f in 144 | (l, s) 145 | 146 | let rec custom_unzoom_string x l s m = 147 | if l = 0 then m else 148 | if l mod 8 <> 0 then 149 | let k = l mod 8 in 150 | let w = Bitword.make k (Char.code s.[l / 8] lsr (8 - k)) in 151 | custom_unzoom_string x (l - k) s (M.unzoom x w m) 152 | else 153 | let w = Bitword.make 8 (Char.code s.[l / 8 - 1]) in 154 | custom_unzoom_string x (l - 8) s (M.unzoom x w m) 155 | 156 | let custom_zoom_string l s m = 157 | let rec loop k m = 158 | if k + 8 <= l then 159 | loop (k + 8) (M.zoom (Bitword.make 8 (Char.code s.[k / 8])) m) 160 | else if k < l then 161 | M.zoom (Bitword.make (l - k) (Char.code s.[l / 8] lsr (8 - l + k))) m 162 | else 163 | m in 164 | loop 0 m 165 | 166 | let () = 167 | Testkit.init "test_bitword_radixmap"; 168 | 169 | let mC = M.const 211 in 170 | assert (M.equal mC mC); 171 | assert (M.is_const mC); 172 | assert (M.value mC = Some 211); 173 | 174 | for _ = 1 to 10000 do 175 | let mA = random_map () in 176 | let mB = random_map () in 177 | let np = Random.int 24 + 1 in 178 | let p = Bitword.random_uniform np in 179 | let p', p'' = Bitword.Le.cut (Random.int (np + 1)) p in 180 | 181 | assert (mC =% M.zoom p mC); 182 | assert (mC =% M.unzoom 211 p mC); 183 | assert (mA =% M.zoom Bitword.empty mA); 184 | assert (mA =% M.unzoom (Random.int 100) Bitword.empty mA); 185 | assert (mA =% M.zoom p (M.unzoom (Random.int 100) p mA)); 186 | assert (mA <>% M.unzoom (-1) p mA); 187 | assert (mA =% M.zoom Bitword.c0 (M.appose mA mB)); 188 | assert (mB =% M.zoom Bitword.c1 (M.appose mA mB)); 189 | let l, s = random_string_path () in 190 | assert (M.unzoom_string (-1) l s mA =% custom_unzoom_string (-1) l s mA); 191 | assert (M.zoom_string l s mA =% custom_zoom_string l s mA); 192 | 193 | assert (M.zoom p mA =% (mA |> M.zoom p' |> M.zoom p'')); 194 | assert (M.unzoom 0 p mA =% M.unzoom 0 p' (M.unzoom 0 p'' mA)); 195 | assert (M.modify p (fun _ -> mB) mA =% 196 | M.modify p' (M.modify p'' (fun _ -> mB)) mA); 197 | assert (mA =% (mA |> M.map lnot |> M.map lnot)); 198 | assert (M.modify p (M.merge max mB) mA =% M.merge max mA (M.unzoom 0 p mB)); 199 | assert (M.merge max mA mB =% 200 | M.map (~-) (M.merge min (M.map (~-) mA) (M.map (~-) mB))) 201 | done; 202 | test_catai_bytes () 203 | -------------------------------------------------------------------------------- /tests/test_ip_radixmap.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module Int_equal = struct 19 | type t = int 20 | let equal (x : t) (y : t) = x = y 21 | end 22 | 23 | module type S = sig 24 | include Ip_radixmap_sig.S with type cod = int 25 | 26 | val max_length : int 27 | val random_address : unit -> address 28 | val random_network : unit -> network 29 | 30 | val subset : subnet: network -> network: network -> bool 31 | 32 | module Infix : sig 33 | val (=@) : address -> address -> bool 34 | val (<:@) : address -> network -> bool 35 | end 36 | end 37 | 38 | module V4 = struct 39 | include Ip_radixmap.Make_v4 (Int_equal) 40 | 41 | let max_length = 32 42 | 43 | let random_address () = 44 | let a0 = Random.bits () land 0xffff in 45 | let a1 = Random.bits () land 0xffff in 46 | Ipaddr.V4.of_int16 (a0, a1) 47 | 48 | let random_network () = 49 | Ipaddr.V4.Prefix.make (Random.int 33) (random_address ()) 50 | 51 | let subset = Ipaddr.V4.Prefix.subset 52 | 53 | module Infix = struct 54 | let (=@) a0 a1 = Ipaddr.V4.compare a0 a1 = 0 55 | let (<:@) addr netw = Ipaddr.V4.Prefix.mem addr netw 56 | end 57 | end 58 | 59 | module V6 = struct 60 | include Ip_radixmap.Make_v6 (Int_equal) 61 | 62 | let max_length = 128 63 | 64 | let random_address () = 65 | let s = String.init 16 (fun _ -> Char.chr (Random.int 0x100)) in 66 | Ipaddr.V6.of_bytes_exn s 67 | 68 | let random_network () = 69 | Ipaddr.V6.Prefix.make (Random.int 129) (random_address ()) 70 | 71 | let subset = Ipaddr.V6.Prefix.subset 72 | 73 | module Infix = struct 74 | let (=@) a0 a1 = Ipaddr.V6.compare a0 a1 = 0 75 | let (<:@) addr netw = Ipaddr.V6.Prefix.mem addr netw 76 | end 77 | end 78 | 79 | module Make (M : S) = struct 80 | open M.Infix 81 | let (=%) mA mB = M.equal mA mB 82 | let (<>%) mA mB = not (M.equal mA mB) 83 | 84 | module Ip_assoc = struct 85 | 86 | type 'a t = 87 | | Empty 88 | | Add_address of M.address * 'a * 'a t 89 | | Add_network of M.network * 'a * 'a t 90 | | Remove_address of M.address * 'a t 91 | | Remove_network of M.network * 'a t 92 | 93 | let empty = Empty 94 | let add_address addr x ipa = Add_address (addr, x, ipa) 95 | let remove_address addr ipa = Remove_address (addr, ipa) 96 | let add_network netw x ipa = Add_network (netw, x, ipa) 97 | let remove_network netw ipa = Remove_network (netw, ipa) 98 | 99 | let rec get_address ipa addr = 100 | (match ipa with 101 | | Empty -> None 102 | | Add_address (addr', v', ipa') -> 103 | if addr =@ addr' then Some v' else get_address ipa' addr 104 | | Remove_address (addr', ipa') -> 105 | if addr =@ addr' then None else get_address ipa' addr 106 | | Add_network (netw', v', ipa') -> 107 | if addr <:@ netw' then Some v' else get_address ipa' addr 108 | | Remove_network (netw', ipa') -> 109 | if addr <:@ netw' then None else get_address ipa' addr) 110 | 111 | end 112 | 113 | let random_map range = 114 | let rec loop n m = 115 | if n = 0 then m else 116 | let k = M.random_network () in 117 | let v = Random.int range in 118 | loop (n - 1) (M.add_network k v m) in 119 | loop (1 lsl (Random.int 12)) M.empty 120 | 121 | let left x y = 122 | (match x, y with 123 | | None, None -> None 124 | | Some x, _ -> Some x 125 | | None, Some y -> Some y) 126 | 127 | let test () = 128 | assert (M.is_empty M.empty); 129 | assert (M.empty =% M.empty); 130 | 131 | for _ = 1 to 1_000 do 132 | let rec loop k m ipa = 133 | for _ = 1 to 10 do 134 | let addr = M.random_address () in 135 | assert (M.get_address m addr = Ip_assoc.get_address ipa addr) 136 | done; 137 | if k = 0 then () else 138 | (match Random.int 4 with 139 | | 0 -> 140 | let addr = M.random_address () in 141 | let m' = M.add_address addr k m in 142 | let ipa' = Ip_assoc.add_address addr k ipa in 143 | assert (M.get_address m' addr = Some k); 144 | assert (m <>% m'); 145 | loop (k - 1) m' ipa' 146 | | 1 -> 147 | let netw = M.random_network () in 148 | let m' = M.add_network netw k m in 149 | let ipa' = Ip_assoc.add_network netw k ipa in 150 | assert (M.get_network m' netw = Some k); 151 | assert (m <>% m'); 152 | loop (k - 1) m' ipa' 153 | | 2 -> 154 | let addr = M.random_address () in 155 | let m' = M.remove_address addr m in 156 | let ipa' = Ip_assoc.remove_address addr ipa in 157 | assert (M.get_address m' addr = None); 158 | loop (k - 1) m' ipa' 159 | | 3 -> 160 | let netw = M.random_network () in 161 | let m' = M.remove_network netw m in 162 | let ipa' = Ip_assoc.remove_network netw ipa in 163 | assert (M.get_network m' netw = None); 164 | loop (k - 1) m' ipa' 165 | | _ -> assert false) in 166 | loop 100 M.empty Ip_assoc.empty 167 | done; 168 | 169 | for _ = 1 to 1_000 do 170 | let sA = random_map 1000 in 171 | let sB = M.filter (fun x -> x <= 700) sA in 172 | let sC = M.filter (fun x -> x >= 300) sA in 173 | assert (M.merge left sB sC =% sA) 174 | done 175 | end 176 | 177 | module Test_v4 = Make (V4) 178 | module Test_v6 = Make (V6) 179 | 180 | let () = 181 | Testkit.init "test_ip_radixmap"; 182 | Test_v4.test (); 183 | Test_v6.test () 184 | -------------------------------------------------------------------------------- /tests/test_ip_radixset.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | module type S = sig 19 | include Ip_radixset_sig.S 20 | 21 | val max_length : int 22 | val random_address : unit -> address 23 | val random_network : unit -> network 24 | end 25 | 26 | module V4 = struct 27 | include Ip_radixset.V4 28 | 29 | let max_length = 32 30 | 31 | let random_address () = 32 | let a0 = Random.bits () land 0xffff in 33 | let a1 = Random.bits () land 0xffff in 34 | Ipaddr.V4.of_int16 (a0, a1) 35 | 36 | let random_network () = 37 | Ipaddr.V4.Prefix.make (Random.int 33) (random_address ()) 38 | end 39 | 40 | module V6 = struct 41 | include Ip_radixset.V6 42 | 43 | let max_length = 128 44 | 45 | let random_address () = 46 | let s = String.init 16 (fun _ -> Char.chr (Random.int 0x100)) in 47 | Ipaddr.V6.of_bytes_exn s 48 | 49 | let random_network () = 50 | Ipaddr.V6.Prefix.make (Random.int 129) (random_address ()) 51 | end 52 | 53 | module Make (M : S) = struct 54 | 55 | let sO = M.empty 56 | let sU = M.full 57 | 58 | let (=%) sA sB = 59 | assert (M.valid sA); 60 | assert (M.valid sB); 61 | M.equal sA sB 62 | 63 | let (<>%) sA sB = not (M.equal sA sB) 64 | 65 | let random_set () = 66 | let rec loop n s = 67 | if n = 0 then s else 68 | loop (n - 1) (M.add_network (M.random_network ()) s) in 69 | loop (1 lsl (Random.int 12)) sO 70 | 71 | let test () = 72 | assert (M.is_empty sO); 73 | assert (M.is_full sU); 74 | assert (not (M.is_empty sU)); 75 | assert (not (M.is_full sO)); 76 | assert (sO =% sO); 77 | assert (sU =% sU); 78 | assert (sO <>% sU); 79 | assert (sU <>% sO); 80 | assert (M.is_empty (M.compl sO sO)); 81 | assert (M.is_empty (M.compl sU sO)); 82 | assert (M.is_empty (M.compl sU sU)); 83 | assert (M.is_full (M.compl sO sU)); 84 | assert (M.is_empty (M.union sO sO)); 85 | assert (M.is_full (M.union sO sU)); 86 | assert (M.is_full (M.union sU sO)); 87 | assert (M.is_full (M.union sU sU)); 88 | assert (M.is_empty (M.inter sO sO)); 89 | assert (M.is_empty (M.inter sO sU)); 90 | assert (M.is_empty (M.inter sU sO)); 91 | assert (M.is_full (M.inter sU sU)); 92 | 93 | let rec loop_mutate n s = 94 | if n > 0 then 95 | (match 2 + Random.int 1 with 96 | | 0 -> 97 | let addr = M.random_address () in 98 | let s' = M.add_address addr s in 99 | assert (M.contains_address s' addr); 100 | assert (M.equal s s' = M.contains_address s addr); 101 | loop_mutate (n - 1) s' 102 | | 1 -> 103 | let addr = M.random_address () in 104 | let s' = M.remove_address addr s in 105 | assert (not (M.contains_address s' addr)); 106 | assert (M.equal s s' <> M.contains_address s addr); 107 | loop_mutate (n - 1) s' 108 | | 2 -> 109 | let net = M.random_network () in 110 | let s' = M.add_network net s in 111 | assert (M.contains_network s' net); 112 | assert (M.equal s s' || not (M.contains_network s net)); 113 | loop_mutate (n - 1) s' 114 | | 3 -> 115 | let net = M.random_network () in 116 | let s' = M.remove_network net s in 117 | assert (not (M.contains_network s' net)); 118 | assert (not (M.equal s s') || not (M.contains_network s net)); 119 | loop_mutate (n - 1) s' 120 | | _ -> assert false) 121 | in 122 | loop_mutate 100_000 sO; 123 | 124 | for _ = 1 to 1_000 do 125 | let sA = random_set () in 126 | let sB = random_set () in 127 | let sX = random_set () in 128 | let sAB = M.inter sA sB and uAB = M.union sA sB in 129 | let cXA = M.compl sX sA and cAX = M.compl sA sX in 130 | let cXB = M.compl sX sB and cBX = M.compl sB sX in 131 | 132 | (* idempotence and dominans *) 133 | assert (sA =% (M.union sA sO)); 134 | assert (sA =% (M.union sO sA)); 135 | assert (sA =% (M.union sA sA)); 136 | assert (sO =% (M.inter sA sO)); 137 | assert (sO =% (M.inter sO sA)); 138 | assert (sA =% (M.inter sA sA)); 139 | assert (sO =% (M.compl sA sO)); 140 | assert (sA =% (M.compl sO sA)); 141 | assert (sO =% (M.compl sA sA)); 142 | assert (cXA =% (M.compl sX cXA)); 143 | 144 | (* complement *) 145 | assert (M.is_empty (M.inter sA (M.compl sA sU))); 146 | assert (M.is_full (M.union sA (M.compl sA sU))); 147 | assert (M.inter sA cAX =% sO); 148 | assert (M.union sA cAX =% M.union sA sX); 149 | assert (sAB =% M.compl (M.compl sB sA) sA); 150 | 151 | (* commutativity *) 152 | assert (sAB =% (M.inter sB sA)); 153 | assert (uAB =% (M.union sB sA)); 154 | 155 | (* associativity *) 156 | assert (M.union sA (M.union sB sX) =% M.union uAB sX); 157 | assert (M.inter sA (M.inter sB sX) =% M.inter sAB sX); 158 | 159 | (* distributivity *) 160 | assert (M.union sAB sX =% M.inter (M.union sA sX) (M.union sB sX)); 161 | assert (M.inter uAB sX =% M.union (M.inter sA sX) (M.inter sB sX)); 162 | 163 | (* distributivity: (A ∖ X) ∪ (B ∖ X) = (A ∪ B) ∖ X *) 164 | assert (M.union cXA cXB =% M.compl sX uAB); 165 | 166 | (* distributivity: (A ∖ X) ∩ B = A ∩ (B ∖ X) = (A ∩ B) ∖ X *) 167 | assert (M.inter cXA sB =% M.compl sX sAB); 168 | assert (M.inter sA cXB =% M.compl sX sAB); 169 | 170 | (* X ∖ (A ∪ B) = (X ∖ A) ∩ (X ∖ B) *) 171 | assert (M.compl uAB sX =% M.inter cAX cBX) 172 | done 173 | end 174 | 175 | module Test_V4 = Make (V4) 176 | module Test_V6 = Make (V6) 177 | 178 | let () = 179 | Testkit.init "test_ip_radixset"; 180 | Test_V4.test (); 181 | Test_V6.test () 182 | -------------------------------------------------------------------------------- /tests/testkit.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2017--2022 Petter A. Urkedal 2 | * 3 | * This library is free software; you can redistribute it and/or modify it 4 | * under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or (at your 6 | * option) any later version, with the LGPL-3.0 Linking Exception. 7 | * 8 | * This library is distributed in the hope that it will be useful, but WITHOUT 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 11 | * License for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * and the LGPL-3.0 Linking Exception along with this library. If not, see 15 | * and , respectively. 16 | *) 17 | 18 | open Printf 19 | 20 | let init test_name = 21 | let seed = 22 | try int_of_string (Unix.getenv "SEED") 23 | with Not_found -> int_of_float (Unix.time ()) in 24 | Random.init seed; 25 | printf "Running %s with seed %d (keep if you hit a bug).\n" 26 | test_name seed 27 | --------------------------------------------------------------------------------