├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── Makefile ├── README.md ├── bench ├── delete.cpp ├── insert.cpp ├── main.cpp ├── mixed.cpp ├── mixed_dense.cpp ├── node_16.cpp ├── node_256.cpp ├── node_4.cpp ├── node_48.cpp ├── query_iteration.cpp ├── query_sparse_uniform.cpp ├── query_sparse_uniform_64.cpp └── query_sparse_zipf.cpp ├── include ├── art.hpp └── art │ ├── art.hpp │ ├── child_it.hpp │ ├── inner_node.hpp │ ├── leaf_node.hpp │ ├── node.hpp │ ├── node_16.hpp │ ├── node_256.hpp │ ├── node_4.hpp │ ├── node_48.hpp │ └── tree_it.hpp ├── report ├── .gitignore ├── .latexmkrc ├── art.bib ├── art.tex ├── compressions_dense.dat ├── compressions_paths.dat ├── compressions_sparse.dat └── images │ ├── IFIlogo-eps-converted-to.pdf │ ├── IFIlogo.eps │ ├── art_nodes_draw-eps-converted-to.pdf │ ├── art_nodes_draw.eps │ ├── dbtgBW-eps-converted-to.pdf │ ├── dbtgBW.eps │ ├── trie_s1_draw-eps-converted-to.pdf │ ├── trie_s1_draw.eps │ ├── trie_s2_draw-eps-converted-to.pdf │ └── trie_s2_draw.eps ├── src └── example.cpp ├── test ├── art.cpp ├── inner_node.cpp ├── main.cpp ├── node.cpp ├── node_16.cpp ├── node_256.cpp ├── node_4.cpp ├── node_48.cpp └── tree_it.cpp └── third_party └── zipf ├── zipf.cpp └── zipf.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build 3 | *.swp 4 | compile_commands.json 5 | dataset.txt 6 | massif.* 7 | cachegrind.* 8 | callgrind.* 9 | cmake-build-debug 10 | .idea 11 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/doctest"] 2 | path = third_party/doctest 3 | url = https://github.com/onqtam/doctest.git 4 | [submodule "third_party/benchpress"] 5 | path = third_party/benchpress 6 | url = https://github.com/cjgdev/benchpress.git 7 | [submodule "third_party/benchmark"] 8 | path = third_party/benchmark 9 | url = https://github.com/google/benchmark.git 10 | [submodule "third_party/picobench"] 11 | path = third_party/picobench 12 | url = https://github.com/iboB/picobench.git 13 | [submodule "third_party/cryptopp"] 14 | path = third_party/cryptopp 15 | url = https://github.com/weidai11/cryptopp.git 16 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.0) 2 | 3 | project(art) 4 | 5 | set(CMAKE_CXX_STANDARD 11) 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | 8 | set(CMAKE_CXX_FLAGS "-Wall -Wextra") 9 | set(CMAKE_CXX_FLAGS_DEBUG "-g") 10 | set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 11 | 12 | add_library(art INTERFACE) 13 | 14 | target_include_directories( 15 | art 16 | INTERFACE 17 | "$" 18 | "$" 19 | ) 20 | 21 | ### dependencies ### 22 | 23 | # doctest 24 | set(DOCTEST_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party/doctest/doctest) 25 | add_library(doctest INTERFACE) 26 | target_include_directories(doctest INTERFACE ${DOCTEST_INCLUDE_DIR}) 27 | 28 | # picobench 29 | set(PICOBENCH_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party/picobench/include) 30 | add_library(picobench INTERFACE) 31 | target_include_directories(picobench INTERFACE ${PICOBENCH_INCLUDE_DIR}) 32 | 33 | # zipf 34 | set(ZIPF_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party/zipf) 35 | add_library(zipf ${PROJECT_SOURCE_DIR}/third_party/zipf/zipf.cpp) 36 | target_include_directories(zipf PUBLIC ${ZIPF_INCLUDE_DIR}) 37 | 38 | ### executables ### 39 | 40 | # main executable 41 | add_executable(main 42 | "${PROJECT_SOURCE_DIR}/src/example.cpp" 43 | ) 44 | target_link_libraries(main art zipf) 45 | 46 | # test executable 47 | add_executable(test 48 | "${PROJECT_SOURCE_DIR}/test/art.cpp" 49 | "${PROJECT_SOURCE_DIR}/test/main.cpp" 50 | "${PROJECT_SOURCE_DIR}/test/node.cpp" 51 | "${PROJECT_SOURCE_DIR}/test/inner_node.cpp" 52 | "${PROJECT_SOURCE_DIR}/test/node_4.cpp" 53 | "${PROJECT_SOURCE_DIR}/test/node_16.cpp" 54 | "${PROJECT_SOURCE_DIR}/test/node_48.cpp" 55 | "${PROJECT_SOURCE_DIR}/test/node_256.cpp" 56 | "${PROJECT_SOURCE_DIR}/test/tree_it.cpp" 57 | ) 58 | target_link_libraries(test art doctest) 59 | 60 | # bench executable 61 | add_executable(bench 62 | "${PROJECT_SOURCE_DIR}/bench/delete.cpp" 63 | "${PROJECT_SOURCE_DIR}/bench/insert.cpp" 64 | "${PROJECT_SOURCE_DIR}/bench/main.cpp" 65 | "${PROJECT_SOURCE_DIR}/bench/mixed.cpp" 66 | "${PROJECT_SOURCE_DIR}/bench/mixed_dense.cpp" 67 | # "${PROJECT_SOURCE_DIR}/bench/node_4.cpp" 68 | # "${PROJECT_SOURCE_DIR}/bench/node_16.cpp" 69 | # "${PROJECT_SOURCE_DIR}/bench/node_48.cpp" 70 | # "${PROJECT_SOURCE_DIR}/bench/node_256.cpp" 71 | "${PROJECT_SOURCE_DIR}/bench/query_iteration.cpp" 72 | "${PROJECT_SOURCE_DIR}/bench/query_sparse_uniform.cpp" 73 | "${PROJECT_SOURCE_DIR}/bench/query_sparse_uniform_64.cpp" 74 | "${PROJECT_SOURCE_DIR}/bench/query_sparse_zipf.cpp" 75 | ) 76 | target_link_libraries(bench art picobench zipf) 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | MAKEFLAGS += --silent 2 | 3 | export BUILD_DIR ?= build 4 | 5 | default: debug 6 | 7 | release: 8 | mkdir -p ./$(BUILD_DIR) && cd ./$(BUILD_DIR) && cmake ../ -DCMAKE_BUILD_TYPE=Release && cmake --build . 9 | 10 | debug: 11 | mkdir -p ./$(BUILD_DIR) && cd ./$(BUILD_DIR) && cmake ../ -DCMAKE_BUILD_TYPE=Debug && cmake --build . 12 | 13 | test: 14 | @if [ -f ./$(BUILD_DIR)/test ]; then ./$(BUILD_DIR)/test ${ARGS}; else echo "Please run 'make' or 'make release' first" && exit 1; fi 15 | 16 | bench: 17 | @if [ -f ./$(BUILD_DIR)/bench ]; then ./$(BUILD_DIR)/bench; else echo "Please run 'make' or 'make release' first" && exit 1; fi 18 | 19 | clean: 20 | rm -rf ./$(BUILD_DIR) 21 | 22 | .PHONY: test bench 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adaptive Radix Tree 2 | 3 | The goal of this project is to study and implement the Adaptive Radix Tree (ART), 4 | as proposed by Leis et al. ART, which is a trie based data structure, achieves 5 | its performance, and space efficiency, by compressing the tree both vertically, 6 | i.e., if a node has no siblings it is merged with its parent, and horizontally, 7 | i.e., uses an array which grows as the number of children increases. Vertical 8 | compression reduces the tree height and horizontal compression decreases a node’s size. 9 | 10 | ## Usage 11 | 12 | ```cpp 13 | #include 14 | 15 | using namespace art; 16 | 17 | int main() { 18 | art> m; 19 | 20 | // set k 21 | m.set("k", std::make_shared(new MyResource())); 22 | 23 | // get k 24 | std::shared_ptr ptr = m.get("k"); 25 | 26 | // delete k 27 | m.del("k"); 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | ## Contributing 34 | 35 | ```cpp 36 | # clone repository with submodules 37 | git clone --recurse-submodules https://github.com/rafaelkallis/adaptive-radix-tree 38 | 39 | # build test binaries 40 | make [release] 41 | 42 | # run tests 43 | make test 44 | 45 | # run benchmarks 46 | make bench 47 | ``` 48 | 49 | ## Benchmark results 50 | (16M keys, `art::art` vs `std::map` vs `std::unordered_map`) 51 | ``` 52 | delete: 53 | =============================================================================== 54 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 55 | =============================================================================== 56 | art_delete_sparse * | 1600000 | 711.271 | 444 | - | 2249495.0 57 | red_black_delete_sparse | 1600000 | 1411.551 | 882 | 1.985 | 1133505.1 58 | hashmap_delete_sparse | 1600000 | 491.755 | 307 | 0.691 | 3253652.5 59 | =============================================================================== 60 | insert: 61 | =============================================================================== 62 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 63 | =============================================================================== 64 | art_insert_sparse * | 1600000 | 609.976 | 381 | - | 2623054.7 65 | red_black_insert_sparse | 1600000 | 1291.967 | 807 | 2.118 | 1238421.8 66 | hashmap_insert_sparse | 1600000 | 627.865 | 392 | 1.029 | 2548317.4 67 | =============================================================================== 68 | mixed: 69 | =============================================================================== 70 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 71 | =============================================================================== 72 | art_mixed_sparse * | 1600000 | 662.734 | 414 | - | 2414241.9 73 | red_black_mixed_sparse | 1600000 | 1209.072 | 755 | 1.824 | 1323328.8 74 | hashmap_mixed_sparse | 1600000 | 614.750 | 384 | 0.928 | 2602684.4 75 | =============================================================================== 76 | query sparse uniform: 77 | =============================================================================== 78 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 79 | =============================================================================== 80 | art_q_s_u * | 1600000 | 710.420 | 444 | - | 2252187.4 81 | red_black_q_s_u | 1600000 | 1077.434 | 673 | 1.517 | 1485009.8 82 | hashmap_q_s_u | 1600000 | 368.342 | 230 | 0.518 | 4343790.8 83 | =============================================================================== 84 | query sparse uniform base 64 keys: 85 | =============================================================================== 86 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 87 | =============================================================================== 88 | art_q_s_u * | 1600000 | 713.069 | 445 | - | 2243821.6 89 | red_black_q_s_u | 1600000 | 1704.574 | 1065 | 2.390 | 938650.9 90 | hashmap_q_s_u | 1600000 | 466.918 | 291 | 0.655 | 3426726.0 91 | =============================================================================== 92 | query sparse zipf: 93 | =============================================================================== 94 | Name (baseline is *) | Dim | Total ms | ns/op |Baseline| Ops/second 95 | =============================================================================== 96 | art_q_s_z * | 1600000 | 411.221 | 257 | - | 3890849.2 97 | red_black_q_s_z | 1600000 | 747.299 | 467 | 1.817 | 2141044.3 98 | hashmap_q_s_z | 1600000 | 413.103 | 258 | 1.005 | 3873125.6 99 | =============================================================================== 100 | ``` 101 | 102 | You can replicate using `make release && make bench` 103 | 104 | 105 | ## References 106 | 107 | * [The Adaptive Radix Tree: ARTful Indexing for Main-Memory Databases](http://www-db.in.tum.de/~leis/papers/ART.pdf) 108 | * [The Adaptive Radix Tree](http://rafaelkallis.com/static/media/the_adaptive_radix_tree_rafael_kallis.23779b20.pdf) 109 | -------------------------------------------------------------------------------- /bench/delete.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file deletion microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using picobench::state; 14 | 15 | PICOBENCH_SUITE("delete"); 16 | 17 | static void art_delete_sparse(state &s) { 18 | art::art m; 19 | int v = 1; 20 | std::mt19937_64 g1(0); 21 | for (auto i __attribute__((unused)) : s) { 22 | m.set(std::to_string(g1()).c_str(), &v); 23 | } 24 | std::mt19937_64 g2(0); 25 | for (auto i __attribute__((unused)) : s) { 26 | m.del(std::to_string(g2()).c_str()); 27 | } 28 | } 29 | PICOBENCH(art_delete_sparse); 30 | 31 | static void red_black_delete_sparse(state &s) { 32 | std::map m; 33 | int v = 1; 34 | std::mt19937_64 g1(0); 35 | for (auto i __attribute__((unused)) : s) { 36 | m[std::to_string(g1()).c_str()] = v; 37 | } 38 | std::mt19937_64 g2(0); 39 | for (auto i __attribute__((unused)) : s) { 40 | m.erase(m.find(std::to_string(g2()))); 41 | } 42 | } 43 | PICOBENCH(red_black_delete_sparse); 44 | 45 | static void hashmap_delete_sparse(state &s) { 46 | std::unordered_map m; 47 | int v = 1; 48 | std::mt19937_64 g1(0); 49 | for (auto i __attribute__((unused)) : s) { 50 | m[std::to_string(g1())] = v; 51 | } 52 | std::mt19937_64 g2(0); 53 | for (auto i __attribute__((unused)) : s) { 54 | m.erase(m.find(std::to_string(g2()))); 55 | } 56 | } 57 | PICOBENCH(hashmap_delete_sparse); 58 | -------------------------------------------------------------------------------- /bench/insert.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file insertion microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using picobench::state; 16 | 17 | PICOBENCH_SUITE("insert"); 18 | 19 | static void art_insert_sparse(state &s) { 20 | art::art m; 21 | int v = 1; 22 | std::mt19937_64 rng(0); 23 | for (auto i __attribute__((unused)) : s) { 24 | m.set(std::to_string(rng()).c_str(), &v); 25 | } 26 | } 27 | PICOBENCH(art_insert_sparse); 28 | 29 | static void red_black_insert_sparse(state &s) { 30 | std::map m; 31 | int v = 1; 32 | std::mt19937_64 rng(0); 33 | for (auto i __attribute__((unused)) : s) { 34 | m[std::to_string(rng())] = v; 35 | } 36 | } 37 | PICOBENCH(red_black_insert_sparse); 38 | 39 | static void hashmap_insert_sparse(state &s) { 40 | std::unordered_map m; 41 | int v = 1; 42 | std::random_device rd; 43 | std::mt19937_64 g(rd()); 44 | for (auto i __attribute__((unused)) : s) { 45 | m[std::to_string(g())] = v; 46 | } 47 | } 48 | PICOBENCH(hashmap_insert_sparse); 49 | -------------------------------------------------------------------------------- /bench/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file bench entrypoint 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #define PICOBENCH_IMPLEMENT 7 | #include "picobench/picobench.hpp" 8 | #include 9 | 10 | int main(int argc, char *argv[]) { 11 | picobench::runner runner; 12 | runner.parse_cmd_line(argc, argv); 13 | if (runner.should_run()) { 14 | runner.set_default_state_iterations({1600000}); 15 | runner.run_benchmarks(); 16 | auto report = runner.generate_report(); 17 | report.to_text(std::cout); 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /bench/mixed.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file mixed benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include "zipf.hpp" 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | using picobench::state; 19 | using std::getline; 20 | using std::hash; 21 | using std::ifstream; 22 | using std::map; 23 | using std::mt19937_64; 24 | using std::string; 25 | using std::to_string; 26 | using std::unordered_map; 27 | 28 | PICOBENCH_SUITE("mixed"); 29 | 30 | static void art_mixed_sparse(state &s) { 31 | art::art m; 32 | fast_zipf rng(10000000); 33 | hash h; 34 | string k; 35 | int v = 1; 36 | for (auto i __attribute__((unused)) : s) { 37 | k = to_string(h(rng())); 38 | if (m.get(k.c_str()) == nullptr) { 39 | m.set(k.c_str(), &v); 40 | } else { 41 | m.del(k.c_str()); 42 | } 43 | } 44 | } 45 | PICOBENCH(art_mixed_sparse); 46 | 47 | static void red_black_mixed_sparse(state &s) { 48 | map m; 49 | fast_zipf rng(10000000); 50 | hash h; 51 | string k; 52 | int v = 1; 53 | for (auto i __attribute__((unused)) : s) { 54 | k = to_string(h(rng())); 55 | if (m[k] == nullptr) { 56 | m[k] = &v; 57 | } else { 58 | m.erase(m.find(k)); 59 | } 60 | } 61 | } 62 | PICOBENCH(red_black_mixed_sparse); 63 | 64 | static void hashmap_mixed_sparse(state &s) { 65 | unordered_map m; 66 | fast_zipf rng(10000000); 67 | hash h; 68 | string k; 69 | int v = 1; 70 | for (auto i __attribute__((unused)) : s) { 71 | k = to_string(h(rng())); 72 | if (m[k] == nullptr) { 73 | m[k] = &v; 74 | } else { 75 | m.erase(m.find(k)); 76 | } 77 | } 78 | } 79 | PICOBENCH(hashmap_mixed_sparse); 80 | -------------------------------------------------------------------------------- /bench/mixed_dense.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file mixed benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include "zipf.hpp" 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | using picobench::state; 19 | using std::getline; 20 | using std::hash; 21 | using std::ifstream; 22 | using std::map; 23 | using std::mt19937_64; 24 | using std::string; 25 | using std::to_string; 26 | using std::unordered_map; 27 | 28 | PICOBENCH_SUITE("mixed_dense"); 29 | 30 | static void art_mixed(state &s) __attribute__((unused)); 31 | static void art_mixed(state &s) { 32 | ifstream file("dataset.txt"); 33 | if (!file) { 34 | return; 35 | } 36 | unordered_map dataset; 37 | uint32_t n = 0; 38 | string line; 39 | while (getline(file, line)) { 40 | dataset[n++] = line; 41 | } 42 | file.close(); 43 | 44 | int v = 1; 45 | fast_zipf rng(n); 46 | 47 | art::art m; 48 | string k; 49 | for (auto i __attribute__((unused)) : s) { 50 | k = dataset[rng()]; 51 | if (m.get(k.c_str()) == nullptr) { 52 | m.set(k.c_str(), &v); 53 | } else { 54 | m.del(k.c_str()); 55 | } 56 | } 57 | } 58 | /* PICOBENCH(art_mixed).iterations({1000000}); */ 59 | 60 | static void red_black_mixed(state &s) __attribute__((unused)); 61 | static void red_black_mixed(state &s) { 62 | ifstream file("dataset.txt"); 63 | if (!file) { 64 | return; 65 | } 66 | unordered_map dataset; 67 | uint32_t n = 0; 68 | string line; 69 | while (getline(file, line)) { 70 | dataset[n++] = line; 71 | } 72 | file.close(); 73 | 74 | int v = 1; 75 | fast_zipf rng(n); 76 | 77 | map m; 78 | string k; 79 | for (auto i __attribute__((unused)) : s) { 80 | k = dataset[rng()]; 81 | if (m[k] == nullptr) { 82 | m[k] = &v; 83 | } else { 84 | m.erase(m.find(k)); 85 | } 86 | } 87 | } 88 | /* PICOBENCH(red_black_mixed).iterations({1000000}); */ 89 | 90 | static void hashmap_mixed(state &s) __attribute__((unused)); 91 | static void hashmap_mixed(state &s) { 92 | ifstream file("dataset.txt"); 93 | if (!file) { 94 | return; 95 | } 96 | unordered_map dataset; 97 | uint32_t n = 0; 98 | string line; 99 | while (getline(file, line)) { 100 | dataset[n++] = line; 101 | } 102 | file.close(); 103 | 104 | int v = 1; 105 | fast_zipf rng(n); 106 | 107 | unordered_map m; 108 | string k; 109 | for (auto i __attribute__((unused)) : s) { 110 | k = dataset[rng()]; 111 | if (m[k] == nullptr) { 112 | m[k] = &v; 113 | } else { 114 | m.erase(m.find(k)); 115 | } 116 | } 117 | } 118 | /* PICOBENCH(hashmap_mixed).iterations({1000000}); */ 119 | -------------------------------------------------------------------------------- /bench/node_16.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_16 benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | 9 | using namespace art; 10 | using picobench::state; 11 | 12 | PICOBENCH_SUITE("node_16"); 13 | 14 | static void node_16_constructor(state &s) { 15 | for (auto i __attribute__((unused)) : s) { 16 | node_16 n; 17 | } 18 | } 19 | PICOBENCH(node_16_constructor); 20 | 21 | static void node_16_find_child(state &s) { 22 | node_16 n; 23 | leaf_node child(nullptr); 24 | for (int i = 0; i < 16; ++i) { 25 | n.set_child((i * 17) - 128, &child); 26 | } 27 | for (auto i __attribute__((unused)) : s) { 28 | auto child_out __attribute__((unused)) = n.find_child(((rand() % 16) * 17) - 128); 29 | } 30 | } 31 | PICOBENCH(node_16_find_child); 32 | 33 | static void node_16_set_child(state &s) { 34 | auto n = new node_16(); 35 | leaf_node child(nullptr); 36 | for (auto i __attribute__((unused)) : s) { 37 | if (n->is_full()) { 38 | delete n; 39 | n = new node_16(); 40 | } 41 | n->set_child(((rand() % 16) * 17) - 128, &child); 42 | } 43 | } 44 | PICOBENCH(node_16_set_child); 45 | 46 | static void node_16_grow(state &s) { 47 | for (auto i __attribute__((unused)) : s) { 48 | auto *n = new node_16(); 49 | auto *new_n = n->grow(); 50 | delete new_n; 51 | } 52 | } 53 | PICOBENCH(node_16_grow); 54 | 55 | static void node_16_next_partial_key(state &s) { 56 | node_16 n; 57 | leaf_node child(nullptr); 58 | for (int i = 0; i < 16; ++i) { 59 | n.set_child((i * 17) - 128, &child); 60 | } 61 | for (auto i __attribute__((unused)) : s) { 62 | auto next_partial_key __attribute__((unused)) = n.next_partial_key((rand() % 256) - 128); 63 | } 64 | } 65 | PICOBENCH(node_16_next_partial_key); 66 | 67 | static void node_16_prev_partial_key(state &s) { 68 | node_16 n; 69 | leaf_node child(nullptr); 70 | for (int i = 0; i < 16; ++i) { 71 | n.set_child((i * 17) - 128, &child); 72 | } 73 | for (auto i __attribute__((unused)) : s) { 74 | auto prev_partial_key __attribute__((unused)) = n.prev_partial_key((rand() % 256) - 128); 75 | } 76 | } 77 | PICOBENCH(node_16_prev_partial_key); 78 | -------------------------------------------------------------------------------- /bench/node_256.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_256 benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | 9 | using namespace art; 10 | using picobench::state; 11 | 12 | PICOBENCH_SUITE("node_256"); 13 | 14 | static void node_256_constructor(state &s) { 15 | for (auto i __attribute__((unused)) : s) { 16 | node_256 n; 17 | } 18 | } 19 | PICOBENCH(node_256_constructor); 20 | 21 | static void node_256_find_child(state &s) { 22 | node_256 n; 23 | leaf_node child(nullptr); 24 | for (int i = 0; i < 256; ++i) { 25 | n.set_child(i - 128, &child); 26 | } 27 | for (auto i __attribute__((unused)) : s) { 28 | auto child = n.find_child((rand() % 256) - 128); 29 | } 30 | } 31 | PICOBENCH(node_256_find_child); 32 | 33 | static void node_256_set_child(state &s) { 34 | node_256 *n = new node_256(); 35 | leaf_node child(nullptr); 36 | for (auto i __attribute__((unused)) : s) { 37 | if (n->is_full()) { 38 | delete n; 39 | n = new node_256(); 40 | } 41 | n->set_child((rand() % 256) - 128, &child); 42 | } 43 | } 44 | PICOBENCH(node_256_set_child); 45 | 46 | static void node_256_next_partial_key(state &s) { 47 | node_256 n; 48 | leaf_node child(nullptr); 49 | for (int i = 0; i < 256; ++i) { 50 | n.set_child(i - 128, &child); 51 | } 52 | for (auto i __attribute__((unused)) : s) { 53 | auto next_partial_key = n.next_partial_key((rand() % 256) - 128); 54 | } 55 | } 56 | PICOBENCH(node_256_next_partial_key); 57 | 58 | static void node_256_prev_partial_key(state &s) { 59 | node_256 n; 60 | leaf_node child(nullptr); 61 | for (int i = 0; i < 256; ++i) { 62 | n.set_child(i - 128, &child); 63 | } 64 | for (auto i __attribute__((unused)) : s) { 65 | auto prev_partial_key = n.prev_partial_key((rand() % 256) - 128); 66 | } 67 | } 68 | PICOBENCH(node_256_prev_partial_key); 69 | -------------------------------------------------------------------------------- /bench/node_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_4 benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | 9 | using namespace art; 10 | using picobench::state; 11 | 12 | PICOBENCH_SUITE("node_4"); 13 | 14 | static void node_4_constructor(state &s) { 15 | for (auto i __attribute__((unused)) : s) { 16 | node_4 n; 17 | } 18 | } 19 | PICOBENCH(node_4_constructor); 20 | 21 | static void node_4_find_child(state &s) { 22 | node_4 n; 23 | 24 | leaf_node c0(nullptr); 25 | leaf_node c1(nullptr); 26 | leaf_node c2(nullptr); 27 | leaf_node c3(nullptr); 28 | 29 | n.set_child(-128, &c0); 30 | n.set_child(-43, &c1); 31 | n.set_child(42, &c2); 32 | n.set_child(127, &c3); 33 | 34 | char partial_keys[] = {-128, -43, 42, 127}; 35 | node **child = nullptr; 36 | for (auto i __attribute__((unused)) : s) { 37 | child = n.find_child(partial_keys[rand() % 4]); 38 | } 39 | } 40 | PICOBENCH(node_4_find_child); 41 | 42 | static void node_4_set_child(state &s) { 43 | auto n = new node_4(); 44 | leaf_node child(nullptr); 45 | char partial_keys[] = {-128, -43, 42, 127}; 46 | for (auto i __attribute__((unused)) : s) { 47 | if (n->is_full()) { 48 | delete n; 49 | n = new node_4(); 50 | } 51 | 52 | n->set_child(partial_keys[rand() % 4], &child); 53 | } 54 | } 55 | PICOBENCH(node_4_set_child); 56 | 57 | static void node_4_grow(state &s) { 58 | inner_node *n = nullptr; 59 | inner_node *new_n = nullptr; 60 | for (auto i __attribute__((unused)) : s) { 61 | n = new node_4(); 62 | new_n = n->grow(); 63 | delete new_n; 64 | } 65 | } 66 | PICOBENCH(node_4_grow); 67 | 68 | static void node_4_next_partial_key(state &s) { 69 | node_4 n; 70 | 71 | leaf_node c0(nullptr); 72 | leaf_node c1(nullptr); 73 | leaf_node c2(nullptr); 74 | leaf_node c3(nullptr); 75 | 76 | n.set_child(-128, &c0); 77 | n.set_child(-43, &c1); 78 | n.set_child(42, &c2); 79 | n.set_child(127, &c3); 80 | 81 | for (auto i __attribute__((unused)) : s) { 82 | auto next_partial_key = n.next_partial_key((rand() % 256) - 128); 83 | } 84 | } 85 | PICOBENCH(node_4_next_partial_key); 86 | 87 | static void node_4_prev_partial_key(state &s) { 88 | node_4 n; 89 | 90 | leaf_node c0(nullptr); 91 | leaf_node c1(nullptr); 92 | leaf_node c2(nullptr); 93 | leaf_node c3(nullptr); 94 | 95 | n.set_child(-128, &c0); 96 | n.set_child(-43, &c1); 97 | n.set_child(42, &c2); 98 | n.set_child(127, &c3); 99 | 100 | for (auto i __attribute__((unused)) : s) { 101 | auto prev_partial_key = n.prev_partial_key((rand() % 256) - 128); 102 | } 103 | } 104 | PICOBENCH(node_4_prev_partial_key); 105 | -------------------------------------------------------------------------------- /bench/node_48.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_48 benchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include 9 | 10 | using namespace art; 11 | using picobench::state; 12 | 13 | PICOBENCH_SUITE("node_48"); 14 | 15 | static void node_48_constructor(state &s) { 16 | for (auto i __attribute__((unused)) : s) { 17 | node_48 n; 18 | } 19 | } 20 | PICOBENCH(node_48_constructor); 21 | 22 | static void node_48_find_child(state &s) { 23 | node_48 n; 24 | leaf_node child(nullptr); 25 | for (int i = 0; i < 48; ++i) { 26 | n.set_child(std::floor(5.4468 * i) - 128, &child); 27 | } 28 | for (auto i __attribute__((unused)) : s) { 29 | auto child = n.find_child(std::floor(5.4468 * (rand() % 48)) - 128); 30 | } 31 | } 32 | PICOBENCH(node_48_find_child); 33 | 34 | static void node_48_set_child(state &s) { 35 | node_48 *n = new node_48(); 36 | leaf_node child(nullptr); 37 | for (auto i __attribute__((unused)) : s) { 38 | if (n->is_full()) { 39 | delete n; 40 | n = new node_48(); 41 | } 42 | n->set_child((rand() % 256) - 128, &child); 43 | } 44 | } 45 | PICOBENCH(node_48_set_child); 46 | 47 | static void node_48_grow(state &s) { 48 | for (auto i __attribute__((unused)) : s) { 49 | node_48 *n = new node_48(); 50 | node *new_n = n->grow(); 51 | delete new_n; 52 | } 53 | } 54 | PICOBENCH(node_48_grow); 55 | 56 | static void node_48_next_partial_key(state &s) { 57 | node_48 n; 58 | leaf_node child(nullptr); 59 | for (int i = 0; i < 48; ++i) { 60 | n.set_child(std::floor(5.4468 * i) - 128, &child); 61 | } 62 | for (auto i __attribute__((unused)) : s) { 63 | auto next_partial_key = n.next_partial_key((rand() % 256) - 128); 64 | } 65 | } 66 | PICOBENCH(node_48_next_partial_key); 67 | 68 | static void node_48_prev_partial_key(state &s) { 69 | node_48 n; 70 | leaf_node child(nullptr); 71 | for (int i = 0; i < 48; ++i) { 72 | n.set_child(std::floor(5.4468 * i) - 128, &child); 73 | } 74 | for (auto i __attribute__((unused)) : s) { 75 | auto prev_partial_key = n.prev_partial_key((rand() % 256) - 128); 76 | } 77 | } 78 | PICOBENCH(node_48_prev_partial_key); 79 | -------------------------------------------------------------------------------- /bench/query_iteration.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file query iteration microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include "zipf.hpp" 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using picobench::state; 15 | using std::string; 16 | using std::to_string; 17 | using std::hash; 18 | using std::mt19937_64; 19 | using std::map; 20 | using std::unordered_map; 21 | 22 | PICOBENCH_SUITE("query iteration"); 23 | 24 | static void full_scan_zipf(state &s) { 25 | art::art m; 26 | hash h; 27 | int v = 1; 28 | int *v_ptr = &v; 29 | art::tree_it it, it_end; 30 | fast_zipf rng(1000000); 31 | for (int i = 0; i < 100000; ++i) { 32 | m.set(to_string(h(rng())).c_str(), v_ptr); 33 | } 34 | for (auto i __attribute__((unused)) : s) { 35 | for (it = m.begin(), it_end = m.end(); it != it_end; ++it) {} 36 | } 37 | } 38 | PICOBENCH(full_scan_zipf).iterations({1000}); 39 | 40 | static void full_scan_uniform(state &s) { 41 | art::art m; 42 | hash h; 43 | int v = 1; 44 | int *v_ptr = &v; 45 | art::tree_it it, it_end; 46 | mt19937_64 rng(0); 47 | for (int i = 0; i < 100000; ++i) { 48 | m.set(to_string(h(rng())).c_str(), v_ptr); 49 | } 50 | for (auto i __attribute__((unused)) : s) { 51 | for (it = m.begin(), it_end = m.end(); it != it_end; ++it) {} 52 | } 53 | } 54 | PICOBENCH(full_scan_uniform).iterations({1000}); -------------------------------------------------------------------------------- /bench/query_sparse_uniform.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file query microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using picobench::state; 17 | using std::string; 18 | using std::to_string; 19 | using std::hash; 20 | using std::mt19937_64; 21 | using std::map; 22 | using std::unordered_map; 23 | 24 | PICOBENCH_SUITE("query sparse uniform"); 25 | 26 | static void art_q_s_u(state &s) { 27 | art::art m; 28 | hash h; 29 | int v = 1; 30 | int *v_ptr = &v; 31 | mt19937_64 rng1(0); 32 | for (auto i __attribute__((unused)) : s) { 33 | m.set(to_string(h(rng1())).c_str(), v_ptr); 34 | } 35 | mt19937_64 rng2(0); 36 | for (auto i __attribute__((unused)) : s) { 37 | v_ptr = m.get(to_string(h(rng2())).c_str()); 38 | } 39 | } 40 | PICOBENCH(art_q_s_u) 41 | /* .iterations({4000000}) */ 42 | ; 43 | 44 | static void red_black_q_s_u(state &s) { 45 | map m; 46 | hash h; 47 | int v = 1; 48 | mt19937_64 rng1(0); 49 | for (auto i __attribute__((unused)) : s) { 50 | m[to_string(h(rng1()))] = v; 51 | } 52 | mt19937_64 rng2(0); 53 | for (auto i __attribute__((unused)) : s) { 54 | v = m[to_string(h(rng2()))]; 55 | } 56 | } 57 | PICOBENCH(red_black_q_s_u) 58 | /* .iterations({4000000}) */ 59 | ; 60 | 61 | static void hashmap_q_s_u(state &s) { 62 | unordered_map m; 63 | hash h; 64 | int v = 1; 65 | mt19937_64 rng1(0); 66 | for (auto i __attribute__((unused)) : s) { 67 | m[to_string(h(rng1()))] = v; 68 | } 69 | mt19937_64 rng2(0); 70 | for (auto i __attribute__((unused)) : s) { 71 | v = m[to_string(h(rng2()))]; 72 | } 73 | } 74 | PICOBENCH(hashmap_q_s_u) 75 | /* .iterations({4000000}) */ 76 | ; 77 | -------------------------------------------------------------------------------- /bench/query_sparse_uniform_64.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file query microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using picobench::state; 18 | using std::hash; 19 | using std::map; 20 | using std::mt19937_64; 21 | using std::string; 22 | using std::to_string; 23 | using std::unordered_map; 24 | 25 | PICOBENCH_SUITE("query sparse uniform base 64 keys"); 26 | 27 | std::string to_base64(const std::string& string_); 28 | 29 | static void art_q_s_u(state &s) { 30 | art::art m; 31 | hash h; 32 | int v = 1; 33 | int *v_ptr = &v; 34 | mt19937_64 rng1(0); 35 | for (auto i __attribute__((unused)) : s) { 36 | m.set(to_base64(to_string(h(rng1()))).c_str(), v_ptr); 37 | } 38 | mt19937_64 rng2(0); 39 | for (auto i __attribute__((unused)) : s) { 40 | v_ptr = m.get(to_base64(to_string(h(rng2()))).c_str()); 41 | } 42 | } 43 | PICOBENCH(art_q_s_u) 44 | /* .iterations({4000000}) */ 45 | ; 46 | 47 | static void red_black_q_s_u(state &s) { 48 | map m; 49 | hash h; 50 | int v = 1; 51 | mt19937_64 rng1(0); 52 | for (auto i __attribute__((unused)) : s) { 53 | m[to_base64(to_string(h(rng1())))] = v; 54 | } 55 | mt19937_64 rng2(0); 56 | for (auto i __attribute__((unused)) : s) { 57 | v = m[to_base64(to_string(h(rng2())))]; 58 | } 59 | } 60 | PICOBENCH(red_black_q_s_u) 61 | /* .iterations({4000000}) */ 62 | ; 63 | 64 | static void hashmap_q_s_u(state &s) { 65 | unordered_map m; 66 | hash h; 67 | int v = 1; 68 | mt19937_64 rng1(0); 69 | for (auto i __attribute__((unused)) : s) { 70 | m[to_base64(to_string(h(rng1())))] = v; 71 | } 72 | mt19937_64 rng2(0); 73 | for (auto i __attribute__((unused)) : s) { 74 | v = m[to_base64(to_string(h(rng2())))]; 75 | } 76 | } 77 | PICOBENCH(hashmap_q_s_u) 78 | /* .iterations({4000000}) */ 79 | ; 80 | 81 | static const std::string base64_chars = 82 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 83 | "abcdefghijklmnopqrstuvwxyz" 84 | "0123456789+/"; 85 | 86 | std::string to_base64(const std::string& string_) { 87 | const char *bytes_to_encode = string_.c_str(); 88 | int in_len = string_.length(); 89 | std::string ret; 90 | int i = 0; 91 | int j = 0; 92 | unsigned char char_array_3[3]; 93 | unsigned char char_array_4[4]; 94 | 95 | while (in_len--) { 96 | char_array_3[i++] = *(bytes_to_encode++); 97 | if (i == 3) { 98 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 99 | char_array_4[1] = 100 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 101 | char_array_4[2] = 102 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 103 | char_array_4[3] = char_array_3[2] & 0x3f; 104 | 105 | for (i = 0; (i < 4); i++) 106 | ret += base64_chars[char_array_4[i]]; 107 | i = 0; 108 | } 109 | } 110 | 111 | if (i) { 112 | for (j = i; j < 3; j++) 113 | char_array_3[j] = '\0'; 114 | 115 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 116 | char_array_4[1] = 117 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 118 | char_array_4[2] = 119 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 120 | 121 | for (j = 0; (j < i + 1); j++) 122 | ret += base64_chars[char_array_4[j]]; 123 | 124 | while ((i++ < 3)) 125 | ret += '='; 126 | } 127 | 128 | return ret; 129 | } 130 | -------------------------------------------------------------------------------- /bench/query_sparse_zipf.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file query microbenchmarks 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "picobench/picobench.hpp" 8 | #include "zipf.hpp" 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | using picobench::state; 16 | using std::string; 17 | using std::to_string; 18 | using std::hash; 19 | using std::mt19937_64; 20 | using std::map; 21 | using std::unordered_map; 22 | 23 | PICOBENCH_SUITE("query sparse zipf"); 24 | 25 | static void art_q_s_z(state &s) { 26 | art::art m; 27 | hash h; 28 | int v = 1; 29 | int *v_ptr = &v; 30 | fast_zipf rng1(1000000); 31 | for (auto i __attribute__((unused)) : s) { 32 | m.set(to_string(h(rng1())).c_str(), v_ptr); 33 | } 34 | fast_zipf rng2(1000000); 35 | for (auto i __attribute__((unused)) : s) { 36 | v_ptr = m.get(to_string(h(rng2())).c_str()); 37 | } 38 | } 39 | PICOBENCH(art_q_s_z); 40 | 41 | static void red_black_q_s_z(state &s) { 42 | map m; 43 | hash h; 44 | int v = 1; 45 | fast_zipf rng1(1000000); 46 | for (auto i __attribute__((unused)) : s) { 47 | m[to_string(h(rng1()))] = v; 48 | } 49 | fast_zipf rng2(1000000); 50 | for (auto i __attribute__((unused)) : s) { 51 | v = m[to_string(h(rng2()))]; 52 | } 53 | } 54 | PICOBENCH(red_black_q_s_z); 55 | 56 | static void hashmap_q_s_z(state &s) { 57 | unordered_map m; 58 | hash h; 59 | int v = 1; 60 | fast_zipf rng1(1000000); 61 | for (auto i __attribute__((unused)) : s) { 62 | m[to_string(h(rng1()))] = v; 63 | } 64 | fast_zipf rng2(1000000); 65 | for (auto i __attribute__((unused)) : s) { 66 | v = m[to_string(h(rng2()))]; 67 | } 68 | } 69 | PICOBENCH(hashmap_q_s_z); 70 | -------------------------------------------------------------------------------- /include/art.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file adaptive radix tree 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_HPP 7 | #define ART_HPP 8 | 9 | #include "art/art.hpp" 10 | #include "art/child_it.hpp" 11 | #include "art/inner_node.hpp" 12 | #include "art/leaf_node.hpp" 13 | #include "art/node.hpp" 14 | #include "art/node_16.hpp" 15 | #include "art/node_256.hpp" 16 | #include "art/node_4.hpp" 17 | #include "art/node_48.hpp" 18 | #include "art/tree_it.hpp" 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /include/art/art.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file adaptive radix tree 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_ART_HPP 7 | #define ART_ART_HPP 8 | 9 | #include "leaf_node.hpp" 10 | #include "inner_node.hpp" 11 | #include "node.hpp" 12 | #include "node_4.hpp" 13 | #include "tree_it.hpp" 14 | #include 15 | #include 16 | #include 17 | 18 | namespace art { 19 | 20 | template class art { 21 | public: 22 | ~art(); 23 | 24 | /** 25 | * Finds the value associated with the given key. 26 | * 27 | * @param key - The key to find. 28 | * @return the value associated with the key or a default constructed value. 29 | */ 30 | T get(const char *key) const; 31 | 32 | /** 33 | * Associates the given key with the given value. 34 | * If another value is already associated with the given key, 35 | * since the method consumer is the resource owner. 36 | * 37 | * @param key - The key to associate with the value. 38 | * @param value - The value to be associated with the key. 39 | * @return a nullptr if no other value is associated with they or the 40 | * previously associated value. 41 | */ 42 | T set(const char *key, T value); 43 | 44 | /** 45 | * Deletes the given key and returns it's associated value. 46 | * The associated value is returned, 47 | * since the method consumer is the resource owner. 48 | * If no value is associated with the given key, nullptr is returned. 49 | * 50 | * @param key - The key to delete. 51 | * @return the values assciated with they key or a nullptr otherwise. 52 | */ 53 | T del(const char *key); 54 | 55 | /** 56 | * Forward iterator that traverses the tree in lexicographic order. 57 | */ 58 | tree_it begin(); 59 | 60 | /** 61 | * Forward iterator that traverses the tree in lexicographic order starting 62 | * from the provided key. 63 | */ 64 | tree_it begin(const char *key); 65 | 66 | /** 67 | * Iterator to the end of the lexicographic order. 68 | */ 69 | tree_it end(); 70 | 71 | private: 72 | node *root_ = nullptr; 73 | }; 74 | 75 | template art::~art() { 76 | if (root_ == nullptr) { 77 | return; 78 | } 79 | std::stack *> node_stack; 80 | node_stack.push(root_); 81 | node *cur; 82 | inner_node *cur_inner; 83 | child_it it, it_end; 84 | while (!node_stack.empty()) { 85 | cur = node_stack.top(); 86 | node_stack.pop(); 87 | if (!cur->is_leaf()) { 88 | cur_inner = static_cast*>(cur); 89 | for (it = cur_inner->begin(), it_end = cur_inner->end(); it != it_end; ++it) { 90 | node_stack.push(*cur_inner->find_child(*it)); 91 | } 92 | } 93 | if (cur->prefix_ != nullptr) { 94 | delete[] cur->prefix_; 95 | } 96 | delete cur; 97 | } 98 | } 99 | 100 | template 101 | T art::get(const char *key) const { 102 | node *cur = root_, **child; 103 | int depth = 0, key_len = std::strlen(key) + 1; 104 | while (cur != nullptr) { 105 | if (cur->prefix_len_ != cur->check_prefix(key + depth, key_len - depth)) { 106 | /* prefix mismatch */ 107 | return T{}; 108 | } 109 | if (cur->prefix_len_ == key_len - depth) { 110 | /* exact match */ 111 | return cur->is_leaf() ? static_cast*>(cur)->value_ : T{}; 112 | } 113 | child = static_cast*>(cur)->find_child(key[depth + cur->prefix_len_]); 114 | depth += (cur->prefix_len_ + 1); 115 | cur = child != nullptr ? *child : nullptr; 116 | } 117 | return T{}; 118 | } 119 | 120 | template 121 | T art::set(const char *key, T value) { 122 | int key_len = std::strlen(key) + 1, depth = 0, prefix_match_len; 123 | if (root_ == nullptr) { 124 | root_ = new leaf_node(value); 125 | root_->prefix_ = new char[key_len]; 126 | std::copy(key, key + key_len + 1, root_->prefix_); 127 | root_->prefix_len_ = key_len; 128 | return T{}; 129 | } 130 | 131 | node **cur = &root_, **child; 132 | inner_node **cur_inner; 133 | char child_partial_key; 134 | bool is_prefix_match; 135 | 136 | while (true) { 137 | /* number of bytes of the current node's prefix that match the key */ 138 | prefix_match_len = (**cur).check_prefix(key + depth, key_len - depth); 139 | 140 | /* true if the current node's prefix matches with a part of the key */ 141 | is_prefix_match = (std::min((**cur).prefix_len_, key_len - depth)) == 142 | prefix_match_len; 143 | 144 | if (is_prefix_match && (**cur).prefix_len_ == key_len - depth) { 145 | /* exact match: 146 | * => "replace" 147 | * => replace value of current node. 148 | * => return old value to caller to handle. 149 | * _ _ 150 | * | | 151 | * (aa) (aa) 152 | * a / \ b +[aaaaa,v3] a / \ b 153 | * / \ ==========> / \ 154 | * *(aa)->v1 ()->v2 *(aa)->v3 ()->v2 155 | * 156 | */ 157 | 158 | /* cur must be a leaf */ 159 | auto cur_leaf = static_cast*>(*cur); 160 | T old_value = cur_leaf->value_; 161 | cur_leaf->value_ = value; 162 | return old_value; 163 | } 164 | 165 | if (!is_prefix_match) { 166 | /* prefix mismatch: 167 | * => new parent node with common prefix and no associated value. 168 | * => new node with value to insert. 169 | * => current and new node become children of new parent node. 170 | * 171 | * | | 172 | * *(aa) +(a)->Ø 173 | * a / \ b +[ab,v3] a / \ b 174 | * / \ =======> / \ 175 | * (aa)->v1 ()->v2 *()->Ø +()->v3 176 | * a / \ b 177 | * / \ 178 | * (aa)->v1 ()->v2 179 | * /|\ /|\ 180 | */ 181 | 182 | auto new_parent = new node_4(); 183 | new_parent->prefix_ = new char[prefix_match_len]; 184 | std::copy((**cur).prefix_, (**cur).prefix_ + prefix_match_len, 185 | new_parent->prefix_); 186 | new_parent->prefix_len_ = prefix_match_len; 187 | new_parent->set_child((**cur).prefix_[prefix_match_len], *cur); 188 | 189 | // TODO(rafaelkallis): shrink? 190 | /* memmove((**cur).prefix_, (**cur).prefix_ + prefix_match_len + 1, */ 191 | /* (**cur).prefix_len_ - prefix_match_len - 1); */ 192 | /* (**cur).prefix_len_ -= prefix_match_len + 1; */ 193 | 194 | auto old_prefix = (**cur).prefix_; 195 | auto old_prefix_len = (**cur).prefix_len_; 196 | (**cur).prefix_ = new char[old_prefix_len - prefix_match_len - 1]; 197 | (**cur).prefix_len_ = old_prefix_len - prefix_match_len - 1; 198 | std::copy(old_prefix + prefix_match_len + 1, old_prefix + old_prefix_len, 199 | (**cur).prefix_); 200 | delete old_prefix; 201 | 202 | auto new_node = new leaf_node(value); 203 | new_node->prefix_ = new char[key_len - depth - prefix_match_len - 1]; 204 | std::copy(key + depth + prefix_match_len + 1, key + key_len, 205 | new_node->prefix_); 206 | new_node->prefix_len_ = key_len - depth - prefix_match_len - 1; 207 | new_parent->set_child(key[depth + prefix_match_len], new_node); 208 | 209 | *cur = new_parent; 210 | return T{}; 211 | } 212 | 213 | /* must be inner node */ 214 | cur_inner = reinterpret_cast**>(cur); 215 | child_partial_key = key[depth + (**cur).prefix_len_]; 216 | child = (**cur_inner).find_child(child_partial_key); 217 | 218 | if (child == nullptr) { 219 | /* 220 | * no child associated with the next partial key. 221 | * => create new node with value to insert. 222 | * => new node becomes current node's child. 223 | * 224 | * *(aa)->Ø *(aa)->Ø 225 | * a / +[aab,v2] a / \ b 226 | * / ========> / \ 227 | * (a)->v1 (a)->v1 +()->v2 228 | */ 229 | 230 | if ((**cur_inner).is_full()) { 231 | *cur_inner = (**cur_inner).grow(); 232 | } 233 | 234 | auto new_node = new leaf_node(value); 235 | new_node->prefix_ = new char[key_len - depth - (**cur).prefix_len_ - 1]; 236 | std::copy(key + depth + (**cur).prefix_len_ + 1, key + key_len, 237 | new_node->prefix_); 238 | new_node->prefix_len_ = key_len - depth - (**cur).prefix_len_ - 1; 239 | (**cur_inner).set_child(child_partial_key, new_node); 240 | return T{}; 241 | } 242 | 243 | /* propagate down and repeat: 244 | * 245 | * *(aa)->Ø (aa)->Ø 246 | * a / \ b +[aaba,v3] a / \ b repeat 247 | * / \ =========> / \ ========> ... 248 | * (a)->v1 ()->v2 (a)->v1 *()->v2 249 | */ 250 | 251 | depth += (**cur).prefix_len_ + 1; 252 | cur = child; 253 | } 254 | } 255 | 256 | template 257 | T art::del(const char *key) { 258 | int depth = 0, key_len = std::strlen(key) + 1; 259 | 260 | if (root_ == nullptr) { 261 | return T{}; 262 | } 263 | 264 | /* pointer to parent, current and child node */ 265 | node **cur = &root_; 266 | inner_node **par = nullptr; 267 | 268 | /* partial key of current and child node */ 269 | char cur_partial_key = 0; 270 | 271 | while (cur != nullptr) { 272 | if ((**cur).prefix_len_ != 273 | (**cur).check_prefix(key + depth, key_len - depth)) { 274 | /* prefix mismatch => key doesn't exist */ 275 | 276 | return T{}; 277 | } 278 | 279 | if (key_len == depth + (**cur).prefix_len_) { 280 | /* exact match */ 281 | if (!(**cur).is_leaf()) { 282 | return T{}; 283 | } 284 | auto value = static_cast*>(*cur)->value_; 285 | auto n_siblings = par != nullptr ? (**par).n_children() - 1 : 0; 286 | 287 | if (n_siblings == 0) { 288 | /* 289 | * => must be root node 290 | * => delete root node 291 | * 292 | * | | 293 | * (aa)->v1 (aa)->v1 294 | * | a -[aaaaa] 295 | * | =======> 296 | * *(aa)->v2 297 | */ 298 | 299 | if ((**cur).prefix_ != nullptr) { 300 | delete[](**cur).prefix_; 301 | } 302 | delete (*cur); 303 | *cur = nullptr; 304 | 305 | } else if (n_siblings == 1) { 306 | /* => delete leaf node 307 | * => replace parent with sibling 308 | * 309 | * |a |a 310 | * | | 311 | * (aa) -"aaaaabaa" | 312 | * a / \ b ==========> / 313 | * / \ / 314 | * (aa)->v1 *()->v2 (aaaaa)->v1 315 | * /|\ /|\ 316 | */ 317 | 318 | /* find sibling */ 319 | auto sibling_partial_key = (**par).next_partial_key(0); 320 | if (sibling_partial_key == cur_partial_key) { 321 | sibling_partial_key = (**par).next_partial_key(cur_partial_key + 1); 322 | } 323 | auto sibling = *(**par).find_child(sibling_partial_key); 324 | 325 | auto old_prefix = sibling->prefix_; 326 | auto old_prefix_len = sibling->prefix_len_; 327 | 328 | sibling->prefix_ = new char[(**par).prefix_len_ + 1 + old_prefix_len]; 329 | sibling->prefix_len_ = (**par).prefix_len_ + 1 + old_prefix_len; 330 | std::copy((**par).prefix_, (**par).prefix_ + (**par).prefix_len_, 331 | sibling->prefix_); 332 | sibling->prefix_[(**par).prefix_len_] = sibling_partial_key; 333 | std::copy(old_prefix, old_prefix + old_prefix_len, 334 | sibling->prefix_ + (**par).prefix_len_ + 1); 335 | if (old_prefix != nullptr) { 336 | delete[] old_prefix; 337 | } 338 | if ((**cur).prefix_ != nullptr) { 339 | delete[](**cur).prefix_; 340 | } 341 | delete (*cur); 342 | if ((**par).prefix_ != nullptr) { 343 | delete[](**par).prefix_; 344 | } 345 | delete (*par); 346 | 347 | /* this looks crazy, but I know what I'm doing */ 348 | *par = static_cast*>(sibling); 349 | 350 | } else /* if (n_siblings > 1) */ { 351 | /* => delete leaf node 352 | * 353 | * |a |a 354 | * | | 355 | * (aa) -"aaaaabaa" (aa) 356 | * a / | \ b ==========> a / | 357 | * / | \ / | 358 | * *()->v1 359 | */ 360 | 361 | if ((**cur).prefix_ != nullptr) { 362 | delete[](**cur).prefix_; 363 | } 364 | delete (*cur); 365 | (**par).del_child(cur_partial_key); 366 | if ((**par).is_underfull()) { 367 | *par = (**par).shrink(); 368 | } 369 | } 370 | 371 | return value; 372 | } 373 | 374 | /* propagate down and repeat */ 375 | cur_partial_key = key[depth + (**cur).prefix_len_]; 376 | depth += (**cur).prefix_len_ + 1; 377 | par = reinterpret_cast**>(cur); 378 | cur = (**par).find_child(cur_partial_key); 379 | } 380 | return T{}; 381 | } 382 | 383 | template tree_it art::begin() { 384 | return tree_it::min(this->root_); 385 | } 386 | 387 | template tree_it art::begin(const char *key) { 388 | return tree_it::greater_equal(this->root_, key); 389 | } 390 | 391 | template tree_it art::end() { 392 | return tree_it(); 393 | } 394 | 395 | } // namespace art 396 | 397 | #endif 398 | -------------------------------------------------------------------------------- /include/art/child_it.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file child iterator 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_CHILD_IT_HPP 7 | #define ART_CHILD_IT_HPP 8 | 9 | #include 10 | 11 | namespace art { 12 | 13 | template class inner_node; 14 | 15 | template class child_it { 16 | public: 17 | child_it() = default; 18 | explicit child_it(inner_node *n); 19 | child_it(inner_node *n, int relative_index); 20 | child_it(const child_it &other) = default; 21 | child_it(child_it &&other) noexcept = default; 22 | child_it &operator=(const child_it &other) = default; 23 | child_it &operator=(child_it &&other) noexcept = default; 24 | 25 | 26 | using iterator_category = std::bidirectional_iterator_tag; 27 | using value_type = const char; 28 | using difference_type = int; 29 | using pointer = value_type *; 30 | using reference = value_type &; 31 | 32 | reference operator*() const; 33 | pointer operator->() const; 34 | child_it &operator++(); 35 | child_it operator++(int); 36 | child_it &operator--(); 37 | child_it operator--(int); 38 | bool operator==(const child_it &rhs) const; 39 | bool operator!=(const child_it &rhs) const; 40 | bool operator<(const child_it &rhs) const; 41 | bool operator>(const child_it &rhs) const; 42 | bool operator<=(const child_it &rhs) const; 43 | bool operator>=(const child_it &rhs) const; 44 | 45 | char get_partial_key() const; 46 | node *get_child_node() const; 47 | 48 | private: 49 | inner_node *node_ = nullptr; 50 | char cur_partial_key_ = -128; 51 | int relative_index_ = 0; 52 | }; 53 | 54 | template child_it::child_it(inner_node *n) : child_it(n, 0) {} 55 | 56 | template 57 | child_it::child_it(inner_node *n, int relative_index) 58 | : node_(n), cur_partial_key_(0), relative_index_(relative_index) { 59 | if (relative_index_ < 0) { 60 | /* relative_index is out of bounds, no seek */ 61 | return; 62 | } 63 | 64 | if (relative_index_ >= node_->n_children()) { 65 | /* relative_index is out of bounds, no seek */ 66 | return; 67 | } 68 | 69 | if (relative_index_ == node_->n_children() - 1) { 70 | cur_partial_key_ = node_->prev_partial_key(127); 71 | return; 72 | } 73 | 74 | cur_partial_key_ = node_->next_partial_key(-128); 75 | for (int i = 0; i < relative_index_; ++i) { 76 | cur_partial_key_ = node_->next_partial_key(cur_partial_key_ + 1); 77 | } 78 | } 79 | 80 | template 81 | typename child_it::reference child_it::operator*() const { 82 | if (relative_index_ < 0 || relative_index_ >= node_->n_children()) { 83 | throw std::out_of_range("child iterator is out of range"); 84 | } 85 | 86 | return cur_partial_key_; 87 | } 88 | 89 | template 90 | typename child_it::pointer child_it::operator->() const { 91 | if (relative_index_ < 0 || relative_index_ >= node_->n_children()) { 92 | throw std::out_of_range("child iterator is out of range"); 93 | } 94 | 95 | return &cur_partial_key_; 96 | } 97 | 98 | template child_it &child_it::operator++() { 99 | ++relative_index_; 100 | if (relative_index_ < 0) { 101 | return *this; 102 | } else if (relative_index_ == 0) { 103 | cur_partial_key_ = node_->next_partial_key(-128); 104 | } else if (relative_index_ < node_->n_children()) { 105 | cur_partial_key_ = node_->next_partial_key(cur_partial_key_ + 1); 106 | } 107 | return *this; 108 | } 109 | 110 | template child_it child_it::operator++(int) { 111 | auto old = *this; 112 | operator++(); 113 | return old; 114 | } 115 | 116 | template child_it &child_it::operator--() { 117 | --relative_index_; 118 | if (relative_index_ > node_->n_children() - 1) { 119 | return *this; 120 | } else if (relative_index_ == node_->n_children() - 1) { 121 | cur_partial_key_ = node_->prev_partial_key(127); 122 | } else if (relative_index_ >= 0) { 123 | cur_partial_key_ = node_->prev_partial_key(cur_partial_key_ - 1); 124 | } 125 | return *this; 126 | } 127 | 128 | template child_it child_it::operator--(int) { 129 | auto old = *this; 130 | operator--(); 131 | return old; 132 | } 133 | 134 | template bool child_it::operator==(const child_it &rhs) const { 135 | return node_ == rhs.node_ && relative_index_ == rhs.relative_index_; 136 | } 137 | 138 | template bool child_it::operator<(const child_it &rhs) const { 139 | return node_ == rhs.node_ && relative_index_ < rhs.relative_index_; 140 | } 141 | 142 | template bool child_it::operator!=(const child_it &rhs) const { 143 | return !((*this) == rhs); 144 | } 145 | 146 | template bool child_it::operator>=(const child_it &rhs) const { 147 | return !((*this) < rhs); 148 | } 149 | 150 | template bool child_it::operator<=(const child_it &rhs) const { 151 | return (rhs >= (*this)); 152 | } 153 | 154 | template bool child_it::operator>(const child_it &rhs) const { 155 | return (rhs < (*this)); 156 | } 157 | 158 | template 159 | char child_it::get_partial_key() const { 160 | return cur_partial_key_; 161 | } 162 | 163 | template 164 | node *child_it::get_child_node() const { 165 | assert(0 <= relative_index_ && relative_index_ < node_->n_children()); 166 | return *node_->find_child(cur_partial_key_); 167 | } 168 | 169 | } // namespace art 170 | 171 | #endif 172 | -------------------------------------------------------------------------------- /include/art/inner_node.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file inner_node header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_INNER_NODE_HPP 7 | #define ART_INNER_NODE_HPP 8 | 9 | #include "child_it.hpp" 10 | #include "leaf_node.hpp" 11 | #include "node.hpp" 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace art { 21 | 22 | template class inner_node : public node { 23 | public: 24 | inner_node() = default; 25 | inner_node(const inner_node &other) = default; 26 | inner_node(inner_node &&other) noexcept = default; 27 | inner_node &operator=(const inner_node &other) = default; 28 | inner_node &operator=(inner_node &&other) noexcept = default; 29 | virtual ~inner_node() = default; 30 | 31 | bool is_leaf() const override; 32 | 33 | /** 34 | * Finds and returns the child node identified by the given partial key. 35 | * 36 | * @param partial_key - The partial key associated with the child. 37 | * @return Child node identified by the given partial key or 38 | * a null pointer of no child node is associated with the partial key. 39 | */ 40 | virtual node **find_child(char partial_key) = 0; 41 | 42 | /** 43 | * Adds the given node to the node's children. 44 | * No bounds checking is done. 45 | * If a child already exists under the given partial key, the child 46 | * is overwritten without deleting it. 47 | * 48 | * @pre Node should not be full. 49 | * @param partial_key - The partial key associated with the child. 50 | * @param child - The child node. 51 | */ 52 | virtual void set_child(char partial_key, node *child) = 0; 53 | 54 | /** 55 | * Deletes the child associated with the given partial key. 56 | * 57 | * @param partial_key - The partial key associated with the child. 58 | */ 59 | virtual node *del_child(char partial_key) = 0; 60 | 61 | /** 62 | * Creates and returns a new node with bigger children capacity. 63 | * The current node gets deleted. 64 | * 65 | * @return node with bigger capacity 66 | */ 67 | virtual inner_node *grow() = 0; 68 | 69 | /** 70 | * Creates and returns a new node with lesser children capacity. 71 | * The current node gets deleted. 72 | * 73 | * @pre node must be undefull 74 | * @return node with lesser capacity 75 | */ 76 | virtual inner_node *shrink() = 0; 77 | 78 | /** 79 | * Determines if the node is full, i.e. can carry no more child nodes. 80 | */ 81 | virtual bool is_full() const = 0; 82 | 83 | /** 84 | * Determines if the node is underfull, i.e. carries less child nodes than 85 | * intended. 86 | */ 87 | virtual bool is_underfull() const = 0; 88 | 89 | virtual int n_children() const = 0; 90 | 91 | virtual char next_partial_key(char partial_key) const = 0; 92 | 93 | virtual char prev_partial_key(char partial_key) const = 0; 94 | 95 | /** 96 | * Iterator on the first child node. 97 | * 98 | * @return Iterator on the first child node. 99 | */ 100 | child_it begin(); 101 | std::reverse_iterator> rbegin(); 102 | 103 | /** 104 | * Iterator on after the last child node. 105 | * 106 | * @return Iterator on after the last child node. 107 | */ 108 | child_it end(); 109 | std::reverse_iterator> rend(); 110 | }; 111 | 112 | template bool inner_node::is_leaf() const { return false; } 113 | 114 | template child_it inner_node::begin() { 115 | return child_it(this); 116 | } 117 | 118 | template std::reverse_iterator> inner_node::rbegin() { 119 | return std::reverse_iterator>(end()); 120 | } 121 | 122 | template child_it inner_node::end() { 123 | return child_it(this, n_children()); 124 | } 125 | 126 | template std::reverse_iterator> inner_node::rend() { 127 | return std::reverse_iterator>(begin()); 128 | } 129 | 130 | } // namespace art 131 | 132 | #endif 133 | -------------------------------------------------------------------------------- /include/art/leaf_node.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file leaf_node header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_LEAF_NODE_HPP 7 | #define ART_LEAF_NODE_HPP 8 | 9 | #include "node.hpp" 10 | 11 | namespace art { 12 | 13 | template class art; 14 | 15 | template class leaf_node : public node { 16 | public: 17 | explicit leaf_node(T value); 18 | bool is_leaf() const override; 19 | 20 | T value_; 21 | }; 22 | 23 | template 24 | leaf_node::leaf_node(T value): value_(value) {} 25 | 26 | template 27 | bool leaf_node::is_leaf() const { return true; } 28 | 29 | } // namespace art 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/art/node.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file trie nodes header. 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_NODE_HPP 7 | #define ART_NODE_HPP 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace art { 18 | 19 | template class node { 20 | public: 21 | virtual ~node() = default; 22 | 23 | node() = default; 24 | node(const node &other) = default; 25 | node(node &&other) noexcept = default; 26 | node &operator=(const node &other) = default; 27 | node &operator=(node &&other) noexcept = default; 28 | 29 | /** 30 | * Determines if this node is a leaf node, i.e., contains a value. 31 | * Needed for downcasting a node instance to a leaf_node or inner_node instance. 32 | */ 33 | virtual bool is_leaf() const = 0; 34 | 35 | /** 36 | * Determines the number of matching bytes between the node's prefix and the key. 37 | * 38 | * Given a node with prefix: "abbbd", a key "abbbccc", 39 | * check_prefix returns 4, since byte 4 of the prefix ('d') does not 40 | * match byte 4 of the key ('c'). 41 | * 42 | * key: "abbbccc" 43 | * prefix: "abbbd" 44 | * ^^^^* 45 | * index: 01234 46 | */ 47 | int check_prefix(const char *key, int key_len) const; 48 | 49 | char *prefix_ = nullptr; 50 | uint16_t prefix_len_ = 0; 51 | }; 52 | 53 | template 54 | int node::check_prefix(const char *key, int /* key_len */) const { 55 | return std::mismatch(prefix_, prefix_ + prefix_len_, key).second - key; 56 | } 57 | 58 | } // namespace art 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/art/node_16.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_16 header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_NODE_16_HPP 7 | #define ART_NODE_16_HPP 8 | 9 | #include "inner_node.hpp" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #if defined(__i386__) || defined(__amd64__) 16 | #include 17 | #endif 18 | 19 | namespace art { 20 | 21 | template class node_4; 22 | template class node_48; 23 | 24 | template class node_16 : public inner_node { 25 | friend class node_4; 26 | friend class node_48; 27 | public: 28 | node **find_child(char partial_key) override; 29 | void set_child(char partial_key, node *child) override; 30 | node *del_child(char partial_key) override; 31 | inner_node *grow() override; 32 | inner_node *shrink() override; 33 | bool is_full() const override; 34 | bool is_underfull() const override; 35 | 36 | char next_partial_key(char partial_key) const override; 37 | 38 | char prev_partial_key(char partial_key) const override; 39 | 40 | int n_children() const override; 41 | 42 | private: 43 | uint8_t n_children_ = 0; 44 | char keys_[16]; 45 | node *children_[16]; 46 | }; 47 | 48 | template node **node_16::find_child(char partial_key) { 49 | #if defined(__i386__) || defined(__amd64__) 50 | int bitfield = 51 | _mm_movemask_epi8(_mm_cmpeq_epi8(_mm_set1_epi8(partial_key), 52 | _mm_loadu_si128((__m128i *)keys_))) & 53 | ((1 << n_children_) - 1); 54 | return (bool)bitfield ? &children_[__builtin_ctz(bitfield)] : nullptr; 55 | #else 56 | int lo, mid, hi; 57 | lo = 0; 58 | hi = n_children_; 59 | while (lo < hi) { 60 | mid = (lo + hi) / 2; 61 | if (partial_key < keys_[mid]) { 62 | hi = mid; 63 | } else if (partial_key > keys_[mid]) { 64 | lo = mid + 1; 65 | } else { 66 | return &children_[mid]; 67 | } 68 | } 69 | return nullptr; 70 | #endif 71 | } 72 | 73 | template 74 | void node_16::set_child(char partial_key, node *child) { 75 | /* determine index for child */ 76 | int child_i; 77 | for (int i = this->n_children_ - 1;; --i) { 78 | if (i >= 0 && partial_key < this->keys_[i]) { 79 | /* move existing sibling to the right */ 80 | this->keys_[i + 1] = this->keys_[i]; 81 | this->children_[i + 1] = this->children_[i]; 82 | } else { 83 | child_i = i + 1; 84 | break; 85 | } 86 | } 87 | 88 | this->keys_[child_i] = partial_key; 89 | this->children_[child_i] = child; 90 | ++n_children_; 91 | } 92 | 93 | template node *node_16::del_child(char partial_key) { 94 | node *child_to_delete = nullptr; 95 | for (int i = 0; i < n_children_; ++i) { 96 | if (child_to_delete == nullptr && partial_key == keys_[i]) { 97 | child_to_delete = children_[i]; 98 | } 99 | if (child_to_delete != nullptr) { 100 | /* move existing sibling to the left */ 101 | keys_[i] = i < n_children_ - 1 ? keys_[i + 1] : 0; 102 | children_[i] = i < n_children_ - 1 ? children_[i + 1] : nullptr; 103 | } 104 | } 105 | if (child_to_delete != nullptr) { 106 | --n_children_; 107 | } 108 | return child_to_delete; 109 | } 110 | 111 | template inner_node *node_16::grow() { 112 | auto new_node = new node_48(); 113 | new_node->prefix_ = this->prefix_; 114 | new_node->prefix_len_ = this->prefix_len_; 115 | new_node->n_children_ = this->n_children_; 116 | std::copy(this->children_, this->children_ + this->n_children_, new_node->children_); 117 | for (int i = 0; i < n_children_; ++i) { 118 | new_node->indexes_[(uint8_t) this->keys_[i]] = i; 119 | } 120 | delete this; 121 | return new_node; 122 | } 123 | 124 | template inner_node *node_16::shrink() { 125 | auto new_node = new node_4(); 126 | new_node->prefix_ = this->prefix_; 127 | new_node->prefix_len_ = this->prefix_len_; 128 | new_node->n_children_ = this->n_children_; 129 | std::copy(this->keys_, this->keys_ + this->n_children_, new_node->keys_); 130 | std::copy(this->children_, this->children_ + this->n_children_, new_node->children_); 131 | delete this; 132 | return new_node; 133 | } 134 | 135 | template bool node_16::is_full() const { 136 | return n_children_ == 16; 137 | } 138 | 139 | template bool node_16::is_underfull() const { 140 | return n_children_ == 4; 141 | } 142 | 143 | template char node_16::next_partial_key(char partial_key) const { 144 | for (int i = 0; i < n_children_; ++i) { 145 | if (keys_[i] >= partial_key) { 146 | return keys_[i]; 147 | } 148 | } 149 | throw std::out_of_range("provided partial key does not have a successor"); 150 | } 151 | 152 | template char node_16::prev_partial_key(char partial_key) const { 153 | for (int i = n_children_ - 1; i >= 0; --i) { 154 | if (keys_[i] <= partial_key) { 155 | return keys_[i]; 156 | } 157 | } 158 | throw std::out_of_range("provided partial key does not have a predecessor"); 159 | } 160 | 161 | template int node_16::n_children() const { return n_children_; } 162 | 163 | } // namespace art 164 | 165 | #endif 166 | -------------------------------------------------------------------------------- /include/art/node_256.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_256 header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_NODE_256_HPP 7 | #define ART_NODE_256_HPP 8 | 9 | #include "inner_node.hpp" 10 | #include 11 | #include 12 | 13 | namespace art { 14 | 15 | template class node_48; 16 | 17 | template class node_256 : public inner_node { 18 | friend class node_48; 19 | public: 20 | node_256(); 21 | 22 | node **find_child(char partial_key) override; 23 | void set_child(char partial_key, node *child) override; 24 | node *del_child(char partial_key) override; 25 | inner_node *grow() override; 26 | inner_node *shrink() override; 27 | bool is_full() const override; 28 | bool is_underfull() const override; 29 | 30 | char next_partial_key(char partial_key) const override; 31 | 32 | char prev_partial_key(char partial_key) const override; 33 | 34 | int n_children() const override; 35 | 36 | private: 37 | uint16_t n_children_ = 0; 38 | std::array *, 256> children_; 39 | }; 40 | 41 | template node_256::node_256() { children_.fill(nullptr); } 42 | 43 | template node **node_256::find_child(char partial_key) { 44 | return children_[128 + partial_key] != nullptr ? &children_[128 + partial_key] 45 | : nullptr; 46 | } 47 | 48 | template 49 | void node_256::set_child(char partial_key, node *child) { 50 | children_[128 + partial_key] = child; 51 | ++n_children_; 52 | } 53 | 54 | template node *node_256::del_child(char partial_key) { 55 | node *child_to_delete = children_[128 + partial_key]; 56 | if (child_to_delete != nullptr) { 57 | children_[128 + partial_key] = nullptr; 58 | --n_children_; 59 | } 60 | return child_to_delete; 61 | } 62 | 63 | template inner_node *node_256::grow() { 64 | throw std::runtime_error("node_256 cannot grow"); 65 | } 66 | 67 | template inner_node *node_256::shrink() { 68 | auto new_node = new node_48(); 69 | new_node->prefix_ = this->prefix_; 70 | new_node->prefix_len_ = this->prefix_len_; 71 | for (int partial_key = 0; partial_key < 256; ++partial_key) { 72 | if (children_[128 + partial_key] != nullptr) { 73 | new_node->set_child(partial_key, children_[128 + partial_key]); 74 | } 75 | } 76 | delete this; 77 | return new_node; 78 | } 79 | 80 | template bool node_256::is_full() const { 81 | return n_children_ == 256; 82 | } 83 | 84 | template bool node_256::is_underfull() const { 85 | return n_children_ == 48; 86 | } 87 | 88 | template char node_256::next_partial_key(char partial_key) const { 89 | while (true) { 90 | if (children_[128 + partial_key] != nullptr) { 91 | return partial_key; 92 | } 93 | if (partial_key == 127) { 94 | throw std::out_of_range("provided partial key does not have a successor"); 95 | } 96 | ++partial_key; 97 | } 98 | } 99 | 100 | template char node_256::prev_partial_key(char partial_key) const { 101 | while (true) { 102 | if (children_[128 + partial_key] != nullptr) { 103 | return partial_key; 104 | } 105 | if (partial_key == -128) { 106 | throw std::out_of_range( 107 | "provided partial key does not have a predecessor"); 108 | } 109 | --partial_key; 110 | } 111 | } 112 | 113 | template int node_256::n_children() const { return n_children_; } 114 | 115 | } // namespace art 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /include/art/node_4.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_4 header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_NODE_4_HPP 7 | #define ART_NODE_4_HPP 8 | 9 | #include "inner_node.hpp" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace art { 17 | 18 | template class node_0; 19 | template class node_16; 20 | 21 | template class node_4 : public inner_node { 22 | friend class node_0; 23 | friend class node_16; 24 | 25 | public: 26 | node **find_child(char partial_key) override; 27 | void set_child(char partial_key, node *child) override; 28 | node *del_child(char partial_key) override; 29 | inner_node *grow() override; 30 | inner_node *shrink() override; 31 | bool is_full() const override; 32 | bool is_underfull() const override; 33 | 34 | char next_partial_key(char partial_key) const override; 35 | 36 | char prev_partial_key(char partial_key) const override; 37 | 38 | int n_children() const override; 39 | 40 | private: 41 | uint8_t n_children_ = 0; 42 | char keys_[4]; 43 | node *children_[4]; 44 | }; 45 | 46 | template node **node_4::find_child(char partial_key) { 47 | for (int i = 0; i < n_children_; ++i) { 48 | if (keys_[i] == partial_key) { 49 | return &children_[i]; 50 | } 51 | } 52 | return nullptr; 53 | } 54 | 55 | template void node_4::set_child(char partial_key, node *child) { 56 | /* determine index for child */ 57 | int c_i; 58 | for (c_i = 0; c_i < n_children_ && partial_key >= keys_[c_i]; ++c_i) { 59 | } 60 | std::memmove(keys_ + c_i + 1, keys_ + c_i, n_children_ - c_i); 61 | std::memmove(children_ + c_i + 1, children_ + c_i, 62 | (n_children_ - c_i) * sizeof(void *)); 63 | 64 | keys_[c_i] = partial_key; 65 | children_[c_i] = child; 66 | ++n_children_; 67 | } 68 | 69 | template node *node_4::del_child(char partial_key) { 70 | node *child_to_delete = nullptr; 71 | for (int i = 0; i < n_children_; ++i) { 72 | if (child_to_delete == nullptr && partial_key == keys_[i]) { 73 | child_to_delete = children_[i]; 74 | } 75 | if (child_to_delete != nullptr) { 76 | /* move existing sibling to the left */ 77 | keys_[i] = i < n_children_ - 1 ? keys_[i + 1] : 0; 78 | children_[i] = i < n_children_ - 1 ? children_[i + 1] : nullptr; 79 | } 80 | } 81 | if (child_to_delete != nullptr) { 82 | --n_children_; 83 | } 84 | return child_to_delete; 85 | } 86 | 87 | template inner_node *node_4::grow() { 88 | auto new_node = new node_16(); 89 | new_node->prefix_ = this->prefix_; 90 | new_node->prefix_len_ = this->prefix_len_; 91 | new_node->n_children_ = this->n_children_; 92 | std::copy(this->keys_, this->keys_ + this->n_children_, new_node->keys_); 93 | std::copy(this->children_, this->children_ + this->n_children_, new_node->children_); 94 | delete this; 95 | return new_node; 96 | } 97 | 98 | template inner_node *node_4::shrink() { 99 | throw std::runtime_error("node_4 cannot shrink"); 100 | } 101 | 102 | template bool node_4::is_full() const { return n_children_ == 4; } 103 | 104 | template bool node_4::is_underfull() const { 105 | return false; 106 | } 107 | 108 | template char node_4::next_partial_key(char partial_key) const { 109 | for (int i = 0; i < n_children_; ++i) { 110 | if (keys_[i] >= partial_key) { 111 | return keys_[i]; 112 | } 113 | } 114 | /* return 0; */ 115 | throw std::out_of_range("provided partial key does not have a successor"); 116 | } 117 | 118 | template char node_4::prev_partial_key(char partial_key) const { 119 | for (int i = n_children_ - 1; i >= 0; --i) { 120 | if (keys_[i] <= partial_key) { 121 | return keys_[i]; 122 | } 123 | } 124 | /* return 255; */ 125 | throw std::out_of_range("provided partial key does not have a predecessor"); 126 | } 127 | 128 | template int node_4::n_children() const { 129 | return this->n_children_; 130 | } 131 | 132 | } // namespace art 133 | 134 | #endif 135 | -------------------------------------------------------------------------------- /include/art/node_48.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_48 header 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_NODE_48_HPP 7 | #define ART_NODE_48_HPP 8 | 9 | #include "inner_node.hpp" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace art { 16 | 17 | template class node_16; 18 | template class node_256; 19 | 20 | template class node_48 : public inner_node { 21 | friend class node_16; 22 | friend class node_256; 23 | 24 | public: 25 | node_48(); 26 | 27 | node **find_child(char partial_key) override; 28 | void set_child(char partial_key, node *child) override; 29 | node *del_child(char partial_key) override; 30 | inner_node *grow() override; 31 | inner_node *shrink() override; 32 | bool is_full() const override; 33 | bool is_underfull() const override; 34 | 35 | char next_partial_key(char partial_key) const override; 36 | char prev_partial_key(char partial_key) const override; 37 | 38 | int n_children() const override; 39 | 40 | private: 41 | static const char EMPTY; 42 | 43 | uint8_t n_children_ = 0; 44 | char indexes_[256]; 45 | node *children_[48]; 46 | }; 47 | 48 | template node_48::node_48() { 49 | std::fill(this->indexes_, this->indexes_ + 256, node_48::EMPTY); 50 | std::fill(this->children_, this->children_ + 48, nullptr); 51 | } 52 | 53 | template node **node_48::find_child(char partial_key) { 54 | // TODO(rafaelkallis): direct lookup instead of temp save? 55 | uint8_t index = indexes_[128 + partial_key]; 56 | return node_48::EMPTY != index ? &children_[index] : nullptr; 57 | } 58 | 59 | template 60 | void node_48::set_child(char partial_key, node *child) { 61 | 62 | // TODO(rafaelkallis): pick random starting entry in order to increase 63 | // performance? i.e. for (int i = random([0,48)); i != (i-1) % 48; i = (i+1) % 64 | // 48){} 65 | 66 | /* find empty child entry */ 67 | for (int i = 0; i < 48; ++i) { 68 | if (children_[i] == nullptr) { 69 | indexes_[128 + partial_key] = (uint8_t) i; 70 | children_[i] = child; 71 | break; 72 | } 73 | } 74 | ++n_children_; 75 | } 76 | 77 | template node *node_48::del_child(char partial_key) { 78 | node *child_to_delete = nullptr; 79 | unsigned char index = indexes_[128 + partial_key]; 80 | if (index != node_48::EMPTY) { 81 | child_to_delete = children_[index]; 82 | indexes_[128 + partial_key] = node_48::EMPTY; 83 | children_[index] = nullptr; 84 | --n_children_; 85 | } 86 | return child_to_delete; 87 | } 88 | 89 | template inner_node *node_48::grow() { 90 | auto new_node = new node_256(); 91 | new_node->prefix_ = this->prefix_; 92 | new_node->prefix_len_ = this->prefix_len_; 93 | uint8_t index; 94 | for (int partial_key = -128; partial_key < 127; ++partial_key) { 95 | index = indexes_[128 + partial_key]; 96 | if (index != node_48::EMPTY) { 97 | new_node->set_child(partial_key, children_[index]); 98 | } 99 | } 100 | delete this; 101 | return new_node; 102 | } 103 | 104 | template inner_node *node_48::shrink() { 105 | auto new_node = new node_16(); 106 | new_node->prefix_ = this->prefix_; 107 | new_node->prefix_len_ = this->prefix_len_; 108 | uint8_t index; 109 | for (int partial_key = -128; partial_key < 127; ++partial_key) { 110 | index = indexes_[128 + partial_key]; 111 | if (index != node_48::EMPTY) { 112 | new_node->set_child(partial_key, children_[index]); 113 | } 114 | } 115 | delete this; 116 | return new_node; 117 | } 118 | 119 | template bool node_48::is_full() const { 120 | return n_children_ == 48; 121 | } 122 | 123 | template bool node_48::is_underfull() const { 124 | return n_children_ == 16; 125 | } 126 | 127 | template const char node_48::EMPTY = 48; 128 | 129 | template char node_48::next_partial_key(char partial_key) const { 130 | while (true) { 131 | if (indexes_[128 + partial_key] != node_48::EMPTY) { 132 | return partial_key; 133 | } 134 | if (partial_key == 127) { 135 | throw std::out_of_range("provided partial key does not have a successor"); 136 | } 137 | ++partial_key; 138 | } 139 | } 140 | 141 | template char node_48::prev_partial_key(char partial_key) const { 142 | while (true) { 143 | if (indexes_[128 + partial_key] != node_48::EMPTY) { 144 | return partial_key; 145 | } 146 | if (partial_key == -128) { 147 | throw std::out_of_range( 148 | "provided partial key does not have a predecessor"); 149 | } 150 | --partial_key; 151 | } 152 | } 153 | 154 | template int node_48::n_children() const { return n_children_; } 155 | 156 | } // namespace art 157 | 158 | #endif 159 | -------------------------------------------------------------------------------- /include/art/tree_it.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file tree iterator 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #ifndef ART_TREE_IT_HPP 7 | #define ART_TREE_IT_HPP 8 | 9 | #include "child_it.hpp" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace art { 18 | 19 | template class node; 20 | template class inner_node; 21 | template class leaf_node; 22 | 23 | template class tree_it { 24 | public: 25 | struct step { 26 | node *child_node_; // no ownership 27 | int depth_; 28 | char *key_; // ownership 29 | child_it child_it_; 30 | child_it child_it_end_; 31 | 32 | step(); 33 | step(int depth, child_it c_it, child_it c_it_end); 34 | step(node *node, int depth, const char *key, child_it c_it, child_it c_it_end); 35 | step(const step &other); 36 | step(step &&other); 37 | ~step(); 38 | 39 | step& operator=(const step &other); 40 | step& operator=(step &&other) noexcept; 41 | 42 | step &operator++(); 43 | step operator++(int); 44 | }; 45 | 46 | tree_it(); 47 | explicit tree_it(node *root, std::vector traversal_stack); 48 | 49 | static tree_it min(node *root); 50 | static tree_it greater_equal(node *root, const char *key); 51 | 52 | using iterator_category = std::forward_iterator_tag; 53 | using value_type = T; 54 | using difference_type = int; 55 | using pointer = value_type *; 56 | /* using reference = const value_type &; */ 57 | 58 | /* reference operator*(); */ 59 | value_type operator*(); 60 | pointer operator->(); 61 | tree_it &operator++(); 62 | tree_it operator++(int); 63 | bool operator==(const tree_it &rhs) const; 64 | bool operator!=(const tree_it &rhs) const; 65 | 66 | template void key(OutputIt key) const; 67 | int get_key_len() const; 68 | const std::string key() const; 69 | 70 | 71 | private: 72 | step &get_step(); 73 | const step &get_step() const; 74 | node *get_node() const; 75 | const char *get_key() const; 76 | int get_depth() const; 77 | 78 | void seek_leaf(); 79 | 80 | node *root_; 81 | std::vector traversal_stack_; 82 | }; 83 | 84 | template 85 | tree_it::step::step() 86 | : step(nullptr, 0, nullptr, {}, {}) {} 87 | 88 | template 89 | tree_it::step::step(int depth, child_it c_it, child_it c_it_end) 90 | : child_node_(c_it != c_it_end ? c_it.get_child_node() : nullptr), 91 | depth_(depth), 92 | key_(depth ? new char[depth] : nullptr), 93 | child_it_(c_it), 94 | child_it_end_(c_it_end) {} 95 | 96 | template 97 | tree_it::step::step(node *node, int depth, const char *key, child_it c_it, child_it c_it_end) 98 | : child_node_(node), 99 | depth_(depth), 100 | key_(depth ? new char[depth] : nullptr), 101 | child_it_(c_it), 102 | child_it_end_(c_it_end) { 103 | std::copy_n(key, depth, key_); 104 | } 105 | 106 | template 107 | typename tree_it::step &tree_it::step::operator++() { 108 | assert(child_it_ != child_it_end_); 109 | ++child_it_; 110 | child_node_ = child_it_ != child_it_end_ 111 | ? child_it_.get_child_node() 112 | : nullptr; 113 | key_[depth_ - 1] = child_it_ != child_it_end_ 114 | ? child_it_.get_partial_key() 115 | : '\0'; 116 | return *this; 117 | } 118 | 119 | template 120 | typename tree_it::step tree_it::step::operator++(int) { 121 | auto old = *this; 122 | operator++(); 123 | return old; 124 | } 125 | 126 | template 127 | tree_it::step::step(const tree_it::step &other) 128 | : step(other.child_node_, other.depth_, other.key_, other.child_it_, other.child_it_end_) {} 129 | 130 | template 131 | tree_it::step::step(tree_it::step &&other) 132 | : child_node_(other.child_node_), 133 | depth_(other.depth_), 134 | key_(other.key_), 135 | child_it_(other.child_it_), 136 | child_it_end_(other.child_it_end_) { 137 | other.child_node_ = nullptr; 138 | other.depth_ = 0; 139 | other.key_ = nullptr; 140 | other.child_it_ = {}; 141 | other.child_it_end_ = {}; 142 | } 143 | 144 | template 145 | tree_it::step::~step() { 146 | delete [] key_; 147 | } 148 | 149 | template 150 | typename tree_it::step& tree_it::step::operator=(const tree_it::step &other) { 151 | if (this != &other) { 152 | node *node = other.child_node_; 153 | int depth = other.depth_; 154 | char *key = depth ? new char[depth] : nullptr; 155 | std::copy_n(other.key_, other.depth_, key); 156 | child_it c_it = other.child_it_; 157 | child_it c_it_end = other.child_it_end_; 158 | 159 | child_node_ = node; 160 | depth_ = depth; 161 | delete [] key_; 162 | key_ = key; 163 | child_it_ = c_it; 164 | child_it_end_ = c_it_end; 165 | } 166 | return *this; 167 | } 168 | 169 | template 170 | typename tree_it::step& tree_it::step::operator=(tree_it::step &&other) noexcept { 171 | if (this != &other) { 172 | child_node_ = other.child_node_; 173 | other.child_node_ = nullptr; 174 | 175 | depth_ = other.depth_; 176 | other.depth_ = 0; 177 | 178 | delete [] key_; 179 | key_ = other.key_; 180 | other.key_ = nullptr; 181 | 182 | child_it_ = other.child_it_; 183 | other.child_it_ = {}; 184 | 185 | child_it_end_ = other.child_it_end_; 186 | other.child_it_end_ = {}; 187 | } 188 | return *this; 189 | } 190 | 191 | template 192 | tree_it::tree_it() {} 193 | 194 | template 195 | tree_it::tree_it(node *root, std::vector traversal_stack) : root_(root), traversal_stack_(traversal_stack) { 196 | seek_leaf(); 197 | } 198 | 199 | template tree_it tree_it::min(node *root) { 200 | return tree_it::greater_equal(root, ""); 201 | } 202 | 203 | template 204 | tree_it tree_it::greater_equal(node *root, const char *key) { 205 | assert(root != nullptr); 206 | 207 | int key_len = std::strlen(key); 208 | std::vector::step> traversal_stack; 209 | 210 | // sentinel child iterator for root 211 | traversal_stack.push_back({root, 0, nullptr, {nullptr, -2}, {nullptr, -1}}); 212 | 213 | while (true) { 214 | tree_it::step &cur_step = traversal_stack.back(); 215 | node *cur_node = cur_step.child_node_; 216 | int cur_depth = cur_step.depth_; 217 | 218 | int prefix_match_len = std::min(cur_node->check_prefix(key + cur_depth, key_len - cur_depth), key_len - cur_depth); 219 | // if search key "equals" the prefix 220 | if (key_len == cur_depth + prefix_match_len) { 221 | return tree_it(root, traversal_stack); 222 | } 223 | // if search key is "greater than" the prefix 224 | if (prefix_match_len < cur_node->prefix_len_ && key[cur_depth + prefix_match_len] > cur_node->prefix_[prefix_match_len]) { 225 | ++cur_step; 226 | return tree_it(root, traversal_stack); 227 | } 228 | if (cur_node->is_leaf()) { 229 | continue; 230 | } 231 | // seek subtree where search key is "lesser than or equal" the subtree partial key 232 | inner_node *cur_inner_node = static_cast *>(cur_node); 233 | child_it c_it = cur_inner_node->begin(); 234 | child_it c_it_end = cur_inner_node->end(); 235 | // TODO more efficient with specialized node search method? 236 | for (; c_it != c_it_end; ++c_it) { 237 | if (key[cur_depth + cur_node->prefix_len_] <= c_it.get_partial_key()) { 238 | break; 239 | } 240 | } 241 | int depth = cur_depth + cur_node->prefix_len_ + 1; 242 | tree_it::step child(depth, c_it, c_it_end); 243 | /* compute child key: cur_key + cur_node->prefix_ + child_partial_key */ 244 | std::copy_n(cur_step.key_, cur_depth, child.key_); 245 | std::copy_n(cur_node->prefix_, cur_node->prefix_len_, child.key_ + cur_depth); 246 | child.key_[cur_depth + cur_node->prefix_len_] = c_it.get_partial_key(); 247 | traversal_stack.push_back(child); 248 | } 249 | } 250 | 251 | template typename tree_it::value_type tree_it::operator*() { 252 | assert(get_node()->is_leaf()); 253 | return static_cast *>(get_node())->value_; 254 | } 255 | 256 | template typename tree_it::pointer tree_it::operator->() { 257 | assert(get_node()->is_leaf()); 258 | return &static_cast *>(get_node())->value_; 259 | } 260 | 261 | template tree_it &tree_it::operator++() { 262 | assert(get_node()->is_leaf()); 263 | ++get_step(); 264 | seek_leaf(); 265 | return *this; 266 | } 267 | 268 | template tree_it tree_it::operator++(int) { 269 | auto old = *this; 270 | operator++(); 271 | return old; 272 | } 273 | 274 | template bool tree_it::operator==(const tree_it &rhs) const { 275 | if (traversal_stack_.empty() && rhs.traversal_stack_.empty()) { 276 | /* both are empty */ 277 | return true; 278 | } 279 | if (traversal_stack_.empty() || rhs.traversal_stack_.empty()) { 280 | /* one is empty */ 281 | return false; 282 | } 283 | return get_node() == rhs.get_node(); 284 | } 285 | 286 | template bool tree_it::operator!=(const tree_it &rhs) const { 287 | return !(*this == rhs); 288 | } 289 | 290 | template 291 | template 292 | void tree_it::key(OutputIt key) const { 293 | std::copy_n(get_key(), get_depth(), key); 294 | std::copy_n(get_node()->prefix_, get_node()->prefix_len_, key + get_depth()); 295 | } 296 | 297 | template 298 | int tree_it::get_key_len() const { 299 | return get_depth() + get_node()->prefix_len_; 300 | } 301 | 302 | template 303 | const std::string tree_it::key() const { 304 | std::string str(get_depth() + get_node()->prefix_len_ - 1, 0); 305 | key(str.begin()); 306 | return str; 307 | } 308 | 309 | template 310 | void tree_it::seek_leaf() { 311 | /* traverse up until a node on the right is found or stack gets empty */ 312 | for (; get_step().child_it_ == get_step().child_it_end_; ++get_step()) { 313 | traversal_stack_.pop_back(); 314 | if (get_step().child_node_ == root_) { // root guard 315 | traversal_stack_.pop_back(); 316 | assert(traversal_stack_.empty()); 317 | return; 318 | } 319 | } 320 | 321 | /* find leftmost leaf node */ 322 | while (!get_node()->is_leaf()) { 323 | inner_node *cur_inner_node = static_cast *>(get_node()); 324 | int depth = get_depth() + get_node()->prefix_len_ + 1; 325 | child_it c_it = cur_inner_node->begin(); 326 | child_it c_it_end = cur_inner_node->end(); 327 | tree_it::step child(depth, c_it, c_it_end); 328 | /* compute child key: cur_key + cur_node->prefix_ + child_partial_key */ 329 | std::copy_n(get_key(), get_depth(), child.key_); 330 | std::copy_n(get_node()->prefix_, get_node()->prefix_len_, child.key_ + get_depth()); 331 | child.key_[get_depth() + get_node()->prefix_len_] = c_it.get_partial_key(); 332 | traversal_stack_.push_back(child); 333 | } 334 | } 335 | 336 | template 337 | node * tree_it::get_node() const { 338 | return get_step().child_node_; 339 | } 340 | 341 | template 342 | int tree_it::get_depth() const { 343 | return get_step().depth_; 344 | } 345 | 346 | template 347 | const char * tree_it::get_key() const { 348 | return get_step().key_; 349 | } 350 | 351 | template 352 | typename tree_it::step &tree_it::get_step() { 353 | assert(!traversal_stack_.empty()); 354 | return traversal_stack_.back(); 355 | } 356 | 357 | template 358 | const typename tree_it::step &tree_it::get_step() const { 359 | assert(!traversal_stack_.empty()); 360 | return traversal_stack_.back(); 361 | } 362 | 363 | } // namespace art 364 | 365 | #endif 366 | -------------------------------------------------------------------------------- /report/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | art.pdf 3 | art.ps 4 | 5 | *.aux 6 | *.glo 7 | *.idx 8 | *.log 9 | *.toc 10 | *.ist 11 | *.acn 12 | *.acr 13 | *.alg 14 | *.bbl 15 | *.blg 16 | *.dvi 17 | *.glg 18 | *.gls 19 | *.ilg 20 | *.ind 21 | *.lof 22 | *.lot 23 | *.maf 24 | *.mtc 25 | *.mtc1 26 | *.out 27 | *.synctex.gz 28 | *.fdb_latexmk 29 | *.fls 30 | _minted-* 31 | -------------------------------------------------------------------------------- /report/.latexmkrc: -------------------------------------------------------------------------------- 1 | @default_files = ("art.tex"); 2 | $pdflatex = "pdflatex --shell-escape"; 3 | $pdf_mode = "1"; 4 | $pdf_previewer = "epdfview"; 5 | -------------------------------------------------------------------------------- /report/art.bib: -------------------------------------------------------------------------------- 1 | @inproceedings{leis2013adaptive, 2 | title={The adaptive radix tree: ARTful indexing for main-memory databases}, 3 | author={Leis, Viktor and Kemper, Alfons and Neumann, Thomas}, 4 | booktitle={2013 IEEE 29th International Conference on Data Engineering (ICDE)}, 5 | pages={38--49}, 6 | year={2013}, 7 | organization={IEEE} 8 | } 9 | 10 | @book{cormen2009introduction, 11 | title={Introduction to algorithms}, 12 | author={Cormen, Thomas H and Leiserson, Charles E and Rivest, Ronald L and Stein, Clifford}, 13 | year={2009}, 14 | publisher={MIT press} 15 | } 16 | 17 | @article{fredkin1960trie, 18 | title={Trie memory}, 19 | author={Fredkin, Edward}, 20 | journal={Communications of the ACM}, 21 | volume={3}, 22 | number={9}, 23 | pages={490--499}, 24 | year={1960}, 25 | publisher={ACM} 26 | } 27 | 28 | @article{morrison1968patricia, 29 | title={PATRICIA—practical algorithm to retrieve information coded in alphanumeric}, 30 | author={Morrison, Donald R}, 31 | journal={Journal of the ACM (JACM)}, 32 | volume={15}, 33 | number={4}, 34 | pages={514--534}, 35 | year={1968}, 36 | publisher={ACM} 37 | } 38 | 39 | @unpublished{wellenzohn2017wapi, 40 | author = {Kevin Wellenzohn and 41 | Michael B{\"o}hlen and 42 | Sven Helmer and 43 | Marcel Reutegger and 44 | Sherif Sakr}, 45 | title = {Workload-Aware Contention-Management in Indexes for Hierarchical Data}, 46 | note = {To be published}, 47 | } 48 | -------------------------------------------------------------------------------- /report/compressions_dense.dat: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 0.111111 3 | 2 0.222222 4 | 3 0.333333 5 | 4 0.444444 6 | 5 0.555555 7 | 6 0.666666 8 | 7 0.777777 9 | 8 0.888888 10 | 9 1.000000 11 | 10 1.111111 12 | -------------------------------------------------------------------------------- /report/compressions_paths.dat: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 0.122902 3 | 2 0.222553 4 | 3 0.359982 5 | 4 0.496738 6 | 5 0.660941 7 | 6 0.809558 8 | 7 0.949176 9 | 8 1.119511 10 | 9 1.323475 11 | 10 1.521763 12 | -------------------------------------------------------------------------------- /report/compressions_sparse.dat: -------------------------------------------------------------------------------- 1 | 0 0 2 | 1 0.376552 3 | 2 0.745005 4 | 3 1.110076 5 | 4 1.477715 6 | 5 1.856687 7 | 6 2.257919 8 | 7 2.689165 9 | 8 3.144610 10 | 9 3.570837 11 | 10 3.967367 12 | -------------------------------------------------------------------------------- /report/images/IFIlogo-eps-converted-to.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafaelkallis/adaptive-radix-tree/1023b8440eaa4f713fd2b916a6c45a42baf5447b/report/images/IFIlogo-eps-converted-to.pdf -------------------------------------------------------------------------------- /report/images/art_nodes_draw-eps-converted-to.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafaelkallis/adaptive-radix-tree/1023b8440eaa4f713fd2b916a6c45a42baf5447b/report/images/art_nodes_draw-eps-converted-to.pdf -------------------------------------------------------------------------------- /report/images/dbtgBW-eps-converted-to.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafaelkallis/adaptive-radix-tree/1023b8440eaa4f713fd2b916a6c45a42baf5447b/report/images/dbtgBW-eps-converted-to.pdf -------------------------------------------------------------------------------- /report/images/dbtgBW.eps: -------------------------------------------------------------------------------- 1 | %!PS-Adobe-3.0 EPSF-3.0 2 | %%Creator: (ImageMagick) 3 | %%Title: (dbtgBW.eps) 4 | %%CreationDate: (Fri Jan 22 16:59:03 2010) 5 | %%BoundingBox: 0 0 63 60 6 | %%HiResBoundingBox: 0 0 62.9921 60 7 | %%DocumentData: Clean7Bit 8 | %%LanguageLevel: 1 9 | %%Pages: 1 10 | %%EndComments 11 | 12 | %%BeginDefaults 13 | %%EndDefaults 14 | 15 | %%BeginProlog 16 | % 17 | % Display a color image. The image is displayed in color on 18 | % Postscript viewers or printers that support color, otherwise 19 | % it is displayed as grayscale. 20 | % 21 | /DirectClassPacket 22 | { 23 | % 24 | % Get a DirectClass packet. 25 | % 26 | % Parameters: 27 | % red. 28 | % green. 29 | % blue. 30 | % length: number of pixels minus one of this color (optional). 31 | % 32 | currentfile color_packet readhexstring pop pop 33 | compression 0 eq 34 | { 35 | /number_pixels 3 def 36 | } 37 | { 38 | currentfile byte readhexstring pop 0 get 39 | /number_pixels exch 1 add 3 mul def 40 | } ifelse 41 | 0 3 number_pixels 1 sub 42 | { 43 | pixels exch color_packet putinterval 44 | } for 45 | pixels 0 number_pixels getinterval 46 | } bind def 47 | 48 | /DirectClassImage 49 | { 50 | % 51 | % Display a DirectClass image. 52 | % 53 | systemdict /colorimage known 54 | { 55 | columns rows 8 56 | [ 57 | columns 0 0 58 | rows neg 0 rows 59 | ] 60 | { DirectClassPacket } false 3 colorimage 61 | } 62 | { 63 | % 64 | % No colorimage operator; convert to grayscale. 65 | % 66 | columns rows 8 67 | [ 68 | columns 0 0 69 | rows neg 0 rows 70 | ] 71 | { GrayDirectClassPacket } image 72 | } ifelse 73 | } bind def 74 | 75 | /GrayDirectClassPacket 76 | { 77 | % 78 | % Get a DirectClass packet; convert to grayscale. 79 | % 80 | % Parameters: 81 | % red 82 | % green 83 | % blue 84 | % length: number of pixels minus one of this color (optional). 85 | % 86 | currentfile color_packet readhexstring pop pop 87 | color_packet 0 get 0.299 mul 88 | color_packet 1 get 0.587 mul add 89 | color_packet 2 get 0.114 mul add 90 | cvi 91 | /gray_packet exch def 92 | compression 0 eq 93 | { 94 | /number_pixels 1 def 95 | } 96 | { 97 | currentfile byte readhexstring pop 0 get 98 | /number_pixels exch 1 add def 99 | } ifelse 100 | 0 1 number_pixels 1 sub 101 | { 102 | pixels exch gray_packet put 103 | } for 104 | pixels 0 number_pixels getinterval 105 | } bind def 106 | 107 | /GrayPseudoClassPacket 108 | { 109 | % 110 | % Get a PseudoClass packet; convert to grayscale. 111 | % 112 | % Parameters: 113 | % index: index into the colormap. 114 | % length: number of pixels minus one of this color (optional). 115 | % 116 | currentfile byte readhexstring pop 0 get 117 | /offset exch 3 mul def 118 | /color_packet colormap offset 3 getinterval def 119 | color_packet 0 get 0.299 mul 120 | color_packet 1 get 0.587 mul add 121 | color_packet 2 get 0.114 mul add 122 | cvi 123 | /gray_packet exch def 124 | compression 0 eq 125 | { 126 | /number_pixels 1 def 127 | } 128 | { 129 | currentfile byte readhexstring pop 0 get 130 | /number_pixels exch 1 add def 131 | } ifelse 132 | 0 1 number_pixels 1 sub 133 | { 134 | pixels exch gray_packet put 135 | } for 136 | pixels 0 number_pixels getinterval 137 | } bind def 138 | 139 | /PseudoClassPacket 140 | { 141 | % 142 | % Get a PseudoClass packet. 143 | % 144 | % Parameters: 145 | % index: index into the colormap. 146 | % length: number of pixels minus one of this color (optional). 147 | % 148 | currentfile byte readhexstring pop 0 get 149 | /offset exch 3 mul def 150 | /color_packet colormap offset 3 getinterval def 151 | compression 0 eq 152 | { 153 | /number_pixels 3 def 154 | } 155 | { 156 | currentfile byte readhexstring pop 0 get 157 | /number_pixels exch 1 add 3 mul def 158 | } ifelse 159 | 0 3 number_pixels 1 sub 160 | { 161 | pixels exch color_packet putinterval 162 | } for 163 | pixels 0 number_pixels getinterval 164 | } bind def 165 | 166 | /PseudoClassImage 167 | { 168 | % 169 | % Display a PseudoClass image. 170 | % 171 | % Parameters: 172 | % class: 0-PseudoClass or 1-Grayscale. 173 | % 174 | currentfile buffer readline pop 175 | token pop /class exch def pop 176 | class 0 gt 177 | { 178 | currentfile buffer readline pop 179 | token pop /depth exch def pop 180 | /grays columns 8 add depth sub depth mul 8 idiv string def 181 | columns rows depth 182 | [ 183 | columns 0 0 184 | rows neg 0 rows 185 | ] 186 | { currentfile grays readhexstring pop } image 187 | } 188 | { 189 | % 190 | % Parameters: 191 | % colors: number of colors in the colormap. 192 | % colormap: red, green, blue color packets. 193 | % 194 | currentfile buffer readline pop 195 | token pop /colors exch def pop 196 | /colors colors 3 mul def 197 | /colormap colors string def 198 | currentfile colormap readhexstring pop pop 199 | systemdict /colorimage known 200 | { 201 | columns rows 8 202 | [ 203 | columns 0 0 204 | rows neg 0 rows 205 | ] 206 | { PseudoClassPacket } false 3 colorimage 207 | } 208 | { 209 | % 210 | % No colorimage operator; convert to grayscale. 211 | % 212 | columns rows 8 213 | [ 214 | columns 0 0 215 | rows neg 0 rows 216 | ] 217 | { GrayPseudoClassPacket } image 218 | } ifelse 219 | } ifelse 220 | } bind def 221 | 222 | /DisplayImage 223 | { 224 | % 225 | % Display a DirectClass or PseudoClass image. 226 | % 227 | % Parameters: 228 | % x & y translation. 229 | % x & y scale. 230 | % label pointsize. 231 | % image label. 232 | % image columns & rows. 233 | % class: 0-DirectClass or 1-PseudoClass. 234 | % compression: 0-none or 1-RunlengthEncoded. 235 | % hex color packets. 236 | % 237 | gsave 238 | /buffer 512 string def 239 | /byte 1 string def 240 | /color_packet 3 string def 241 | /pixels 768 string def 242 | 243 | currentfile buffer readline pop 244 | token pop /x exch def 245 | token pop /y exch def pop 246 | x y translate 247 | currentfile buffer readline pop 248 | token pop /x exch def 249 | token pop /y exch def pop 250 | currentfile buffer readline pop 251 | token pop /pointsize exch def pop 252 | /Times-Roman findfont pointsize scalefont setfont 253 | x y scale 254 | currentfile buffer readline pop 255 | token pop /columns exch def 256 | token pop /rows exch def pop 257 | currentfile buffer readline pop 258 | token pop /class exch def pop 259 | currentfile buffer readline pop 260 | token pop /compression exch def pop 261 | class 0 gt { PseudoClassImage } { DirectClassImage } ifelse 262 | grestore 263 | } bind def 264 | %%EndProlog 265 | %%Page: 1 1 266 | %%PageBoundingBox: 0 0 63 60 267 | userdict begin 268 | DisplayImage 269 | 0 0 270 | 62.9921 59.9925 271 | 12.000000 272 | 63 60 273 | 1 274 | 1 275 | 1 276 | 8 277 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 278 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 279 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 280 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 281 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 282 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 283 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 284 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 285 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 286 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 287 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE854A 288 | 575757575757575757575757575757575757575757575757575757575757575757575757 289 | 5757575768FFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE854A575757575757575757 290 | 5757575757575757575757575757575757575757575757575757575757575768FFFFFFFF 291 | FFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE864A575757575757575757575757575757575757 292 | 5757575757575757575757575757575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFE 293 | FEFEFEFEFEFEFE864A575757575757575757575757575757575757575757575757575757 294 | 5757575757575757575757575768FFFFFFFFFFFFFFFFFEFBF1F1F3FEFEFEFEFEFEFE864A 295 | 575757575757575757575757575757575757575757575757575757575757575757575757 296 | 5757575768FFFFFFFFFFFFFFFFFEB8253533255CDCFEFEFEFE864A575757575757575757 297 | 5757575757575757575757575757575757575757575757575757575757575768FFFFFFFF 298 | FFFFFFFFFEB84FFEFEF98644F8FEFEFE864A57575757575757575757575757574B40322A 299 | 28272627282A323B4A5757575757575757575757575768FFFFFFFFFFFFFFFFFEB84FFEFE 300 | FEFE4FB3FEFEFE864A575757575757575757575757422726222020202020202020202226 301 | 263B575757575757575757575768FFFFFFFFFFFFFFFFFEB84FFEFEFEFE7B8DFEFEFE864A 302 | 57575757575757575757572A23202020202020202020202020202020232C575757575757 303 | 5757575768FFFFFFFFFFFFFFFFFEB84FFEFEFEFE7787FEFEFE874A575757575757575757 304 | 573D262020202020202020202020202020202020253B57575757575757575768FFFFFFFF 305 | FFFFFFFFFEB84FFEFEFEFE50A9FEFEFE874A57575757575757575757433D272020202020 306 | 2020202020202020202027334257575757575757575768FFFFFFFFFFFFFFFFFEB84FFEFE 307 | FEB12FF2FEFEFE874A57575757575757575757439E843835232020202020202020202234 308 | 3878A34257575757575757575768FFFFFFFFFFFFFFFFFEB82C55533D3CC8FEFEFEFE874A 309 | 57575757575757575757439EFEFEC65C3532322B38393D495A8DC6FEFEA3425757575757 310 | 5757575768FFFFFFFFFFFFFFFFFEF1D2D2D2E3FDFEFEFEFEFE874A575757575757575757 311 | 57439EFEFEFEA660888844FDFEFEFEFEFEFEFEFEA34257575757575757575768FFFFFFFF 312 | FFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE874A57575757575757575757439EFEFEFEA66088 313 | 8842FEFEFEFEFEFEFEFEFEA34257575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFE 314 | FEFEFEFEFEFEFE874A57575757575757575757439EFEFEFEA939454535B5FEFEFEFEFEFE 315 | FEFEA34257575757575757575768FFFFFFFFFFFFFFE8CECECECECECECECECECECECE7147 316 | 57575757575757575757439EFEFEFEDC74FEFEFE4D3138383887FEFEFEA3425757575757 317 | 5757575768FFFFFFFFFFFFFFC28282828282828282828282824D43575757575757575757 318 | 57439EFEDCB37B81FEFEFE682020202080FEFEFEA34257575757575757575768FFFFFFFF 319 | FFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE874A57575757575757575757439EFEB2758E62FE 320 | FEFE682020202080FEFEFEA34257575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFE 321 | FEFEFEFEFEFEFE874A57575757575757575757439EFEB29DEF62FEFEFE843C263E3E95FE 322 | FEFEA34257575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE874A 323 | 57575757575757575757439EFEB29DEF62FEFEFEDC75FE51FEFEFEFEFEA3425757575757 324 | 5757575768FFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE874A575757575757575757 325 | 57439EFEB28CBE62EC9F894AB2D5A749B9D2FEFEA34257575757575757575768FFFFFFFF 326 | FFFFFFFFFEFAC4C4C4D5F8FEFEFEFEFE874A57575757575757575757439EFEAB452E2C37 327 | 3B89702D3939847753CFFEA34257575757575757575768FFFFFFFFFFFFFFFFFEEB206161 328 | 5133CDFEFEFEFE874A57575757575757575757439E40312020202039FAA2202036DFB92F 329 | 3144A34257575757575757575768FFFFFFFFFFFFFFFFFEEB20FBFEFEA160FEFEFEFE8949 330 | 575757575757575757573B2D2020202020202B363620202C363627202029425757575757 331 | 5757575768FFFFFFFFFFFFFFFFFEEB20FBFEFE9674FEFEFEFE8949575757575757575757 332 | 57402B20202020202020202020202020202020202B4057575757575757575768FFFFFFFF 333 | FFFFFFFFFEEB2051514435EFFEFEFEFE894957575757575757575757574E272020202020 334 | 2020202020202020202026425757575757575757575768FFFFFFFFFFFFFFFFFEEB20D2D5 335 | C77C50FEFEFEFE89495757575757575757575757574B3227272320202020202020232727 336 | 3447575757575757575757575768FFFFFFFFFFFFFFFFFEEB20FBFEFEFC27DCFEFEFE8949 337 | 5757575757575757575757575757574B433B34343234343D454B57575757575757575757 338 | 5757575768FFFFFFFFFFFFFFFFFEEB20FBFEFEB62AF7FEFEFE8949575757575757575757 339 | 5757575757575757575757575757575757575757575757575757575757575768FFFFFFFF 340 | FFFFFFFFFEEB2027272C49BFFEFEFEFE8949575757575757575757575757575757575757 341 | 5757575757575757575757575757575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFE 342 | FEFEFEFEFEFEFE8949575757575757575757575757575757575757575757575757575757 343 | 5757575757575757575757575768FFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE8949 344 | 575757575757575757575757575757575757575757575757575757575757575757575757 345 | 5757575768FFFFFFFFFFFFFFFFFEFEFEFEFEFEFEFEFEFEFEFE8949575757575757575757 346 | 5757575757575757575757575757575757575757575757575757575757575768FFFFFFFF 347 | FFFFFFE6C7C7C7C7C7C7C7C7C7C7C7C76E63797979797979797979797979797979797979 348 | 79797979797979797979797979786F7979797979797983FFFFFFFFFFFFFF883A3A3A3A3A 349 | 3A3A3A3A3A3A3A426E898989898989898989898989898989898989898989898989898989 350 | 898989897E428989898989898995FFFFFFFFFFFFFF951212121212121212121212125CC7 351 | FEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEFEE868FEFEFE 352 | FEFEFEFEFFFFFFFFFFFFFFFF951212121212121212121212125CC7FEFEFEFED4C4C4C4C4 353 | C4CBFEFEFEFEFEFEFEFEFEECB9B5DBFEFEFEFEFEFEFEE868FEFEFEFEFEFEFEFFFFFFFFFF 354 | FFFFFF951212121212121212121212125CC6FEFEFEFE8C6262225A6274FEFEFEFEFEFEFE 355 | FE93316C7B436CFAFEFEFEFEFEE868FEFEFEFEFEFEFEFFFFFFFFFFFFFFFF951212121212 356 | 121212121212125CC6FEFEFEFEFEFEFE26E2FEFEFEFEFEFEFEFEFEB83AE6FEFEF955A9FE 357 | FEFEFEFEE868FEFEFEFEFEFEFEFFFFFFFFFFFFFFFF951212121212121212121212125CC6 358 | FEFEFEFEFEFEFE26E2FEFEFEFEFEFEFEFEFE5B9CFEFEFEFEE3EBFEFEFEFEFEE867F5F5F5 359 | F5F5F5F5F7FFFFFFFFFFFFFF951212121212121212121212125CC6FEFEFEFEFEFEFE26E2 360 | FEFEFEFEFEFEFEFEFE33C7FEFEDCD2D2DCFEFEFEFEFEE62B4A4A4A4A4A4A4A5EFFFFFFFF 361 | FFFFFF951212121212121212121212125CC6FEFEFEFEFEFEFE26E2FEFEFEFEFEFEFEFEFE 362 | 37B9FEFE7C554453FEFEFEFEFEE863DADADADADADADADFFFFFFFFFFFFFFF951212121212 363 | 121212121212125CC6FEFEFEFEFEFEFE26E2FEFEFEFEFEFEFEFEFE6886FEFEFEFEB953FE 364 | FEFEFEFEE863DADADADADADADADFFFFFFFFFFFFFFF951212121212121212121212125CC6 365 | FEFEFEFEFEFEFE26E2FEFEFEFEFEFEFEFEFED42FB6FBFEEB6E57FEFEFEFEFEE863DADADA 366 | DADADADADFFFFFFFFFFFFFFF951212121212121212121212125CC5FEFEFEFEFEFEFE26E2 367 | FEFEFEFEFEFEFEFEFEFECD51283C2F7CEFFEFEFEFEFEE863DADADADADADADADFFFFFFFFF 368 | FFFFFF951212121212121212121212125CC5FEFEFEFEFEFEFEFCFEFEFEFEFEFEFEFEFEFE 369 | FEFEFEF1E8FEFEFEFEFEFEFEFEE863DADADADADADADADFFFFFFFFFFFFFFFCB9595959595 370 | 95959595959595AFE3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 371 | FFFFFFFFF7ABF5F5F5F5F5F5F5F5FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 372 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 373 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 374 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 375 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 376 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 377 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 378 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 379 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 380 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 381 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 382 | end 383 | %%PageTrailer 384 | %%Trailer 385 | %%EOF 386 | -------------------------------------------------------------------------------- /report/images/trie_s1_draw-eps-converted-to.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafaelkallis/adaptive-radix-tree/1023b8440eaa4f713fd2b916a6c45a42baf5447b/report/images/trie_s1_draw-eps-converted-to.pdf -------------------------------------------------------------------------------- /report/images/trie_s2_draw-eps-converted-to.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rafaelkallis/adaptive-radix-tree/1023b8440eaa4f713fd2b916a6c45a42baf5447b/report/images/trie_s2_draw-eps-converted-to.pdf -------------------------------------------------------------------------------- /src/example.cpp: -------------------------------------------------------------------------------- 1 | #include "art.hpp" 2 | #include "zipf.hpp" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using std::string; 14 | 15 | void art_bench() { 16 | std::ifstream file("dataset.txt"); 17 | std::unordered_map dataset; 18 | uint32_t n = 0; 19 | string line; 20 | while (std::getline(file, line)) { 21 | dataset[n++] = line; 22 | } 23 | file.close(); 24 | 25 | fast_zipf rng(n); 26 | art::art m; 27 | /* std::map m; */ 28 | /* std::unordered_map m; */ 29 | int v = 1; 30 | std::mt19937_64 g(0); 31 | for (uint32_t i = 0; i < 1000000; ++i) { 32 | auto k = dataset[rng()]; 33 | m.set(k.c_str(), &v); 34 | /* m[dataset[rng()]] = &v; */ 35 | } 36 | } 37 | 38 | /* void art_sparse_uniform() { */ 39 | /* art::art m; */ 40 | /* int n = 1000; */ 41 | /* int v = 1; */ 42 | /* std::mt19937_64 rng1(0); */ 43 | /* std::string k; */ 44 | /* for (int i = 0; i < n; ++i) { */ 45 | /* k = std::to_string(rng1()); */ 46 | /* m.set(k.c_str(), &v); */ 47 | /* } */ 48 | 49 | /* std::mt19937_64 rng2(0); */ 50 | /* for (int i = 0; i < n; ++i) { */ 51 | /* k = std::to_string(rng2()); */ 52 | /* m.get(k.c_str()); */ 53 | /* } */ 54 | 55 | /* std::mt19937_64 rng3(0); */ 56 | /* for (int i = 0; i < n; ++i) { */ 57 | /* k = std::to_string(rng3()); */ 58 | /* m.del(k.c_str()); */ 59 | /* } */ 60 | /* } */ 61 | 62 | /* std::string pad(const std::string &s, char ch, int n) { */ 63 | /* if (s.length() >= n) { */ 64 | /* return s; */ 65 | /* } */ 66 | /* std::string new_s(n, ch); */ 67 | /* for (int i = s.length() - 1; i > -1; --i) { */ 68 | /* new_s[n - i - 1] = s[i]; */ 69 | /* } */ 70 | /* return new_s; */ 71 | /* } */ 72 | 73 | /* void art_compressions_dense_insert() { */ 74 | /* art::art m; */ 75 | /* int v = 1; */ 76 | /* std::string k; */ 77 | /* int i; */ 78 | /* for (i = 0; i < 10000000; ++i) { */ 79 | /* if (i % 1000000 == 0) { */ 80 | /* std::cout << i << " " << m.n_compress << std::endl; */ 81 | /* } */ 82 | /* k = pad(std::to_string(i), '0', 7); */ 83 | /* m.set(k.c_str(), &v); */ 84 | /* } */ 85 | /* std::cout << i << " " << m.n_compress << std::endl; */ 86 | /* } */ 87 | 88 | /* void art_compressions_dense_delete() { */ 89 | /* art::art m; */ 90 | /* int v = 1; */ 91 | /* std::string k; */ 92 | /* int i; */ 93 | /* for (i = 0; i < 10000000; ++i) { */ 94 | /* k = pad(std::to_string(i), '0', 7); */ 95 | /* m.set(k.c_str(), &v); */ 96 | /* } */ 97 | /* m.n_compress = 0; */ 98 | /* for (i = 0; i < 10000000; ++i) { */ 99 | /* if (i % 1000000 == 0) { */ 100 | /* std::cout << i << " " << m.n_compress << std::endl; */ 101 | /* } */ 102 | /* k = pad(std::to_string(i), '0', 7); */ 103 | /* m.del(k.c_str()); */ 104 | /* } */ 105 | /* std::cout << i << " " << m.n_compress << std::endl; */ 106 | /* } */ 107 | 108 | /* void art_compressions_paths_insert() { */ 109 | /* art::art m; */ 110 | /* int v = 1; */ 111 | /* auto file = std::ifstream("dataset.txt"); */ 112 | /* std::string line; */ 113 | /* int i = 0; */ 114 | /* while (std::getline(file, line)) { */ 115 | /* if (i % 1000000 == 0) { */ 116 | /* std::cout << i << " " << m.n_compress << std::endl; */ 117 | /* } */ 118 | /* m.set(line.c_str(), line.length(), &v); */ 119 | /* ++i; */ 120 | /* } */ 121 | /* file.close(); */ 122 | /* std::cout << i << " " << m.n_compress << std::endl; */ 123 | /* } */ 124 | 125 | /* void art_compressions_paths_delete() { */ 126 | /* art::art m; */ 127 | /* int v = 1; */ 128 | /* auto file = std::ifstream("dataset.txt"); */ 129 | /* std::string line; */ 130 | /* while (std::getline(file, line)) { */ 131 | /* m.set(line.c_str(), line.length(), &v); */ 132 | /* } */ 133 | /* file.close(); */ 134 | /* file = std::ifstream("dataset.txt"); */ 135 | /* m.n_compress = 0; */ 136 | /* int i = 0; */ 137 | /* while (std::getline(file, line)) { */ 138 | /* if (i % 1000000 == 0) { */ 139 | /* std::cout << i << " " << m.n_compress << std::endl; */ 140 | /* } */ 141 | /* m.del(line.c_str(), line.length()); */ 142 | /* ++i; */ 143 | /* } */ 144 | /* file.close(); */ 145 | /* std::cout << i << " " << m.n_compress << std::endl; */ 146 | /* } */ 147 | 148 | /* void art_compressions_sparse_insert() { */ 149 | /* art::art m; */ 150 | /* int v = 1; */ 151 | /* std::mt19937_64 rng1(0); */ 152 | /* std::string k; */ 153 | /* int i; */ 154 | /* for (i = 0; i < 10000000; ++i) { */ 155 | /* if (i % 1000000 == 0) { */ 156 | /* std::cout << i << " " << m.n_compress << std::endl; */ 157 | /* } */ 158 | /* k = std::to_string(rng1()); */ 159 | /* m.set(k.c_str(), &v); */ 160 | /* } */ 161 | /* std::cout << i << " " << m.n_compress << std::endl; */ 162 | /* } */ 163 | 164 | /* void art_compressions_sparse_delete() { */ 165 | /* art::art m; */ 166 | /* int v = 1; */ 167 | /* std::mt19937_64 rng1(0); */ 168 | /* std::string k; */ 169 | /* for (int i = 0; i < 10000000; ++i) { */ 170 | /* k = std::to_string(rng1()); */ 171 | /* m.set(k.c_str(), &v); */ 172 | /* } */ 173 | /* m.n_compress = 0; */ 174 | /* std::mt19937_64 rng2(0); */ 175 | /* int i = 0; */ 176 | /* for (; i < 10000000; ++i) { */ 177 | /* if (i % 1000000 == 0) { */ 178 | /* std::cout << i << " " << m.n_compress << std::endl; */ 179 | /* } */ 180 | /* k = std::to_string(rng2()); */ 181 | /* m.del(k.c_str()); */ 182 | /* } */ 183 | /* std::cout << i << " " << m.n_compress << std::endl; */ 184 | /* } */ 185 | 186 | void casual_stress_test(int n) { 187 | art::art m; 188 | int v = 1; 189 | std::mt19937_64 rng1(0); 190 | std::string k; 191 | int i; 192 | for (i = 0; i < n; ++i) { 193 | k = std::to_string(rng1()); 194 | m.set(k.c_str(), &v); 195 | if ((i % (1<<20)) == 0) { 196 | std::cout << i << std::endl; 197 | } 198 | } 199 | std::cout << i << std::endl; 200 | } 201 | 202 | int main() { 203 | /* std::cout << "sparse insert" << std::endl; */ 204 | /* art_compressions_sparse_insert(); */ 205 | /* std::cout << "sparse delete" << std::endl; */ 206 | /* art_compressions_sparse_delete(); */ 207 | /* std::cout << "paths insert" << std::endl; */ 208 | /* art_compressions_paths_insert(); */ 209 | /* std::cout << "paths delete" << std::endl; */ 210 | /* art_compressions_paths_delete(); */ 211 | /* std::cout << "dense insert" << std::endl; */ 212 | /* art_compressions_dense_insert(); */ 213 | /* std::cout << "dense delete" << std::endl; */ 214 | /* art_compressions_dense_delete(); */ 215 | 216 | /* casual_stress_test(16 * 1000 * 1000); */ 217 | 218 | // 219 | // simple example 220 | // 221 | 222 | art::art m; 223 | 224 | m.set("aa", 0); 225 | m.set("aaaa", 1); 226 | m.set("aaaaaaa", 2); 227 | m.set("aaaaaaaaaa", 3); 228 | m.set("aaaaaaaba", 4); 229 | m.set("aaaabaa", 5); 230 | m.set("aaaabaaaaa", 6); 231 | 232 | /* The above statements construct the following tree: 233 | * 234 | * (aa) 235 | * $_____/ |a 236 | * / | 237 | * ()->0 (a) 238 | * $_____/ |a\____________b 239 | * / | \ 240 | * ()->1 (aa) (aa) 241 | * $_____/ |a\___b |$\____a 242 | * / | \ | \ 243 | * ()->2 (aa$)->3 (a$)->4 ()->5 (aa$)->6 244 | * 245 | */ 246 | 247 | auto it = m.begin(); 248 | auto it_end = m.end(); 249 | for (int i = 0; it != it_end; ++i, ++it) { 250 | std::cout << it.key() << " -> " << *it << std::endl; 251 | } 252 | 253 | return 0; 254 | } 255 | -------------------------------------------------------------------------------- /test/art.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file art tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::array; 16 | using std::hash; 17 | using std::mt19937; 18 | using std::mt19937_64; 19 | using std::random_device; 20 | using std::shuffle; 21 | using std::string; 22 | using std::to_string; 23 | 24 | TEST_SUITE("art") { 25 | 26 | TEST_CASE("set") { 27 | 28 | art::art trie; 29 | 30 | int dummy_value_1; 31 | int dummy_value_2; 32 | 33 | SUBCASE("insert into empty tree") { 34 | REQUIRE_EQ(nullptr, trie.get("abc")); 35 | trie.set("abc", &dummy_value_1); 36 | REQUIRE_EQ(&dummy_value_1, trie.get("abc")); 37 | } 38 | 39 | SUBCASE("insert into empty tree & replace") { 40 | trie.set("abc", &dummy_value_1); 41 | trie.set("abc", &dummy_value_2); 42 | REQUIRE_EQ(&dummy_value_2, trie.get("abc")); 43 | } 44 | 45 | SUBCASE("insert value s.t. existing value is a prefix") { 46 | const char * prefix_key = "abc"; 47 | const char * key = "abcde"; 48 | trie.set(prefix_key, &dummy_value_1); 49 | trie.set(key, &dummy_value_2); 50 | REQUIRE_EQ(&dummy_value_1, trie.get(prefix_key)); 51 | REQUIRE_EQ(&dummy_value_2, trie.get(key)); 52 | } 53 | 54 | SUBCASE("insert value s.t. new value is a prefix") { 55 | trie.set("abcde", &dummy_value_1); 56 | trie.set("abc", &dummy_value_2); 57 | REQUIRE_EQ(&dummy_value_1, trie.get("abcde")); 58 | REQUIRE_EQ(&dummy_value_2, trie.get("abc")); 59 | } 60 | 61 | SUBCASE("insert key s.t. it mismatches existing key") { 62 | const char *key1 = "aaaaa"; 63 | const char *key2 = "aabaa"; 64 | trie.set(key1, &dummy_value_1); 65 | trie.set(key2, &dummy_value_2); 66 | REQUIRE_EQ(&dummy_value_1, trie.get(key1)); 67 | REQUIRE_EQ(&dummy_value_2, trie.get(key2)); 68 | } 69 | 70 | SUBCASE("monte carlo") { 71 | const int n = 1000; 72 | string keys[n]; 73 | int *values[n]; 74 | /* rng */ 75 | mt19937_64 g(0); 76 | for (int experiment = 0; experiment < 10; experiment += 1) { 77 | for (int i = 0; i < n; i += 1) { 78 | keys[i] = to_string(g()); 79 | values[i] = new int(); 80 | } 81 | 82 | art::art m; 83 | 84 | for (int i = 0; i < n; i += 1) { 85 | m.set(keys[i].c_str(), values[i]); 86 | 87 | for (int j = 0; j < i; j += 1) { 88 | REQUIRE_EQ(values[j], m.get(keys[j].c_str())); 89 | } 90 | } 91 | 92 | for (int i = 0; i < n; i += 1) { 93 | delete values[i]; 94 | } 95 | } 96 | } 97 | } 98 | 99 | TEST_CASE("delete value") { 100 | 101 | auto key0 = "aa"; 102 | auto int0 = 0; 103 | auto key1 = "aaaa"; 104 | auto int1 = 1; 105 | auto key2 = "aaaaaaa"; 106 | auto int2 = 2; 107 | auto key3 = "aaaaaaaaaa"; 108 | auto int3 = 3; 109 | auto key4 = "aaaaaaaba"; 110 | auto int4 = 4; 111 | auto key5 = "aaaabaa"; 112 | auto int5 = 5; 113 | auto key6 = "aaaabaaaaa"; 114 | auto int6 = 6; 115 | auto key7 = "aaaaaaaaaaa"; 116 | auto int7 = 7; 117 | auto key8 = "aaaaaaaaaab"; 118 | auto int8 = 8; 119 | auto key9 = "aaaaaaaaaac"; 120 | auto int9 = 9; 121 | 122 | art::art m; 123 | 124 | m.set(key0, &int0); 125 | m.set(key1, &int1); 126 | m.set(key2, &int2); 127 | m.set(key3, &int3); 128 | m.set(key4, &int4); 129 | m.set(key5, &int5); 130 | m.set(key6, &int6); 131 | m.set(key7, &int7); 132 | m.set(key8, &int8); 133 | m.set(key9, &int9); 134 | 135 | /* The above statements construct the following tree: 136 | * 137 | * (aa) 138 | * $______/ |a 139 | * / | 140 | * ()->0 (a) 141 | * $______/ |a\___________b 142 | * / | \ 143 | * ()->1 (aa) (aa) 144 | * $______/ |a\___b |$\____a 145 | * / | \ | \ 146 | * ()->2 (aa$)->3 (a$)->4 ()->5 (aa) 147 | * $/ \a 148 | * / \ 149 | * ()->6 ()->7 150 | */ 151 | 152 | SUBCASE("delete non existing value") { 153 | REQUIRE(m.del("aaaaa") == nullptr); 154 | REQUIRE(m.del("aaaaaa") == nullptr); 155 | REQUIRE(m.del("aaaab") == nullptr); 156 | REQUIRE(m.del("aaaaba") == nullptr); 157 | REQUIRE(m.get(key0) == &int0); 158 | REQUIRE(m.get(key1) == &int1); 159 | REQUIRE(m.get(key2) == &int2); 160 | REQUIRE(m.get(key3) == &int3); 161 | REQUIRE(m.get(key4) == &int4); 162 | REQUIRE(m.get(key5) == &int5); 163 | REQUIRE(m.get(key6) == &int6); 164 | REQUIRE(m.get(key7) == &int7); 165 | REQUIRE(m.get(key8) == &int8); 166 | REQUIRE(m.get(key9) == &int9); 167 | } 168 | 169 | SUBCASE("n_children == 0 && n_siblings == 0 (6)") { 170 | REQUIRE(m.del(key6) == &int6); 171 | REQUIRE(m.get(key0) == &int0); 172 | REQUIRE(m.get(key1) == &int1); 173 | REQUIRE(m.get(key2) == &int2); 174 | REQUIRE(m.get(key3) == &int3); 175 | REQUIRE(m.get(key4) == &int4); 176 | REQUIRE(m.get(key5) == &int5); 177 | REQUIRE(m.get(key6) == nullptr); 178 | REQUIRE(m.get(key7) == &int7); 179 | REQUIRE(m.get(key8) == &int8); 180 | REQUIRE(m.get(key9) == &int9); 181 | } 182 | 183 | SUBCASE("n_children == 0 && n_siblings == 1 (4)") { 184 | REQUIRE(m.del(key4) == &int4); 185 | REQUIRE(m.get(key0) == &int0); 186 | REQUIRE(m.get(key1) == &int1); 187 | REQUIRE(m.get(key2) == &int2); 188 | REQUIRE(m.get(key3) == &int3); 189 | REQUIRE(m.get(key4) == nullptr); 190 | REQUIRE(m.get(key5) == &int5); 191 | REQUIRE(m.get(key6) == &int6); 192 | REQUIRE(m.get(key7) == &int7); 193 | REQUIRE(m.get(key8) == &int8); 194 | REQUIRE(m.get(key9) == &int9); 195 | } 196 | 197 | SUBCASE("n_children == 0 && n_siblings > 1 (7)") { 198 | REQUIRE(m.del(key7) == &int7); 199 | REQUIRE(m.get(key0) == &int0); 200 | REQUIRE(m.get(key1) == &int1); 201 | REQUIRE(m.get(key2) == &int2); 202 | REQUIRE(m.get(key3) == &int3); 203 | REQUIRE(m.get(key4) == &int4); 204 | REQUIRE(m.get(key5) == &int5); 205 | REQUIRE(m.get(key6) == &int6); 206 | REQUIRE(m.get(key7) == nullptr); 207 | REQUIRE(m.get(key8) == &int8); 208 | REQUIRE(m.get(key9) == &int9); 209 | } 210 | 211 | SUBCASE("n_children == 1 (0),(5)") { 212 | REQUIRE(m.del(key0) == &int0); 213 | REQUIRE(m.get(key0) == nullptr); 214 | REQUIRE(m.get(key1) == &int1); 215 | REQUIRE(m.get(key2) == &int2); 216 | REQUIRE(m.get(key3) == &int3); 217 | REQUIRE(m.get(key4) == &int4); 218 | REQUIRE(m.get(key5) == &int5); 219 | REQUIRE(m.get(key6) == &int6); 220 | REQUIRE(m.get(key7) == &int7); 221 | REQUIRE(m.get(key8) == &int8); 222 | REQUIRE(m.get(key9) == &int9); 223 | 224 | REQUIRE(m.del(key5) == &int5); 225 | REQUIRE(m.get(key0) == nullptr); 226 | REQUIRE(m.get(key1) == &int1); 227 | REQUIRE(m.get(key2) == &int2); 228 | REQUIRE(m.get(key3) == &int3); 229 | REQUIRE(m.get(key4) == &int4); 230 | REQUIRE(m.get(key5) == nullptr); 231 | REQUIRE(m.get(key6) == &int6); 232 | REQUIRE(m.get(key7) == &int7); 233 | REQUIRE(m.get(key8) == &int8); 234 | REQUIRE(m.get(key9) == &int9); 235 | } 236 | 237 | SUBCASE("n_children > 1 (3),(2),(1)") { 238 | REQUIRE(m.del(key3) == &int3); 239 | REQUIRE(m.get(key0) == &int0); 240 | REQUIRE(m.get(key1) == &int1); 241 | REQUIRE(m.get(key2) == &int2); 242 | REQUIRE(m.get(key3) == nullptr); 243 | REQUIRE(m.get(key4) == &int4); 244 | REQUIRE(m.get(key5) == &int5); 245 | REQUIRE(m.get(key6) == &int6); 246 | REQUIRE(m.get(key7) == &int7); 247 | REQUIRE(m.get(key8) == &int8); 248 | REQUIRE(m.get(key9) == &int9); 249 | 250 | REQUIRE(m.del(key2) == &int2); 251 | REQUIRE(m.get(key0) == &int0); 252 | REQUIRE(m.get(key1) == &int1); 253 | REQUIRE(m.get(key2) == nullptr); 254 | REQUIRE(m.get(key3) == nullptr); 255 | REQUIRE(m.get(key4) == &int4); 256 | REQUIRE(m.get(key5) == &int5); 257 | REQUIRE(m.get(key6) == &int6); 258 | REQUIRE(m.get(key7) == &int7); 259 | REQUIRE(m.get(key8) == &int8); 260 | REQUIRE(m.get(key9) == &int9); 261 | 262 | REQUIRE(m.del(key1) == &int1); 263 | REQUIRE(m.get(key0) == &int0); 264 | REQUIRE(m.get(key1) == nullptr); 265 | REQUIRE(m.get(key2) == nullptr); 266 | REQUIRE(m.get(key3) == nullptr); 267 | REQUIRE(m.get(key4) == &int4); 268 | REQUIRE(m.get(key5) == &int5); 269 | REQUIRE(m.get(key6) == &int6); 270 | REQUIRE(m.get(key7) == &int7); 271 | REQUIRE(m.get(key8) == &int8); 272 | REQUIRE(m.get(key9) == &int9); 273 | } 274 | } 275 | 276 | TEST_CASE("monte carlo delete") { 277 | art::art m; 278 | mt19937_64 rng1(0); 279 | for (int i = 0; i < 1000000; ++i) { 280 | auto k = to_string(rng1()); 281 | auto v = new int(); 282 | *v = i; 283 | REQUIRE(m.set(k.c_str(), v) == nullptr); 284 | } 285 | mt19937_64 rng2(0); 286 | for (int i = 0; i < 1000000; ++i) { 287 | auto k = to_string(rng2()); 288 | auto get_res = m.get(k.c_str()); 289 | auto del_res = m.del(k.c_str()); 290 | REQUIRE(m.get(k.c_str()) == nullptr); 291 | REQUIRE(get_res == del_res); 292 | REQUIRE(del_res != nullptr); 293 | REQUIRE(*del_res == i); 294 | delete del_res; 295 | } 296 | } 297 | } 298 | -------------------------------------------------------------------------------- /test/inner_node.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file inner node abstract class tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace ::art; 17 | 18 | using std::array; 19 | using std::make_pair; 20 | using std::make_shared; 21 | using std::mt19937; 22 | using std::pair; 23 | using std::random_device; 24 | using std::shared_ptr; 25 | using std::shuffle; 26 | using std::string; 27 | 28 | TEST_SUITE("inner_node") { 29 | 30 | TEST_CASE("iteration") { 31 | node_4 m; 32 | 33 | leaf_node n0(nullptr); 34 | leaf_node n1(nullptr); 35 | leaf_node n2(nullptr); 36 | leaf_node n3(nullptr); 37 | 38 | m.set_child(0, &n0); 39 | m.set_child(5, &n1); 40 | m.set_child(6, &n2); 41 | m.set_child(127, &n3); 42 | 43 | auto it = m.begin(); 44 | auto it_end = m.end(); 45 | 46 | // 0 47 | REQUIRE(it < it_end); 48 | REQUIRE(it <= it_end); 49 | REQUIRE(it_end > it); 50 | REQUIRE(it_end >= it); 51 | REQUIRE(it != it_end); 52 | REQUIRE_EQ(0, *it); 53 | 54 | ++it; 55 | // 1 56 | REQUIRE(it < it_end); 57 | REQUIRE_EQ(5, *it); 58 | 59 | ++it; 60 | // 2 61 | REQUIRE(it < it_end); 62 | REQUIRE_EQ(6, *it); 63 | 64 | ++it; 65 | // 3 66 | REQUIRE(it < it_end); 67 | REQUIRE_EQ(127, *it); 68 | 69 | ++it; 70 | // 4 (overflow) 71 | REQUIRE(it == it_end); 72 | REQUIRE(it <= it_end); 73 | REQUIRE(it >= it_end); 74 | 75 | --it; 76 | // 3 77 | REQUIRE(it < it_end); 78 | REQUIRE_EQ(127, *it); 79 | 80 | --it; 81 | // 2 82 | REQUIRE(it < it_end); 83 | REQUIRE_EQ(6, *it); 84 | 85 | --it; 86 | // 1 87 | REQUIRE(it < it_end); 88 | REQUIRE_EQ(5, *it); 89 | 90 | --it; 91 | // 0 92 | REQUIRE(it < it_end); 93 | REQUIRE_EQ(0, *it); 94 | 95 | --it; 96 | // -1 (underflow) 97 | REQUIRE(it < it_end); 98 | 99 | ++it; 100 | // 0 101 | REQUIRE(it < it_end); 102 | REQUIRE_EQ(0, *it); 103 | } 104 | 105 | TEST_CASE("reverse iteration") { 106 | node_4 m; 107 | 108 | leaf_node n0(nullptr); 109 | leaf_node n1(nullptr); 110 | leaf_node n2(nullptr); 111 | leaf_node n3(nullptr); 112 | 113 | m.set_child(0, &n0); 114 | m.set_child(5, &n1); 115 | m.set_child(6, &n2); 116 | m.set_child(127, &n3); 117 | 118 | auto it = m.rbegin(); 119 | auto it_end = m.rend(); 120 | 121 | // 0 122 | REQUIRE(it < it_end); 123 | REQUIRE(it <= it_end); 124 | REQUIRE(it_end > it); 125 | REQUIRE(it_end >= it); 126 | REQUIRE(it != it_end); 127 | REQUIRE_EQ(127, (int) *it); 128 | 129 | ++it; 130 | // 1 131 | REQUIRE_EQ(6, (int) *it); 132 | 133 | ++it; 134 | // 2 135 | REQUIRE(it != it_end); 136 | REQUIRE_EQ(5, (int) *it); 137 | 138 | ++it; 139 | // 3 140 | REQUIRE(it != it_end); 141 | REQUIRE_EQ(0, *it); 142 | 143 | ++it; 144 | // 4 (overflow) 145 | REQUIRE(it == it_end); 146 | REQUIRE(it <= it_end); 147 | REQUIRE(it >= it_end); 148 | 149 | --it; 150 | // 3 151 | REQUIRE(it != it_end); 152 | REQUIRE_EQ(0, *it); 153 | 154 | --it; 155 | // 2 156 | REQUIRE(it != it_end); 157 | REQUIRE_EQ(5, (int) *it); 158 | 159 | --it; 160 | // 1 161 | REQUIRE(it != it_end); 162 | REQUIRE_EQ(6, (int) *it); 163 | 164 | --it; 165 | // 0 166 | REQUIRE(it != it_end); 167 | REQUIRE_EQ(127, (int) *it); 168 | 169 | --it; 170 | // -1 (underflow) 171 | REQUIRE(it != it_end); 172 | 173 | ++it; 174 | // 0 175 | REQUIRE(it != it_end); 176 | REQUIRE_EQ(127, (int) *it); 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file test entrypoint 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN 7 | #include "doctest.h" 8 | -------------------------------------------------------------------------------- /test/node.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node abstract class tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace ::art; 17 | 18 | using std::array; 19 | using std::make_pair; 20 | using std::make_shared; 21 | using std::mt19937; 22 | using std::pair; 23 | using std::random_device; 24 | using std::shared_ptr; 25 | using std::shuffle; 26 | using std::string; 27 | 28 | TEST_SUITE("node") { 29 | 30 | TEST_CASE("check_prefix") { 31 | leaf_node node(nullptr); 32 | string key = "000100001"; 33 | int key_len = key.length() + 1; // +1 for \0 34 | string prefix = "0000"; 35 | int prefix_len = prefix.length() + 1; // +1 for \0 36 | 37 | node.prefix_ = (char *) prefix.c_str(); 38 | node.prefix_len_ = prefix_len; 39 | 40 | CHECK_EQ(3, node.check_prefix(key.c_str() + 0, key_len - 0)); 41 | CHECK_EQ(2, node.check_prefix(key.c_str() + 1, key_len - 1)); 42 | CHECK_EQ(1, node.check_prefix(key.c_str() + 2, key_len - 2)); 43 | CHECK_EQ(0, node.check_prefix(key.c_str() + 3, key_len - 3)); 44 | CHECK_EQ(4, node.check_prefix(key.c_str() + 4, key_len - 4)); 45 | CHECK_EQ(3, node.check_prefix(key.c_str() + 5, key_len - 5)); 46 | CHECK_EQ(2, node.check_prefix(key.c_str() + 6, key_len - 6)); 47 | CHECK_EQ(1, node.check_prefix(key.c_str() + 7, key_len - 7)); 48 | CHECK_EQ(0, node.check_prefix(key.c_str() + 8, key_len - 8)); 49 | CHECK_EQ(0, node.check_prefix(key.c_str() + 9, key_len - 9)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/node_16.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_16 tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace art; 14 | 15 | using std::array; 16 | using std::mt19937; 17 | using std::random_device; 18 | using std::shuffle; 19 | 20 | TEST_SUITE("node 16") { 21 | 22 | TEST_CASE("monte carlo") { 23 | /* set up */ 24 | array partial_keys; 25 | array *, 256> children; 26 | 27 | for (int i = 0; i < 256; i += 1) { 28 | /* populate partial_keys with all values in the partial_keys_t domain */ 29 | partial_keys[i] = i; 30 | 31 | /* populate child nodes */ 32 | children[i] = new leaf_node(nullptr); 33 | } 34 | 35 | /* rng */ 36 | random_device rd; 37 | mt19937 g(rd()); 38 | 39 | for (int experiment = 0; experiment < 1000; experiment += 1) { 40 | /* test subject */ 41 | node_16 node; 42 | 43 | /* shuffle in order to make a seemingly random insertion order */ 44 | shuffle(partial_keys.begin(), partial_keys.end(), g); 45 | for (int i = 0; i < 16; i += 1) { 46 | REQUIRE_FALSE(node.is_full()); 47 | 48 | auto partial_key = partial_keys[i]; 49 | auto child = children[partial_key]; 50 | node.set_child(partial_key, child); 51 | 52 | for (int j = 0; j <= i; j += 1) { 53 | auto p_k = partial_keys[j]; 54 | auto expected_child = children[p_k]; 55 | auto actual_child_ptr = node.find_child(p_k); 56 | REQUIRE(actual_child_ptr != nullptr); 57 | auto actual_child = *actual_child_ptr; 58 | REQUIRE_EQ(expected_child, actual_child); 59 | } 60 | } 61 | REQUIRE(node.is_full()); 62 | } 63 | 64 | /* tear down */ 65 | for (int i = 0; i < 256; i += 1) { 66 | delete children[i]; 67 | } 68 | } 69 | 70 | TEST_CASE("delete child") { 71 | leaf_node n0(nullptr); 72 | leaf_node n1(nullptr); 73 | leaf_node n2(nullptr); 74 | leaf_node n3(nullptr); 75 | leaf_node n4(nullptr); 76 | leaf_node n5(nullptr); 77 | leaf_node n6(nullptr); 78 | 79 | node_16 subject; 80 | 81 | subject.set_child(1, &n1); 82 | subject.set_child(2, &n2); 83 | subject.set_child(4, &n4); 84 | subject.set_child(5, &n5); 85 | 86 | SUBCASE("delete child that doesn't exist (0)") { 87 | REQUIRE(subject.del_child(0) == nullptr); 88 | REQUIRE(*subject.find_child(1) == &n1); 89 | REQUIRE(*subject.find_child(2) == &n2); 90 | REQUIRE(*subject.find_child(4) == &n4); 91 | REQUIRE(*subject.find_child(5) == &n5); 92 | } 93 | 94 | SUBCASE("delete min (1)") { 95 | REQUIRE(subject.del_child(1) == &n1); 96 | REQUIRE(subject.find_child(1) == nullptr); 97 | REQUIRE(*subject.find_child(2) == &n2); 98 | REQUIRE(*subject.find_child(4) == &n4); 99 | REQUIRE(*subject.find_child(5) == &n5); 100 | } 101 | 102 | SUBCASE("delete inner (2)") { 103 | REQUIRE(subject.del_child(2) == &n2); 104 | REQUIRE(*subject.find_child(1) == &n1); 105 | REQUIRE(subject.find_child(2) == nullptr); 106 | REQUIRE(*subject.find_child(4) == &n4); 107 | REQUIRE(*subject.find_child(5) == &n5); 108 | } 109 | 110 | SUBCASE("delete child that doesn't exist (3)") { 111 | REQUIRE(subject.del_child(3) == nullptr); 112 | REQUIRE(*subject.find_child(1) == &n1); 113 | REQUIRE(*subject.find_child(2) == &n2); 114 | REQUIRE(*subject.find_child(4) == &n4); 115 | REQUIRE(*subject.find_child(5) == &n5); 116 | } 117 | 118 | SUBCASE("delete inner (4)") { 119 | REQUIRE(subject.del_child(4) == &n4); 120 | REQUIRE(*subject.find_child(1) == &n1); 121 | REQUIRE(*subject.find_child(2) == &n2); 122 | REQUIRE(subject.find_child(4) == nullptr); 123 | REQUIRE(*subject.find_child(5) == &n5); 124 | } 125 | 126 | SUBCASE("delete max (5)") { 127 | REQUIRE(subject.del_child(5) == &n5); 128 | REQUIRE(*subject.find_child(1) == &n1); 129 | REQUIRE(*subject.find_child(2) == &n2); 130 | REQUIRE(*subject.find_child(4) == &n4); 131 | REQUIRE(subject.find_child(5) == nullptr); 132 | } 133 | 134 | SUBCASE("delete child that doesn't exist (6)") { 135 | REQUIRE(subject.del_child(6) == nullptr); 136 | REQUIRE(*subject.find_child(1) == &n1); 137 | REQUIRE(*subject.find_child(2) == &n2); 138 | REQUIRE(*subject.find_child(4) == &n4); 139 | REQUIRE(*subject.find_child(5) == &n5); 140 | } 141 | } 142 | 143 | TEST_CASE("next partial key") { 144 | node_16 n; 145 | 146 | SUBCASE("completely empty node") { 147 | REQUIRE_THROWS_AS(n.next_partial_key(0), std::out_of_range); 148 | } 149 | 150 | SUBCASE("child at -128") { 151 | n.set_child(-128, nullptr); 152 | REQUIRE_EQ(-128, n.next_partial_key(-128)); 153 | for (int i = 1; i < 256; ++i) { 154 | REQUIRE_THROWS_AS(n.next_partial_key(i - 128), std::out_of_range); 155 | } 156 | } 157 | 158 | 159 | SUBCASE("child at 127") { 160 | n.set_child(127, nullptr); 161 | for (int i = 0; i < 256; ++i) { 162 | REQUIRE_EQ(127, n.next_partial_key(i - 128)); 163 | } 164 | } 165 | 166 | SUBCASE("dense children") { 167 | n.set_child(0, nullptr); 168 | n.set_child(1, nullptr); 169 | n.set_child(2, nullptr); 170 | n.set_child(3, nullptr); 171 | REQUIRE_EQ(0, n.next_partial_key(0)); 172 | REQUIRE_EQ(1, n.next_partial_key(1)); 173 | REQUIRE_EQ(2, n.next_partial_key(2)); 174 | REQUIRE_EQ(3, n.next_partial_key(3)); 175 | REQUIRE_THROWS_AS(n.next_partial_key(4), std::out_of_range); 176 | } 177 | 178 | SUBCASE("sparse children") { 179 | n.set_child(0, nullptr); 180 | n.set_child(5, nullptr); 181 | n.set_child(10, nullptr); 182 | n.set_child(100, nullptr); 183 | REQUIRE_EQ(0, n.next_partial_key(0)); 184 | REQUIRE_EQ(5, n.next_partial_key(1)); 185 | REQUIRE_EQ(10, n.next_partial_key(6)); 186 | REQUIRE_EQ(100, n.next_partial_key(11)); 187 | REQUIRE_THROWS_AS(n.next_partial_key(101), std::out_of_range); 188 | } 189 | } 190 | 191 | TEST_CASE("previous partial key") { 192 | node_16 n; 193 | 194 | SUBCASE("completely empty node") { 195 | REQUIRE_THROWS_AS(n.prev_partial_key(127), std::out_of_range); 196 | } 197 | 198 | SUBCASE("child at -128") { 199 | n.set_child(-128, nullptr); 200 | for (int i = 0; i < 256; ++i) { 201 | REQUIRE_EQ(-128, n.prev_partial_key(i - 128)); 202 | } 203 | } 204 | 205 | 206 | SUBCASE("child at 127") { 207 | n.set_child(127, nullptr); 208 | REQUIRE_EQ(127, n.prev_partial_key(127)); 209 | for (int i = 0; i < 255; ++i) { 210 | REQUIRE_THROWS_AS(n.prev_partial_key(i - 128), std::out_of_range); 211 | } 212 | } 213 | 214 | SUBCASE("dense children") { 215 | n.set_child(1, nullptr); 216 | n.set_child(2, nullptr); 217 | n.set_child(3, nullptr); 218 | n.set_child(4, nullptr); 219 | REQUIRE_EQ(1, n.prev_partial_key(1)); 220 | REQUIRE_EQ(2, n.prev_partial_key(2)); 221 | REQUIRE_EQ(3, n.prev_partial_key(3)); 222 | REQUIRE_EQ(4, n.prev_partial_key(4)); 223 | REQUIRE_EQ(4, n.prev_partial_key(127)); 224 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 225 | } 226 | 227 | SUBCASE("sparse children") { 228 | n.set_child(1, nullptr); 229 | n.set_child(5, nullptr); 230 | n.set_child(10, nullptr); 231 | n.set_child(100, nullptr); 232 | REQUIRE_EQ(1, n.prev_partial_key(4)); 233 | REQUIRE_EQ(5, n.prev_partial_key(9)); 234 | REQUIRE_EQ(10, n.prev_partial_key(99)); 235 | REQUIRE_EQ(100, n.prev_partial_key(127)); 236 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 237 | } 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /test/node_256.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_256 tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace art; 14 | 15 | using std::array; 16 | using std::mt19937; 17 | using std::random_device; 18 | using std::shuffle; 19 | 20 | TEST_SUITE("node 256") { 21 | 22 | TEST_CASE("monte carlo") { 23 | /* set up */ 24 | array partial_keys; 25 | array *, 256> children; 26 | 27 | for (int i = 0; i < 256; i += 1) { 28 | /* populate partial_keys with all values in the char domain */ 29 | partial_keys[i] = i - 128; 30 | children[i] = new leaf_node(nullptr); 31 | } 32 | 33 | /* rng */ 34 | mt19937 g(0); 35 | 36 | for (int experiment = 0; experiment < 100; experiment += 1) { 37 | /* test subject */ 38 | node_256 node; 39 | 40 | /* shuffle in order to make a seemingly random insertion order */ 41 | shuffle(partial_keys.begin(), partial_keys.end(), g); 42 | for (int i = 0; i < 256; i += 1) { 43 | REQUIRE_FALSE(node.is_full()); 44 | 45 | auto partial_key = partial_keys[i]; 46 | auto child = children[i]; 47 | node.set_child(partial_key, child); 48 | 49 | for (int j = 0; j <= i; j += 1) { 50 | auto p_k = partial_keys[j]; 51 | auto expected_child = children[j]; 52 | auto actual_child_ptr = node.find_child(p_k); 53 | REQUIRE(actual_child_ptr != nullptr); 54 | auto actual_child = *actual_child_ptr; 55 | REQUIRE_EQ(expected_child, actual_child); 56 | } 57 | } 58 | REQUIRE(node.is_full()); 59 | } 60 | 61 | for (int i = 0; i < 256; ++i) { 62 | delete children[i]; 63 | } 64 | } 65 | 66 | TEST_CASE("delete child") { 67 | leaf_node n0(nullptr); 68 | leaf_node n1(nullptr); 69 | leaf_node n2(nullptr); 70 | leaf_node n3(nullptr); 71 | leaf_node n4(nullptr); 72 | leaf_node n5(nullptr); 73 | leaf_node n6(nullptr); 74 | 75 | node_256 subject; 76 | 77 | subject.set_child(1, &n1); 78 | subject.set_child(2, &n2); 79 | subject.set_child(4, &n4); 80 | subject.set_child(5, &n5); 81 | 82 | SUBCASE("delete child that doesn't exist (0)") { 83 | REQUIRE(subject.del_child(0) == nullptr); 84 | REQUIRE(*subject.find_child(1) == &n1); 85 | REQUIRE(*subject.find_child(2) == &n2); 86 | REQUIRE(*subject.find_child(4) == &n4); 87 | REQUIRE(*subject.find_child(5) == &n5); 88 | } 89 | 90 | SUBCASE("delete min (1)") { 91 | REQUIRE(subject.del_child(1) == &n1); 92 | REQUIRE(subject.find_child(1) == nullptr); 93 | REQUIRE(*subject.find_child(2) == &n2); 94 | REQUIRE(*subject.find_child(4) == &n4); 95 | REQUIRE(*subject.find_child(5) == &n5); 96 | } 97 | 98 | SUBCASE("delete inner (2)") { 99 | REQUIRE(subject.del_child(2) == &n2); 100 | REQUIRE(*subject.find_child(1) == &n1); 101 | REQUIRE(subject.find_child(2) == nullptr); 102 | REQUIRE(*subject.find_child(4) == &n4); 103 | REQUIRE(*subject.find_child(5) == &n5); 104 | } 105 | 106 | SUBCASE("delete child that doesn't exist (3)") { 107 | REQUIRE(subject.del_child(3) == nullptr); 108 | REQUIRE(*subject.find_child(1) == &n1); 109 | REQUIRE(*subject.find_child(2) == &n2); 110 | REQUIRE(*subject.find_child(4) == &n4); 111 | REQUIRE(*subject.find_child(5) == &n5); 112 | } 113 | 114 | SUBCASE("delete inner (4)") { 115 | REQUIRE(subject.del_child(4) == &n4); 116 | REQUIRE(*subject.find_child(1) == &n1); 117 | REQUIRE(*subject.find_child(2) == &n2); 118 | REQUIRE(subject.find_child(4) == nullptr); 119 | REQUIRE(*subject.find_child(5) == &n5); 120 | } 121 | 122 | SUBCASE("delete max (5)") { 123 | REQUIRE(subject.del_child(5) == &n5); 124 | REQUIRE(*subject.find_child(1) == &n1); 125 | REQUIRE(*subject.find_child(2) == &n2); 126 | REQUIRE(*subject.find_child(4) == &n4); 127 | REQUIRE(subject.find_child(5) == nullptr); 128 | } 129 | 130 | SUBCASE("delete child that doesn't exist (6)") { 131 | REQUIRE(subject.del_child(6) == nullptr); 132 | REQUIRE(*subject.find_child(1) == &n1); 133 | REQUIRE(*subject.find_child(2) == &n2); 134 | REQUIRE(*subject.find_child(4) == &n4); 135 | REQUIRE(*subject.find_child(5) == &n5); 136 | } 137 | } 138 | 139 | TEST_CASE("next partial key") { 140 | node_256 n; 141 | 142 | SUBCASE("completely empty node") { 143 | REQUIRE_THROWS_AS(n.next_partial_key(-128), std::out_of_range); 144 | } 145 | 146 | SUBCASE("child at -128") { 147 | leaf_node n0(nullptr); 148 | n.set_child(-128, &n0); 149 | REQUIRE_EQ(-128, n.next_partial_key(-128)); 150 | for (int i = 1; i < 256; ++i) { 151 | REQUIRE_THROWS_AS(n.next_partial_key(i - 128), std::out_of_range); 152 | } 153 | } 154 | 155 | SUBCASE("child at 127") { 156 | leaf_node n0(nullptr); 157 | n.set_child(127, &n0); 158 | for (int i = 0; i < 256; ++i) { 159 | REQUIRE_EQ(127, n.next_partial_key(i - 128)); 160 | } 161 | } 162 | 163 | SUBCASE("dense children") { 164 | leaf_node n0(nullptr); 165 | leaf_node n1(nullptr); 166 | leaf_node n2(nullptr); 167 | leaf_node n3(nullptr); 168 | n.set_child(0, &n0); 169 | n.set_child(1, &n1); 170 | n.set_child(2, &n2); 171 | n.set_child(3, &n3); 172 | REQUIRE_EQ(0, n.next_partial_key(0)); 173 | REQUIRE_EQ(1, n.next_partial_key(1)); 174 | REQUIRE_EQ(2, n.next_partial_key(2)); 175 | REQUIRE_EQ(3, n.next_partial_key(3)); 176 | REQUIRE_THROWS_AS(n.next_partial_key(4), std::out_of_range); 177 | } 178 | 179 | SUBCASE("sparse children") { 180 | leaf_node n0(nullptr); 181 | leaf_node n1(nullptr); 182 | leaf_node n2(nullptr); 183 | leaf_node n3(nullptr); 184 | n.set_child(0, &n0); 185 | n.set_child(5, &n1); 186 | n.set_child(10, &n2); 187 | n.set_child(100, &n3); 188 | REQUIRE_EQ(0, n.next_partial_key(0)); 189 | REQUIRE_EQ(5, n.next_partial_key(1)); 190 | REQUIRE_EQ(10, n.next_partial_key(6)); 191 | REQUIRE_EQ(100, n.next_partial_key(11)); 192 | REQUIRE_THROWS_AS(n.next_partial_key(101), std::out_of_range); 193 | } 194 | } 195 | 196 | TEST_CASE("previous partial key") { 197 | node_256 n; 198 | 199 | SUBCASE("completely empty node") { 200 | for (int i = 0; i < 256; ++i) { 201 | REQUIRE_THROWS_AS(n.prev_partial_key(i - 128), std::out_of_range); 202 | } 203 | } 204 | 205 | SUBCASE("child at -128") { 206 | leaf_node n0(nullptr); 207 | n.set_child(-128, &n0); 208 | for (int i = 0; i < 256; ++i) { 209 | REQUIRE_EQ(-128, n.prev_partial_key(i - 128)); 210 | } 211 | } 212 | 213 | SUBCASE("child at 127") { 214 | leaf_node n0(nullptr); 215 | n.set_child(127, &n0); 216 | REQUIRE_EQ(127, n.prev_partial_key(127)); 217 | for (int i = 0; i < 255; ++i) { 218 | REQUIRE_THROWS_AS(n.prev_partial_key(i - 128), std::out_of_range); 219 | } 220 | } 221 | 222 | SUBCASE("dense children") { 223 | leaf_node n0(nullptr); 224 | leaf_node n1(nullptr); 225 | leaf_node n2(nullptr); 226 | leaf_node n3(nullptr); 227 | n.set_child(1, &n0); 228 | n.set_child(2, &n1); 229 | n.set_child(3, &n2); 230 | n.set_child(4, &n3); 231 | REQUIRE_EQ(1, n.prev_partial_key(1)); 232 | REQUIRE_EQ(2, n.prev_partial_key(2)); 233 | REQUIRE_EQ(3, n.prev_partial_key(3)); 234 | REQUIRE_EQ(4, n.prev_partial_key(4)); 235 | REQUIRE_EQ(4, n.prev_partial_key(127)); 236 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 237 | } 238 | 239 | SUBCASE("sparse children") { 240 | leaf_node n0(nullptr); 241 | leaf_node n1(nullptr); 242 | leaf_node n2(nullptr); 243 | leaf_node n3(nullptr); 244 | n.set_child(1, &n0); 245 | n.set_child(5, &n1); 246 | n.set_child(10, &n2); 247 | n.set_child(100, &n3); 248 | REQUIRE_EQ(1, n.prev_partial_key(4)); 249 | REQUIRE_EQ(5, n.prev_partial_key(9)); 250 | REQUIRE_EQ(10, n.prev_partial_key(99)); 251 | REQUIRE_EQ(100, n.prev_partial_key(127)); 252 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 253 | } 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /test/node_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_4 tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace art; 15 | 16 | using std::array; 17 | using std::mt19937; 18 | using std::random_device; 19 | using std::shuffle; 20 | 21 | TEST_SUITE("node 4") { 22 | 23 | TEST_CASE("monte carlo insert") { 24 | /* set up */ 25 | array partial_keys; 26 | array *, 256> children; 27 | 28 | for (int i = 0; i < 256; i += 1) { 29 | /* populate partial_keys with all values in the partial_keys_t domain */ 30 | partial_keys[i] = i; 31 | 32 | /* populate child nodes */ 33 | children[i] = new leaf_node(nullptr); 34 | } 35 | 36 | /* rng */ 37 | random_device rd; 38 | mt19937 g(rd()); 39 | 40 | for (int experiment = 0; experiment < 10000; experiment += 1) { 41 | /* test subject */ 42 | node_4 node; 43 | 44 | /* shuffle in order to make a seemingly random insertion order */ 45 | shuffle(partial_keys.begin(), partial_keys.end(), g); 46 | for (int i = 0; i < 4; i += 1) { 47 | REQUIRE_FALSE(node.is_full()); 48 | 49 | auto partial_key = partial_keys[i]; 50 | auto child = children[partial_key]; 51 | node.set_child(partial_key, child); 52 | 53 | for (int j = 0; j <= i; j += 1) { 54 | auto p_k = partial_keys[j]; 55 | auto expected_child = children[p_k]; 56 | auto actual_child_ptr = node.find_child(p_k); 57 | REQUIRE(actual_child_ptr != nullptr); 58 | auto actual_child = *actual_child_ptr; 59 | REQUIRE_EQ(expected_child, actual_child); 60 | } 61 | } 62 | REQUIRE(node.is_full()); 63 | } 64 | 65 | /* tear down */ 66 | for (int i = 0; i < 256; i += 1) { 67 | delete children[i]; 68 | } 69 | } 70 | 71 | TEST_CASE("delete child") { 72 | leaf_node n0(nullptr); 73 | leaf_node n1(nullptr); 74 | leaf_node n2(nullptr); 75 | leaf_node n3(nullptr); 76 | leaf_node n4(nullptr); 77 | leaf_node n5(nullptr); 78 | leaf_node n6(nullptr); 79 | 80 | node_4 subject; 81 | 82 | subject.set_child(1, &n1); 83 | subject.set_child(2, &n2); 84 | subject.set_child(4, &n4); 85 | subject.set_child(5, &n5); 86 | 87 | SUBCASE("delete child that doesn't exist (0)") { 88 | REQUIRE(subject.del_child(0) == nullptr); 89 | REQUIRE(*subject.find_child(1) == &n1); 90 | REQUIRE(*subject.find_child(2) == &n2); 91 | REQUIRE(*subject.find_child(4) == &n4); 92 | REQUIRE(*subject.find_child(5) == &n5); 93 | } 94 | 95 | SUBCASE("delete min (1)") { 96 | REQUIRE(subject.del_child(1) == &n1); 97 | REQUIRE(subject.find_child(1) == nullptr); 98 | REQUIRE(*subject.find_child(2) == &n2); 99 | REQUIRE(*subject.find_child(4) == &n4); 100 | REQUIRE(*subject.find_child(5) == &n5); 101 | } 102 | 103 | SUBCASE("delete inner (2)") { 104 | REQUIRE(subject.del_child(2) == &n2); 105 | REQUIRE(*subject.find_child(1) == &n1); 106 | REQUIRE(subject.find_child(2) == nullptr); 107 | REQUIRE(*subject.find_child(4) == &n4); 108 | REQUIRE(*subject.find_child(5) == &n5); 109 | } 110 | 111 | SUBCASE("delete child that doesn't exist (3)") { 112 | REQUIRE(subject.del_child(3) == nullptr); 113 | REQUIRE(*subject.find_child(1) == &n1); 114 | REQUIRE(*subject.find_child(2) == &n2); 115 | REQUIRE(*subject.find_child(4) == &n4); 116 | REQUIRE(*subject.find_child(5) == &n5); 117 | } 118 | 119 | SUBCASE("delete inner (4)") { 120 | REQUIRE(subject.del_child(4) == &n4); 121 | REQUIRE(*subject.find_child(1) == &n1); 122 | REQUIRE(*subject.find_child(2) == &n2); 123 | REQUIRE(subject.find_child(4) == nullptr); 124 | REQUIRE(*subject.find_child(5) == &n5); 125 | } 126 | 127 | SUBCASE("delete max (5)") { 128 | REQUIRE(subject.del_child(5) == &n5); 129 | REQUIRE(*subject.find_child(1) == &n1); 130 | REQUIRE(*subject.find_child(2) == &n2); 131 | REQUIRE(*subject.find_child(4) == &n4); 132 | REQUIRE(subject.find_child(5) == nullptr); 133 | } 134 | 135 | SUBCASE("delete child that doesn't exist (6)") { 136 | REQUIRE(subject.del_child(6) == nullptr); 137 | REQUIRE(*subject.find_child(1) == &n1); 138 | REQUIRE(*subject.find_child(2) == &n2); 139 | REQUIRE(*subject.find_child(4) == &n4); 140 | REQUIRE(*subject.find_child(5) == &n5); 141 | } 142 | } 143 | 144 | TEST_CASE("next partial key") { 145 | node_4 n; 146 | 147 | SUBCASE("completely empty node") { 148 | REQUIRE_THROWS_AS(n.next_partial_key(0), std::out_of_range); 149 | } 150 | 151 | SUBCASE("child at -128") { 152 | n.set_child(-128, nullptr); 153 | REQUIRE_EQ(-128, n.next_partial_key(-128)); 154 | for (int i = 1; i < 256; ++i) { 155 | REQUIRE_THROWS_AS(n.next_partial_key(i - 128), std::out_of_range); 156 | } 157 | } 158 | 159 | SUBCASE("child at 127") { 160 | n.set_child(127, nullptr); 161 | for (int i = 0; i < 256; ++i) { 162 | REQUIRE_EQ(127, n.next_partial_key(i - 128)); 163 | } 164 | } 165 | 166 | SUBCASE("dense children") { 167 | n.set_child(0, nullptr); 168 | n.set_child(1, nullptr); 169 | n.set_child(2, nullptr); 170 | n.set_child(3, nullptr); 171 | REQUIRE_EQ(0, n.next_partial_key(0)); 172 | REQUIRE_EQ(1, n.next_partial_key(1)); 173 | REQUIRE_EQ(2, n.next_partial_key(2)); 174 | REQUIRE_EQ(3, n.next_partial_key(3)); 175 | REQUIRE_THROWS_AS(n.next_partial_key(4), std::out_of_range); 176 | } 177 | 178 | SUBCASE("sparse children") { 179 | n.set_child(0, nullptr); 180 | n.set_child(5, nullptr); 181 | n.set_child(10, nullptr); 182 | n.set_child(100, nullptr); 183 | REQUIRE_EQ(0, n.next_partial_key(0)); 184 | REQUIRE_EQ(5, n.next_partial_key(1)); 185 | REQUIRE_EQ(10, n.next_partial_key(6)); 186 | REQUIRE_EQ(100, n.next_partial_key(11)); 187 | REQUIRE_THROWS_AS(n.next_partial_key(101), std::out_of_range); 188 | } 189 | } 190 | 191 | TEST_CASE("previous partial key") { 192 | node_4 n; 193 | 194 | SUBCASE("completely empty node") { 195 | REQUIRE_THROWS_AS(n.prev_partial_key(127), std::out_of_range); 196 | } 197 | 198 | SUBCASE("child at -128") { 199 | n.set_child(-128, nullptr); 200 | for (int i = 0; i < 256; ++i) { 201 | REQUIRE_EQ(-128, n.prev_partial_key(i - 128)); 202 | } 203 | } 204 | 205 | SUBCASE("child at 127") { 206 | n.set_child(127, nullptr); 207 | REQUIRE_EQ(127, n.prev_partial_key(127)); 208 | for (int i = 0; i < 255; ++i) { 209 | REQUIRE_THROWS_AS(n.prev_partial_key(i - 128), std::out_of_range); 210 | } 211 | } 212 | 213 | SUBCASE("dense children") { 214 | n.set_child(1, nullptr); 215 | n.set_child(2, nullptr); 216 | n.set_child(3, nullptr); 217 | n.set_child(4, nullptr); 218 | REQUIRE_EQ(1, n.prev_partial_key(1)); 219 | REQUIRE_EQ(2, n.prev_partial_key(2)); 220 | REQUIRE_EQ(3, n.prev_partial_key(3)); 221 | REQUIRE_EQ(4, n.prev_partial_key(4)); 222 | REQUIRE_EQ(4, n.prev_partial_key(127)); 223 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 224 | } 225 | 226 | SUBCASE("sparse children") { 227 | n.set_child(1, nullptr); 228 | n.set_child(5, nullptr); 229 | n.set_child(10, nullptr); 230 | n.set_child(100, nullptr); 231 | REQUIRE_EQ(1, n.prev_partial_key(4)); 232 | REQUIRE_EQ(5, n.prev_partial_key(9)); 233 | REQUIRE_EQ(10, n.prev_partial_key(99)); 234 | REQUIRE_EQ(100, n.prev_partial_key(127)); 235 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 236 | } 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /test/node_48.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file node_48 tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace art; 14 | 15 | using std::array; 16 | using std::mt19937; 17 | using std::shuffle; 18 | 19 | TEST_SUITE("node 48") { 20 | 21 | TEST_CASE("monte carlo") { 22 | /* set up */ 23 | array partial_keys; 24 | array *, 256> children; 25 | 26 | for (int i = 0; i < 256; i += 1) { 27 | /* populate partial_keys with all values in the partial_keys_t domain */ 28 | partial_keys[i] = i - 128; 29 | 30 | /* populate child nodes */ 31 | children[i] = new leaf_node(nullptr); 32 | } 33 | 34 | /* rng */ 35 | mt19937 g(0); 36 | 37 | for (int experiment = 0; experiment < 10000; experiment += 1) { 38 | /* test subject */ 39 | node_48 node; 40 | 41 | /* shuffle in order to make a seemingly random insertion order */ 42 | shuffle(partial_keys.begin(), partial_keys.end(), g); 43 | 44 | for (int i = 0; i < 48; i += 1) { 45 | REQUIRE_FALSE(node.is_full()); 46 | 47 | auto partial_key = partial_keys[i]; 48 | auto child = children[i]; 49 | node.set_child(partial_key, child); 50 | 51 | for (int j = 0; j <= i; j += 1) { 52 | auto p_k = partial_keys[j]; 53 | auto expected_child = children[j]; 54 | auto actual_child_ptr = node.find_child(p_k); 55 | REQUIRE(actual_child_ptr != nullptr); 56 | auto actual_child = *actual_child_ptr; 57 | REQUIRE_EQ(expected_child, actual_child); 58 | } 59 | } 60 | REQUIRE(node.is_full()); 61 | } 62 | 63 | /* tear down */ 64 | for (int i = 0; i < 256; i += 1) { 65 | delete children[i]; 66 | } 67 | } 68 | 69 | TEST_CASE("delete child") { 70 | leaf_node n0(nullptr); 71 | leaf_node n1(nullptr); 72 | leaf_node n2(nullptr); 73 | leaf_node n3(nullptr); 74 | leaf_node n4(nullptr); 75 | leaf_node n5(nullptr); 76 | leaf_node n6(nullptr); 77 | 78 | node_48 subject; 79 | 80 | subject.set_child(1, &n1); 81 | subject.set_child(2, &n2); 82 | subject.set_child(4, &n4); 83 | subject.set_child(5, &n5); 84 | 85 | SUBCASE("delete child that doesn't exist (0)") { 86 | REQUIRE(subject.del_child(0) == nullptr); 87 | REQUIRE(*subject.find_child(1) == &n1); 88 | REQUIRE(*subject.find_child(2) == &n2); 89 | REQUIRE(*subject.find_child(4) == &n4); 90 | REQUIRE(*subject.find_child(5) == &n5); 91 | } 92 | 93 | SUBCASE("delete min (1)") { 94 | REQUIRE(subject.del_child(1) == &n1); 95 | REQUIRE(subject.find_child(1) == nullptr); 96 | REQUIRE(*subject.find_child(2) == &n2); 97 | REQUIRE(*subject.find_child(4) == &n4); 98 | REQUIRE(*subject.find_child(5) == &n5); 99 | } 100 | 101 | SUBCASE("delete inner (2)") { 102 | REQUIRE(subject.del_child(2) == &n2); 103 | REQUIRE(*subject.find_child(1) == &n1); 104 | REQUIRE(subject.find_child(2) == nullptr); 105 | REQUIRE(*subject.find_child(4) == &n4); 106 | REQUIRE(*subject.find_child(5) == &n5); 107 | } 108 | 109 | SUBCASE("delete child that doesn't exist (3)") { 110 | REQUIRE(subject.del_child(3) == nullptr); 111 | REQUIRE(*subject.find_child(1) == &n1); 112 | REQUIRE(*subject.find_child(2) == &n2); 113 | REQUIRE(*subject.find_child(4) == &n4); 114 | REQUIRE(*subject.find_child(5) == &n5); 115 | } 116 | 117 | SUBCASE("delete inner (4)") { 118 | REQUIRE(subject.del_child(4) == &n4); 119 | REQUIRE(*subject.find_child(1) == &n1); 120 | REQUIRE(*subject.find_child(2) == &n2); 121 | REQUIRE(subject.find_child(4) == nullptr); 122 | REQUIRE(*subject.find_child(5) == &n5); 123 | } 124 | 125 | SUBCASE("delete max (5)") { 126 | REQUIRE(subject.del_child(5) == &n5); 127 | REQUIRE(*subject.find_child(1) == &n1); 128 | REQUIRE(*subject.find_child(2) == &n2); 129 | REQUIRE(*subject.find_child(4) == &n4); 130 | REQUIRE(subject.find_child(5) == nullptr); 131 | } 132 | 133 | SUBCASE("delete child that doesn't exist (6)") { 134 | REQUIRE(subject.del_child(6) == nullptr); 135 | REQUIRE(*subject.find_child(1) == &n1); 136 | REQUIRE(*subject.find_child(2) == &n2); 137 | REQUIRE(*subject.find_child(4) == &n4); 138 | REQUIRE(*subject.find_child(5) == &n5); 139 | } 140 | } 141 | 142 | TEST_CASE("next partial key") { 143 | node_48 n; 144 | 145 | SUBCASE("completely empty node") { 146 | for (int i = 0; i < 256; ++i) { 147 | REQUIRE_THROWS_AS(n.next_partial_key(-128), std::out_of_range); 148 | } 149 | } 150 | 151 | SUBCASE("child at -128") { 152 | n.set_child(-128, nullptr); 153 | REQUIRE_EQ(-128, n.next_partial_key(-128)); 154 | for (int i = 1; i < 256; ++i) { 155 | REQUIRE_THROWS_AS(n.next_partial_key(i - 128), std::out_of_range); 156 | } 157 | } 158 | 159 | SUBCASE("child at 127") { 160 | n.set_child(127, nullptr); 161 | for (int i = 0; i < 256; ++i) { 162 | REQUIRE_EQ(127, n.next_partial_key(i - 128)); 163 | } 164 | } 165 | 166 | SUBCASE("dense children") { 167 | n.set_child(0, nullptr); 168 | n.set_child(1, nullptr); 169 | n.set_child(2, nullptr); 170 | n.set_child(3, nullptr); 171 | REQUIRE_EQ(0, n.next_partial_key(0)); 172 | REQUIRE_EQ(1, n.next_partial_key(1)); 173 | REQUIRE_EQ(2, n.next_partial_key(2)); 174 | REQUIRE_EQ(3, n.next_partial_key(3)); 175 | REQUIRE_THROWS_AS(n.next_partial_key(4), std::out_of_range); 176 | } 177 | 178 | SUBCASE("sparse children") { 179 | n.set_child(0, nullptr); 180 | n.set_child(5, nullptr); 181 | n.set_child(10, nullptr); 182 | n.set_child(100, nullptr); 183 | REQUIRE_EQ(0, n.next_partial_key(0)); 184 | REQUIRE_EQ(5, n.next_partial_key(1)); 185 | REQUIRE_EQ(10, n.next_partial_key(6)); 186 | REQUIRE_EQ(100, n.next_partial_key(11)); 187 | REQUIRE_THROWS_AS(n.next_partial_key(101), std::out_of_range); 188 | } 189 | } 190 | 191 | TEST_CASE("previous partial key") { 192 | node_48 n; 193 | 194 | SUBCASE("completely empty node") { 195 | REQUIRE_THROWS_AS(n.prev_partial_key(127), std::out_of_range); 196 | } 197 | 198 | SUBCASE("child at -128") { 199 | n.set_child(-128, nullptr); 200 | for (int i = 0; i < 256; ++i) { 201 | REQUIRE_EQ(-128, n.prev_partial_key(i - 128)); 202 | } 203 | } 204 | 205 | SUBCASE("child at 127") { 206 | n.set_child(127, nullptr); 207 | REQUIRE_EQ(127, n.prev_partial_key(127)); 208 | for (int i = 0; i < 255; ++i) { 209 | REQUIRE_THROWS_AS(n.prev_partial_key(i - 128), std::out_of_range); 210 | } 211 | } 212 | 213 | SUBCASE("dense children") { 214 | n.set_child(1, nullptr); 215 | n.set_child(2, nullptr); 216 | n.set_child(3, nullptr); 217 | n.set_child(4, nullptr); 218 | REQUIRE_EQ(1, n.prev_partial_key(1)); 219 | REQUIRE_EQ(2, n.prev_partial_key(2)); 220 | REQUIRE_EQ(3, n.prev_partial_key(3)); 221 | REQUIRE_EQ(4, n.prev_partial_key(4)); 222 | REQUIRE_EQ(4, n.prev_partial_key(127)); 223 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 224 | } 225 | 226 | SUBCASE("sparse children") { 227 | n.set_child(1, nullptr); 228 | n.set_child(5, nullptr); 229 | n.set_child(10, nullptr); 230 | n.set_child(100, nullptr); 231 | REQUIRE_EQ(1, n.prev_partial_key(4)); 232 | REQUIRE_EQ(5, n.prev_partial_key(9)); 233 | REQUIRE_EQ(10, n.prev_partial_key(99)); 234 | REQUIRE_EQ(100, n.prev_partial_key(127)); 235 | REQUIRE_THROWS_AS(n.prev_partial_key(0), std::out_of_range); 236 | } 237 | } 238 | } 239 | -------------------------------------------------------------------------------- /test/tree_it.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file tree_it tests 3 | * @author Rafael Kallis 4 | */ 5 | 6 | #include "art.hpp" 7 | #include "doctest.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | using std::array; 16 | using std::hash; 17 | using std::mt19937; 18 | using std::mt19937_64; 19 | using std::random_device; 20 | using std::shuffle; 21 | using std::string; 22 | using std::to_string; 23 | 24 | TEST_SUITE("tree_it") { 25 | TEST_CASE("full lexicographic traversal") { 26 | SUBCASE("controlled test") { 27 | int int0 = 0; 28 | int int1 = 1; 29 | int int2 = 2; 30 | int int3 = 4; 31 | int int4 = 5; 32 | int int5 = 5; 33 | int int6 = 6; 34 | 35 | art::art m; 36 | 37 | m.set("aa", &int0); 38 | m.set("aaaa", &int1); 39 | m.set("aaaaaaa", &int2); 40 | m.set("aaaaaaaaaa", &int3); 41 | m.set("aaaaaaaba", &int4); 42 | m.set("aaaabaa", &int5); 43 | m.set("aaaabaaaaa", &int6); 44 | 45 | /* The above statements construct the following tree: 46 | * 47 | * (aa) 48 | * $_____/ |a 49 | * / | 50 | * ()->0 (a) 51 | * $_____/ |a\____________b 52 | * / | \ 53 | * ()->1 (aa) (aa) 54 | * $_____/ |a\___b |$\____a 55 | * / | \ | \ 56 | * ()->2 (aa$)->3 (a$)->4 ()->5 (aa$)->6 57 | * 58 | */ 59 | 60 | auto it = m.begin(); 61 | auto it_end = m.end(); 62 | std::string key; 63 | key.reserve(20); 64 | 65 | // 0 66 | REQUIRE(it != it_end); 67 | REQUIRE_EQ(&int0, *it); 68 | it.key(key.begin()); 69 | REQUIRE(std::equal(key.begin(), key.begin() + 3, "aa")); 70 | REQUIRE_EQ("aa", it.key()); 71 | 72 | ++it; 73 | // 1 74 | REQUIRE(it != it_end); 75 | REQUIRE_EQ(&int1, *it); 76 | it.key(key.begin()); 77 | REQUIRE(std::equal(key.begin(), key.begin() + 5, "aaaa")); 78 | REQUIRE_EQ("aaaa", it.key()); 79 | 80 | ++it; 81 | // 2 82 | REQUIRE(it != it_end); 83 | REQUIRE_EQ(&int2, *it); 84 | it.key(key.begin()); 85 | REQUIRE(std::equal(key.begin(), key.begin() + 8, "aaaaaaa")); 86 | REQUIRE_EQ("aaaaaaa", it.key()); 87 | 88 | ++it; 89 | // 3 90 | REQUIRE(it != it_end); 91 | REQUIRE_EQ(&int3, *it); 92 | it.key(key.begin()); 93 | REQUIRE(std::equal(key.begin(), key.begin() + 11, "aaaaaaaaaa")); 94 | REQUIRE_EQ("aaaaaaaaaa", it.key()); 95 | 96 | ++it; 97 | // 4 98 | REQUIRE(it != it_end); 99 | REQUIRE_EQ(&int4, *it); 100 | it.key(key.begin()); 101 | REQUIRE(std::equal(key.begin(), key.begin() + 10, "aaaaaaaba")); 102 | REQUIRE_EQ("aaaaaaaba", it.key()); 103 | 104 | ++it; 105 | // 5 106 | REQUIRE(it != it_end); 107 | REQUIRE_EQ(&int5, *it); 108 | it.key(key.begin()); 109 | REQUIRE(std::equal(key.begin(), key.begin() + 8, "aaaabaa")); 110 | REQUIRE_EQ("aaaabaa", it.key()); 111 | 112 | ++it; 113 | // 6 114 | REQUIRE(it != it_end); 115 | REQUIRE_EQ(&int6, *it); 116 | it.key(key.begin()); 117 | REQUIRE(std::equal(key.begin(), key.begin() + 11, "aaaabaaaaa")); 118 | REQUIRE_EQ("aaaabaaaaa", it.key()); 119 | 120 | ++it; 121 | // 7 (overflow) 122 | REQUIRE(it == it_end); 123 | } 124 | 125 | SUBCASE("tree_len") { 126 | int n = 0x10000; 127 | char key[5]; 128 | int value; 129 | art::art m; 130 | for (int i = 0; i < n; ++i) { 131 | std::snprintf(key, 5, "%04X", i); 132 | m.set(key, &value); 133 | } 134 | 135 | auto it = m.begin(); 136 | auto it_end = m.end(); 137 | int actual_n = 0; 138 | for (; it != it_end; ++it) { 139 | ++actual_n; 140 | } 141 | REQUIRE_EQ(n, actual_n); 142 | } 143 | } 144 | 145 | TEST_CASE("range lexicographic traversal") { 146 | SUBCASE("controlled test") { 147 | int int0 = 0; 148 | int int1 = 1; 149 | int int2 = 2; 150 | int int3 = 3; 151 | int int4 = 4; 152 | int int5 = 5; 153 | int int6 = 6; 154 | 155 | art::art m; 156 | 157 | m.set("aa", &int0); 158 | m.set("aaaa", &int1); 159 | m.set("aaaaaaa", &int2); 160 | m.set("aaaaaaaaaa", &int3); 161 | m.set("aaaaaaaba", &int4); 162 | m.set("aaaabaa", &int5); 163 | m.set("aaaabaaaaa", &int6); 164 | 165 | /* The above statements construct the following tree: 166 | * 167 | * (aa) 168 | * $_____/ |a 169 | * / | 170 | * ()->0 (a) 171 | * $_____/ |a\____________b 172 | * / | \ 173 | * ()->1 (aa) (aa) 174 | * $ ____/ |a\___b |a\____$ 175 | * / | \ | \ 176 | * ()->2 (aa$)->3 (a$)->4 ()->5 (aa$)->6 177 | * 178 | */ 179 | 180 | // iterator on ["aaaaaaaaaa",3] 181 | auto it = m.begin("aaaaaaaaaa"); 182 | 183 | // iterator on ["aaaabaaaaa",6] 184 | auto it_end = m.begin("aaaabaaaaa"); 185 | 186 | // 3 187 | REQUIRE(it != it_end); 188 | REQUIRE_EQ(&int3, *it); 189 | 190 | ++it; 191 | // 4 192 | REQUIRE(it != it_end); 193 | REQUIRE_EQ(&int4, *it); 194 | 195 | ++it; 196 | // 5 197 | REQUIRE(it != it_end); 198 | REQUIRE_EQ(&int5, *it); 199 | 200 | ++it; 201 | // 6 202 | REQUIRE(it == it_end); 203 | } 204 | 205 | SUBCASE("monte carlo") { 206 | mt19937_64 rng(0); 207 | int n_bytes = 4; 208 | int n = 1 << (n_bytes * 4); 209 | char keys[n][n_bytes + 1]; 210 | int value; 211 | art::art m; 212 | for (int i = 0; i < n; ++i) { 213 | std::snprintf(keys[i], n_bytes + 1, "%04X", i); // note: change format if you change n_bytes 214 | m.set(keys[i], &value); 215 | } 216 | for (int experiment = 0; experiment < 1000; ++experiment) { 217 | int start = rng() % n; 218 | int end; 219 | do { 220 | end = rng() % n; 221 | } while (end < start); 222 | auto it = m.begin(keys[start]); 223 | auto it_end = m.begin(keys[end]); 224 | int actual_n = 0; 225 | for (; it != it_end; ++it) { 226 | ++actual_n; 227 | } 228 | REQUIRE_EQ(end - start, actual_n); 229 | } 230 | } 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /third_party/zipf/zipf.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "zipf.hpp" 3 | 4 | zipf::zipf(uint32_t size, double skew, uint32_t seed) 5 | : size_(size), skew_(skew) { 6 | denominator_ = 0; 7 | for (uint32_t rank = 1; rank <= size; ++rank) { 8 | denominator_ += (1.0 / std::pow(rank, skew_)); 9 | } 10 | rnd_ = std::bind(std::uniform_real_distribution(0.0, 1.0), 11 | std::mt19937(seed)); 12 | } 13 | 14 | uint32_t zipf::operator()() { 15 | double prob = rnd_(); 16 | double cum_prob = 0.0; 17 | uint32_t rank = 0; 18 | while (cum_prob < prob) { 19 | ++rank; 20 | cum_prob += probability_of(rank); 21 | } 22 | return rank - 1; 23 | } 24 | 25 | double zipf::probability_of(int rank) { 26 | return (1.0 / std::pow(rank, skew_)) / denominator_; 27 | } 28 | 29 | fast_zipf::fast_zipf(uint32_t size, double skew, uint32_t seed) 30 | : zipf(size, skew, seed) { 31 | cdf_values_ = new double[this->size_]; 32 | materialize_cdf(); 33 | } 34 | 35 | fast_zipf::~fast_zipf() { delete[] cdf_values_; } 36 | 37 | uint32_t fast_zipf::operator()() { return binary_search_in_cdf(this->rnd_()); } 38 | 39 | void fast_zipf::materialize_cdf() { 40 | double cdf = 0; 41 | for (uint32_t rank = 1; rank <= this->size_; ++rank) { 42 | cdf += this->probability_of(rank); 43 | cdf_values_[rank - 1] = cdf; 44 | } 45 | } 46 | 47 | uint32_t fast_zipf::binary_search_in_cdf(double key) { 48 | int lo = 0; 49 | int hi = this->size_ - 1; 50 | while (lo <= hi) { 51 | int mid = lo + (hi - lo) / 2; 52 | if (cdf_values_[mid] == key) { 53 | return mid; 54 | } else if (cdf_values_[mid] < key) { 55 | lo = mid + 1; 56 | } else { 57 | hi = mid - 1; 58 | } 59 | } 60 | return lo; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /third_party/zipf/zipf.hpp: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ZIPF_HPP 3 | #define ZIPF_HPP 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class zipf { 11 | public: 12 | explicit zipf(uint32_t size, double skew = 1.0, uint32_t seed = 0); 13 | 14 | uint32_t operator()(); 15 | 16 | protected: 17 | const uint32_t size_; 18 | const double skew_; 19 | double denominator_; 20 | std::function rnd_; 21 | 22 | double probability_of(int rank); 23 | }; 24 | 25 | class fast_zipf : public zipf { 26 | public: 27 | explicit fast_zipf(uint32_t size, double skew = 1.0, uint32_t seed = 0); 28 | ~fast_zipf(); 29 | 30 | uint32_t operator()(); 31 | 32 | private: 33 | double *cdf_values_; 34 | 35 | void materialize_cdf(); 36 | uint32_t binary_search_in_cdf(double key); 37 | }; 38 | 39 | #endif 40 | --------------------------------------------------------------------------------