├── .gitignore ├── AUTHORS ├── COPYING ├── ChangeLog ├── INSTALL ├── Makefile.am ├── NEWS ├── README ├── README.md ├── autogen.sh ├── configure.ac ├── macros ├── ax_boost_base.m4 ├── ax_boost_system.m4 ├── ax_boost_timer.m4 └── ax_cxx_compile_stdcxx.m4 ├── murrayc-suffix-tree.pc.in ├── murrayc-suffix-tree ├── iter_range.h ├── radix_tree.h ├── suffix_array.h ├── suffix_tree.h └── trie.h └── tests ├── test_pg1400.txt ├── test_radix_tree.cc ├── test_suffix_array.cc ├── test_suffix_tree.cc └── test_trie.cc /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.lo 3 | *.a 4 | *.la 5 | *~ 6 | *.gcno 7 | *.gcda 8 | *.gcov 9 | /gcov-report.txt 10 | 11 | .deps 12 | .dirstamp 13 | .libs 14 | stamp-h1 15 | stamp-h2 16 | /INSTALL 17 | /compile 18 | /configure.lineno 19 | /depcomp 20 | /ABOUT-NLS 21 | /config.rpath 22 | 23 | Makefile 24 | Makefile.in 25 | /configure 26 | /config.sub 27 | /config.status 28 | /config.log 29 | /config.h 30 | /config.h.in 31 | /config.guess 32 | /autom4te.cache 33 | /aclocal.m4 34 | /mkinstalldirs 35 | /missing 36 | /ltmain.sh 37 | /libtool 38 | /install-sh 39 | /intltool-extract.in 40 | /intltool-merge.in 41 | /intltool-update.in 42 | /compile-binding.am 43 | /dist-changelog.am 44 | /doc-reference.am 45 | /generate-binding.am 46 | /messages.mo 47 | /omf.make 48 | /xmldocs.make 49 | 50 | /test-driver 51 | /test-suite.log 52 | 53 | /tests/test_trie 54 | /tests/test_radix_tree 55 | /tests/test_suffix_tree 56 | /tests/test_suffix_array 57 | 58 | /murrayc-suffix-tree-0-1.pc 59 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/murraycu/murrayc-suffix-tree/d6ec1a131adedaaa0af88cac21635e4c42988b14/AUTHORS -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | The ChangeLog is auto-generated when releasing. If you 2 | are seeing this, use 'git log' for a detailed list of changes. 3 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Installation Instructions 2 | ************************* 3 | 4 | Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation, 5 | Inc. 6 | 7 | Copying and distribution of this file, with or without modification, 8 | are permitted in any medium without royalty provided the copyright 9 | notice and this notice are preserved. This file is offered as-is, 10 | without warranty of any kind. 11 | 12 | Basic Installation 13 | ================== 14 | 15 | Briefly, the shell command `./configure && make && make install' 16 | should configure, build, and install this package. The following 17 | more-detailed instructions are generic; see the `README' file for 18 | instructions specific to this package. Some packages provide this 19 | `INSTALL' file but do not implement all of the features documented 20 | below. The lack of an optional feature in a given package is not 21 | necessarily a bug. More recommendations for GNU packages can be found 22 | in *note Makefile Conventions: (standards)Makefile Conventions. 23 | 24 | The `configure' shell script attempts to guess correct values for 25 | various system-dependent variables used during compilation. It uses 26 | those values to create a `Makefile' in each directory of the package. 27 | It may also create one or more `.h' files containing system-dependent 28 | definitions. Finally, it creates a shell script `config.status' that 29 | you can run in the future to recreate the current configuration, and a 30 | file `config.log' containing compiler output (useful mainly for 31 | debugging `configure'). 32 | 33 | It can also use an optional file (typically called `config.cache' 34 | and enabled with `--cache-file=config.cache' or simply `-C') that saves 35 | the results of its tests to speed up reconfiguring. Caching is 36 | disabled by default to prevent problems with accidental use of stale 37 | cache files. 38 | 39 | If you need to do unusual things to compile the package, please try 40 | to figure out how `configure' could check whether to do them, and mail 41 | diffs or instructions to the address given in the `README' so they can 42 | be considered for the next release. If you are using the cache, and at 43 | some point `config.cache' contains results you don't want to keep, you 44 | may remove or edit it. 45 | 46 | The file `configure.ac' (or `configure.in') is used to create 47 | `configure' by a program called `autoconf'. You need `configure.ac' if 48 | you want to change it or regenerate `configure' using a newer version 49 | of `autoconf'. 50 | 51 | The simplest way to compile this package is: 52 | 53 | 1. `cd' to the directory containing the package's source code and type 54 | `./configure' to configure the package for your system. 55 | 56 | Running `configure' might take a while. While running, it prints 57 | some messages telling which features it is checking for. 58 | 59 | 2. Type `make' to compile the package. 60 | 61 | 3. Optionally, type `make check' to run any self-tests that come with 62 | the package, generally using the just-built uninstalled binaries. 63 | 64 | 4. Type `make install' to install the programs and any data files and 65 | documentation. When installing into a prefix owned by root, it is 66 | recommended that the package be configured and built as a regular 67 | user, and only the `make install' phase executed with root 68 | privileges. 69 | 70 | 5. Optionally, type `make installcheck' to repeat any self-tests, but 71 | this time using the binaries in their final installed location. 72 | This target does not install anything. Running this target as a 73 | regular user, particularly if the prior `make install' required 74 | root privileges, verifies that the installation completed 75 | correctly. 76 | 77 | 6. You can remove the program binaries and object files from the 78 | source code directory by typing `make clean'. To also remove the 79 | files that `configure' created (so you can compile the package for 80 | a different kind of computer), type `make distclean'. There is 81 | also a `make maintainer-clean' target, but that is intended mainly 82 | for the package's developers. If you use it, you may have to get 83 | all sorts of other programs in order to regenerate files that came 84 | with the distribution. 85 | 86 | 7. Often, you can also type `make uninstall' to remove the installed 87 | files again. In practice, not all packages have tested that 88 | uninstallation works correctly, even though it is required by the 89 | GNU Coding Standards. 90 | 91 | 8. Some packages, particularly those that use Automake, provide `make 92 | distcheck', which can by used by developers to test that all other 93 | targets like `make install' and `make uninstall' work correctly. 94 | This target is generally not run by end users. 95 | 96 | Compilers and Options 97 | ===================== 98 | 99 | Some systems require unusual options for compilation or linking that 100 | the `configure' script does not know about. Run `./configure --help' 101 | for details on some of the pertinent environment variables. 102 | 103 | You can give `configure' initial values for configuration parameters 104 | by setting variables in the command line or in the environment. Here 105 | is an example: 106 | 107 | ./configure CC=c99 CFLAGS=-g LIBS=-lposix 108 | 109 | *Note Defining Variables::, for more details. 110 | 111 | Compiling For Multiple Architectures 112 | ==================================== 113 | 114 | You can compile the package for more than one kind of computer at the 115 | same time, by placing the object files for each architecture in their 116 | own directory. To do this, you can use GNU `make'. `cd' to the 117 | directory where you want the object files and executables to go and run 118 | the `configure' script. `configure' automatically checks for the 119 | source code in the directory that `configure' is in and in `..'. This 120 | is known as a "VPATH" build. 121 | 122 | With a non-GNU `make', it is safer to compile the package for one 123 | architecture at a time in the source code directory. After you have 124 | installed the package for one architecture, use `make distclean' before 125 | reconfiguring for another architecture. 126 | 127 | On MacOS X 10.5 and later systems, you can create libraries and 128 | executables that work on multiple system types--known as "fat" or 129 | "universal" binaries--by specifying multiple `-arch' options to the 130 | compiler but only a single `-arch' option to the preprocessor. Like 131 | this: 132 | 133 | ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 134 | CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 135 | CPP="gcc -E" CXXCPP="g++ -E" 136 | 137 | This is not guaranteed to produce working output in all cases, you 138 | may have to build one architecture at a time and combine the results 139 | using the `lipo' tool if you have problems. 140 | 141 | Installation Names 142 | ================== 143 | 144 | By default, `make install' installs the package's commands under 145 | `/usr/local/bin', include files under `/usr/local/include', etc. You 146 | can specify an installation prefix other than `/usr/local' by giving 147 | `configure' the option `--prefix=PREFIX', where PREFIX must be an 148 | absolute file name. 149 | 150 | You can specify separate installation prefixes for 151 | architecture-specific files and architecture-independent files. If you 152 | pass the option `--exec-prefix=PREFIX' to `configure', the package uses 153 | PREFIX as the prefix for installing programs and libraries. 154 | Documentation and other data files still use the regular prefix. 155 | 156 | In addition, if you use an unusual directory layout you can give 157 | options like `--bindir=DIR' to specify different values for particular 158 | kinds of files. Run `configure --help' for a list of the directories 159 | you can set and what kinds of files go in them. In general, the 160 | default for these options is expressed in terms of `${prefix}', so that 161 | specifying just `--prefix' will affect all of the other directory 162 | specifications that were not explicitly provided. 163 | 164 | The most portable way to affect installation locations is to pass the 165 | correct locations to `configure'; however, many packages provide one or 166 | both of the following shortcuts of passing variable assignments to the 167 | `make install' command line to change installation locations without 168 | having to reconfigure or recompile. 169 | 170 | The first method involves providing an override variable for each 171 | affected directory. For example, `make install 172 | prefix=/alternate/directory' will choose an alternate location for all 173 | directory configuration variables that were expressed in terms of 174 | `${prefix}'. Any directories that were specified during `configure', 175 | but not in terms of `${prefix}', must each be overridden at install 176 | time for the entire installation to be relocated. The approach of 177 | makefile variable overrides for each directory variable is required by 178 | the GNU Coding Standards, and ideally causes no recompilation. 179 | However, some platforms have known limitations with the semantics of 180 | shared libraries that end up requiring recompilation when using this 181 | method, particularly noticeable in packages that use GNU Libtool. 182 | 183 | The second method involves providing the `DESTDIR' variable. For 184 | example, `make install DESTDIR=/alternate/directory' will prepend 185 | `/alternate/directory' before all installation names. The approach of 186 | `DESTDIR' overrides is not required by the GNU Coding Standards, and 187 | does not work on platforms that have drive letters. On the other hand, 188 | it does better at avoiding recompilation issues, and works well even 189 | when some directory options were not specified in terms of `${prefix}' 190 | at `configure' time. 191 | 192 | Optional Features 193 | ================= 194 | 195 | If the package supports it, you can cause programs to be installed 196 | with an extra prefix or suffix on their names by giving `configure' the 197 | option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. 198 | 199 | Some packages pay attention to `--enable-FEATURE' options to 200 | `configure', where FEATURE indicates an optional part of the package. 201 | They may also pay attention to `--with-PACKAGE' options, where PACKAGE 202 | is something like `gnu-as' or `x' (for the X Window System). The 203 | `README' should mention any `--enable-' and `--with-' options that the 204 | package recognizes. 205 | 206 | For packages that use the X Window System, `configure' can usually 207 | find the X include and library files automatically, but if it doesn't, 208 | you can use the `configure' options `--x-includes=DIR' and 209 | `--x-libraries=DIR' to specify their locations. 210 | 211 | Some packages offer the ability to configure how verbose the 212 | execution of `make' will be. For these packages, running `./configure 213 | --enable-silent-rules' sets the default to minimal output, which can be 214 | overridden with `make V=1'; while running `./configure 215 | --disable-silent-rules' sets the default to verbose, which can be 216 | overridden with `make V=0'. 217 | 218 | Particular systems 219 | ================== 220 | 221 | On HP-UX, the default C compiler is not ANSI C compatible. If GNU 222 | CC is not installed, it is recommended to use the following options in 223 | order to use an ANSI C compiler: 224 | 225 | ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" 226 | 227 | and if that doesn't work, install pre-built binaries of GCC for HP-UX. 228 | 229 | HP-UX `make' updates targets which have the same time stamps as 230 | their prerequisites, which makes it generally unusable when shipped 231 | generated files such as `configure' are involved. Use GNU `make' 232 | instead. 233 | 234 | On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot 235 | parse its `' header file. The option `-nodtk' can be used as 236 | a workaround. If GNU CC is not installed, it is therefore recommended 237 | to try 238 | 239 | ./configure CC="cc" 240 | 241 | and if that doesn't work, try 242 | 243 | ./configure CC="cc -nodtk" 244 | 245 | On Solaris, don't put `/usr/ucb' early in your `PATH'. This 246 | directory contains several dysfunctional programs; working variants of 247 | these programs are available in `/usr/bin'. So, if you need `/usr/ucb' 248 | in your `PATH', put it _after_ `/usr/bin'. 249 | 250 | On Haiku, software installed for all users goes in `/boot/common', 251 | not `/usr/local'. It is recommended to use the following options: 252 | 253 | ./configure --prefix=/boot/common 254 | 255 | Specifying the System Type 256 | ========================== 257 | 258 | There may be some features `configure' cannot figure out 259 | automatically, but needs to determine by the type of machine the package 260 | will run on. Usually, assuming the package is built to be run on the 261 | _same_ architectures, `configure' can figure that out, but if it prints 262 | a message saying it cannot guess the machine type, give it the 263 | `--build=TYPE' option. TYPE can either be a short name for the system 264 | type, such as `sun4', or a canonical name which has the form: 265 | 266 | CPU-COMPANY-SYSTEM 267 | 268 | where SYSTEM can have one of these forms: 269 | 270 | OS 271 | KERNEL-OS 272 | 273 | See the file `config.sub' for the possible values of each field. If 274 | `config.sub' isn't included in this package, then this package doesn't 275 | need to know the machine type. 276 | 277 | If you are _building_ compiler tools for cross-compiling, you should 278 | use the option `--target=TYPE' to select the type of system they will 279 | produce code for. 280 | 281 | If you want to _use_ a cross compiler, that generates code for a 282 | platform different from the build platform, you should specify the 283 | "host" platform (i.e., that on which the generated programs will 284 | eventually be run) with `--host=TYPE'. 285 | 286 | Sharing Defaults 287 | ================ 288 | 289 | If you want to set default values for `configure' scripts to share, 290 | you can create a site shell script called `config.site' that gives 291 | default values for variables like `CC', `cache_file', and `prefix'. 292 | `configure' looks for `PREFIX/share/config.site' if it exists, then 293 | `PREFIX/etc/config.site' if it exists. Or, you can set the 294 | `CONFIG_SITE' environment variable to the location of the site script. 295 | A warning: not all `configure' scripts look for a site script. 296 | 297 | Defining Variables 298 | ================== 299 | 300 | Variables not defined in a site shell script can be set in the 301 | environment passed to `configure'. However, some packages may run 302 | configure again during the build, and the customized values of these 303 | variables may be lost. In order to avoid this problem, you should set 304 | them in the `configure' command line, using `VAR=value'. For example: 305 | 306 | ./configure CC=/usr/local2/bin/gcc 307 | 308 | causes the specified `gcc' to be used as the C compiler (unless it is 309 | overridden in the site shell script). 310 | 311 | Unfortunately, this technique does not work for `CONFIG_SHELL' due to 312 | an Autoconf limitation. Until the limitation is lifted, you can use 313 | this workaround: 314 | 315 | CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash 316 | 317 | `configure' Invocation 318 | ====================== 319 | 320 | `configure' recognizes the following options to control how it 321 | operates. 322 | 323 | `--help' 324 | `-h' 325 | Print a summary of all of the options to `configure', and exit. 326 | 327 | `--help=short' 328 | `--help=recursive' 329 | Print a summary of the options unique to this package's 330 | `configure', and exit. The `short' variant lists options used 331 | only in the top level, while the `recursive' variant lists options 332 | also present in any nested packages. 333 | 334 | `--version' 335 | `-V' 336 | Print the version of Autoconf used to generate the `configure' 337 | script, and exit. 338 | 339 | `--cache-file=FILE' 340 | Enable the cache: use and save the results of the tests in FILE, 341 | traditionally `config.cache'. FILE defaults to `/dev/null' to 342 | disable caching. 343 | 344 | `--config-cache' 345 | `-C' 346 | Alias for `--cache-file=config.cache'. 347 | 348 | `--quiet' 349 | `--silent' 350 | `-q' 351 | Do not print messages saying which checks are being made. To 352 | suppress all normal output, redirect it to `/dev/null' (any error 353 | messages will still be shown). 354 | 355 | `--srcdir=DIR' 356 | Look for the package's source code in directory DIR. Usually 357 | `configure' can determine that directory automatically. 358 | 359 | `--prefix=DIR' 360 | Use DIR as the installation prefix. *note Installation Names:: 361 | for more details, including other options available for fine-tuning 362 | the installation locations. 363 | 364 | `--no-create' 365 | `-n' 366 | Run the configure checks, but stop before creating any output 367 | files. 368 | 369 | `configure' also accepts some other, not widely useful, options. Run 370 | `configure --help' for more details. 371 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # Put compiled object files into the same sub-directory as their sources 2 | AUTOMAKE_OPTIONS = subdir-objects 3 | 4 | # Point to our macro directory and pick up user flags from the environment 5 | ACLOCAL_AMFLAGS = -I macros ${ACLOCAL_FLAGS} 6 | 7 | # Make warnings always fatal at distcheck time, and disable execution 8 | # of tools that change files not installed by the package itself 9 | DISTCHECK_CONFIGURE_FLAGS = --enable-warnings=fatal 10 | 11 | pkgconfigdir = $(libdir)/pkgconfig 12 | nodist_pkgconfig_DATA = $(MURRAYC_SUFFIX_TREE_MODULE_NAME).pc 13 | 14 | AM_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) 15 | 16 | COMMON_CXXFLAGS = $(PROJECT_CFLAGS) \ 17 | $(PROJECT_WXXFLAGS) \ 18 | $(BOOST_CPPFLAGS) 19 | COMMON_LIBS = $(PROJECT_LIBS) \ 20 | $(BOOST_SYSTEM_LIB) \ 21 | $(BOOST_TIMER_LIB) 22 | 23 | # TODO: Why do we still need to specify this for each executable that we build? 24 | AM_CXXFLAGS = $(COMMON_CXXFLAGS) 25 | 26 | check_PROGRAMS = \ 27 | tests/test_trie \ 28 | tests/test_radix_tree \ 29 | tests/test_suffix_tree \ 30 | tests/test_suffix_array 31 | 32 | TESTS = $(check_PROGRAMS) 33 | 34 | #List of source files needed to build the executable: 35 | tests_test_trie_SOURCES = \ 36 | murrayc-suffix-tree/trie.h \ 37 | tests/test_trie.cc 38 | tests_test_trie_CXXFLAGS = \ 39 | $(COMMON_CXXFLAGS) 40 | tests_test_trie_LDADD = \ 41 | $(COMMON_LIBS) 42 | 43 | tests_test_radix_tree_SOURCES = \ 44 | murrayc-suffix-tree/radix_tree.h \ 45 | tests/test_radix_tree.cc 46 | tests_test_radix_tree_CXXFLAGS = \ 47 | $(COMMON_CXXFLAGS) 48 | tests_test_radix_tree_LDADD = \ 49 | $(COMMON_LIBS) 50 | 51 | tests_test_suffix_tree_SOURCES = \ 52 | murrayc-suffix-tree/suffix_tree.h \ 53 | murrayc-suffix-tree/iter_range.h \ 54 | tests/test_suffix_tree.cc 55 | tests_test_suffix_tree_CXXFLAGS = \ 56 | $(COMMON_CXXFLAGS) \ 57 | -DMURRAYC_SUFFIX_TREE_TESTS_DIR=\""$(abs_top_srcdir)/tests/"\" 58 | tests_test_suffix_tree_LDADD = \ 59 | $(COMMON_LIBS) 60 | 61 | tests_test_suffix_array_SOURCES = \ 62 | murrayc-suffix-tree/suffix_array.h \ 63 | murrayc-suffix-tree/iter_range.h \ 64 | tests/test_suffix_array.cc 65 | tests_test_suffix_array_CXXFLAGS = \ 66 | $(COMMON_CXXFLAGS) 67 | tests_test_suffix_array_LDADD = \ 68 | $(COMMON_LIBS) 69 | 70 | EXTRA_DIST = tests/test_pg1400.txt 71 | 72 | # TODO: We shouldn't have to mention this: 73 | DISTCLEANFILES = stamp-h2 74 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/murraycu/murrayc-suffix-tree/d6ec1a131adedaaa0af88cac21635e4c42988b14/NEWS -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/murraycu/murrayc-suffix-tree/d6ec1a131adedaaa0af88cac21635e4c42988b14/README -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # murrayc-suffix-tree 2 | 3 | This experiment tries to provide a generic Suffix Tree implementation for C++. 4 | 5 | 6 | ## Basic Usage 7 | 8 | Include the specific header. For instance, 9 | ```C++ 10 | #include 11 | ``` 12 | 13 | If your source file is program.cc, you can compile it with: 14 | ```shell 15 | g++ program.cc -o program `pkg-config --cflags --libs murrayc-suffix-tree-1.0` 16 | ``` 17 | 18 | ## Using Autotools 19 | 20 | Alternatively, if using autoconf, use the following in configure.ac: 21 | ```m4 22 | PKG_CHECK_MODULES([DEPS], [murrayc-suffix-tree-1.0]) 23 | ``` 24 | 25 | Then use the generated DEPS_CFLAGS and DEPS_LIBS variables in the project Makefile.am files. For example: 26 | ```Makefile 27 | yourprogram_CPPFLAGS = $(DEPS_CFLAGS) 28 | yourprogram_LDADD = $(DEPS_LIBS) 29 | ``` 30 | 31 | Your PKG_CHECK_MODULES() call should also mention any other libraries that you need to use via pkg-config. 32 | 33 | ## Using CMake 34 | 35 | If using CMake, use the following in CMakeList.txt: 36 | ```CMake 37 | include(FindPkgConfig) 38 | pkg_check_modules(DEPS REQUIRED murrayc-suffix-tree-1.0) 39 | include_directories(${DEPS_INCLUDE_DIRS}) 40 | target_link_libraries(yourprogram ${DEPS_LIBRARIES}) 41 | ``` 42 | 43 | Your pkg_check_modules() call should also mention any other libraries that you need to use via pkg-config 44 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh -e 2 | test -n "$srcdir" || srcdir=`dirname "$0"` 3 | test -n "$srcdir" || srcdir=. 4 | ( 5 | cd "$srcdir" && 6 | mm-common-prepare --copy --force && 7 | autoreconf --force --install 8 | ) || exit 9 | test -n "$NOCONFIGURE" || "$srcdir/configure" --enable-maintainer-mode "$@" 10 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([murrayc-suffix-tree],[0.0.1]) 2 | AC_PREREQ(2.63) 3 | 4 | AC_CONFIG_SRCDIR([murrayc-suffix-tree/suffix_tree.h]) 5 | AC_CONFIG_MACRO_DIR([macros]) 6 | $AC_CONFIG_HEADERS([config.h]) 7 | 8 | AM_INIT_AUTOMAKE([1.10 -Wno-portability no-define nostdinc tar-ustar]) 9 | m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])]) 10 | AM_MAINTAINER_MODE 11 | AC_ARG_VAR([ACLOCAL_FLAGS], [aclocal flags, e.g. -I ]) 12 | 13 | AC_PROG_CC 14 | AM_PROG_CC_C_O 15 | AC_PROG_CXX 16 | 17 | AX_CXX_COMPILE_STDCXX([14], [noext],[mandatory]) 18 | 19 | 20 | //PKG_CHECK_MODULES([PROJECT], [gdkmm-3.0]) 21 | 22 | # See http://www.gnu.org/software/autoconf-archive/ax_boost_base.html 23 | # Boost System is needed by Boost Timer 24 | AX_BOOST_BASE 25 | AX_BOOST_SYSTEM 26 | AX_BOOST_TIMER 27 | 28 | # Allow use of mm-common macros for the warnings option. 29 | MM_PREREQ([0.9.5]) 30 | MM_INIT_MODULE([murrayc-suffix-tree-0-1]) 31 | 32 | # http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html 33 | AC_SUBST([LIBMURRAYC_SUFFIX_TREE_SO_VERSION], [0:0:0]) 34 | 35 | AC_LANG([C++]) 36 | MM_ARG_ENABLE_WARNINGS([PROJECT_WXXFLAGS], 37 | [-Wall], 38 | [-pedantic -Wall -Wextra -Wsuggest-override -Wshadow -Wformat-security -Wno-missing-field-initializers], 39 | []) 40 | 41 | 42 | AC_CONFIG_FILES([Makefile 43 | ${MURRAYC_SUFFIX_TREE_MODULE_NAME}.pc:murrayc-suffix-tree.pc.in]) 44 | 45 | AC_OUTPUT 46 | -------------------------------------------------------------------------------- /macros/ax_boost_base.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_boost_base.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_BOOST_BASE([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Test for the Boost C++ libraries of a particular version (or newer) 12 | # 13 | # If no path to the installed boost library is given the macro searchs 14 | # under /usr, /usr/local, /opt and /opt/local and evaluates the 15 | # $BOOST_ROOT environment variable. Further documentation is available at 16 | # . 17 | # 18 | # This macro calls: 19 | # 20 | # AC_SUBST(BOOST_CPPFLAGS) / AC_SUBST(BOOST_LDFLAGS) 21 | # 22 | # And sets: 23 | # 24 | # HAVE_BOOST 25 | # 26 | # LICENSE 27 | # 28 | # Copyright (c) 2008 Thomas Porschberg 29 | # Copyright (c) 2009 Peter Adolphs 30 | # 31 | # Copying and distribution of this file, with or without modification, are 32 | # permitted in any medium without royalty provided the copyright notice 33 | # and this notice are preserved. This file is offered as-is, without any 34 | # warranty. 35 | 36 | #serial 26 37 | 38 | AC_DEFUN([AX_BOOST_BASE], 39 | [ 40 | AC_ARG_WITH([boost], 41 | [AS_HELP_STRING([--with-boost@<:@=ARG@:>@], 42 | [use Boost library from a standard location (ARG=yes), 43 | from the specified location (ARG=), 44 | or disable it (ARG=no) 45 | @<:@ARG=yes@:>@ ])], 46 | [ 47 | if test "$withval" = "no"; then 48 | want_boost="no" 49 | elif test "$withval" = "yes"; then 50 | want_boost="yes" 51 | ac_boost_path="" 52 | else 53 | want_boost="yes" 54 | ac_boost_path="$withval" 55 | fi 56 | ], 57 | [want_boost="yes"]) 58 | 59 | 60 | AC_ARG_WITH([boost-libdir], 61 | AS_HELP_STRING([--with-boost-libdir=LIB_DIR], 62 | [Force given directory for boost libraries. Note that this will override library path detection, so use this parameter only if default library detection fails and you know exactly where your boost libraries are located.]), 63 | [ 64 | if test -d "$withval" 65 | then 66 | ac_boost_lib_path="$withval" 67 | else 68 | AC_MSG_ERROR(--with-boost-libdir expected directory name) 69 | fi 70 | ], 71 | [ac_boost_lib_path=""] 72 | ) 73 | 74 | if test "x$want_boost" = "xyes"; then 75 | boost_lib_version_req=ifelse([$1], ,1.20.0,$1) 76 | boost_lib_version_req_shorten=`expr $boost_lib_version_req : '\([[0-9]]*\.[[0-9]]*\)'` 77 | boost_lib_version_req_major=`expr $boost_lib_version_req : '\([[0-9]]*\)'` 78 | boost_lib_version_req_minor=`expr $boost_lib_version_req : '[[0-9]]*\.\([[0-9]]*\)'` 79 | boost_lib_version_req_sub_minor=`expr $boost_lib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'` 80 | if test "x$boost_lib_version_req_sub_minor" = "x" ; then 81 | boost_lib_version_req_sub_minor="0" 82 | fi 83 | WANT_BOOST_VERSION=`expr $boost_lib_version_req_major \* 100000 \+ $boost_lib_version_req_minor \* 100 \+ $boost_lib_version_req_sub_minor` 84 | AC_MSG_CHECKING(for boostlib >= $boost_lib_version_req) 85 | succeeded=no 86 | 87 | dnl On 64-bit systems check for system libraries in both lib64 and lib. 88 | dnl The former is specified by FHS, but e.g. Debian does not adhere to 89 | dnl this (as it rises problems for generic multi-arch support). 90 | dnl The last entry in the list is chosen by default when no libraries 91 | dnl are found, e.g. when only header-only libraries are installed! 92 | libsubdirs="lib" 93 | ax_arch=`uname -m` 94 | case $ax_arch in 95 | x86_64) 96 | libsubdirs="lib64 libx32 lib lib64" 97 | ;; 98 | ppc64|s390x|sparc64|aarch64|ppc64le) 99 | libsubdirs="lib64 lib lib64 ppc64le" 100 | ;; 101 | esac 102 | 103 | dnl allow for real multi-arch paths e.g. /usr/lib/x86_64-linux-gnu. Give 104 | dnl them priority over the other paths since, if libs are found there, they 105 | dnl are almost assuredly the ones desired. 106 | AC_REQUIRE([AC_CANONICAL_HOST]) 107 | libsubdirs="lib/${host_cpu}-${host_os} $libsubdirs" 108 | 109 | case ${host_cpu} in 110 | i?86) 111 | libsubdirs="lib/i386-${host_os} $libsubdirs" 112 | ;; 113 | esac 114 | 115 | dnl first we check the system location for boost libraries 116 | dnl this location ist chosen if boost libraries are installed with the --layout=system option 117 | dnl or if you install boost with RPM 118 | if test "$ac_boost_path" != ""; then 119 | BOOST_CPPFLAGS="-I$ac_boost_path/include" 120 | for ac_boost_path_tmp in $libsubdirs; do 121 | if test -d "$ac_boost_path"/"$ac_boost_path_tmp" ; then 122 | BOOST_LDFLAGS="-L$ac_boost_path/$ac_boost_path_tmp" 123 | break 124 | fi 125 | done 126 | elif test "$cross_compiling" != yes; then 127 | for ac_boost_path_tmp in /usr /usr/local /opt /opt/local ; do 128 | if test -d "$ac_boost_path_tmp/include/boost" && test -r "$ac_boost_path_tmp/include/boost"; then 129 | for libsubdir in $libsubdirs ; do 130 | if ls "$ac_boost_path_tmp/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi 131 | done 132 | BOOST_LDFLAGS="-L$ac_boost_path_tmp/$libsubdir" 133 | BOOST_CPPFLAGS="-I$ac_boost_path_tmp/include" 134 | break; 135 | fi 136 | done 137 | fi 138 | 139 | dnl overwrite ld flags if we have required special directory with 140 | dnl --with-boost-libdir parameter 141 | if test "$ac_boost_lib_path" != ""; then 142 | BOOST_LDFLAGS="-L$ac_boost_lib_path" 143 | fi 144 | 145 | CPPFLAGS_SAVED="$CPPFLAGS" 146 | CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" 147 | export CPPFLAGS 148 | 149 | LDFLAGS_SAVED="$LDFLAGS" 150 | LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" 151 | export LDFLAGS 152 | 153 | AC_REQUIRE([AC_PROG_CXX]) 154 | AC_LANG_PUSH(C++) 155 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ 156 | @%:@include 157 | ]], [[ 158 | #if BOOST_VERSION >= $WANT_BOOST_VERSION 159 | // Everything is okay 160 | #else 161 | # error Boost version is too old 162 | #endif 163 | ]])],[ 164 | AC_MSG_RESULT(yes) 165 | succeeded=yes 166 | found_system=yes 167 | ],[ 168 | ]) 169 | AC_LANG_POP([C++]) 170 | 171 | 172 | 173 | dnl if we found no boost with system layout we search for boost libraries 174 | dnl built and installed without the --layout=system option or for a staged(not installed) version 175 | if test "x$succeeded" != "xyes"; then 176 | CPPFLAGS="$CPPFLAGS_SAVED" 177 | LDFLAGS="$LDFLAGS_SAVED" 178 | BOOST_CPPFLAGS= 179 | BOOST_LDFLAGS= 180 | _version=0 181 | if test "$ac_boost_path" != ""; then 182 | if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then 183 | for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do 184 | _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'` 185 | V_CHECK=`expr $_version_tmp \> $_version` 186 | if test "$V_CHECK" = "1" ; then 187 | _version=$_version_tmp 188 | fi 189 | VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'` 190 | BOOST_CPPFLAGS="-I$ac_boost_path/include/boost-$VERSION_UNDERSCORE" 191 | done 192 | dnl if nothing found search for layout used in Windows distributions 193 | if test -z "$BOOST_CPPFLAGS"; then 194 | if test -d "$ac_boost_path/boost" && test -r "$ac_boost_path/boost"; then 195 | BOOST_CPPFLAGS="-I$ac_boost_path" 196 | fi 197 | fi 198 | fi 199 | else 200 | if test "$cross_compiling" != yes; then 201 | for ac_boost_path in /usr /usr/local /opt /opt/local ; do 202 | if test -d "$ac_boost_path" && test -r "$ac_boost_path"; then 203 | for i in `ls -d $ac_boost_path/include/boost-* 2>/dev/null`; do 204 | _version_tmp=`echo $i | sed "s#$ac_boost_path##" | sed 's/\/include\/boost-//' | sed 's/_/./'` 205 | V_CHECK=`expr $_version_tmp \> $_version` 206 | if test "$V_CHECK" = "1" ; then 207 | _version=$_version_tmp 208 | best_path=$ac_boost_path 209 | fi 210 | done 211 | fi 212 | done 213 | 214 | VERSION_UNDERSCORE=`echo $_version | sed 's/\./_/'` 215 | BOOST_CPPFLAGS="-I$best_path/include/boost-$VERSION_UNDERSCORE" 216 | if test "$ac_boost_lib_path" = ""; then 217 | for libsubdir in $libsubdirs ; do 218 | if ls "$best_path/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi 219 | done 220 | BOOST_LDFLAGS="-L$best_path/$libsubdir" 221 | fi 222 | fi 223 | 224 | if test "x$BOOST_ROOT" != "x"; then 225 | for libsubdir in $libsubdirs ; do 226 | if ls "$BOOST_ROOT/stage/$libsubdir/libboost_"* >/dev/null 2>&1 ; then break; fi 227 | done 228 | if test -d "$BOOST_ROOT" && test -r "$BOOST_ROOT" && test -d "$BOOST_ROOT/stage/$libsubdir" && test -r "$BOOST_ROOT/stage/$libsubdir"; then 229 | version_dir=`expr //$BOOST_ROOT : '.*/\(.*\)'` 230 | stage_version=`echo $version_dir | sed 's/boost_//' | sed 's/_/./g'` 231 | stage_version_shorten=`expr $stage_version : '\([[0-9]]*\.[[0-9]]*\)'` 232 | V_CHECK=`expr $stage_version_shorten \>\= $_version` 233 | if test "$V_CHECK" = "1" -a "$ac_boost_lib_path" = "" ; then 234 | AC_MSG_NOTICE(We will use a staged boost library from $BOOST_ROOT) 235 | BOOST_CPPFLAGS="-I$BOOST_ROOT" 236 | BOOST_LDFLAGS="-L$BOOST_ROOT/stage/$libsubdir" 237 | fi 238 | fi 239 | fi 240 | fi 241 | 242 | CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" 243 | export CPPFLAGS 244 | LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" 245 | export LDFLAGS 246 | 247 | AC_LANG_PUSH(C++) 248 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ 249 | @%:@include 250 | ]], [[ 251 | #if BOOST_VERSION >= $WANT_BOOST_VERSION 252 | // Everything is okay 253 | #else 254 | # error Boost version is too old 255 | #endif 256 | ]])],[ 257 | AC_MSG_RESULT(yes) 258 | succeeded=yes 259 | found_system=yes 260 | ],[ 261 | ]) 262 | AC_LANG_POP([C++]) 263 | fi 264 | 265 | if test "$succeeded" != "yes" ; then 266 | if test "$_version" = "0" ; then 267 | AC_MSG_NOTICE([[We could not detect the boost libraries (version $boost_lib_version_req_shorten or higher). If you have a staged boost library (still not installed) please specify \$BOOST_ROOT in your environment and do not give a PATH to --with-boost option. If you are sure you have boost installed, then check your version number looking in . See http://randspringer.de/boost for more documentation.]]) 268 | else 269 | AC_MSG_NOTICE([Your boost libraries seems to old (version $_version).]) 270 | fi 271 | # execute ACTION-IF-NOT-FOUND (if present): 272 | ifelse([$3], , :, [$3]) 273 | else 274 | AC_SUBST(BOOST_CPPFLAGS) 275 | AC_SUBST(BOOST_LDFLAGS) 276 | AC_DEFINE(HAVE_BOOST,,[define if the Boost library is available]) 277 | # execute ACTION-IF-FOUND (if present): 278 | ifelse([$2], , :, [$2]) 279 | fi 280 | 281 | CPPFLAGS="$CPPFLAGS_SAVED" 282 | LDFLAGS="$LDFLAGS_SAVED" 283 | fi 284 | 285 | ]) 286 | -------------------------------------------------------------------------------- /macros/ax_boost_system.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_boost_system.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_BOOST_SYSTEM 8 | # 9 | # DESCRIPTION 10 | # 11 | # Test for System library from the Boost C++ libraries. The macro requires 12 | # a preceding call to AX_BOOST_BASE. Further documentation is available at 13 | # . 14 | # 15 | # This macro calls: 16 | # 17 | # AC_SUBST(BOOST_SYSTEM_LIB) 18 | # 19 | # And sets: 20 | # 21 | # HAVE_BOOST_SYSTEM 22 | # 23 | # LICENSE 24 | # 25 | # Copyright (c) 2008 Thomas Porschberg 26 | # Copyright (c) 2008 Michael Tindal 27 | # Copyright (c) 2008 Daniel Casimiro 28 | # 29 | # Copying and distribution of this file, with or without modification, are 30 | # permitted in any medium without royalty provided the copyright notice 31 | # and this notice are preserved. This file is offered as-is, without any 32 | # warranty. 33 | 34 | #serial 17 35 | 36 | AC_DEFUN([AX_BOOST_SYSTEM], 37 | [ 38 | AC_ARG_WITH([boost-system], 39 | AS_HELP_STRING([--with-boost-system@<:@=special-lib@:>@], 40 | [use the System library from boost - it is possible to specify a certain library for the linker 41 | e.g. --with-boost-system=boost_system-gcc-mt ]), 42 | [ 43 | if test "$withval" = "no"; then 44 | want_boost="no" 45 | elif test "$withval" = "yes"; then 46 | want_boost="yes" 47 | ax_boost_user_system_lib="" 48 | else 49 | want_boost="yes" 50 | ax_boost_user_system_lib="$withval" 51 | fi 52 | ], 53 | [want_boost="yes"] 54 | ) 55 | 56 | if test "x$want_boost" = "xyes"; then 57 | AC_REQUIRE([AC_PROG_CC]) 58 | AC_REQUIRE([AC_CANONICAL_BUILD]) 59 | CPPFLAGS_SAVED="$CPPFLAGS" 60 | CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" 61 | export CPPFLAGS 62 | 63 | LDFLAGS_SAVED="$LDFLAGS" 64 | LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" 65 | export LDFLAGS 66 | 67 | AC_CACHE_CHECK(whether the Boost::System library is available, 68 | ax_cv_boost_system, 69 | [AC_LANG_PUSH([C++]) 70 | CXXFLAGS_SAVE=$CXXFLAGS 71 | 72 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include ]], 73 | [[boost::system::system_category]])], 74 | ax_cv_boost_system=yes, ax_cv_boost_system=no) 75 | CXXFLAGS=$CXXFLAGS_SAVE 76 | AC_LANG_POP([C++]) 77 | ]) 78 | if test "x$ax_cv_boost_system" = "xyes"; then 79 | AC_SUBST(BOOST_CPPFLAGS) 80 | 81 | AC_DEFINE(HAVE_BOOST_SYSTEM,,[define if the Boost::System library is available]) 82 | BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'` 83 | 84 | LDFLAGS_SAVE=$LDFLAGS 85 | if test "x$ax_boost_user_system_lib" = "x"; then 86 | for libextension in `ls -r $BOOSTLIBDIR/libboost_system* 2>/dev/null | sed 's,.*/lib,,' | sed 's,\..*,,'` ; do 87 | ax_lib=${libextension} 88 | AC_CHECK_LIB($ax_lib, exit, 89 | [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break], 90 | [link_system="no"]) 91 | done 92 | if test "x$link_system" != "xyes"; then 93 | for libextension in `ls -r $BOOSTLIBDIR/boost_system* 2>/dev/null | sed 's,.*/,,' | sed -e 's,\..*,,'` ; do 94 | ax_lib=${libextension} 95 | AC_CHECK_LIB($ax_lib, exit, 96 | [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break], 97 | [link_system="no"]) 98 | done 99 | fi 100 | 101 | else 102 | for ax_lib in $ax_boost_user_system_lib boost_system-$ax_boost_user_system_lib; do 103 | AC_CHECK_LIB($ax_lib, exit, 104 | [BOOST_SYSTEM_LIB="-l$ax_lib"; AC_SUBST(BOOST_SYSTEM_LIB) link_system="yes"; break], 105 | [link_system="no"]) 106 | done 107 | 108 | fi 109 | if test "x$ax_lib" = "x"; then 110 | AC_MSG_ERROR(Could not find a version of the library!) 111 | fi 112 | if test "x$link_system" = "xno"; then 113 | AC_MSG_ERROR(Could not link against $ax_lib !) 114 | fi 115 | fi 116 | 117 | CPPFLAGS="$CPPFLAGS_SAVED" 118 | LDFLAGS="$LDFLAGS_SAVED" 119 | fi 120 | ]) 121 | 122 | -------------------------------------------------------------------------------- /macros/ax_boost_timer.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_boost_timer.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_BOOST_TIMER 8 | # 9 | # DESCRIPTION 10 | # 11 | # Test for System library from the Boost C++ libraries. The macro requires 12 | # a preceding call to AX_BOOST_BASE. Further documentation is available at 13 | # . 14 | # 15 | # This macro calls: 16 | # 17 | # AC_SUBST(BOOST_TIMER_LIB) 18 | # 19 | # And sets: 20 | # 21 | # HAVE_BOOST_TIMER 22 | # 23 | # LICENSE 24 | # 25 | # Copyright (c) 2012 Xiyue Deng 26 | # Copyright (c) 2012 Murray Cumming 27 | # 28 | # Copying and distribution of this file, with or without modification, are 29 | # permitted in any medium without royalty provided the copyright notice 30 | # and this notice are preserved. This file is offered as-is, without any 31 | # warranty. 32 | 33 | #serial 2 (based on serial 1 of ax_boost_locale.m4 with some simple find/replace by Murray Cumming) 34 | 35 | AC_DEFUN([AX_BOOST_TIMER], 36 | [ 37 | AC_ARG_WITH([boost-timer], 38 | AS_HELP_STRING([--with-boost-timer@<:@=special-lib@:>@], 39 | [use the Timer library from boost - it is possible to specify a certain library for the linker 40 | e.g. --with-boost-timer=boost_timer-gcc-mt ]), 41 | [ 42 | if test "$withval" = "no"; then 43 | want_boost="no" 44 | elif test "$withval" = "yes"; then 45 | want_boost="yes" 46 | ax_boost_user_timer_lib="" 47 | else 48 | want_boost="yes" 49 | ax_boost_user_timer_lib="$withval" 50 | fi 51 | ], 52 | [want_boost="yes"] 53 | ) 54 | 55 | if test "x$want_boost" = "xyes"; then 56 | AC_REQUIRE([AC_PROG_CC]) 57 | AC_REQUIRE([AC_CANONICAL_BUILD]) 58 | CPPFLAGS_SAVED="$CPPFLAGS" 59 | CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS" 60 | export CPPFLAGS 61 | 62 | LDFLAGS_SAVED="$LDFLAGS" 63 | LDFLAGS="$LDFLAGS $BOOST_LDFLAGS" 64 | export LDFLAGS 65 | 66 | AC_CACHE_CHECK(whether the Boost::Timer library is available, 67 | ax_cv_boost_timer, 68 | [AC_LANG_PUSH([C++]) 69 | CXXFLAGS_SAVE=$CXXFLAGS 70 | 71 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include ]], 72 | [[boost::timer::cpu_timer().stop();]])], 73 | ax_cv_boost_timer=yes, ax_cv_boost_timer=no) 74 | CXXFLAGS=$CXXFLAGS_SAVE 75 | AC_LANG_POP([C++]) 76 | ]) 77 | if test "x$ax_cv_boost_timer" = "xyes"; then 78 | AC_SUBST(BOOST_CPPFLAGS) 79 | 80 | AC_DEFINE(HAVE_BOOST_TIMER,,[define if the Boost::Timer library is available]) 81 | BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'` 82 | 83 | LDFLAGS_SAVE=$LDFLAGS 84 | if test "x$ax_boost_user_timer_lib" = "x"; then 85 | for libextension in `ls $BOOSTLIBDIR/libboost_timer*.so* $BOOSTLIBDIR/libboost_timer*.dylib* $BOOSTLIBDIR/libboost_timer*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_timer.*\)\.so.*$;\1;' -e 's;^lib\(boost_timer.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_timer.*\)\.a.*$;\1;'` ; do 86 | ax_lib=${libextension} 87 | AC_CHECK_LIB($ax_lib, exit, 88 | [BOOST_TIMER_LIB="-l$ax_lib"; AC_SUBST(BOOST_TIMER_LIB) link_timer="yes"; break], 89 | [link_timer="no"]) 90 | done 91 | if test "x$link_timer" != "xyes"; then 92 | for libextension in `ls $BOOSTLIBDIR/boost_timer*.dll* $BOOSTLIBDIR/boost_timer*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_timer.*\)\.dll.*$;\1;' -e 's;^\(boost_timer.*\)\.a.*$;\1;'` ; do 93 | ax_lib=${libextension} 94 | AC_CHECK_LIB($ax_lib, exit, 95 | [BOOST_TIMER_LIB="-l$ax_lib"; AC_SUBST(BOOST_TIMER_LIB) link_timer="yes"; break], 96 | [link_timer="no"]) 97 | done 98 | fi 99 | 100 | else 101 | for ax_lib in $ax_boost_user_timer_lib boost_timer-$ax_boost_user_timer_lib; do 102 | AC_CHECK_LIB($ax_lib, exit, 103 | [BOOST_TIMER_LIB="-l$ax_lib"; AC_SUBST(BOOST_TIMER_LIB) link_timer="yes"; break], 104 | [link_timer="no"]) 105 | done 106 | 107 | fi 108 | if test "x$ax_lib" = "x"; then 109 | AC_MSG_ERROR(Could not find a version of the library!) 110 | fi 111 | if test "x$link_timer" = "xno"; then 112 | AC_MSG_ERROR(Could not link against $ax_lib !) 113 | fi 114 | fi 115 | 116 | CPPFLAGS="$CPPFLAGS_SAVED" 117 | LDFLAGS="$LDFLAGS_SAVED" 118 | fi 119 | ]) 120 | -------------------------------------------------------------------------------- /macros/ax_cxx_compile_stdcxx.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check for baseline language coverage in the compiler for the specified 12 | # version of the C++ standard. If necessary, add switches to CXXFLAGS to 13 | # enable support. VERSION may be '11' (for the C++11 standard) or '14' 14 | # (for the C++14 standard). 15 | # 16 | # The second argument, if specified, indicates whether you insist on an 17 | # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g. 18 | # -std=c++11). If neither is specified, you get whatever works, with 19 | # preference for an extended mode. 20 | # 21 | # The third argument, if specified 'mandatory' or if left unspecified, 22 | # indicates that baseline support for the specified C++ standard is 23 | # required and that the macro should error out if no mode with that 24 | # support is found. If specified 'optional', then configuration proceeds 25 | # regardless, after defining HAVE_CXX${VERSION} if and only if a 26 | # supporting mode is found. 27 | # 28 | # LICENSE 29 | # 30 | # Copyright (c) 2008 Benjamin Kosnik 31 | # Copyright (c) 2012 Zack Weinberg 32 | # Copyright (c) 2013 Roy Stogner 33 | # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov 34 | # Copyright (c) 2015 Paul Norman 35 | # Copyright (c) 2015 Moritz Klammler 36 | # 37 | # Copying and distribution of this file, with or without modification, are 38 | # permitted in any medium without royalty provided the copyright notice 39 | # and this notice are preserved. This file is offered as-is, without any 40 | # warranty. 41 | 42 | #serial 1 43 | 44 | dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro 45 | dnl (serial version number 13). 46 | 47 | AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl 48 | m4_if([$1], [11], [], 49 | [$1], [14], [], 50 | [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])], 51 | [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl 52 | m4_if([$2], [], [], 53 | [$2], [ext], [], 54 | [$2], [noext], [], 55 | [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl 56 | m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true], 57 | [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true], 58 | [$3], [optional], [ax_cxx_compile_cxx$1_required=false], 59 | [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])]) 60 | AC_LANG_PUSH([C++])dnl 61 | ac_success=no 62 | AC_CACHE_CHECK(whether $CXX supports C++$1 features by default, 63 | ax_cv_cxx_compile_cxx$1, 64 | [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], 65 | [ax_cv_cxx_compile_cxx$1=yes], 66 | [ax_cv_cxx_compile_cxx$1=no])]) 67 | if test x$ax_cv_cxx_compile_cxx$1 = xyes; then 68 | ac_success=yes 69 | fi 70 | 71 | m4_if([$2], [noext], [], [dnl 72 | if test x$ac_success = xno; then 73 | for switch in -std=gnu++$1 -std=gnu++0x; do 74 | cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) 75 | AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, 76 | $cachevar, 77 | [ac_save_CXXFLAGS="$CXXFLAGS" 78 | CXXFLAGS="$CXXFLAGS $switch" 79 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], 80 | [eval $cachevar=yes], 81 | [eval $cachevar=no]) 82 | CXXFLAGS="$ac_save_CXXFLAGS"]) 83 | if eval test x\$$cachevar = xyes; then 84 | CXXFLAGS="$CXXFLAGS $switch" 85 | ac_success=yes 86 | break 87 | fi 88 | done 89 | fi]) 90 | 91 | m4_if([$2], [ext], [], [dnl 92 | if test x$ac_success = xno; then 93 | dnl HP's aCC needs +std=c++11 according to: 94 | dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf 95 | dnl Cray's crayCC needs "-h std=c++11" 96 | for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do 97 | cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) 98 | AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, 99 | $cachevar, 100 | [ac_save_CXXFLAGS="$CXXFLAGS" 101 | CXXFLAGS="$CXXFLAGS $switch" 102 | AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], 103 | [eval $cachevar=yes], 104 | [eval $cachevar=no]) 105 | CXXFLAGS="$ac_save_CXXFLAGS"]) 106 | if eval test x\$$cachevar = xyes; then 107 | CXXFLAGS="$CXXFLAGS $switch" 108 | ac_success=yes 109 | break 110 | fi 111 | done 112 | fi]) 113 | AC_LANG_POP([C++]) 114 | if test x$ax_cxx_compile_cxx$1_required = xtrue; then 115 | if test x$ac_success = xno; then 116 | AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.]) 117 | fi 118 | else 119 | if test x$ac_success = xno; then 120 | HAVE_CXX$1=0 121 | AC_MSG_NOTICE([No compiler with C++$1 support was found]) 122 | else 123 | HAVE_CXX$1=1 124 | AC_DEFINE(HAVE_CXX$1,1, 125 | [define if the compiler supports basic C++$1 syntax]) 126 | fi 127 | 128 | AC_SUBST(HAVE_CXX$1) 129 | fi 130 | ]) 131 | 132 | 133 | dnl Test body for checking C++11 support 134 | 135 | m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11], 136 | _AX_CXX_COMPILE_STDCXX_testbody_new_in_11 137 | ) 138 | 139 | 140 | dnl Test body for checking C++14 support 141 | 142 | m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14], 143 | _AX_CXX_COMPILE_STDCXX_testbody_new_in_11 144 | _AX_CXX_COMPILE_STDCXX_testbody_new_in_14 145 | ) 146 | 147 | 148 | dnl Tests for new features in C++11 149 | 150 | m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[ 151 | 152 | // If the compiler admits that it is not ready for C++11, why torture it? 153 | // Hopefully, this will speed up the test. 154 | 155 | #ifndef __cplusplus 156 | 157 | #error "This is not a C++ compiler" 158 | 159 | #elif __cplusplus < 201103L 160 | 161 | #error "This is not a C++11 compiler" 162 | 163 | #else 164 | 165 | namespace cxx11 166 | { 167 | 168 | namespace test_static_assert 169 | { 170 | 171 | template 172 | struct check 173 | { 174 | static_assert(sizeof(int) <= sizeof(T), "not big enough"); 175 | }; 176 | 177 | } 178 | 179 | namespace test_final_override 180 | { 181 | 182 | struct Base 183 | { 184 | virtual void f() {} 185 | }; 186 | 187 | struct Derived : public Base 188 | { 189 | virtual void f() override {} 190 | }; 191 | 192 | } 193 | 194 | namespace test_double_right_angle_brackets 195 | { 196 | 197 | template < typename T > 198 | struct check {}; 199 | 200 | typedef check single_type; 201 | typedef check> double_type; 202 | typedef check>> triple_type; 203 | typedef check>>> quadruple_type; 204 | 205 | } 206 | 207 | namespace test_decltype 208 | { 209 | 210 | int 211 | f() 212 | { 213 | int a = 1; 214 | decltype(a) b = 2; 215 | return a + b; 216 | } 217 | 218 | } 219 | 220 | namespace test_type_deduction 221 | { 222 | 223 | template < typename T1, typename T2 > 224 | struct is_same 225 | { 226 | static const bool value = false; 227 | }; 228 | 229 | template < typename T > 230 | struct is_same 231 | { 232 | static const bool value = true; 233 | }; 234 | 235 | template < typename T1, typename T2 > 236 | auto 237 | add(T1 a1, T2 a2) -> decltype(a1 + a2) 238 | { 239 | return a1 + a2; 240 | } 241 | 242 | int 243 | test(const int c, volatile int v) 244 | { 245 | static_assert(is_same::value == true, ""); 246 | static_assert(is_same::value == false, ""); 247 | static_assert(is_same::value == false, ""); 248 | auto ac = c; 249 | auto av = v; 250 | auto sumi = ac + av + 'x'; 251 | auto sumf = ac + av + 1.0; 252 | static_assert(is_same::value == true, ""); 253 | static_assert(is_same::value == true, ""); 254 | static_assert(is_same::value == true, ""); 255 | static_assert(is_same::value == false, ""); 256 | static_assert(is_same::value == true, ""); 257 | return (sumf > 0.0) ? sumi : add(c, v); 258 | } 259 | 260 | } 261 | 262 | namespace test_noexcept 263 | { 264 | 265 | int f() { return 0; } 266 | int g() noexcept { return 0; } 267 | 268 | static_assert(noexcept(f()) == false, ""); 269 | static_assert(noexcept(g()) == true, ""); 270 | 271 | } 272 | 273 | namespace test_constexpr 274 | { 275 | 276 | template < typename CharT > 277 | unsigned long constexpr 278 | strlen_c_r(const CharT *const s, const unsigned long acc) noexcept 279 | { 280 | return *s ? strlen_c_r(s + 1, acc + 1) : acc; 281 | } 282 | 283 | template < typename CharT > 284 | unsigned long constexpr 285 | strlen_c(const CharT *const s) noexcept 286 | { 287 | return strlen_c_r(s, 0UL); 288 | } 289 | 290 | static_assert(strlen_c("") == 0UL, ""); 291 | static_assert(strlen_c("1") == 1UL, ""); 292 | static_assert(strlen_c("example") == 7UL, ""); 293 | static_assert(strlen_c("another\0example") == 7UL, ""); 294 | 295 | } 296 | 297 | namespace test_rvalue_references 298 | { 299 | 300 | template < int N > 301 | struct answer 302 | { 303 | static constexpr int value = N; 304 | }; 305 | 306 | answer<1> f(int&) { return answer<1>(); } 307 | answer<2> f(const int&) { return answer<2>(); } 308 | answer<3> f(int&&) { return answer<3>(); } 309 | 310 | void 311 | test() 312 | { 313 | int i = 0; 314 | const int c = 0; 315 | static_assert(decltype(f(i))::value == 1, ""); 316 | static_assert(decltype(f(c))::value == 2, ""); 317 | static_assert(decltype(f(0))::value == 3, ""); 318 | } 319 | 320 | } 321 | 322 | namespace test_uniform_initialization 323 | { 324 | 325 | struct test 326 | { 327 | static const int zero {}; 328 | static const int one {1}; 329 | }; 330 | 331 | static_assert(test::zero == 0, ""); 332 | static_assert(test::one == 1, ""); 333 | 334 | } 335 | 336 | namespace test_lambdas 337 | { 338 | 339 | void 340 | test1() 341 | { 342 | auto lambda1 = [](){}; 343 | auto lambda2 = lambda1; 344 | lambda1(); 345 | lambda2(); 346 | } 347 | 348 | int 349 | test2() 350 | { 351 | auto a = [](int i, int j){ return i + j; }(1, 2); 352 | auto b = []() -> int { return '0'; }(); 353 | auto c = [=](){ return a + b; }(); 354 | auto d = [&](){ return c; }(); 355 | auto e = [a, &b](int x) mutable { 356 | const auto identity = [](int y){ return y; }; 357 | for (auto i = 0; i < a; ++i) 358 | a += b--; 359 | return x + identity(a + b); 360 | }(0); 361 | return a + b + c + d + e; 362 | } 363 | 364 | int 365 | test3() 366 | { 367 | const auto nullary = [](){ return 0; }; 368 | const auto unary = [](int x){ return x; }; 369 | using nullary_t = decltype(nullary); 370 | using unary_t = decltype(unary); 371 | const auto higher1st = [](nullary_t f){ return f(); }; 372 | const auto higher2nd = [unary](nullary_t f1){ 373 | return [unary, f1](unary_t f2){ return f2(unary(f1())); }; 374 | }; 375 | return higher1st(nullary) + higher2nd(nullary)(unary); 376 | } 377 | 378 | } 379 | 380 | namespace test_variadic_templates 381 | { 382 | 383 | template 384 | struct sum; 385 | 386 | template 387 | struct sum 388 | { 389 | static constexpr auto value = N0 + sum::value; 390 | }; 391 | 392 | template <> 393 | struct sum<> 394 | { 395 | static constexpr auto value = 0; 396 | }; 397 | 398 | static_assert(sum<>::value == 0, ""); 399 | static_assert(sum<1>::value == 1, ""); 400 | static_assert(sum<23>::value == 23, ""); 401 | static_assert(sum<1, 2>::value == 3, ""); 402 | static_assert(sum<5, 5, 11>::value == 21, ""); 403 | static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, ""); 404 | 405 | } 406 | 407 | // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae 408 | // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function 409 | // because of this. 410 | namespace test_template_alias_sfinae 411 | { 412 | 413 | struct foo {}; 414 | 415 | template 416 | using member = typename T::member_type; 417 | 418 | template 419 | void func(...) {} 420 | 421 | template 422 | void func(member*) {} 423 | 424 | void test(); 425 | 426 | void test() { func(0); } 427 | 428 | } 429 | 430 | } // namespace cxx11 431 | 432 | #endif // __cplusplus >= 201103L 433 | 434 | ]]) 435 | 436 | 437 | dnl Tests for new features in C++14 438 | 439 | m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[ 440 | 441 | // If the compiler admits that it is not ready for C++14, why torture it? 442 | // Hopefully, this will speed up the test. 443 | 444 | #ifndef __cplusplus 445 | 446 | #error "This is not a C++ compiler" 447 | 448 | #elif __cplusplus < 201402L 449 | 450 | #error "This is not a C++14 compiler" 451 | 452 | #else 453 | 454 | namespace cxx14 455 | { 456 | 457 | namespace test_polymorphic_lambdas 458 | { 459 | 460 | int 461 | test() 462 | { 463 | const auto lambda = [](auto&&... args){ 464 | const auto istiny = [](auto x){ 465 | return (sizeof(x) == 1UL) ? 1 : 0; 466 | }; 467 | const int aretiny[] = { istiny(args)... }; 468 | return aretiny[0]; 469 | }; 470 | return lambda(1, 1L, 1.0f, '1'); 471 | } 472 | 473 | } 474 | 475 | namespace test_binary_literals 476 | { 477 | 478 | constexpr auto ivii = 0b0000000000101010; 479 | static_assert(ivii == 42, "wrong value"); 480 | 481 | } 482 | 483 | namespace test_generalized_constexpr 484 | { 485 | 486 | template < typename CharT > 487 | constexpr unsigned long 488 | strlen_c(const CharT *const s) noexcept 489 | { 490 | auto length = 0UL; 491 | for (auto p = s; *p; ++p) 492 | ++length; 493 | return length; 494 | } 495 | 496 | static_assert(strlen_c("") == 0UL, ""); 497 | static_assert(strlen_c("x") == 1UL, ""); 498 | static_assert(strlen_c("test") == 4UL, ""); 499 | static_assert(strlen_c("another\0test") == 7UL, ""); 500 | 501 | } 502 | 503 | namespace test_lambda_init_capture 504 | { 505 | 506 | int 507 | test() 508 | { 509 | auto x = 0; 510 | const auto lambda1 = [a = x](int b){ return a + b; }; 511 | const auto lambda2 = [a = lambda1(x)](){ return a; }; 512 | return lambda2(); 513 | } 514 | 515 | } 516 | 517 | namespace test_digit_seperators 518 | { 519 | 520 | constexpr auto ten_million = 100'000'000; 521 | static_assert(ten_million == 100000000, ""); 522 | 523 | } 524 | 525 | namespace test_return_type_deduction 526 | { 527 | 528 | auto f(int& x) { return x; } 529 | decltype(auto) g(int& x) { return x; } 530 | 531 | template < typename T1, typename T2 > 532 | struct is_same 533 | { 534 | static constexpr auto value = false; 535 | }; 536 | 537 | template < typename T > 538 | struct is_same 539 | { 540 | static constexpr auto value = true; 541 | }; 542 | 543 | int 544 | test() 545 | { 546 | auto x = 0; 547 | static_assert(is_same::value, ""); 548 | static_assert(is_same::value, ""); 549 | return x; 550 | } 551 | 552 | } 553 | 554 | } // namespace cxx14 555 | 556 | #endif // __cplusplus >= 201402L 557 | 558 | ]]) 559 | -------------------------------------------------------------------------------- /murrayc-suffix-tree.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | datarootdir=@datarootdir@ 5 | datadir=@datadir@ 6 | includedir=@includedir@ 7 | 8 | Name: murrayc-suffix-tree 9 | Description: A generic templated suffix tree implementation. 10 | Version: @PACKAGE_VERSION@ 11 | URL: https://github.com/murraycu/murrayc-suffix-tree 12 | Libs: -L${libdir} -lmurrayc-suffix-tree-@MURRAYC_SUFFIX_TREE_API_VERSION@ 13 | Cflags: -I${includedir}/murrayc-suffix-tree-@MURRAYC_SUFFIX_TREE_API_VERSION@ -I${libdir}/murrayc-suffix-tree-@MURRAYC_SUFFIX_TREE_API_VERSION@/include 14 | -------------------------------------------------------------------------------- /murrayc-suffix-tree/iter_range.h: -------------------------------------------------------------------------------- 1 | #ifndef MURRAYC_SUFFIX_TREE_ITER_RANGE_H 2 | #define MURRAYC_SUFFIX_TREE_ITER_RANGE_H 3 | 4 | #include 5 | 6 | template 7 | class IterRange { 8 | public: 9 | IterRange() = default; 10 | 11 | IterRange(const T_Iterator& start, const T_Iterator& end) 12 | : start_(start), end_(end) { 13 | } 14 | 15 | IterRange(const T_Iterator& start, const std::shared_ptr& end) 16 | : start_(start), global_end_(end) { 17 | } 18 | 19 | IterRange(const IterRange& src) = default; 20 | IterRange& operator=(const IterRange& src) = default; 21 | IterRange(IterRange&& src) = default; 22 | IterRange& operator=(IterRange&& src) = default; 23 | 24 | /* 25 | static char 26 | debug_global_end(const std::shared_ptr& end) { 27 | if (!end) { 28 | return '!'; 29 | } 30 | 31 | T_Iterator iter = *end; 32 | return (iter == T_Iterator() ? '?' : *iter); 33 | } 34 | */ 35 | 36 | bool operator==(const IterRange& src) const { 37 | return start_ == src.start_ && 38 | end_ == src.end_ && 39 | global_end_ == src.global_end_; 40 | } 41 | 42 | inline bool empty() const { 43 | return start_ >= end(); 44 | } 45 | 46 | inline T_Iterator end() const { 47 | if (global_end_) { 48 | return *(global_end_); 49 | } 50 | 51 | return end_; 52 | } 53 | 54 | inline std::size_t size() const { 55 | const auto e = end(); 56 | if (e <= start_) { 57 | return 0; 58 | } 59 | 60 | return e - start_; 61 | } 62 | 63 | inline IterRange substr(std::size_t start) const { 64 | const auto start_used = start_ + start; 65 | const auto key_end = end(); 66 | return IterRange( 67 | (start_used < key_end) ? start_used : key_end, 68 | key_end); 69 | } 70 | 71 | inline IterRange substr(std::size_t start, std::size_t len) const { 72 | const auto start_used = start_ + start; 73 | const auto end_used = start_ + len; 74 | const auto key_end = end(); 75 | return IterRange( 76 | (start_used < key_end) ? start_used : key_end, 77 | (end_used < key_end) ? end_used : key_end); 78 | } 79 | 80 | bool has_prefix(std::size_t str_start_pos, const IterRange& prefix, std::size_t prefix_start_pos = 0) const { 81 | const auto prefix_start = prefix.start_ + prefix_start_pos; 82 | const auto prefix_end = prefix.end(); 83 | const auto iters = std::mismatch(start_ + str_start_pos, end(), 84 | prefix_start, prefix_end); 85 | return iters.second == prefix_end; 86 | } 87 | 88 | std::size_t common_prefix(std::size_t str_start_pos, const IterRange& prefix, std::size_t prefix_start_pos = 0) const { 89 | const auto str_start = start_ + str_start_pos; 90 | const auto iters = std::mismatch(str_start, end(), 91 | prefix.start_ + prefix_start_pos, prefix.end()); 92 | return std::distance(str_start, iters.first); 93 | } 94 | /** Make sure that the range has its own fixed end value, 95 | * instead of sharing a variable end value. 96 | */ 97 | void set_end_from_global() { 98 | if (!global_end_) { 99 | end_ = T_Iterator(); 100 | return; 101 | } 102 | 103 | end_ = *global_end_; 104 | 105 | global_end_.reset(); 106 | } 107 | 108 | std::string to_string() const { 109 | if (global_end_) { 110 | return std::string(start_, *global_end_); 111 | } else { 112 | return std::string(start_, end_); 113 | } 114 | } 115 | 116 | T_Iterator start_; 117 | T_Iterator end_; 118 | std::shared_ptr global_end_; 119 | }; 120 | 121 | template 122 | inline bool operator<(const IterRange& a, const IterRange& b) { 123 | if (a.empty() && b.empty()) { 124 | return false; 125 | } 126 | 127 | // TODO: Performance: Avoid the temporary std::string instances: 128 | std::string stra(a.start_, a.end()); 129 | std::string strb(b.start_, b.end()); 130 | return stra < strb; 131 | } 132 | 133 | template 134 | inline bool operator<=(const IterRange& a, const IterRange& b) { 135 | if (a.empty() && b.empty()) { 136 | return true; 137 | } 138 | 139 | // TODO: Performance: Avoid the temporary std::string instances: 140 | std::string stra(a.start_, a.end()); 141 | std::string strb(b.start_, b.end()); 142 | return stra <= strb; 143 | } 144 | 145 | template 146 | inline bool operator>(const IterRange& a, const IterRange& b) { 147 | if (a.empty() && b.empty()) { 148 | return false; 149 | } 150 | 151 | // TODO: Performance: Avoid the temporary std::string instances: 152 | std::string stra(a.start_, a.end()); 153 | std::string strb(b.start_, b.end()); 154 | return stra > strb; 155 | } 156 | 157 | template 158 | inline bool operator>=(const IterRange& a, const IterRange& b) { 159 | if (a.empty() && b.empty()) { 160 | return true; 161 | } 162 | 163 | // TODO: Performance: Avoid the temporary std::string instances: 164 | std::string stra(a.start_, a.end()); 165 | std::string strb(b.start_, b.end()); 166 | return stra >= strb; 167 | } 168 | #endif // MURRAYC_SUFFIX_TREE_ITER_RANGE_H 169 | -------------------------------------------------------------------------------- /murrayc-suffix-tree/radix_tree.h: -------------------------------------------------------------------------------- 1 | #ifndef MURRAYC_SUFFIX_TREE_RADIX_TREE_H 2 | #define MURRAYC_SUFFIX_TREE_RADIX_TREE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | class RadixTree { 12 | public: 13 | bool exists(const T_Key& key) const { 14 | const auto node = find_node(key); 15 | return node != nullptr; 16 | }; 17 | 18 | /** Get the first value associates with the key. 19 | * 20 | * Returns T_Value() if the key was not found. 21 | */ 22 | T_Value get_value(const T_Key& key) const { 23 | const auto node = find_node(key); 24 | if (!node) { 25 | return T_Value(); 26 | } 27 | 28 | if (node->values_.empty()) { 29 | return T_Value(); 30 | } 31 | 32 | return node->values_[0]; 33 | } 34 | 35 | /** Get the first value associates with the key. 36 | * 37 | * Returns T_Value() if the key was not found. 38 | */ 39 | std::vector get_values(const T_Key& key) const { 40 | const auto node = find_node(key); 41 | if (!node) { 42 | return {}; 43 | } 44 | 45 | return node->values_; 46 | } 47 | 48 | using Matches = std::vector>>; 49 | Matches find_matches(const T_Key& prefix) const { 50 | //std::cout << "find_matches(): prefix=" << prefix << std::endl; 51 | if (prefix.empty()) { 52 | return {}; 53 | } 54 | 55 | const auto prefix_len = prefix.size(); 56 | 57 | std::stack> stack_result; 58 | 59 | //Stack of prefixes+nodes. 60 | using Item = std::tuple; 61 | std::stack stack_starts; 62 | stack_starts.emplace(0, T_Key(), &root); 63 | 64 | while (!stack_starts.empty()) { 65 | const auto item = stack_starts.top(); 66 | stack_starts.pop(); 67 | 68 | const auto prefix_pos = std::get<0>(item); 69 | const auto& key = std::get<1>(item); 70 | const auto node = std::get<2>(item); 71 | 72 | //If we have already used all of the prefix, 73 | //then use all leaf nodes, 74 | //because we are just looking for the (candidate) values below an identified intermediate candidate node. 75 | if (prefix_pos >= prefix_len) { 76 | stack_result.emplace(key, node); 77 | continue; 78 | } 79 | 80 | for (auto edge : node->children_) { 81 | const auto& edge_part = edge.part_; 82 | //std::cout << " edge: " << edge_part << std::endl; 83 | 84 | const auto child_key = key + edge_part; 85 | 86 | if (has_prefix(prefix, prefix_pos, edge_part, 0)) { 87 | // The part is a prefix of the remaining key, so follow it: 88 | stack_starts.emplace(prefix_pos + edge_part.size(), child_key, edge.dest_); 89 | } else if (has_prefix(edge_part, 0, prefix, prefix_pos)) { 90 | // The remaining key is a prefix of the part, so use it as part of matches: 91 | stack_starts.emplace(prefix_len, child_key, edge.dest_); 92 | } 93 | } 94 | } 95 | 96 | //Find all descendent leaves: 97 | Matches result; 98 | while(!stack_result.empty()) { 99 | const auto item = stack_result.top(); 100 | stack_result.pop(); 101 | 102 | const auto& key = item.first; 103 | const auto node = item.second; 104 | 105 | //Use it if it is a leaf node: 106 | if (node->has_value()) { 107 | result.emplace_back(key, node->values_); 108 | } 109 | 110 | for (auto edge : node->children_) { 111 | stack_result.emplace(key + edge.part_, edge.dest_); 112 | } 113 | } 114 | 115 | return result; 116 | } 117 | 118 | /** 119 | * Returns T_Value() if the key was not found. 120 | */ 121 | std::set find_match_values(const T_Key& prefix) const { 122 | std::set result; 123 | 124 | //std::cout << "find_matches(): prefix=" << prefix << std::endl; 125 | if (prefix.empty()) { 126 | return result; 127 | } 128 | 129 | const auto prefix_len = prefix.size(); 130 | 131 | // Find all the nodes whose descendants (including themselves) should be in the result, 132 | // putting these in a second stack. 133 | using Item = std::pair; 134 | std::stack stack; 135 | stack.emplace(0, &root); 136 | 137 | while (!stack.empty()) { 138 | const auto item = stack.top(); 139 | stack.pop(); 140 | 141 | const auto prefix_pos = item.first; 142 | const auto node = item.second; 143 | 144 | //If we have already used all of the prefix, 145 | //then use all subsequent leaf nodes. 146 | if (prefix_pos >= prefix_len) { 147 | if (node->has_value()) { 148 | result.insert(std::begin(node->values_), std::end(node->values_)); 149 | } 150 | } 151 | 152 | for (auto edge : node->children_) { 153 | const auto& edge_part = edge.part_; 154 | 155 | if (has_prefix(prefix, prefix_pos, edge_part, 0)) { 156 | // The part is a prefix of the remaining key, so follow it: 157 | stack.emplace(prefix_pos + edge_part.size(), edge.dest_); 158 | } else if (has_prefix(edge_part, 0, prefix, prefix_pos)) { 159 | // The remaining key is a prefix of the part, so it is a candidate: 160 | stack.emplace(prefix_len, edge.dest_); 161 | } 162 | } 163 | } 164 | 165 | return result; 166 | } 167 | 168 | void insert(const T_Key& key, const T_Value& value) { 169 | // std::cout << "insert(): key=" << key << std::endl; 170 | if (key.empty()) { 171 | return; 172 | } 173 | 174 | auto node = &root; 175 | std::size_t key_pos = 0; 176 | const auto key_size = key.size(); 177 | while (key_pos < key_size) { 178 | //std::cout << "insert(): remaining key=" << key.substr(key_pos) << std::endl; 179 | //Choose the child node, if any: 180 | Node* next = nullptr; 181 | for (auto& edge : node->children_) { 182 | const auto& part = edge.part_; 183 | 184 | const auto prefix_len = common_prefix(part, 0, key, key_pos); 185 | const auto part_len = part.size(); 186 | //std::cout << "key=" << key << ", key_pos=" << key_pos << ", part=" << part << "\n"; 187 | //If the edge's part is a prefix of the remaining key: 188 | if (prefix_len == 0) { 189 | // No match. 190 | continue; 191 | } else if (prefix_len < part_len) { 192 | // If the key is a prefix of the edge's part: 193 | 194 | // Split it, 195 | // adding a new intermediate node in it original node's place, with the original node as a child. 196 | const auto prefix = part.substr(0, prefix_len); 197 | //std::cout << " splitting part=" << part << ", at key prefix: " << key.substr(0, key_pos + 1) << ", with prefix=" << prefix << ", values size: " << dest->values_.size() << std::endl; 198 | const auto suffix_part = part.substr(prefix_len); 199 | //assert(part == (prefix + suffix_part)); 200 | //std::cout << " suffix_part=" << suffix_part << std::endl; 201 | 202 | const auto dest = edge.dest_; 203 | 204 | auto extra_node = new Node; 205 | extra_node->children_.emplace_back(suffix_part, dest); 206 | 207 | edge.part_ = prefix; 208 | edge.dest_ = extra_node; 209 | 210 | // Try the same node again. 211 | // This time it might be a perfect match. 212 | next = node; 213 | break; 214 | } else { 215 | next = edge.dest_; 216 | key_pos += part_len; 217 | break; 218 | } 219 | } 220 | 221 | // Stop when we cannot go further. 222 | if (!next) { 223 | break; 224 | } 225 | 226 | node = next; 227 | } 228 | 229 | if (key_pos > key_size) { 230 | std::cerr << "Unexpected key_pos.\n"; 231 | return; 232 | } 233 | 234 | if (key_pos == key_size) { 235 | //The node already exists, so just add the extra value: 236 | node->values_.emplace_back(value); 237 | return; 238 | } 239 | 240 | // Add a node for the remaining characters: 241 | const auto suffix = key.substr(key_pos); 242 | //std::cout << "Adding suffix: " << suffix << ", with value: " << value << '\n'; 243 | 244 | const auto next = new Node; 245 | node->children_.emplace_back(suffix, next); 246 | 247 | next->values_.emplace_back(value); 248 | //std::cout << "next: " << next << std::endl; 249 | } 250 | 251 | void remove(const T_Key& key) { 252 | auto node = find_node(key); 253 | if (!node) { 254 | return; 255 | } 256 | 257 | //TODO: Each node needs a pointer to its parent. 258 | //We then need to ask its parent to delete its edge to this node, 259 | //and then combine itself with its own parent if it now has only one edge. 260 | } 261 | 262 | private: 263 | class Node { 264 | public: 265 | class Edge { 266 | public: 267 | Edge(const T_Key& part, Node* dest) 268 | : part_(part), 269 | dest_(dest) { 270 | } 271 | 272 | Edge(const Edge& src) = default; 273 | Edge& operator=(const Edge& src) = default; 274 | Edge(Edge&& src) = default; 275 | Edge& operator=(Edge&& src) = default; 276 | 277 | T_Key part_ = T_Key(); 278 | Node* dest_ = nullptr; 279 | }; 280 | 281 | inline bool has_value() const { 282 | return !values_.empty(); 283 | } 284 | 285 | //We could instead have a std::vector children_, 286 | //of size alphabet (such as 26), 287 | //to allow O(1) lookup, at the cost of wasted space. 288 | std::vector children_; 289 | 290 | // TODO: Wastes space on non-leaves. 291 | std::vector values_; 292 | }; 293 | 294 | static 295 | bool has_prefix(const std::string& str, std::size_t str_start_pos, const std::string& prefix, std::size_t prefix_start_pos = 0) { 296 | const auto prefix_start = std::begin(prefix) + prefix_start_pos; 297 | const auto prefix_end = std::end(prefix); 298 | const auto iters = std::mismatch(std:: begin(str) + str_start_pos, std::end(str), 299 | prefix_start, prefix_end); 300 | return iters.second == prefix_end; 301 | 302 | /* 303 | const std::size_t prefix_start_pos = 0; 304 | const auto str_len = str.size() - str_start_pos; 305 | const auto prefix_len = prefix.size() - prefix_start_pos; 306 | 307 | // prefix cannot be a prefix of str if it is longer than str: 308 | if (prefix_len > str_len) { 309 | return false; 310 | } 311 | 312 | const auto len = std::min(str_len, prefix_len); 313 | const auto end = str_start_pos + len; 314 | 315 | auto prefix_iter = std::begin(prefix) + prefix_start_pos; 316 | std::size_t i = str_start_pos; 317 | for (; i < end; ++i) { 318 | if (str[i] != *prefix_iter) { 319 | // Some part of the prefix doesn't match: 320 | return false; 321 | } 322 | 323 | ++prefix_iter; 324 | } 325 | 326 | //All characters in prefix match at the start of str: 327 | return true; 328 | */ 329 | } 330 | 331 | static 332 | std::size_t common_prefix(const std::string& str, std::size_t str_start_pos, const std::string& prefix, std::size_t prefix_start_pos) { 333 | //TODO: Use std::mismatch(). 334 | const auto str_start = std::begin(str) + str_start_pos; 335 | const auto iters = std::mismatch(str_start, std::end(str), 336 | std::begin(prefix) + prefix_start_pos, std::end(prefix)); 337 | return std::distance(str_start, iters.first); 338 | 339 | /* 340 | const auto str_len = str.size() - str_start_pos; 341 | const auto prefix_len = prefix.size() - prefix_start_pos; 342 | 343 | const auto len = std::min(str_len, prefix_len); 344 | const auto str_end = str_start_pos + len; 345 | 346 | std::size_t i_str = str_start_pos; 347 | std::size_t i_prefix = prefix_start_pos; 348 | while(i_str < str_end) { 349 | if(str[i_str] != prefix[i_prefix]) { 350 | break; 351 | } 352 | 353 | ++i_str; 354 | ++i_prefix; 355 | } 356 | 357 | return i_str - str_start_pos; 358 | */ 359 | } 360 | 361 | const Node* find_node(const T_Key& key) const { 362 | //std::cout << "find_node(): key=" << key << std::endl; 363 | if (key.empty()) { 364 | return nullptr; 365 | } 366 | 367 | auto node = &root; 368 | std::size_t key_pos = 0; 369 | const auto key_size = key.size(); 370 | while (key_pos < key_size) { 371 | //std::cout << "find_node(): remaining key=" << key.substr(key_pos) << std::endl; 372 | //std::cout << " children_ size: " << node->children_.size() << std::endl; 373 | //Choose the child node, if any: 374 | Node* next = nullptr; 375 | for (const auto& edge : node->children_) { 376 | const auto& part = edge.part_; 377 | const auto part_size = part.size(); 378 | //std::cout << " key=" << key << ", key_pos=" << key_pos << ", part=" << part << "\n"; 379 | if(!has_prefix(key, key_pos, part)) { 380 | continue; 381 | } 382 | 383 | next = edge.dest_; 384 | key_pos += part_size; 385 | //std::cout << " next: " << next << std::endl; 386 | break; 387 | } 388 | 389 | if (!next) { 390 | return nullptr; 391 | } 392 | 393 | node = next; 394 | } 395 | 396 | if (key_pos < key_size) { 397 | //We didn't find all the parts of the prefix: 398 | return nullptr; 399 | } 400 | 401 | //std::cout << "node: " << node << std::endl; 402 | return node->has_value() ? node : nullptr; 403 | } 404 | 405 | Node root; 406 | }; 407 | 408 | #endif // MURRAYC_SUFFIX_TREE_RADIX_TREE_H 409 | -------------------------------------------------------------------------------- /murrayc-suffix-tree/suffix_array.h: -------------------------------------------------------------------------------- 1 | #ifndef MURRAYC_SUFFIX_TREE_SUFFIX_ARRAY_H 2 | #define MURRAYC_SUFFIX_TREE_SUFFIX_ARRAY_H 3 | 4 | #include "iter_range.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /** 14 | * @tparam T_Key For instance, std::string, or something other container. 15 | * @tparam T_Value The value to associate with each inserted key. 16 | */ 17 | template 18 | class SuffixArray { 19 | public: 20 | SuffixArray() { 21 | } 22 | 23 | SuffixArray(const T_Key& key, const T_Value& value) { 24 | const auto start = std::cbegin(key); 25 | const auto end = start + key.size(); 26 | const Range substr(start, end); 27 | if (str_empty(substr)) { 28 | return; 29 | } 30 | 31 | // TODO: Implement DC3 algorithm (linear), 32 | // instead of this naive O(n log(n)) algorithm: 33 | const auto n = substr.size(); 34 | suffixes_.reserve(n); 35 | for (auto i = start; i != end; ++i) { 36 | Range suffix(i, end); 37 | suffixes_.emplace_back(suffix, value); 38 | } 39 | 40 | std::sort(std::begin(suffixes_), std::end(suffixes_), 41 | [](const auto& a, const auto& b) { 42 | return a.first < b.first; 43 | }); 44 | 45 | // Build LCP array: 46 | lcp_array_.reserve(n - 1); 47 | Range prev = suffixes_[0].first; 48 | for (auto i = std::cbegin(suffixes_) + 1; i < std::cend(suffixes_); ++i) { 49 | const auto& suffix = i->first; 50 | const auto lcp = prev.common_prefix(0, suffix); 51 | lcp_array_.emplace_back(lcp); 52 | prev = suffix; 53 | } 54 | } 55 | 56 | /// Start and end (1 past last position) of a substring in text_; 57 | using KeyIterator = typename T_Key::const_iterator; 58 | 59 | using Range = IterRange; 60 | 61 | /** 62 | * The suffix's begin/end, and the associated value. 63 | */ 64 | using suffix_array_type = std::vector>; 65 | 66 | using lcp_array_type = std::vector; 67 | 68 | SuffixArray(const suffix_array_type& suffixes, const lcp_array_type& lcp_array) 69 | : suffixes_(suffixes), lcp_array_(lcp_array) { 70 | } 71 | 72 | std::pair 73 | get_suffix_array_and_lcp_array() const { 74 | return std::make_pair(suffixes_, lcp_array_); 75 | } 76 | 77 | using Matches = std::set; 78 | 79 | /** Finds the values for any key containing this substring. 80 | */ 81 | Matches find(const T_Key& substr) const { 82 | Matches result; 83 | 84 | if (substr.empty()) { 85 | return result; 86 | } 87 | 88 | 89 | const auto start = std::cbegin(substr); 90 | const auto end = start + substr.size(); 91 | const Range substr_key(start, end); 92 | 93 | const auto matches_with_positions = find_with_positions(substr_key); 94 | 95 | // Convert one container into another: 96 | for (const auto& kv : matches_with_positions) { 97 | result.emplace(kv.second); 98 | } 99 | 100 | return result; 101 | } 102 | 103 | /** 104 | * Like Matches, but provides the range of the prefix too, 105 | * so the caller can know where in the original insert()ed string, 106 | * the substring was found. That would refer to the originally-inserted 107 | * string, but we already require the caller to keep that alive. 108 | */ 109 | using MatchesWithPositions = std::vector>; 110 | 111 | /** Finds the values for any key containing this substring. 112 | */ 113 | MatchesWithPositions find_with_positions(const T_Key& substr) const { 114 | // TODO: This uses binary search (via std::lower_bound() and std::upper_bound()), 115 | // to find the matching suffixes, but that is O(log(n)). A SuffixTree could 116 | // do this in O(m), where m is the length of the pattern being searched. 117 | // We can apparently get that too by using the LCP array: 118 | // https://en.wikipedia.org/wiki/Suffix_array#Applications 119 | MatchesWithPositions result; 120 | 121 | if (substr.empty()) { 122 | return result; 123 | } 124 | 125 | 126 | const auto start = std::cbegin(substr); 127 | const auto end = start + substr.size(); 128 | const Range substr_key(start, end); 129 | return find_with_positions(substr_key); 130 | } 131 | 132 | /** Finds the values for any key containing this substring. 133 | */ 134 | MatchesWithPositions find_with_positions(const Range& substr) const { 135 | MatchesWithPositions result; 136 | 137 | if (str_empty(substr)) { 138 | return result; 139 | } 140 | 141 | const auto sz = substr.size(); 142 | 143 | // Find the first matching suffix: 144 | const auto range_start = std::lower_bound(std::cbegin(suffixes_), std::cend(suffixes_), substr, 145 | [sz](const auto& a, const auto& b) { 146 | const auto& a_start = a.first.substr(0, sz); 147 | return a_start < b; 148 | }); 149 | if (range_start == std::cend(suffixes_)) { 150 | return result; 151 | } 152 | 153 | // Find 1 past the last matching suffix: 154 | const auto range_end = std::upper_bound(range_start, std::cend(suffixes_), substr, 155 | [sz](const auto& a, const auto& b) { 156 | const auto& b_start = b.first.substr(0, sz); 157 | return a < b_start; 158 | }); 159 | 160 | for (auto i = range_start; i < range_end; ++i) { 161 | result.emplace_back(*i); 162 | } 163 | 164 | return result; 165 | } 166 | 167 | private: 168 | static 169 | inline std::size_t str_size(const Range& key) { 170 | return key.size(); 171 | } 172 | 173 | static 174 | inline bool str_empty(const Range& key) { 175 | return key.empty(); 176 | } 177 | 178 | static 179 | bool has_prefix(const Range& str, std::size_t str_start_pos, const Range& prefix, std::size_t prefix_start_pos = 0) { 180 | const auto prefix_start = prefix.start_ + prefix_start_pos; 181 | const auto prefix_end = str_end(prefix); 182 | const auto iters = std::mismatch(str.start_ + str_start_pos, str_end(str), 183 | prefix_start, prefix_end); 184 | return iters.second == prefix_end; 185 | } 186 | 187 | suffix_array_type suffixes_; 188 | lcp_array_type lcp_array_; 189 | }; 190 | 191 | #endif // MURRAYC_SUFFIX_TREE_SUFFIX_ARRAY_H 192 | -------------------------------------------------------------------------------- /murrayc-suffix-tree/suffix_tree.h: -------------------------------------------------------------------------------- 1 | #ifndef MURRAYC_SUFFIX_TREE_SUFFIX_TREE_H 2 | #define MURRAYC_SUFFIX_TREE_SUFFIX_TREE_H 3 | 4 | #include "iter_range.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | /** 15 | * @tparam T_Key For instance, std::string, or something other container. 16 | * @tparam T_Value The value to associate with each inserted key. 17 | */ 18 | template 19 | class SuffixTree { 20 | public: 21 | SuffixTree() { 22 | } 23 | 24 | /// Start and end (1 past last position) of a substring in text_; 25 | using KeyIterator = typename T_Key::const_iterator; 26 | 27 | using Range = IterRange; 28 | 29 | SuffixTree(const T_Key& key, const T_Value& value) { 30 | const auto start = std::cbegin(key); 31 | const auto end = start + key.size(); 32 | const Range substr(start, end); 33 | if (str_empty(substr)) { 34 | return; 35 | } 36 | 37 | insert_ukkonen(substr, value); 38 | } 39 | 40 | /** 41 | * The suffix's begin/end, and the associated value. 42 | */ 43 | using suffix_array_type = std::vector>; 44 | 45 | using lcp_array_type = std::vector; 46 | 47 | /** 48 | * Construct a suffix tree based on a suffix array and LCP array. 49 | */ 50 | SuffixTree(const suffix_array_type& suffixes, const lcp_array_type& lcp_array) { 51 | if (suffixes.empty()) { 52 | return; 53 | } 54 | 55 | insert_sa_and_lcp_array(suffixes, lcp_array); 56 | } 57 | 58 | void insert(const T_Key& key, const T_Value& value) { 59 | const auto start = std::cbegin(key); 60 | const auto end = start + key.size(); 61 | const Range substr(start, end); 62 | if (str_empty(substr)) { 63 | return; 64 | } 65 | 66 | insert(substr, value); 67 | } 68 | 69 | void insert(const typename T_Key::const_iterator& start, const typename T_Key::const_iterator& end, const T_Value& value) { 70 | const Range substr(start, end); 71 | if (str_empty(substr)) { 72 | return; 73 | } 74 | 75 | insert(substr, value); 76 | } 77 | 78 | using Matches = std::set; 79 | 80 | /** Finds the values for any key containing this substring. 81 | */ 82 | Matches find(const T_Key& substr) const { 83 | Matches result; 84 | 85 | if (substr.empty()) { 86 | return result; 87 | } 88 | 89 | 90 | const auto start = std::cbegin(substr); 91 | const auto end = start + substr.size(); 92 | const Range substr_key(start, end); 93 | 94 | const auto matches_with_positions = find_with_positions(substr_key); 95 | 96 | // Convert one container into another: 97 | for (const auto& kv : matches_with_positions) { 98 | result.emplace(kv.second); 99 | } 100 | 101 | return result; 102 | } 103 | 104 | /** 105 | * Like Matches, but provides the range of the prefix too, 106 | * so the caller can know where in the original insert()ed string, 107 | * the substring was found. That would refer to the originally-inserted 108 | * string, but we already require the caller to keep that alive. 109 | */ 110 | using MatchesWithPositions = std::vector>; 111 | 112 | /** Finds the values for any key containing this substring. 113 | */ 114 | MatchesWithPositions find_with_positions(const T_Key& substr) const { 115 | MatchesWithPositions result; 116 | 117 | if (substr.empty()) { 118 | return result; 119 | } 120 | 121 | 122 | const auto start = std::cbegin(substr); 123 | const auto end = start + substr.size(); 124 | const Range substr_key(start, end); 125 | return find_with_positions(substr_key); 126 | } 127 | 128 | void debug_print() const { 129 | std::cout << "Tree:" << std::endl; 130 | debug_print(&root_, 0); 131 | std::cout << std::endl << std::endl; 132 | } 133 | 134 | /** Get the suffix array and the LCP array. 135 | * The LCP array is the length of the common prefix of an item compared to the previous 136 | * item in the suffix array. Therefore there is no LCP value for the first suffix in the 137 | * suffix array. 138 | */ 139 | std::pair 140 | get_suffix_array_and_lcp_array() const { 141 | // Build a suffix array by doing a lexographically-ordered DFS: 142 | suffix_array_type suffixes; 143 | lcp_array_type lcp; 144 | 145 | // The node and its ancestor: 146 | std::stack> s; 147 | s.emplace(&root_, path_type()); 148 | path_type previous_path; 149 | while (!s.empty()) { 150 | const auto node_and_ancestor = s.top(); 151 | s.pop(); 152 | const auto& node = node_and_ancestor.first; 153 | const auto path = node_and_ancestor.second; 154 | const std::size_t depth = path.empty() ? 0 : path.back().second; 155 | 156 | if (node->has_value()) { 157 | for (auto& kv : node->keys_and_values_) { 158 | suffixes.emplace_back(kv.first, kv.second); 159 | if (!previous_path.empty()) { 160 | // TODO: Just calling std::mismatch() on the strings would be much simpler, 161 | // and would be just as linear (O(h), where h is the height of the tree). 162 | // Is it worth keeping these paths in the stack, just to 163 | // avoid a few extra character compares? 164 | const auto common_depth = depth_of_lca(previous_path, path); 165 | //std::cout << "lcp: " << common_depth << std::endl; 166 | lcp.emplace_back(common_depth); 167 | } 168 | previous_path = path; 169 | } 170 | } 171 | 172 | // Reverse sort this child edges, 173 | // so we can put the lexographically-first on the stack last, 174 | // so we deal with it first: 175 | auto children = node->children_; 176 | std::sort(std::begin(children), std::end(children), 177 | [](auto a, auto b) { 178 | // The first characters of the edges will always be different: 179 | const auto& achar = *(a.part_.start_); 180 | const auto& bchar = *(b.part_.start_); 181 | return bchar < achar; 182 | }); 183 | 184 | for (auto edge : children) { 185 | const auto& edge_part = edge.part_; 186 | const auto& d = edge.dest_; 187 | 188 | auto new_path = path; 189 | const auto substr_len = str_size(edge_part); 190 | new_path.emplace_back(d, depth + substr_len); 191 | s.emplace(d, new_path); 192 | } 193 | } 194 | 195 | return std::make_pair(suffixes, lcp); 196 | } 197 | 198 | inline static KeyIterator str_end(const Range& key) { 199 | return key.end(); 200 | } 201 | 202 | private: 203 | 204 | class Node { 205 | public: 206 | Node() = default; 207 | 208 | ~Node() { 209 | for(auto& edge : children_) { 210 | delete edge.dest_; 211 | } 212 | } 213 | 214 | class Edge { 215 | public: 216 | Edge(const Range& part, Node* dest) 217 | : part_(part), 218 | dest_(dest) { 219 | assert(str_size(part)); 220 | } 221 | 222 | void append_node_to_dest(const Range& part, const Range& key, const T_Value& value) { 223 | dest_->append_node(part, key, value); 224 | } 225 | 226 | inline bool dest_has_value() const { 227 | if (!dest_) { 228 | return false; 229 | } 230 | 231 | return dest_->has_value(); 232 | } 233 | 234 | /** This inserts an intermediate node by splitting the edge's part at 235 | * position @pos. 236 | * @result The new intermediate node. 237 | */ 238 | Node* split(std::size_t part_pos) { 239 | const auto prefix_part = str_substr(part_, 0, part_pos); 240 | assert(str_size(prefix_part) > 0); 241 | const auto suffix_part = str_substr(part_, part_pos); 242 | assert(str_size(suffix_part) > 0); 243 | const auto dest = dest_; 244 | 245 | auto extra_node = new Node; 246 | extra_node->append_node(suffix_part, dest); 247 | 248 | part_ = prefix_part; 249 | dest_ = extra_node; 250 | 251 | return extra_node; 252 | } 253 | 254 | Range part_; 255 | Node* dest_ = nullptr; 256 | }; 257 | 258 | Node* append_node(const Range& part, const Range& key, const T_Value& value) { 259 | const auto extra_node = new Node(); 260 | extra_node->keys_and_values_.emplace_back(key, value); 261 | children_.emplace_back(part, extra_node); 262 | return extra_node; 263 | } 264 | 265 | void append_node(const Range& part, Node* node) { 266 | children_.emplace_back(part, node); 267 | } 268 | 269 | inline bool has_value() const { 270 | return !keys_and_values_.empty(); 271 | } 272 | 273 | inline void add_value(const Range& key, const T_Value& value) { 274 | keys_and_values_.emplace_back(std::make_pair(key, value)); 275 | } 276 | 277 | Edge* find_edge_starting_with(const KeyIterator& iter) { 278 | const auto& ch = *iter; 279 | for (auto& edge : children_) { 280 | const auto& start_ch = *(edge.part_.start_); 281 | if (ch == start_ch) { 282 | return &edge; 283 | } 284 | } 285 | 286 | return nullptr; 287 | } 288 | 289 | // We could instead have a std::vector children_, 290 | // of size alphabet (such as 26), 291 | // to allow O(1) lookup, at the cost of wasted space.o 292 | // A hash table might be the simplest way, giving amortized O(1) lookup. 293 | // But see the mention of suffix trays here: 294 | // http://www.murrayc.com/permalink/2016/08/19/suffix-tree-ukkonen-c/ 295 | std::vector children_; 296 | 297 | // TODO: Wastes space on non-leaves. 298 | // TODO: Use a set, though that would not allow duplicates. 299 | // first: The start and end of the actual suffix that this leaf node represents: 300 | // second: The value associated with the string for which this leaf node represents a suffix: 301 | std::vector> keys_and_values_; 302 | }; 303 | 304 | void insert(const Range& key, const T_Value& value) { 305 | //std::cout << "debug: insert(): key.start_=" << static_cast(key.start_) << ", second=" << static_cast(key.end_) << std::endl; 306 | //Insert every suffix of the key: 307 | Range suffix = key; 308 | while(!str_empty(suffix)) { 309 | //std::cout << "insert(): suffix=" << suffix << ", value=" << value <(key_start); 404 | KeyIterator& end = *end_ptr; //end is 1 past the end, so this is equivalent to -1 in the traditional Ukkonnen implementation. 405 | 406 | std::unordered_map suffix_links; 407 | 408 | // The "phases" 409 | for (auto i = key_start; i != key_end; ++i) { 410 | //std::cout << " character: " << *i << std::endl; 411 | const bool is_last_char = (i == key_last); 412 | 413 | ++remaining; 414 | ++end; //This extends all existing paths by one character. 415 | 416 | Node* prev_created_internal_node = nullptr; 417 | 418 | // The "extensions". 419 | while(remaining) { 420 | 421 | //std::cout << " remaining: " << remaining << std::endl; 422 | //std::cout << " end: " << std::distance(key_start, end) << std::endl; 423 | //std::cout << " active.node: " << active.node << std::endl; 424 | if (active.edge_valid) { 425 | //std::cout << " active.edge: " << std::distance(key_start, active.edge) << std::endl; 426 | //std::cout << " active.length: " << active.length << std::endl; 427 | } 428 | 429 | // An active.length of 0 means we ignore the active.edge. 430 | const auto edge_match = (active.edge_valid && active.length) ? 431 | find_partial_edge(active, i) : 432 | find_partial_edge(active.node, i); 433 | auto edge = edge_match.edge_; 434 | const auto part_len_used = edge_match.edge_part_used_; 435 | const bool prefix_used = edge_match.substr_used_ > 0; 436 | 437 | if (!prefix_used || is_last_char) { 438 | Range prefix(i, end_ptr); 439 | Range actual_prefix(i - remaining + 1, end_ptr); 440 | 441 | // Rule 2 extension: There is no match: 442 | if (part_len_used == 0) { 443 | // There is no match: 444 | //std::cout << " Rule 2: Adding edge to active node: " << debug_key(prefix) << std::endl; 445 | active.node->append_node(prefix, actual_prefix, value); 446 | } else { 447 | // There is a partial match, in the middle of an edge: 448 | //std::cout << " Rule 2: Splitting edge " << debug_key(edge->part_) 449 | // << " at " << part_len_used - 1 450 | // << " and adding: " << debug_key(prefix) 451 | // << " for prefix: " << debug_key(actual_prefix) << "(" << std::distance(key_start, actual_prefix.start_) << ")" << std::endl; 452 | auto extra_node = edge->split(part_len_used); 453 | if (is_last_char) { 454 | // Just let the intermediate node have a value, 455 | // instead of having extra leaf nodes just for values. 456 | extra_node->add_value(actual_prefix, value); 457 | } else { 458 | extra_node->append_node(prefix, actual_prefix, value); 459 | } 460 | 461 | // Every internal node should have a suffix link: 462 | //std::cout << " Setting suffix link from " << extra_node << " to root " << &root_ << std::endl; 463 | suffix_links[extra_node] = &root_; 464 | 465 | // A previously-created internal node should now have its suffix link 466 | // updated to this new internal node. 467 | if (prev_created_internal_node) { 468 | //std::cout << " Updating suffix link from " << prev_created_internal_node << " to " << extra_node << std::endl; 469 | suffix_links[prev_created_internal_node] = extra_node; 470 | } 471 | prev_created_internal_node = extra_node; 472 | 473 | // Follow previous suffix link if the active node is not root: 474 | if (active.node != &root_) { 475 | const auto iter = suffix_links.find(active.node); 476 | assert(iter != suffix_links.end()); 477 | //std::cout << " Following suffix link of active node " << active.node << " to " << iter->second << std::endl; 478 | active.node = iter->second; 479 | 480 | // Not changing active.edge or active.length. 481 | // Note: If there are multiple constructions, then active.length 482 | // might now be past the end of the actual edge's part. 483 | } else { 484 | // After creating an internal node, 485 | // decrement active.length and increment active.edge, 486 | // so we look for the same character in an edge that is the same as 487 | // the previously active edge but without the leading character. 488 | --active.length; 489 | ++active.edge; 490 | } 491 | } 492 | 493 | // There is no change to active.node, active.edge, or active.length 494 | // after a Rule 2 extension. 495 | --remaining; 496 | continue; 497 | } 498 | 499 | assert(edge); 500 | 501 | // Rule 3 extension: 502 | //std::cout << " Rule 3: Do nothing." << std::endl; 503 | 504 | active.node = edge_match.parent_node_; 505 | active.edge = edge->part_.start_; //Start of range of the existing edge. 506 | active.edge_valid = true; 507 | active.length = part_len_used; 508 | 509 | // After a rule 3 extension, immediately start the next "phase". 510 | break; 511 | } 512 | } 513 | 514 | // Update all the ranges to use real end values 515 | // instead of one shared end value. 516 | set_global_ends(); 517 | } 518 | 519 | /** Update all ranges (in edge parts and in nodes), 520 | * to stop using the shared_ptr<>. 521 | * This makes subsequent changes and comparisons simpler. 522 | */ 523 | void set_global_ends() { 524 | std::stack s; 525 | s.emplace(&root_); 526 | while (!s.empty()) { 527 | auto node = s.top(); 528 | s.pop(); 529 | 530 | for (auto& p : node->keys_and_values_) { 531 | p.first.set_end_from_global(); 532 | } 533 | 534 | for (auto& e : node->children_) { 535 | e.part_.set_end_from_global(); 536 | s.emplace(e.dest_); 537 | } 538 | } 539 | } 540 | 541 | void insert_sa_and_lcp_array(const suffix_array_type& suffixes, const lcp_array_type& lcp_array) { 542 | assert(suffixes.size() == lcp_array.size() + 1); 543 | 544 | // This code won't work unless the tree is empty: 545 | assert(root_.children_.empty()); 546 | 547 | // Add the first suffix, then subsequent suffixes, 548 | // keeping the path to the currently-added leaf node, 549 | // so we can travel back up it when creating the next leaf node, 550 | // to split at the specified lcp. 551 | 552 | // The parent nodes and their depths: 553 | std::stack> path; 554 | 555 | // Add the first suffix: 556 | auto i = std::cbegin(suffixes); 557 | { 558 | const auto& suffix = i->first; 559 | const auto& value = i->second; 560 | root_.append_node(suffix, suffix /* key */, value); 561 | path.emplace(std::make_pair(&root_, 0)); 562 | } 563 | 564 | auto l = std::cbegin(lcp_array); 565 | for (++i; i != std::cend(suffixes); ++i, ++l) { 566 | const auto& suffix = i->first; 567 | 568 | const auto& value = i->second; 569 | const auto lcp = *l; 570 | //std::cout << debug_key(suffix) << ": lcp=" << lcp << std::endl; 571 | 572 | // Find the parent node at, or higher than, the lcp: 573 | Node* parent = nullptr; 574 | std::size_t depth = 0; 575 | while (!path.empty()) { 576 | const auto p = path.top(); 577 | parent = p.first; 578 | depth = p.second; 579 | if (depth <= lcp) { 580 | // Use it, without popping it: 581 | break; 582 | } 583 | 584 | path.pop(); 585 | } 586 | 587 | // Split if necessary: 588 | const auto suffix_part = str_substr(suffix, lcp); 589 | Node* node = nullptr; 590 | if (depth == lcp) { 591 | // Just add the end of the suffix to the parent node: 592 | node = parent->append_node(suffix_part, suffix /* key */, value); 593 | } else { 594 | // Split the parent node's edge at the appropriate place, 595 | // and add the new node from the split: 596 | const auto iter = suffix.start_ + depth; 597 | auto edge = parent->find_edge_starting_with(iter); 598 | /* 599 | if (!edge) { 600 | std::cerr << "Cannot find edge beginning with " << *iter 601 | << " from node with depth: " << depth << std::endl; 602 | } 603 | */ 604 | assert(edge); 605 | 606 | // Split it: 607 | const auto split_node = edge->split(lcp - depth); 608 | path.emplace(std::make_pair(split_node, lcp)); 609 | node = split_node->append_node(suffix_part, suffix /* key */, value); 610 | } 611 | 612 | path.emplace(std::make_pair(node, str_size(suffix))); 613 | } 614 | } 615 | 616 | static 617 | bool has_prefix(const Range& str, std::size_t str_start_pos, const Range& prefix, std::size_t prefix_start_pos = 0) { 618 | return str.has_prefix(str_start_pos, prefix, prefix_start_pos); 619 | } 620 | 621 | static 622 | std::size_t common_prefix(const Range& str, std::size_t str_start_pos, const Range& prefix, std::size_t prefix_start_pos) { 623 | return str.common_prefix(str_start_pos, prefix, prefix_start_pos); 624 | } 625 | 626 | void insert_single(const Range& key, const T_Value& value) { 627 | //std::cout << "insert(): key=" << debug_key(key) << std::endl; 628 | if (str_empty(key)) { 629 | return; 630 | } 631 | 632 | auto node = &root_; 633 | std::size_t key_pos = 0; 634 | const auto key_size = str_size(key); 635 | //std::cout << "debug: insert_single(): key_size=" << key_size << std::endl; 636 | while (key_pos < key_size) { 637 | //std::cout << "debug: insert(): remaining key=" << key_pos << std::endl; 638 | //std::cout << " debug: node=" << node << std::endl; 639 | //Choose the child node, if any: 640 | Node* next = nullptr; 641 | for (auto& edge : node->children_) { 642 | const auto& part = edge.part_; 643 | 644 | const auto prefix_len = common_prefix(part, 0, key, key_pos); 645 | const auto part_len = str_size(part); 646 | //std::cout << "key=" << debug_key(key) << ", key_pos=" << key_pos << ", part=" << debug_key(part) << 647 | // ", prefix_len=" << prefix_len << ", part_len=" << part_len << "\n"; 648 | //If the edge's part is a prefix of the remaining key: 649 | if (prefix_len == 0) { 650 | // No match. 651 | continue; 652 | } else if (prefix_len < part_len) { 653 | // If the key is a prefix of the edge's part: 654 | 655 | // TODO: 656 | // If this edge leads to the same value (It can lead to many values), 657 | // then do nothing, because find() would already use this edge to find this value. 658 | // I think this is an "implicit" value. murrayc. 659 | 660 | // Split it, 661 | // adding a new intermediate node in it original node's place, with the original node as a child. 662 | edge.split(prefix_len); 663 | 664 | // Try the same node again. 665 | // This time it might be a perfect match. 666 | next = node; 667 | break; 668 | } else { 669 | next = edge.dest_; 670 | key_pos += part_len; 671 | break; 672 | } 673 | } 674 | 675 | // Stop when we cannot go further. 676 | if (!next) { 677 | break; 678 | } 679 | 680 | node = next; 681 | } 682 | 683 | if (key_pos > key_size) { 684 | std::cerr << "Unexpected key_pos.\n"; 685 | return; 686 | } 687 | 688 | if (key_pos == key_size) { 689 | //The node already exists, so just add the extra value: 690 | node->keys_and_values_.emplace_back(key, value); 691 | return; 692 | } 693 | 694 | // Add a node for the remaining characters: 695 | const auto suffix = str_substr(key, key_pos); 696 | //std::cout << "Adding suffix: " << suffix << ", with value: " << value << '\n'; 697 | 698 | node->append_node(suffix, key, value); 699 | } 700 | 701 | /** 702 | * The Edge and the end of matching prefix of the edge's part. 703 | */ 704 | class EdgeMatch { 705 | public: 706 | EdgeMatch() { 707 | } 708 | 709 | EdgeMatch(typename Node::Edge* edge, std::size_t edge_part_used, std::size_t substr_used, Node* parent_node) 710 | : edge_(edge), edge_part_used_(edge_part_used), substr_used_(substr_used), parent_node_(parent_node) { 711 | } 712 | 713 | EdgeMatch(const typename Node::Edge* edge, std::size_t edge_part_used, std::size_t substr_used, const Node* parent_node) 714 | : edge_(const_cast(edge)), edge_part_used_(edge_part_used), substr_used_(substr_used), parent_node_(const_cast(parent_node)) { 715 | } 716 | 717 | typename Node::Edge* edge_ = nullptr; 718 | std::size_t edge_part_used_ = 0; 719 | std::size_t substr_used_ = 0; 720 | Node* parent_node_ = nullptr; 721 | }; 722 | 723 | static 724 | typename Node::Edge* find_edge(Node* node, const KeyIterator& next_char) { 725 | typename Node::Edge* result = nullptr; 726 | 727 | const auto& ch = *next_char; 728 | const auto end = std::end(node->children_); 729 | auto iter = std::find_if(std::begin(node->children_), end, 730 | [&ch]( auto& edge) { 731 | return *(edge.part_.start_) == ch; 732 | }); 733 | if (iter != end) { 734 | result = &(*iter); 735 | } 736 | 737 | return result; 738 | } 739 | 740 | /** Returns the edge and how much of the edge's part represents the @a substr. 741 | */ 742 | EdgeMatch find_partial_edge(const Range& substr) const { 743 | return find_partial_edge(&root_, substr); 744 | } 745 | 746 | /** Returns the edge and how much of the edge's part represents the @a substr. 747 | */ 748 | EdgeMatch find_partial_edge(const Node* start_node, const Range& substr) const { 749 | //std::cout << "find_partial_edge(): substr=" << debug_key(substr) << std::endl; 750 | EdgeMatch result; 751 | 752 | if (str_empty(substr)) { 753 | return result; 754 | } 755 | 756 | const auto substr_len = str_size(substr); 757 | 758 | const Node* node = start_node; 759 | std::size_t substr_pos = 0; 760 | const typename Node::Edge* parent_edge = nullptr; 761 | std::size_t parent_edge_len_used = 0; 762 | while (node) { 763 | bool edge_found = false; 764 | for (auto& edge : node->children_) { 765 | const auto& edge_part = edge.part_; 766 | 767 | const auto len = common_prefix(substr, substr_pos, edge_part, 0); 768 | if (len == 0) { 769 | continue; 770 | } 771 | 772 | const auto substr_remaining_len = substr_len - substr_pos; 773 | //std::cout << " substr_remaining_len=" << substr_remaining_len << std::endl; 774 | if (len == str_size(edge_part)) { 775 | // The remaining substr has edge_part as a prefix. 776 | if (len == substr_remaining_len) { 777 | // And that uses up all of our substr: 778 | return EdgeMatch(&edge, len, substr_len, node); 779 | } else { 780 | // Some of our substr is still unused. 781 | //std::cout << " following partial edge." << std::endl; 782 | // Follow the edge to try to use the rest of the substr: 783 | node = edge.dest_; 784 | substr_pos += str_size(edge_part); 785 | edge_found = true; 786 | 787 | // Remember how we got to the followed edge, 788 | // so we can return that as a partial path if necessary. 789 | parent_edge = &edge; 790 | parent_edge_len_used = len; 791 | break; 792 | } 793 | } else if (len == substr_remaining_len) { 794 | // The edge has the remaining substr as its prefix. 795 | return EdgeMatch(&edge, len, substr_len, node); 796 | } else { 797 | // The edge has some of the remaining substr as its prefix. 798 | return EdgeMatch(&edge, len, substr_pos + len, node); 799 | } 800 | } 801 | 802 | if (!edge_found) { 803 | break; 804 | } 805 | } 806 | 807 | //std::cout << " returning parent_edge=" << static_cast(parent_edge) << 808 | //"parent_edge_len_used=" << parent_edge_len_used << 809 | //"substr_pos=" << substr_pos << std::endl; 810 | return EdgeMatch(parent_edge, parent_edge_len_used, substr_pos, node); 811 | } 812 | 813 | /** Returns the edge and how far along the edge's part the character was found.. 814 | */ 815 | static 816 | EdgeMatch find_partial_edge(Node* start_node, const KeyIterator& next_char) { 817 | auto edge = find_edge(start_node, next_char); 818 | if (!edge) { 819 | return EdgeMatch(); 820 | } 821 | 822 | return EdgeMatch(edge, 1, 1, start_node); 823 | } 824 | 825 | /** Returns the edge and how far along the edge's part the character was found. 826 | */ 827 | static 828 | EdgeMatch find_partial_edge(const ActivePoint& active, const KeyIterator& next_char) { 829 | assert(active.node); 830 | auto edge = find_edge(active.node, active.edge); 831 | assert(edge); 832 | 833 | auto edge_part_pos = active.length; 834 | Node* parent_node = active.node; 835 | while(true) { 836 | const auto& edge_part = edge->part_; 837 | 838 | //This cannot step more than one character away from an intermediate node. 839 | assert(edge_part_pos < (str_size(edge_part) + 1)); 840 | 841 | const auto part_next = std::next(edge_part.start_, edge_part_pos); 842 | if (part_next >= str_end(edge_part)) { 843 | // If the active length tells us to go further than the length of the part, 844 | // step over the destination. 845 | // 846 | // Find the edge from the destination that has the next character: 847 | parent_node = edge->dest_; 848 | edge = find_edge(parent_node, next_char); 849 | if (!edge) { 850 | return EdgeMatch(edge, edge_part_pos, 0, parent_node); 851 | } 852 | 853 | //Try again at the start of the followed edge: 854 | edge_part_pos = 0; 855 | continue; 856 | } 857 | 858 | if (*part_next == *next_char) { 859 | return EdgeMatch(edge, edge_part_pos + 1, 1, parent_node); 860 | } 861 | 862 | return EdgeMatch(edge, edge_part_pos, 0, parent_node); 863 | } 864 | } 865 | 866 | static 867 | inline std::size_t str_size(const Range& key) { 868 | return key.size(); 869 | } 870 | 871 | static 872 | inline bool str_empty(const Range& key) { 873 | return key.empty(); 874 | } 875 | 876 | static 877 | inline Range str_substr(const Range& key, std::size_t start) { 878 | return key.substr(start); 879 | } 880 | 881 | static 882 | inline Range str_substr(const Range& key, std::size_t start, std::size_t len) { 883 | return key.substr(start, len); 884 | } 885 | 886 | static std::string debug_key(const Range& key) { 887 | const auto key_end = str_end(key); 888 | if (key_end <= key.start_) { 889 | return std::string(); 890 | } 891 | 892 | return std::string(key.start_, key_end); 893 | } 894 | 895 | static void debug_print_indent(std::size_t indent) { 896 | for (std::size_t i = 0; i < indent; ++i) { 897 | std::cout << ' '; 898 | } 899 | } 900 | 901 | static void debug_print(const Node* node, std::size_t indent) { 902 | if (!node) { 903 | return; 904 | } 905 | 906 | 907 | for (const auto& edge : node->children_) { 908 | debug_print_indent(indent); 909 | std::cout << debug_key(edge.part_); 910 | if (edge.dest_has_value()) { 911 | std::cout << "("; 912 | bool first = true; 913 | for (const auto value : edge.dest_->keys_and_values_) { 914 | if (!first) { 915 | std::cout << ", "; 916 | } 917 | std::cout << value.second; 918 | first = false; 919 | } 920 | std::cout << ")"; 921 | } 922 | std::cout << std::endl; 923 | 924 | debug_print(edge.dest_, indent + str_size(edge.part_)); 925 | } 926 | } 927 | 928 | using node_and_depth_type = std::pair; 929 | using path_type = std::vector; 930 | 931 | static std::size_t 932 | depth_of_lca(const path_type& a, const path_type& b) { 933 | // Find the first mismatching nodes in the paths: 934 | const auto p = std::mismatch(std::cbegin(a), std::cend(a), 935 | std::cbegin(b), std::cend(b)); 936 | if (p.first == std::cend(a)) { 937 | assert("No common ancestor"); 938 | } 939 | 940 | if (p.first == std::cbegin(a)) { 941 | return 0; 942 | } 943 | 944 | // Get the node just before the mismatch: 945 | auto iter = p.first; 946 | iter--; 947 | return iter->second; 948 | } 949 | 950 | Node root_; 951 | }; 952 | 953 | #endif // MURRAYC_SUFFIX_TREE_SUFFIX_TREE_H 954 | -------------------------------------------------------------------------------- /murrayc-suffix-tree/trie.h: -------------------------------------------------------------------------------- 1 | #ifndef MURRAYC_SUFFIX_TREE_TRIE_H 2 | #define MURRAYC_SUFFIX_TREE_TRIE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | template 12 | class Trie { 13 | public: 14 | bool exists(const T_Key& key) const { 15 | const auto node = find_node(key); 16 | return node != nullptr; 17 | }; 18 | 19 | /** 20 | * Returns T_Value() if the key was not found. 21 | */ 22 | T_Value get_value(const T_Key& key) const { 23 | const auto node = find_node(key); 24 | return node ? node->value : T_Value(); 25 | } 26 | 27 | std::vector find_matches(const T_Key& prefix) { 28 | if (prefix.empty()) { 29 | return {}; 30 | } 31 | 32 | const auto prefix_node = find_node(prefix, false /* not just leaves */); 33 | if (!prefix_node) { 34 | return {}; 35 | } 36 | 37 | std::vector result; 38 | 39 | //Stack of prefixes+nodes. 40 | std::stack> stack; 41 | stack.emplace(prefix, prefix_node); 42 | 43 | while (!stack.empty()) { 44 | const auto item = stack.top(); 45 | stack.pop(); 46 | 47 | const auto& node = item.second; 48 | if (node->children.empty()) { 49 | result.emplace_back(item.first); 50 | } 51 | 52 | for (auto edge : node->children) { 53 | stack.emplace(item.first + edge.part, edge.dest); 54 | } 55 | } 56 | 57 | return result; 58 | } 59 | 60 | void insert(const T_Key& key, const T_Value& value) { 61 | if (key.empty()) { 62 | return; 63 | } 64 | 65 | auto node = &root; 66 | auto appending = false; 67 | for (const auto ch : key) { 68 | Node* next = nullptr; 69 | 70 | if (!appending) { 71 | //Choose the child node, if any: 72 | for (const auto& edge : node->children) { 73 | if (edge.part == ch) { 74 | next = edge.dest; 75 | break; 76 | } 77 | } 78 | } 79 | 80 | // Add a new node, if necessary: 81 | if (!next) { 82 | appending = true; 83 | next = new Node; 84 | typename Node::Edge edge; 85 | edge.part = ch; 86 | edge.dest = next; 87 | node->children.emplace_back(edge); 88 | } 89 | 90 | node = next; 91 | } 92 | 93 | node->is_leaf = true; 94 | node->value = value; 95 | } 96 | 97 | private: 98 | class Node { 99 | public: 100 | class Edge { 101 | public: 102 | char part = 0; 103 | Node* dest = nullptr; 104 | }; 105 | 106 | //We could instead have a std::vector children, 107 | //of size alphabet (such as 26), 108 | //to allow O(1) lookup, at the cost of wasted space. 109 | std::vector children; 110 | 111 | // TODO: Wastes space on non-leaves. 112 | bool is_leaf = false; 113 | T_Value value = 0; 114 | }; 115 | 116 | const Node* find_node(const T_Key& key, bool leaf_only = true) const { 117 | if (key.empty()) { 118 | return nullptr; 119 | } 120 | 121 | auto node = &root; 122 | for (const auto ch : key) { 123 | 124 | //Choose the child node, if any: 125 | Node* next = nullptr; 126 | for (const auto& edge : node->children) { 127 | if (edge.part == ch) { 128 | next = edge.dest; 129 | break; 130 | } 131 | } 132 | 133 | if (!next) { 134 | return nullptr; 135 | } 136 | 137 | node = next; 138 | } 139 | 140 | return (!leaf_only || node->is_leaf) ? node : nullptr; 141 | } 142 | 143 | Node root; 144 | }; 145 | 146 | #endif // MURRAYC_SUFFIX_TREE_TRIE_H 147 | -------------------------------------------------------------------------------- /tests/test_radix_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* 7 | void test_has_prefix() { 8 | using Tree = RadixTree; 9 | 10 | assert(Tree::has_prefix("banana", 0, "banana")); 11 | assert(!Tree::has_prefix("banan", 0, "banana")); 12 | assert(Tree::has_prefix("banana", 0, "banan")); 13 | assert(Tree::has_prefix("banana", 0, "ban")); 14 | assert(!Tree::has_prefix("foo", 0, "banana")); 15 | } 16 | 17 | void test_common_prefix() { 18 | using Tree = RadixTree; 19 | assert(Tree::common_prefix("banana", 0, "bandana", 0) == 3); 20 | assert(Tree::common_prefix("banana", 0, "foo", 0) == 0); 21 | assert(Tree::common_prefix("banana", 0, "banana", 0) == 6); 22 | } 23 | */ 24 | 25 | int main() { 26 | //test_has_prefix(); 27 | //test_prefix_matches(); 28 | //test_common_prefix(); 29 | 30 | using Tree = RadixTree; 31 | Tree radix_tree; 32 | radix_tree.insert("banana", 1); 33 | radix_tree.insert("banana", 9); 34 | radix_tree.insert("bandana", 2); 35 | radix_tree.insert("foo", 3); 36 | radix_tree.insert("foobar", 4); 37 | 38 | assert(radix_tree.exists("foo")); 39 | assert(radix_tree.exists("banana")); 40 | assert(radix_tree.get_value("banana") == 1); 41 | assert(radix_tree.exists("bandana")); 42 | assert(radix_tree.get_value("bandana") == 2); 43 | assert(radix_tree.get_value("foo") == 3); 44 | 45 | assert(!radix_tree.exists("foop")); 46 | assert(radix_tree.get_value("foop") == 0); 47 | assert(!radix_tree.exists("ban")); 48 | assert(radix_tree.get_value("ban") == 0); 49 | 50 | const auto matches = radix_tree.find_matches("ban"); 51 | // TODO: Check wthout caring about the order: 52 | const auto expected_matches = Tree::Matches{{"bandana", {2}}, 53 | {"banana", {1, 9}}}; 54 | /* 55 | for (const auto& match : matches) { 56 | std::cout << "match: " << match.first << ": "; 57 | for (const auto& value : match.second) { 58 | std::cout << value << ", "; 59 | } 60 | std::cout << '\n'; 61 | } 62 | */ 63 | 64 | assert(matches == expected_matches); 65 | 66 | return EXIT_SUCCESS; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /tests/test_suffix_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | 10 | static 11 | bool starts_with(const std::string& a, const std::string& b) { 12 | return a.compare(0, b.size(), b) == 0; 13 | } 14 | 15 | static 16 | void test_simple_single() { 17 | using SA = SuffixArray; 18 | 19 | const std::string str = "banana"; 20 | const std::size_t value = 0; 21 | 22 | const SA::suffix_array_type sa = { 23 | std::make_pair(SA::Range(std::cbegin(str) + 5, std::cend(str)), value), 24 | std::make_pair(SA::Range(std::cbegin(str) + 3, std::cend(str)), value), 25 | std::make_pair(SA::Range(std::cbegin(str) + 1, std::cend(str)), value), 26 | std::make_pair(SA::Range(std::cbegin(str) + 0, std::cend(str)), value), 27 | std::make_pair(SA::Range(std::cbegin(str) + 4, std::cend(str)), value), 28 | std::make_pair(SA::Range(std::cbegin(str) + 2, std::cend(str)), value)}; 29 | const SA::lcp_array_type lcp_array = {1, 3, 0, 0, 2}; 30 | 31 | SA suffix_array(sa, lcp_array); 32 | 33 | { 34 | auto results = suffix_array.find("bob"); 35 | std::cout << "results.size(): " << results.size() << std::endl; 36 | assert(results.size() == 0); 37 | } 38 | 39 | { 40 | auto results = suffix_array.find("an"); 41 | std::cout << "results.size(): " << results.size() << std::endl; 42 | assert(results.size() == 1); 43 | for (const auto& result : results) { 44 | std::cout << result << std::endl; 45 | } 46 | } 47 | } 48 | 49 | static 50 | void test_simple_single_with_positions() { 51 | using SA = SuffixArray; 52 | 53 | const std::string str = "xyzxyaxyz"; 54 | SA suffix_array(str, 0); 55 | 56 | { 57 | auto results = suffix_array.find_with_positions("bob"); 58 | std::cout << "results.size(): " << results.size() << std::endl; 59 | assert(results.size() == 0); 60 | } 61 | 62 | { 63 | auto results = suffix_array.find_with_positions("an"); 64 | std::cout << "results.size(): " << results.size() << std::endl; 65 | assert(results.size() == 0); 66 | } 67 | 68 | { 69 | auto results = suffix_array.find_with_positions("zx"); 70 | std::cout << "results.size(): " << results.size() << std::endl; 71 | assert(results.size() == 1); 72 | 73 | const SA::Range expected_range(std::cbegin(str) + 2, std::cend(str)); 74 | const SA::MatchesWithPositions expected = {{expected_range, 0}}; 75 | assert(results == expected); 76 | for (const auto& result : results) { 77 | const auto& range = result.first; 78 | const auto& value = result.second; 79 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 80 | << std::string(range.start_, range.end_) << ": " << value << std::endl; 81 | } 82 | } 83 | 84 | { 85 | const std::string KEY = "xy"; 86 | auto results = suffix_array.find_with_positions(KEY); 87 | std::cout << "results.size(): " << results.size() << std::endl; 88 | assert(results.size() == 3); 89 | //TODO: Don't check the order: 90 | const SA::MatchesWithPositions expected = { 91 | {SA::Range(std::cbegin(str) + 3, std::cend(str)), 0}, 92 | {SA::Range(std::cbegin(str) + 6, std::cend(str)), 0}, 93 | {SA::Range(std::cbegin(str) + 0, std::cend(str)), 0} 94 | }; 95 | for (const auto& result : results) { 96 | const auto& range = result.first; 97 | const auto& value = result.second; 98 | const auto result_str = range.to_string(); 99 | 100 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 101 | << result_str << ": " << value << std::endl; 102 | assert(starts_with(result_str, KEY)); 103 | } 104 | assert(results == expected); 105 | } 106 | } 107 | 108 | int main() { 109 | test_simple_single(); 110 | test_simple_single_with_positions(); 111 | 112 | return EXIT_SUCCESS; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /tests/test_suffix_tree.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | 10 | static 11 | bool starts_with(const std::string& a, const std::string& b) { 12 | return a.compare(0, b.size(), b) == 0; 13 | } 14 | 15 | static 16 | void test_simple_single() { 17 | using Tree = SuffixTree; 18 | Tree suffix_tree; 19 | 20 | const std::string str1 = "xyzxyaxyz"; // Needs to stay alive as long as the suffix tree. 21 | suffix_tree.insert(str1, 0); 22 | 23 | { 24 | auto results = suffix_tree.find("bob"); 25 | std::cout << "results.size(): " << results.size() << std::endl; 26 | assert(results.size() == 0); 27 | } 28 | 29 | { 30 | auto results = suffix_tree.find("an"); 31 | std::cout << "results.size(): " << results.size() << std::endl; 32 | assert(results.size() == 0); 33 | } 34 | 35 | { 36 | auto results = suffix_tree.find("zx"); 37 | std::cout << "results.size(): " << results.size() << std::endl; 38 | assert(results.size() == 1); 39 | assert(results == Tree::Matches({0})); 40 | for (const auto& result : results) { 41 | std::cout << result << ": " << std::endl; 42 | } 43 | } 44 | } 45 | 46 | static 47 | void test_simple_multiple() { 48 | using Tree = SuffixTree; 49 | Tree suffix_tree; 50 | 51 | // These need to stay alive as long as the suffix tree. 52 | // TODO: Allow use of temporary instances. 53 | const std::string str1 = "banana"; 54 | suffix_tree.insert(str1, 0); 55 | const std::string str2 = "bandana"; 56 | suffix_tree.insert(str2, 1); 57 | const std::string str3 = "bar"; 58 | suffix_tree.insert(str3, 2); 59 | const std::string str4 = "foobar"; 60 | suffix_tree.insert(str4, 3); 61 | 62 | auto results = suffix_tree.find("an"); 63 | std::cout << "results.size(): " << results.size() << std::endl; 64 | assert(results.size() == 2); 65 | assert(results == Tree::Matches({0, 1})); 66 | for (const auto& result : results) { 67 | std::cout << result << ": " << std::endl; 68 | } 69 | 70 | results = suffix_tree.find("bar"); 71 | std::cout << "results.size(): " << results.size() << std::endl; 72 | assert(results.size() == 2); 73 | assert(results == Tree::Matches({2, 3})); 74 | } 75 | 76 | static 77 | void test_full_text_index_individual_strings() { 78 | std::ifstream in; 79 | 80 | const auto filepath = MURRAYC_SUFFIX_TREE_TESTS_DIR "test_pg1400.txt"; 81 | in.open(filepath); 82 | if (!in.is_open()) { 83 | std::cerr << "Could not open file: " << filepath << std::endl; 84 | } 85 | assert(in.is_open()); 86 | 87 | // The actual strings are stored outside of the SuffixTree, 88 | // and must exist for as long as the SuffixTree is used. 89 | std::vector strings; 90 | while (in) { 91 | std::string str; 92 | in >> str; 93 | strings.emplace_back(str); 94 | } 95 | in.close(); 96 | 97 | std::cout << "SuffixTree: Construction:" << std::endl; 98 | boost::timer::auto_cpu_timer timer; 99 | using Tree = SuffixTree; 100 | Tree suffix_tree; 101 | std::size_t pos = 0; 102 | for (const auto& str : strings) { 103 | suffix_tree.insert(str, pos); 104 | ++pos; 105 | } 106 | timer.stop(); 107 | timer.report(); 108 | 109 | std::cout << "SuffixTree: Search:" << std::endl; 110 | timer.start(); 111 | const auto results = suffix_tree.find("xio"); 112 | timer.stop(); 113 | timer.report(); 114 | 115 | assert(results.size() > 10); //TODO: Exact. 116 | for (const auto& result : results) { 117 | std::cout << result << ": " << strings[result] << std::endl; 118 | } 119 | } 120 | 121 | static 122 | void test_full_text_index_one_string() { 123 | // Load the whole text file into one std::string. 124 | std::string str; 125 | std::ifstream in; 126 | 127 | const auto filepath = MURRAYC_SUFFIX_TREE_TESTS_DIR "test_pg1400.txt"; 128 | in.open(filepath); 129 | if (!in.is_open()) { 130 | std::cerr << "Could not open file: " << filepath << std::endl; 131 | } 132 | assert(in.is_open()); 133 | 134 | in.seekg(0, std::ios::end); 135 | str.resize(in.tellg()); 136 | in.seekg(0, std::ios::beg); 137 | in.read(&str[0], str.size()); 138 | in.close(); 139 | 140 | std::cout << "SuffixTree: Construction:" << std::endl; 141 | boost::timer::auto_cpu_timer timer; 142 | using Tree = SuffixTree; 143 | Tree suffix_tree; 144 | 145 | // The actual strings are stored outside of the SuffixTree, 146 | // and must exist for as long as the SuffixTree is used. 147 | 148 | // Parse the text to find the words, 149 | // and add them to the SuffixTree. 150 | // TODO: Make SuffixTree: Support a T_Key type of const char*. 151 | const auto start = std::cbegin(str); 152 | const auto end = std::cend(str); 153 | auto pos = start; 154 | auto word_start = pos; 155 | std::size_t i = 0; 156 | while (pos < end) { 157 | if (std::isspace(*pos)) { 158 | const auto word_end = pos; 159 | suffix_tree.insert(word_start, word_end, i); 160 | word_start = word_end + 1; 161 | ++i; 162 | } 163 | 164 | ++pos; 165 | } 166 | 167 | timer.stop(); 168 | timer.report(); 169 | 170 | std::cout << "SuffixTree: Search:" << std::endl; 171 | timer.start(); 172 | const auto results = suffix_tree.find("xio"); 173 | timer.stop(); 174 | timer.report(); 175 | 176 | assert(results.size() > 10); //TODO: Exact. 177 | for (const auto& result : results) { 178 | std::cout << result << std::endl; 179 | } 180 | } 181 | 182 | static 183 | void test_simple_single_with_positions() { 184 | using Tree = SuffixTree; 185 | Tree suffix_tree; 186 | 187 | const std::string str = "xyzxyaxyz"; 188 | suffix_tree.insert(str, 0); 189 | 190 | { 191 | auto results = suffix_tree.find_with_positions("bob"); 192 | std::cout << "results.size(): " << results.size() << std::endl; 193 | assert(results.size() == 0); 194 | } 195 | 196 | { 197 | auto results = suffix_tree.find_with_positions("an"); 198 | std::cout << "results.size(): " << results.size() << std::endl; 199 | assert(results.size() == 0); 200 | } 201 | 202 | { 203 | auto results = suffix_tree.find_with_positions("zx"); 204 | std::cout << "results.size(): " << results.size() << std::endl; 205 | assert(results.size() == 1); 206 | 207 | const Tree::Range expected_range(std::cbegin(str) + 2, std::cend(str)); 208 | const Tree::MatchesWithPositions expected = {{expected_range, 0}}; 209 | assert(results == expected); 210 | for (const auto& result : results) { 211 | const auto& range = result.first; 212 | const auto& value = result.second; 213 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 214 | << std::string(range.start_, range.end_) << ": " << value << std::endl; 215 | } 216 | } 217 | } 218 | 219 | static 220 | void test_simple_multiple_with_positions() { 221 | using Tree = SuffixTree; 222 | Tree suffix_tree; 223 | 224 | // We keep the strings alive, 225 | // and just pass a reference, 226 | // so we can use the iterators that will 227 | // be returned by find_with_positions() 228 | const std::string str1 = "banana"; 229 | suffix_tree.insert(str1, 0); 230 | const std::string str2 = "bandana"; 231 | suffix_tree.insert(str2, 1); 232 | const std::string str3 = "bar"; 233 | suffix_tree.insert(str3, 2); 234 | const std::string str4 = "foobar"; 235 | suffix_tree.insert(str4, 3); 236 | 237 | { 238 | const auto results = suffix_tree.find_with_positions("an"); 239 | std::cout << "results.size(): " << results.size() << std::endl; 240 | assert(results.size() == 4); 241 | // TODO: Don't test the order: 242 | const Tree::MatchesWithPositions expected = { 243 | {Tree::Range(std::cbegin(str2) + 1, std::cend(str2)), 1}, 244 | {Tree::Range(std::cbegin(str1) + 3, std::cend(str1)), 0}, 245 | {Tree::Range(std::cbegin(str2) + 4, std::cend(str2)), 1}, 246 | {Tree::Range(std::cbegin(str1) + 1, std::cend(str1)), 0} 247 | }; 248 | assert(results == expected); 249 | 250 | for (const auto& result : results) { 251 | const auto& range = result.first; 252 | const auto& value = result.second; 253 | //std::cout << std::distance(std::cbegin(str), range.start_) << ": " 254 | std::cout << range.to_string() << ": " << value << std::endl; 255 | } 256 | } 257 | 258 | { 259 | const auto results = suffix_tree.find_with_positions("bar"); 260 | std::cout << "results.size(): " << results.size() << std::endl; 261 | assert(results.size() == 2); 262 | const Tree::MatchesWithPositions expected = { 263 | {Tree::Range(std::cbegin(str3) + 0, std::cend(str3)), 2}, 264 | {Tree::Range(std::cbegin(str4) + 3, std::cend(str4)), 3} 265 | }; 266 | assert(results == expected); 267 | 268 | for (const auto& result : results) { 269 | const auto& range = result.first; 270 | const auto& value = result.second; 271 | //std::cout << std::distance(std::cbegin(str), range.start_) << ": " 272 | std::cout << std::string(range.start_, range.end_) << ": " << value << std::endl; 273 | } 274 | } 275 | } 276 | 277 | /** Test linear-time creation with Ukkonen's algorithm, 278 | * via the constructor. 279 | */ 280 | static 281 | void test_simple_single_construction() { 282 | using Tree = SuffixTree; 283 | 284 | std::string str = "xyzxyaxyz"; 285 | Tree suffix_tree(str, 0); 286 | 287 | { 288 | auto results = suffix_tree.find("bob"); 289 | std::cout << "results.size(): " << results.size() << std::endl; 290 | assert(results.size() == 0); 291 | } 292 | 293 | { 294 | auto results = suffix_tree.find("an"); 295 | std::cout << "results.size(): " << results.size() << std::endl; 296 | assert(results.size() == 0); 297 | } 298 | 299 | { 300 | auto results = suffix_tree.find("zx"); 301 | std::cout << "results.size(): " << results.size() << std::endl; 302 | assert(results.size() == 1); 303 | assert(results == Tree::Matches({0})); 304 | for (const auto& result : results) { 305 | std::cout << result << ": " << std::endl; 306 | } 307 | } 308 | 309 | { 310 | const std::string KEY = "xy"; 311 | auto results = suffix_tree.find_with_positions(KEY); 312 | std::cout << "results.size(): " << results.size() << std::endl; 313 | assert(results.size() == 3); 314 | //TODO: Don't check the order: 315 | const Tree::MatchesWithPositions expected = { 316 | {Tree::Range(std::cbegin(str) + 3, std::cend(str)), 0}, 317 | {Tree::Range(std::cbegin(str) + 6, std::cend(str)), 0}, 318 | {Tree::Range(std::cbegin(str) + 0, std::cend(str)), 0} 319 | }; 320 | for (const auto& result : results) { 321 | const auto& range = result.first; 322 | const auto& value = result.second; 323 | const auto result_str = range.to_string(); 324 | 325 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 326 | << result_str << ": " << value << std::endl; 327 | assert(starts_with(result_str, KEY)); 328 | } 329 | assert(results == expected); 330 | } 331 | } 332 | 333 | static void 334 | test_get_suffix_array() { 335 | using Tree = SuffixTree; 336 | Tree suffix_tree; 337 | 338 | // We keep the string alive, 339 | // and just pass a reference, 340 | // so we can use the iterators that will 341 | // be returned by get_suffix_array() 342 | const std::string str = "bananabanana"; 343 | suffix_tree.insert(str, 0); 344 | 345 | const auto sa_and_lcp = suffix_tree.get_suffix_array_and_lcp_array(); 346 | const auto& sa = sa_and_lcp.first; 347 | const auto& lcp = sa_and_lcp.second; 348 | std::cout << "Suffix array size: " << sa.size() << std::endl; 349 | assert(sa.size() == 12); 350 | assert(lcp.size() == 11); 351 | 352 | const std::vector expected_lcp = {1, 1, 3, 3, 5, 0, 6, 0, 2, 2, 4}; 353 | assert(lcp == expected_lcp); 354 | 355 | for (const auto p : sa) { 356 | const auto& range = p.first; 357 | const auto& value = p.second; 358 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 359 | << std::string(range.start_, range.end_) << ": " << value << std::endl; 360 | } 361 | 362 | // Check that these are in lexographic order: 363 | // The actual comparision in get_suffix_array() is more efficient: 364 | const bool sorted = std::is_sorted(std::cbegin(sa), std::cend(sa), 365 | [](const auto& a, const auto& b) { 366 | const auto& arange = a.first; 367 | const auto& brange = b.first; 368 | const auto astr = std::string(arange.start_, arange.end_); 369 | const auto bstr = std::string(brange.start_, brange.end_); 370 | return astr < bstr; 371 | }); 372 | assert(sorted); 373 | } 374 | 375 | static void 376 | test_create_from_suffix_array_and_lcp_array() { 377 | using Tree = SuffixTree; 378 | Tree suffix_tree1; 379 | 380 | // We keep the string alive, 381 | // and just pass a reference, 382 | // so we can use the iterators that will 383 | // be returned by get_suffix_array() 384 | const std::string str = "xyzxyaxyz"; 385 | suffix_tree1.insert(str, 0); 386 | 387 | const auto sa_and_lcp = suffix_tree1.get_suffix_array_and_lcp_array(); 388 | const auto& sa = sa_and_lcp.first; 389 | const auto& lcp = sa_and_lcp.second; 390 | 391 | Tree suffix_tree2(sa, lcp); 392 | 393 | { 394 | const std::string KEY = "zx"; 395 | auto results = suffix_tree2.find_with_positions(KEY); 396 | std::cout << "results.size(): " << results.size() << std::endl; 397 | assert(results.size() == 1); 398 | 399 | const Tree::Range expected_range(std::cbegin(str) + 2, std::cend(str)); 400 | const Tree::MatchesWithPositions expected = {{expected_range, 0}}; 401 | assert(results == expected); 402 | for (const auto& result : results) { 403 | const auto& range = result.first; 404 | const auto& value = result.second; 405 | const auto result_str = range.to_string(); 406 | assert(starts_with(result_str, KEY)); 407 | 408 | std::cout << std::distance(std::cbegin(str), range.start_) << ": " 409 | << result_str << ": " << value << std::endl; 410 | } 411 | } 412 | } 413 | 414 | int main() { 415 | test_simple_single(); 416 | test_simple_multiple(); 417 | 418 | test_full_text_index_individual_strings(); 419 | test_full_text_index_one_string(); 420 | 421 | test_simple_single_with_positions(); 422 | test_simple_multiple_with_positions(); 423 | 424 | test_simple_single_construction(); 425 | 426 | test_get_suffix_array(); 427 | test_create_from_suffix_array_and_lcp_array(); 428 | 429 | return EXIT_SUCCESS; 430 | } 431 | 432 | -------------------------------------------------------------------------------- /tests/test_trie.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | 11 | Trie trie; 12 | trie.insert("banana", 1); 13 | trie.insert("bandana", 2); 14 | trie.insert("foo", 3); 15 | trie.insert("foobar", 4); 16 | 17 | assert(trie.exists("banana")); 18 | assert(trie.get_value("banana") == 1); 19 | assert(trie.exists("foo")); 20 | assert(trie.get_value("foo") == 3); 21 | 22 | assert(!trie.exists("foop")); 23 | assert(trie.get_value("foop") == 0); 24 | assert(!trie.exists("ban")); 25 | assert(trie.get_value("ban") == 0); 26 | 27 | const auto matches = trie.find_matches("ban"); 28 | //for (const auto match : matches) { 29 | // std::cout << "match: " << match << '\n'; 30 | //} 31 | 32 | // TODO: Check wthout caring about the order: 33 | const auto expected_matches = std::vector({"bandana", "banana"}); 34 | 35 | assert(matches == expected_matches); 36 | 37 | return EXIT_SUCCESS; 38 | } 39 | 40 | --------------------------------------------------------------------------------