From 564b3d30566e584a6ba01c47af956c08ba30ee80 Mon Sep 17 00:00:00 2001 From: Medusa Slockbower Date: Tue, 3 Jun 2025 14:26:59 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 3 + CMakeLists.txt | 77 ++++++ Doxyfile.in | 5 + LICENSE | 674 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + allocation.h | 187 +++++++++++++ any.h | 126 +++++++++ directed_tree.h | 593 +++++++++++++++++++++++++++++++++++++++++ filesystem.h | 348 ++++++++++++++++++++++++ math.h | 45 ++++ object_pool.h | 187 +++++++++++++ optional.h | 83 ++++++ redec.h | 24 ++ startup.h | 45 ++++ template_utils.h | 42 +++ test/test.cpp | 36 +++ unique_id.h | 59 +++++ vcpkg.json | 9 + 18 files changed, 2545 insertions(+) create mode 100755 .gitignore create mode 100755 CMakeLists.txt create mode 100755 Doxyfile.in create mode 100755 LICENSE create mode 100755 README.md create mode 100755 allocation.h create mode 100755 any.h create mode 100755 directed_tree.h create mode 100755 filesystem.h create mode 100755 math.h create mode 100755 object_pool.h create mode 100755 optional.h create mode 100755 redec.h create mode 100755 startup.h create mode 100755 template_utils.h create mode 100755 test/test.cpp create mode 100755 unique_id.h create mode 100755 vcpkg.json diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..0a5944c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Doxyfile +/build/ +/docs/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..a8304fc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,77 @@ +cmake_minimum_required(VERSION 3.5) + +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 1) +set(PROJECT_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") + +project(open-cpp-utils VERSION ${PROJECT_VERSION}) + +set(OPENCPPUTILS_HEADERS + any.h + directed_tree.h + object_pool.h + optional.h + startup.h + template_utils.h + unique_id.h + redec.h + math.h + filesystem.h + allocation.h +) + +add_library(open-cpp-utils INTERFACE + ${OPENCPPUTILS_HEADERS} +) + +find_package(GTest) + +if (GTest_FOUND) + +# Set CPP Standard +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_C_STANDARD 23) + +add_executable(open-cpp-utils-test + test/test.cpp +) + +target_link_libraries(open-cpp-utils-test PRIVATE + GTest::gtest + open-cpp-utils +) + +endif () + +# DOXYGEN ============================================================================================================== +# https://vicrucann.github.io/tutorials/quick-cmake-doxygen/ + +find_package(Doxygen) + +if(DOXYGEN_FOUND) + get_filename_component(DOXYGEN_PROJECT_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) + set(DOXYGEN_CONFIG_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in) + set(DOXYGEN_CONFIG_OUT ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile) + + configure_file(${DOXYGEN_CONFIG_IN} ${DOXYGEN_CONFIG_OUT} @ONLY) + message("Doxygen Build Started.") + + if(WIN32) + add_custom_target(open-cpp-utils-documentation ALL + COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG_OUT} + COMMAND start firefox "${CMAKE_CURRENT_SOURCE_DIR}/Documentation/html/index.html" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Generating Doxygen Documentation" + VERBATIM) + else() + add_custom_target(open-cpp-utils-documentation ALL + COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_CONFIG_OUT} + COMMAND firefox "${CMAKE_CURRENT_SOURCE_DIR}/Documentation/html/index.html" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Generating Doxygen Documentation" + VERBATIM) + endif() +else() + message("Doxygen not found.") +endif() \ No newline at end of file diff --git a/Doxyfile.in b/Doxyfile.in new file mode 100755 index 0000000..324307d --- /dev/null +++ b/Doxyfile.in @@ -0,0 +1,5 @@ +OUTPUT_DIRECTORY = "@CMAKE_CURRENT_SOURCE_DIR@/Documentation/" +INPUT = "@CMAKE_CURRENT_SOURCE_DIR@/README.md" "@CMAKE_CURRENT_SOURCE_DIR@/" +RECURSIVE = YES +PROJECT_NAME = "@DOXYGEN_PROJECT_NAME@" +PROJECT_NUMBER = "@CMAKE_PROJECT_VERSION@" \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..372d7d9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + open-cpp-utils, an open-source cpp library with data structures that extend the STL. + Copyright (C) 2024 Medusa Slockbower + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + open-cpp-utils Copyright (C) 2024 Medusa Slockbower + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md new file mode 100755 index 0000000..42131d9 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# open-cpp-utils +Open Source Utilities for C++ diff --git a/allocation.h b/allocation.h new file mode 100755 index 0000000..d18ca3a --- /dev/null +++ b/allocation.h @@ -0,0 +1,187 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_ALLOCATION_H +#define OPEN_CPP_UTILS_ALLOCATION_H + +#include +#include + +namespace open_cpp_utils +{ + +template> +class allocation +{ +public: + using data_type = T; + using reference = data_type&; + using const_reference = const data_type&; + using pointer = data_type*; + using const_pointer = const data_type*; + + + allocation(); + allocation(size_t size); + allocation(allocation&& alloc) noexcept; + allocation(const allocation& alloc); + ~allocation(); + + size_t size() const { return Size_; } + bool operator()() const { return Buffer_ != nullptr; } + + void allocate(size_t size); + void reallocate(size_t size); + void free(); + + void clear(size_t offset, size_t size, const T& value); + void copy(const allocation& other, size_t offset, size_t o_offset, size_t size); + + reference operator[](int i) { assert(i >= 0 && i < Size_); return Buffer_[i]; } + const_reference operator[](int i) const { assert(i >= 0 && i < Size_); return Buffer_[i]; } + + operator pointer() { return Buffer_; } + operator const_pointer() const { return Buffer_; } + + allocation& operator=(const allocation& other); + allocation& operator=(allocation&& other) noexcept; + + pointer data() { return Buffer_; } + const_pointer data() const { return Buffer_; } + + pointer begin() { return Buffer_; } + pointer end() { return Buffer_ + Size_; } + + const_pointer begin() const { return Buffer_; } + const_pointer end() const { return Buffer_ + Size_; } + +private: + Alloc Alloc_; + size_t Size_; + T* Buffer_; +}; + +template +allocation::allocation() +: Alloc_(), Size_(0), Buffer_(nullptr) { } + +template +allocation::allocation(size_t size) +: Alloc_(), Size_(size), Buffer_(Alloc_.allocate(Size_)) { } + +template +allocation::allocation(allocation &&alloc) noexcept +: Alloc_(), Size_(alloc.Size_), Buffer_(alloc.Buffer_) { } + +template +allocation::allocation(const allocation &alloc) + : Alloc_(), Size_(alloc.Size_), Buffer_(Alloc_.allocate(Size_)) +{ + copy(alloc, 0, 0, Size_); +} + +template +allocation::~allocation() +{ + Alloc_.deallocate(Buffer_, Size_); + Buffer_ = nullptr; Size_ = 0; +} + +template +void allocation::allocate(size_t size) +{ + assert(Buffer_ != nullptr); + if(Buffer_ == nullptr) return; + + Buffer_ = Alloc_.allocate(size); +} + +template +void allocation::reallocate(size_t size) +{ + // Assertions for debug mode + assert(Buffer_ != nullptr); + + // Safety checks + if(Buffer_ == nullptr) return; + + const allocation old_ = std::move(*this); + std::construct_at(this, size); + std::memcpy(Buffer_, old_.Buffer_, old_.Size_); +} + +template +void allocation::free() +{ + Alloc_.deallocate(Buffer_, Size_); + Buffer_ = nullptr; Size_ = 0; +} + +template +void allocation::clear(size_t offset, size_t size, const T &value) +{ + // Safety checks + if(Buffer_ == nullptr) return; + size = std::min(size, Size_ - offset); + + for(pointer it = Buffer_ + offset; it != Buffer_ + size; ++it) + { + std::destroy_at(it); + std::construct_at(it, value); + } +} + +template +void allocation::copy(const allocation &other, size_t offset, size_t o_offset, size_t size) +{ + // Assertions for debug mode + assert(size <= Size_ - offset && size <= other.Size_ - o_offset); + + // Safety checks + if(Buffer_ == nullptr || other.Buffer_ == nullptr) return; + if(offset >= Size_ || o_offset >= other.Size_) return; + size = std::min(size, Size_ - offset); + size = std::min(size, other.Size_ - o_offset); + + std::memcpy(Buffer_ + offset, other.Buffer_ + o_offset, size * sizeof(data_type)); +} + +template +allocation & allocation::operator=(const allocation &other) +{ + if(&other == this) return *this; + + Size_ = other.Size_; + Buffer_ = Alloc_.allocate(Size_); + copy(other, 0, 0, Size_); + return *this; +} + +template +allocation & allocation::operator=(allocation &&other) noexcept +{ + if(&other == this) return *this; + + Size_ = other.Size_; + Buffer_ = other.Buffer_; + return *this; +} + +} + +#endif //ALLOCATION_H diff --git a/any.h b/any.h new file mode 100755 index 0000000..25926ef --- /dev/null +++ b/any.h @@ -0,0 +1,126 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_ANY_H +#define OPEN_CPP_UTILS_ANY_H + +#include "template_utils.h" + +namespace open_cpp_utils +{ + +/** + * \brief Wrapper for a value with multiple types + * \tparam Ts Types to include, must be unique + */ +template class any; + +template<> class any<> { }; + +template +class any : public any +{ +// Assertions ========================================================================================================== + + static_assert(is_unique); + + +// Typedefs ============================================================================================================ + +public: + using base_type = any; + + using this_type = T; + using reference = T&; + using const_reference = const T&; + using pointer = T*; + using const_pointer = const T*; + + +// Constructors ======================================================================================================== + +public: + any() : base_type() , Value() { } + any(const this_type& value, const Rest&...other) : base_type(other...), Value(value) { } + any(this_type&& value, Rest&&...other) : base_type(other...), Value(value) { } + any(const any& other) = default; + any(any&& other) = default; + ~any() = default; + + +// Operators =========================================================================================================== + +public: + +// Assignment operators ------------------------------------------------------------------------------------------------ + + any& operator=(const any&) = default; + any& operator=(any&&) = default; + + template + any& operator=(const V& v) + { + static_assert(std::disjunction, std::is_same...>{}); + static_cast(*this) = v; + return *this; + } + + template + any& operator=(V&& v) + { + static_assert(std::disjunction, std::is_same...>{}); + static_cast(*this) = v; + return *this; + } + + +// Access -------------------------------------------------------------------------------------------------------------- + + template + V& get() + { + static_assert(std::disjunction, std::is_same...>{}); + return static_cast(*this); + } + + template + const V& get() const + { + static_assert(std::disjunction, std::is_same...>{}); + return static_cast(*this); + } + +// Cast operators ------------------------------------------------------------------------------------------------------ + + operator reference() { return Value; } + operator const_reference() const { return Value; } + operator pointer() { return &Value; } + operator const_pointer() const { return &Value; } + + +// Variables =========================================================================================================== + +private: + static constexpr size_t Size = sizeof...(Rest); + this_type Value; +}; + +} + + +#endif // OPEN_CPP_UTILS_ANY_H diff --git a/directed_tree.h b/directed_tree.h new file mode 100755 index 0000000..5a05414 --- /dev/null +++ b/directed_tree.h @@ -0,0 +1,593 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_DIRECTED_TREE_H +#define OPEN_CPP_UTILS_DIRECTED_TREE_H + +#include +#include +#include + +#include "allocation.h" + +namespace open_cpp_utils +{ + +/** + * \brief Class for creating a directed tree + * \tparam T Type of the data associated with each node + * + * The tree is a series of child nodes in forward linked lists. + * + */ +template> +class directed_tree +{ +// Forward Definitions ================================================================================================= + +public: + class breadth_first; + class pre_order; + class in_order; + class post_order; + class unordered; + +private: + struct Node_; + + +// Typedefs ============================================================================================================ + +public: + using data_type = T; + using node = size_t; + using node_queue = std::deque; + +private: + using hierarchy = allocation::template rebind_alloc>; + using storage = allocation; + + +// Constants =========================================================================================================== + +public: + static constexpr std::integral_constant root{}; + + +// Data Structures ===================================================================================================== + +private: + struct Node_ + { + enum flags + { + valid = 0x0001 + }; + + node parent, child, prev_sibling, next_sibling; + uint32_t flags, depth; + + Node_() : parent(0), child(0), prev_sibling(0), next_sibling(0), flags(0), depth(0) { } + }; + + +// Functions =========================================================================================================== + +private: + +// Helpers ------------------------------------------------------------------------------------------------------------- + + void grow_() + { + size_t capacity = graph_.size(); + capacity = capacity == 0 ? 10 : capacity * 2; + graph_.reallocate(capacity); + data_.reallocate(capacity); + } + + node push_back_(const data_type& data) + { + if(size_ >= graph_.size()) grow_(); + std::construct_at(data_ + size_, data); + return size_++; + } + + node push_back_(data_type&& data) + { + if(size_ >= graph_.size()) grow_(); + std::construct_at(data_ + size_, std::forward(data)); + return size_++; + } + +public: + +// Constructors & Destructor ------------------------------------------------------------------------------------------- + + /** + * \brief Default constructor, initializes empty tree + */ + directed_tree() : size_(0) { } + directed_tree(data_type&& data) : size_(0) { push_back_(std::forward(data)); } + directed_tree(const data_type& data) : size_(0) { push_back_(data); } + ~directed_tree() = default; + + +// Tree Navigation ----------------------------------------------------------------------------------------------------- + + /** + * \brief Check whether a node is valid. O(1) + * \param id Node id to reference + * \return Whether the valid flag is true in the node + */ + [[nodiscard]] bool valid(node id) const { return graph_ ? graph_[id].flags & Node_::valid : false; } + + /** + * \brief Get the parent of a node. O(1) + * \param id Node id to reference + * \return Node id of the parent + */ + [[nodiscard]] node parent(node id) const { return graph_ ? graph_[id].parent : 0; } + + /** + * \brief Get the first child of a node. O(1) + * \param id Node id to reference + * \return Node id of the first child + */ + [[nodiscard]] node first_child(node id) const { return graph_ ? graph_[id].child : 0; } + + /** + * \brief Get the first child of a node. O(1) + * \param id Node id to reference + * \return Node id of the first child + */ + [[nodiscard]] node last_child(node id) const + { + node c = first_child(id); + + while(c != 0) { if(graph_[c].next_sibling == 0) break; c = graph_[c].next_sibling; } + + return c; + } + + /** + * \brief Get the previous sibling of a node. O(1) + * \param id Node id to reference + * \return Node id of the next sibling in the linked list + */ + [[nodiscard]] node prev_sibling(node id) const { return graph_[id].prev_sibling; } + + /** + * \brief Get the next sibling of a node. O(1) + * \param id Node id to reference + * \return Node id of the next sibling in the linked list + */ + [[nodiscard]] node next_sibling(node id) const { return graph_[id].next_sibling; } + + /** + * \brief Get the left most child of a node. O(log(n)) + * \param id Node id to reference + * \return Node id of the left most child + */ + [[nodiscard]] node left_most(node id) const + { + node current = id; + while(id = first_child(current)) current = id; + return current; + } + + /** + * \brief Get the depth of a node + * \param id + * \return + */ + [[nodiscard]] uint32_t depth(node id) const { return graph_[id].depth; } + + +// Tree Modification --------------------------------------------------------------------------------------------------- + + /** + * \brief Get the next id that would be used if insert() were called + * \return Next node id + */ + node next_id() const + { + if(freed_.empty()) return size_; + return freed_.front(); + } + + /** + * \brief Insert a node into the tree as a child of the provided node + * \param data Value to insert + * \param p_id Id of the parent node + * \param sib Child to insert before, passing root specifies to insert to the back + * \return Id of the inserted node + */ + node insert(const data_type& data, node p_id, node sib = 0) + { + // If there are no freed nodes, create a new node and mark it as freed + if(freed_.empty()) + { + freed_.push_back(push_back_(std::forward(data))); + } + else + { + data_[freed_.front()] = data; freed_.pop_front(); + } + + // Pop a freed node from the stack + node id = freed_.front(); freed_.pop_front(); + bool back = sib == 0; + node s_id = back ? last_child(p_id) : sib; + Node_& node = graph_[id]; + Node_& parent = graph_[p_id]; + + Node_& sibling = graph_[s_id]; + if(parent.child == root || (s_id == parent.child && !back)) parent.child = id; + + node.next_sibling = node.prev_sibling = 0; + node.parent = p_id; + node.depth = parent.depth + 1; + node.flags = Node_::valid; + node.child = 0; + + if(s_id == 0) return id; + + if(back) + { + node.next_sibling = sibling.next_sibling; + node.prev_sibling = s_id; + + sibling.next_sibling = id; + } + else + { + node.next_sibling = s_id; + node.prev_sibling = sibling.prev_sibling; + + sibling.prev_sibling = id; + } + + return id; + } + + /** + * \brief Insert a node into the tree as a child of the provided node + * \param data Value to insert + * \param p_id Id of the parent node + * \param sib Child to insert before, passing root specifies to insert to the back + * \return Id of the inserted node + */ + node insert(data_type&& data, node p_id, node sib = root) + { + // If there are no freed nodes, create a new node and mark it as freed + if(freed_.empty()) + { + freed_.push_back(push_back_(std::forward(data))); + } + else + { + std::construct_at(data_ + freed_.front(), std::forward(data)); + } + + // Pop a freed node from the stack + node id = freed_.front(); freed_.pop_front(); + bool back = sib == root; + node s_id = back ? last_child(p_id) : sib; + Node_& node = graph_[id]; + Node_& parent = graph_[p_id]; + + Node_& sibling = graph_[s_id]; + if(parent.child == root || (s_id == parent.child && !back)) parent.child = id; + + node.next_sibling = node.prev_sibling = 0; + node.parent = p_id; + node.depth = parent.depth + 1; + node.flags = Node_::valid; + node.child = 0; + + if(s_id == 0) return id; + + if(back) + { + node.next_sibling = sibling.next_sibling; + node.prev_sibling = s_id; + + sibling.next_sibling = id; + } + else + { + node.next_sibling = s_id; + node.prev_sibling = sibling.prev_sibling; + + sibling.prev_sibling = id; + } + + return id; + } + + void swap(node a, node b) + { + Node_& A = graph_[a]; + Node_& B = graph_[b]; + + std::swap(A, B); + + if(graph_[B.parent].child == a) graph_[B.parent].child = b; + if(graph_[A.parent].child == b) graph_[A.parent].child = a; + } + + void clear() + { + for(int i = 0; i < size_; ++i) + { + if(valid(i) == false) continue; + + graph_[i].flags = 0; + (data_ + i)->~T(); + freed_.push_back(i); + } + + size_ = 0; + } + + /** + * \brief Erase a node in the tree. O(n) + * \param id Id of the node to erase + */ + void erase(node id) + { + if(id == root) return; + + // Mark node as invalid and push it to the freed list + Node_& erased = graph_[id]; + erased.flags = 0; + freed_.push_back(id); + (data_ + id)->~T(); + + // Update the parent's child + Node_& parent = graph_[erased.parent]; + if(parent.child == id) parent.child = erased.next_sibling; + + // Update siblings + if(erased.next_sibling) graph_[erased.next_sibling].prev_sibling = erased.prev_sibling; + if(erased.prev_sibling) graph_[erased.prev_sibling].next_sibling = erased.next_sibling; + + // Erase children - essentially breadth first propagation down the tree + node_queue stack{ erased.child }; + while(stack.empty() == false && stack.front() != 0) + { + node next = stack.front(); stack.pop_front(); + Node_& child = graph_[next]; + child.flags = 0; + freed_.push_back(next); + (data_ + next)->~T(); + + if(child.next_sibling) stack.push_front(child.next_sibling); + if(child.child) stack.push_front(child.child); + } + } + + +// Tree Access --------------------------------------------------------------------------------------------------------- + + /** + * \brief Getter for data associated with a node + * \param id Id of the node to access + * \return Reference to the node's data + */ + data_type& operator[](node id) { return data_[id]; } + + /** + * \brief Constant getter for data associated with a node + * \param node Id of the node to access + * \return Reference to the node's data + */ + [[nodiscard]] const data_type& operator[](node id) const { return data_[id]; } + + +// Visitor Pattern ----------------------------------------------------------------------------------------------------- + + /** + * \brief Traverser-Visitor pattern for accessing the tree + * \tparam V Visitor type. + * \tparam O Order type. Defaults to Pre-Order Traversal. + * \param visitor + */ + template + void traverse(V& visitor) + { + traverser traverser(*this, visitor); + traverser(); + } + + +// Variables ======================================================================================================= + +private: + size_t size_; + hierarchy graph_; + storage data_; + node_queue freed_; + + +// Navigation ====================================================================================================== + +public: + + class unordered + { + public: + unordered(directed_tree& graph) : graph_(graph), current_(root) { } + + node operator()(node id) + { + while(!graph_.valid(current_) || current_ == root) + { + ++current_; + } + + id = current_; + current_ ++; + + return id == graph_.graph_.size() ? 0 : id; + } + + private: + directed_tree& graph_; + node current_; + }; + + /** + * \brief Breadth first traversal + */ + class breadth_first + { + public: + breadth_first(directed_tree& graph) : graph_(graph), visit_queue_(0) { } + + node operator()(node id) + { + id = visit_queue_.back(); visit_queue_.pop_back(); + Node_& current = graph_.graph_[id]; + + if(current.next_sibling) visit_queue_.push_back(current.next_sibling); + if(current.child) visit_queue_.push_front(current.child); + + if(visit_queue_.empty()) return 0; + return id; + } + + private: + directed_tree& graph_; + node_queue visit_queue_; + }; + + + /** + * \brief Pre-order traversal + */ + class pre_order + { + public: + pre_order(directed_tree& graph) : graph_(graph) { } + + node operator()(node id) + { + Node_& current = graph_.graph_[id]; + + if(current.next_sibling) visit_queue_.push_front(current.next_sibling); + if(current.child) visit_queue_.push_front(current.child); + + if(visit_queue_.empty()) return 0; + node next = visit_queue_.front(); visit_queue_.pop_front(); + return next; + } + + private: + directed_tree& graph_; + node_queue visit_queue_; + }; + + + /** + * \brief In-order traversal + */ + class in_order + { + public: + in_order(directed_tree& graph) : graph_(graph) { } + + node operator()(node id) + { + if(id == 0) visit_queue_.push_back(graph_.left_most(id)); + + id = visit_queue_.front(); visit_queue_.pop_front(); + Node_& current = graph_.graph_[id]; + + if(current.Sibling) + { + if(graph_.next_sibling(current.Sibling)) visit_queue_.push_back(current.parent); + visit_queue_.push_back(graph_.left_most(current.Sibling)); + } + + return id; + } + + private: + directed_tree& graph_; + node_queue visit_queue_; + }; + + + /** + * \brief Post-order traversal + */ + class post_order + { + public: + post_order(directed_tree& graph) : graph_(graph) { } + + node operator()(node id) + { + if(visit_queue_.empty()) visit_queue_.push_back(graph_.left_most(id)); + + id = visit_queue_.front(); visit_queue_.pop_front(); + if(id == 0) return id; + Node_& current = graph_.graph_[id]; + + visit_queue_.push_back(current.Sibling ? graph_.left_most(current.Sibling) : graph_.parent(id)); + + return id; + } + + private: + directed_tree& graph_; + node_queue visit_queue_; + }; + + + /** + * \brief Visitor pattern for traversing the tree + */ + template + class traverser + { + public: + using visitor_type = V; + using order_type = O; + + traverser(directed_tree& graph, visitor_type& visitor) : graph_(graph), visitor_(visitor), order_(graph) { } + + void operator()() + { + node id = 0; + while(id = order_(id)) + { + if(visitor_(graph_[id], id)) break; + } + } + + private: + directed_tree& graph_; + visitor_type& visitor_; + order_type order_; + }; +}; +} + +#endif // OPEN_CPP_UTILS_DIRECTED_TREE_H diff --git a/filesystem.h b/filesystem.h new file mode 100755 index 0000000..074c262 --- /dev/null +++ b/filesystem.h @@ -0,0 +1,348 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_FILESYSTEM_H +#define OPEN_CPP_UTILS_FILESYSTEM_H + +#include +#include +#include + +#include "directed_tree.h" + +namespace open_cpp_utils +{ + +using path_t = std::filesystem::path; + +template +class filesystem +{ +// Typedefs ============================================================================================================ + +public: + class file; + using data_t = T_; + using loader = L_; + using file_tree = directed_tree; + using fileptr = file*; + using file_id = typename file_tree::node; + static constexpr file_id root = file_tree::root; + + +// Structs ============================================================================================================= + +public: + class file + { + private: + file(filesystem* parent, file_id node, path_t path, data_t* data) + : parent_(parent), node_(node), path_(std::move(path)), value_(data) + { } + + public: + file() : parent_(nullptr), node_(0), path_(""), value_(nullptr) { } + file(const file&) = default; + file(file&&) = default; + ~file() { delete value_; } + + const path_t& path() const { return path_; } + + data_t* get_data() { return value_; } + const data_t* get_data() const { return value_; } + + filesystem& system() { return *parent_; } + const filesystem& system() const { return *parent_; } + + bool is_directory() const { return std::filesystem::is_directory(path_); } + bool empty() const { return is_empty(path_); } + bool has_subdirectory() const { for(const auto& path : std::filesystem::directory_iterator{ path_ }) { if(std::filesystem::is_directory(path)) return true; } return false; } + + void erase() { delete value_; value_ = nullptr; } + + file_id get_id() const { return node_; } + + data_t* operator->() { return value_; } + const data_t* operator->() const { return value_; } + + data_t* operator*() { return value_; } + const data_t* operator*() const { return value_; } + + private: + filesystem* parent_; + file_id node_; + path_t path_; + data_t* value_; + + friend class filesystem; + }; + + +// Functions =========================================================================================================== + + +// Helpers ------------------------------------------------------------------------------------------------------------- + +private: + + static path_t resolve_(const path_t& path) { return absolute(canonical(path)); } + + static bool is_parent_(const path_t& base, const path_t& path) + { return std::mismatch(path.begin(), path.end(), base.begin(), base.end()).second == base.end(); } + + file_id find_(path_t path) const; + +file_id get_index_(file_id parent, const path_t &path); + + +// Constructors & Destructor ------------------------------------------------------------------------------------------- + +public: + filesystem() = default; + ~filesystem() = default; + + file& operator[](file_id id) { return tree_[id]; } + const file& operator[](file_id id) const { return tree_[id]; } + + file_id load_directory(const path_t& path); + void close_directory(file_id id) { tree_.erase(id); } + + file_id import(const path_t& path, file_id parent); + + file_id create(const std::string& name, file_id parent); + file_id create_folder(const std::string& name, file_id parent); + + void rename(file_id id, const std::string& name); + + void clear() { tree_.clear(); } + void erase(file_id id); + void erase(const path_t& path); + + file_id find(const path_t& path) const; + + file_id parent(file_id id) const { return tree_.parent(tree_[id].get_id()); } + file_id next(file_id id) const { return tree_.next_sibling(tree_[id].get_id()); } + file_id prev(file_id id) const { return tree_.prev_sibling(tree_[id].get_id()); } + file_id begin(file_id id) const { return tree_.first_child(tree_[id].get_id()); } + file_id end(file_id) const { return file_tree::root; } + + uint32_t depth(file_id id) const { return tree_.depth(tree_[id].get_id()); } + + template + void traverse(V& visitor) { tree_.template traverse(visitor); } + +private: + file_tree tree_; +}; + +template +typename filesystem::file_id filesystem::find_(path_t path) const +{ + // Check if the path exists + if(not exists(path)) return file_tree::root; + + // Setup for traversal + path = resolve_(path); + file_id dir = tree_.first_child(file_tree::root); + + // Get the parent folder + while(dir != file_tree::root) + { + if(is_parent_(tree_[dir].path(), path)) break; + dir = tree_.next_sibling(dir); + } + + // Path does not exist in file system + if(dir == file_tree::root) return file_tree::root; + + // Get starting point for iteration + auto parent = tree_[dir].path(); + auto start = std::mismatch(path.begin(), path.end(), parent.begin(), parent.end()).first; + + // Parse down the tree + for(auto it = start; it != path.end(); ++it) + { + for(file_id child = tree_.first_child(dir); child != file_tree::root; child = tree_.next_sibling(child)) + { + if(tree_[child].path().filename() == it->filename()) + { + dir = child; + break; + } + } + } + + return dir; +} + +template +typename filesystem::file_id filesystem::get_index_(file_id parent, const path_t &path) +{ + file_id dir = tree_.first_child(parent); + + // Get the insertion index + while(dir != file_tree::root) + { + if(tree_[dir].path().filename().compare(path.filename()) > 0) break; + + dir = tree_.next_sibling(dir); + } + + return dir; +} + +template +typename filesystem::file_id filesystem::load_directory(const path_t& path) +{ + if(not exists(path)) return root; + + const path_t directory = canonical(absolute(path)); + file_id dir = tree_.first_child(file_tree::root); + + // Validate this isn't a subdirectory + while(dir != file_tree::root) + { + if(is_parent_(tree_[dir].path(), directory)) return find(directory); + dir = tree_.next_sibling(dir); + } + + dir = get_index_(file_tree::root, directory); + dir = tree_.insert(file(this, tree_.next_id(), directory, nullptr), file_tree::root, dir); + tree_[dir].value_ = loader::load(directory, dir); + file_id res = dir; + + using iter_t = std::filesystem::directory_iterator; + std::stack> working; + working.emplace(directory, dir, iter_t(directory)); + + while(not working.empty()) + { + auto& top = working.top(); + const file_id p_dir = std::get<1>(top); + const iter_t& it = std::get<2>(top); + + if(std::filesystem::begin(it) == std::filesystem::end(it)) + { + working.pop(); + continue; + } + + const path_t create_path = *it; + + file_id created = tree_.insert(file(this, tree_.next_id(), create_path, nullptr), p_dir); + tree_[created].value_ = loader::load(create_path, created); + + if(is_directory(create_path)) + { + working.emplace(create_path, created, iter_t(create_path)); + } + + ++std::get<2>(top); + } + + return res; +} + +template +typename filesystem::file_id filesystem::import(const path_t& path, file_id parent) +{ + if(not exists(path)) return root; + + file& prnt = tree_[parent]; + path_t nloc = prnt.path() / path.filename(); + const file_id node = tree_.insert(file(this, tree_.next_id(), nloc, nullptr), parent, get_index_(parent, nloc)); + tree_[node].value_ = loader::import(path, nloc, node); + return node; +} + +template +typename filesystem::file_id filesystem::create(const std::string &name, file_id parent) +{ + file& prnt = tree_[parent]; + const file_id p_dir = prnt.get_id(); + const path_t path = prnt.path() / name; + + if(exists(path)) return 0; + + const file_id node = tree_.insert(file(this, tree_.next_id(), path, nullptr), parent, get_index_(parent, path)); + tree_[node].value_ = loader::create(path, node); + return node; +} + +template +typename filesystem::file_id filesystem::create_folder(const std::string &name, file_id parent) +{ + file& prnt = tree_[parent]; + const path_t path = prnt.path() / name; + + if(exists(path)) return 0; + + create_directory(path); + data_t* data = loader::load(path); + + const file_id node = tree_.insert(file(this, tree_.next_id(), path, data), parent, get_index_(parent, path)); + + return tree_[node]; +} + +template +void filesystem::rename(file_id id, const std::string& name) +{ + file& file = tree_[id]; + const std::string new_name = path_t(name).stem().string() + file.path().extension().string(); + + fileptr current = &file; + while(true) + { + fileptr next = &tree_[filesystem::next(current->get_id())]; + fileptr prev = &tree_[filesystem::prev(current->get_id())]; + + if(next != &tree_[root] && new_name.compare(next->path().filename().string()) > 0) { tree_.swap(current->get_id(), next->get_id()); current = next; continue; } + if(next != &tree_[root] && new_name.compare(prev->path().filename().string()) < 0) { tree_.swap(current->get_id(), prev->get_id()); current = prev; continue; } + break; + } + + const path_t new_path = current->path().parent_path() / new_name; + std::filesystem::rename(current->path(), new_path); + current->path_ = new_path; +} + +template +void filesystem::erase(file_id id) +{ + file& file = tree_[id]; + std::filesystem::remove(file.path()); + tree_.erase(id); +} + +template +void filesystem::erase(const path_t &path) +{ + const file_id id = find_(path); + erase(tree_[id]); +} + +template +typename filesystem::file_id filesystem::find(const path_t &path) const +{ + return find_(path); +} + +} + +#endif // OPEN_CPP_UTILS_FILESYSTEM_H diff --git a/math.h b/math.h new file mode 100755 index 0000000..7334d0b --- /dev/null +++ b/math.h @@ -0,0 +1,45 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_MATH_H +#define OPEN_CPP_UTILS_MATH_H + +#include + +namespace open_cpp_utils +{ + +template +bool is_prime(T x) +{ + if(x <= 1) return false; + if(x == 2 || x == 3) return true; + if(x % 2 == 0 || x % 3 == 0) return false; + + T limit = static_cast(sqrt(x)); + for(T i = 5; i <= limit; i += 6) + { + if(x % i == 0 || x % (i + 2) == 0) return false; + } + + return true; +} + +} + +#endif // OPEN_CPP_UTILS_MATH_H diff --git a/object_pool.h b/object_pool.h new file mode 100755 index 0000000..1532dcf --- /dev/null +++ b/object_pool.h @@ -0,0 +1,187 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_OBJECT_POOL_H +#define OPEN_CPP_UTILS_OBJECT_POOL_H + +#include + +#include "allocation.h" +#include "optional.h" + +namespace open_cpp_utils +{ + +/** + * \brief + * \tparam T + */ +template +class object_list +{ +// Typedefs ============================================================================================================ + +public: + using value_type = T; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + + using uuid_type = uint64_t; + + class iterator; + class const_iterator; + +private: + using node = optional; + using obj_alloc = std::vector; + using free_stack = std::deque; + + +// Functions =========================================================================================================== + +public: + +// Constructors & Destructor ------------------------------------------------------------------------------------------- + + object_list() = default; + object_list(const object_list& other) = default; + object_list(object_list&& other) = default; + ~object_list() = default; + + +// Modifiers ----------------------------------------------------------------------------------------------------------- + + size_t size() const { return data_.size(); } + size_t capacity() const { return data_.capacity(); } + + +// Modifiers ----------------------------------------------------------------------------------------------------------- + + void clear() { data_.clear(); freed_.clear(); } + + uuid_type insert(const_reference& value); + void erase(uuid_type id); + + object_list& operator=(const object_list&) = default; + object_list& operator=(object_list&&) = default; + + +// Accessors ----------------------------------------------------------------------------------------------------------- + + reference operator[](uuid_type id) { assert(data_[id]()); return data_[id]; } + const_reference operator[](uuid_type id) const { assert(data_[id]()); return data_[id]; } + bool operator()(uuid_type id) const { return data_[id](); } + + iterator begin() { return { this, data_.begin() }; } + iterator end() { return { this, data_.end() }; } + + const_iterator begin() const { return { this, data_.begin() }; } + const_iterator end() const { return { this, data_.end() }; } + +// Variables =========================================================================================================== + +private: + obj_alloc data_; + free_stack freed_; + +public: + class iterator + { + public: + using obj_iterator = typename obj_alloc::iterator; + + iterator(object_list* list, obj_iterator start) : list_(list), it_(start) + { + while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } + } + + iterator& operator++() + { + ++it_; + while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } + return *this; + } + + iterator operator++(int) + { + iterator ret = this; + ++it_; + while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } + return ret; + } + + reference operator*() { return *it_; } + + bool operator==(const iterator& other) const { return list_ == other.list_ && it_ == other.it_; } + bool operator!=(const const_iterator& other) const { return list_ != other.list_ || it_ != other.it_; } + + uuid_type id() { return (&*it_) - list_->data_.data(); } // Convert to node pointer + + private: + object_list* list_; + obj_iterator it_; + }; + + class const_iterator + { + public: + using obj_iterator = typename obj_alloc::pointer; + + const_iterator(const object_list* list, obj_iterator start) : list_(list), it_(start) { } + + const_iterator& operator++() + { ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return *this; } + + const_iterator operator++(int) + { const_iterator ret = this; ++it_; while(it_ != list_->data_.end() && not (*it_)()) { ++it_; } return ret; } + + const_reference operator*() { return *it_; } + + bool operator==(const const_iterator& other) const { return list_ == other.list_ && it_ == other.it_; } + bool operator!=(const const_iterator& other) const { return list_ != other.list_ || it_ != other.it_; } + + uuid_type id() { return (&*it_) - list_->data_.data(); } // Convert to node pointer + + private: + const object_list* list_; + obj_iterator it_; + }; +}; + +template +typename object_list::uuid_type object_list::insert(const_reference value) +{ + if(freed_.empty()) { data_.push_back(value); return data_.size() - 1; } + + uuid_type id = freed_.back(); freed_.pop_back(); + data_[id] = value; + return id; +} + +template +void object_list::erase(uuid_type id) +{ + data_[id].reset(); + freed_.push_back(id); +} + +} // open_cpp_utils + +#endif // OPEN_CPP_UTILS_OBJECT_POOL_H diff --git a/optional.h b/optional.h new file mode 100755 index 0000000..0292e2b --- /dev/null +++ b/optional.h @@ -0,0 +1,83 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_OPTIONAL_H +#define OPEN_CPP_UTILS_OPTIONAL_H + +#include + +namespace open_cpp_utils +{ + template + class optional + { + public: + using value_type = T; + + optional() : valid_(false) { } + optional(const value_type& data) : data_(data), valid_(true) { } + optional(value_type&& data) : data_(data), valid_(true) { } + optional(const optional& other) = default; + optional(optional&& other) = default; + + optional& operator=(const optional& other) = default; + optional& operator=(optional&& other) = default; + + optional& operator=(const value_type& data) { data_ = data; valid_ = true; return *this; } + optional& operator=(value_type&& data) { data_ = data; valid_ = true; return *this; } + + bool operator==(const optional& b) const + { + if(not(valid_ || b.valid_)) return valid_ == b.valid_; + return data_ == b.data_; + } + + value_type& operator+=(const value_type& data) { assert(valid_); data_ += data; return data_; } + value_type& operator-=(const value_type& data) { assert(valid_); data_ += data; return data_; } + value_type& operator*=(const value_type& data) { assert(valid_); data_ += data; return data_; } + value_type& operator/=(const value_type& data) { assert(valid_); data_ += data; return data_; } + value_type& operator%=(const value_type& data) { assert(valid_); data_ += data; return data_; } + + value_type& operator<<=(const value_type& data) { assert(valid_); data_ <<= data; return data_; } + value_type& operator>>=(const value_type& data) { assert(valid_); data_ >>= data; return data_; } + value_type& operator|=(const value_type& data) { assert(valid_); data_ |= data; return data_; } + value_type& operator&=(const value_type& data) { assert(valid_); data_ &= data; return data_; } + value_type& operator^=(const value_type& data) { assert(valid_); data_ ^= data; return data_; } + + [[nodiscard]] bool operator()() const { return valid_; } + + operator value_type() const { assert(valid_); return data_; } + + operator value_type&() { assert(valid_); return data_; } + operator const value_type&() const { assert(valid_); return data_; } + + value_type* operator->() { assert(valid_); return &data_; } + const value_type* operator->() const { assert(valid_); return &data_; } + + value_type& operator*() { assert(valid_); return data_; } + const value_type& operator*() const { assert(valid_); return data_; } + + void reset() { valid_ = false; } + + private: + value_type data_; + bool valid_; + }; +} + +#endif // OPEN_CPP_UTILS_OPTIONAL_H diff --git a/redec.h b/redec.h new file mode 100755 index 0000000..aae6df3 --- /dev/null +++ b/redec.h @@ -0,0 +1,24 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_REDEC_H +#define OPEN_CPP_UTILS_REDEC_H + +#include "dynarray.h" + +#endif // OPEN_CPP_UTILS_REDEC_H diff --git a/startup.h b/startup.h new file mode 100755 index 0000000..1b3c8bb --- /dev/null +++ b/startup.h @@ -0,0 +1,45 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_STARTUP_H +#define OPEN_CPP_UTILS_STARTUP_H + +#ifdef __cplusplus +#define STARTUP(f) \ + inline static void f(void); \ + struct f##_t_ { inline f##_t_(void) { f(); } }; inline static f##_t_ f##_; \ + inline static void f(void) +#elif defined(_MSC_VER) +#pragma section(".CRT$XCU",read) + #define INITIALIZER2_(f,p) \ + static void f(void); \ + __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ + __pragma(comment(linker,"/include:" p #f "_")) \ + static void f(void) + #ifdef _WIN64 + #define STARTUP(f) INITIALIZER2_(f,"") + #else + #define STARTUP(f) INITIALIZER2_(f,"_") + #endif +#else + #define STARTUP(f) \ + static void f(void) __attribute__((constructor)); \ + static void f(void) +#endif + +#endif // OPEN_CPP_UTILS_STARTUP_H diff --git a/template_utils.h b/template_utils.h new file mode 100755 index 0000000..436d649 --- /dev/null +++ b/template_utils.h @@ -0,0 +1,42 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_TEMPLATE_UTILS_H +#define OPEN_CPP_UTILS_TEMPLATE_UTILS_H + +namespace open_cpp_utils +{ + +/** + * \file template_utils.h + * \brief Provides compile time evaluation utilities for templates and template packs + */ + +/** + * \brief Check if a sequence of types is unique + * \tparam Ts + */ +template +inline static constexpr bool is_unique = std::true_type{}; + +template +inline constexpr bool is_unique = std::bool_constant<(!std::is_same_v && ...) && is_unique>{}; + +} + +#endif // OPEN_CPP_UTILS_TEMPLATE_UTILS_H diff --git a/test/test.cpp b/test/test.cpp new file mode 100755 index 0000000..bc9d516 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,36 @@ +// ===================================================================================================================== +// OpenShaderDesigner, an open source software utility to create materials and shaders. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#include "../object_pool.h" +#include "../directed_tree.h" +#include "../filesystem.h" + +using namespace open_cpp_utils; + +struct loader +{ + +}; + +int main(char** args, int argc) +{ + object_list test_obj_list; + directed_tree test_dir_tree; + filesystem test_filesystem; + return 0; +} diff --git a/unique_id.h b/unique_id.h new file mode 100755 index 0000000..1a99e00 --- /dev/null +++ b/unique_id.h @@ -0,0 +1,59 @@ +// ===================================================================================================================== +// open-cpp-utils, an open-source cpp library with data structures that extend the STL. +// Copyright (C) 2024 Medusa Slockbower +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// ===================================================================================================================== + +#ifndef OPEN_CPP_UTILS_UNIQUE_ID_H +#define OPEN_CPP_UTILS_UNIQUE_ID_H + +#include + +namespace open_cpp_utils +{ + +// Internal function for incrementing an id associated with a type +template uint64_t _increment_id() +{ + static uint64_t current = 0; + return current++; +} + +/** + * \brief Generate a unique id for a type given a base type + * \tparam Base Base type for id categorization + * \tparam Type Type for id generation + * \return Generated ID for Type and Base combination + * + * Example: + * + * unique_id() = 0 + * unique_id() = 1 + * + * unique_id() = 0 + */ +template uint64_t unique_id() +{ + static bool initialized = false; + static uint64_t id = 0; + + if(initialized) return id; + initialized = true; + return id = _increment_id(); +} + +} + +#endif // OPEN_CPP_UTILS_UNIQUE_ID_H diff --git a/vcpkg.json b/vcpkg.json new file mode 100755 index 0000000..a24db18 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,9 @@ +{ + "name" : "open-cpp-utils", + "version-string" : "1.0.0", + "builtin-baseline" : "7a57b42f959ad138a5283477fe2e6c97a7cb852f", + "dependencies" : [ { + "name" : "gtest", + "version>=" : "1.14.0#1" + } ] +} \ No newline at end of file