diff --git a/CMakeLists.txt b/CMakeLists.txt index f671647..e7d6744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,13 @@ project( LANGUAGES CXX ) +# [CMAKE.SKIP_EXAMPLES] +option( + BEMAN_EXEMPLAR_BUILD_EXAMPLES + "Enable building examples. Default: ON. Values: { ON, OFF }." + ${PROJECT_IS_TOP_LEVEL} +) + # [CMAKE.SKIP_TESTS] option( BEMAN_INPLACE_VECTOR_BUILD_TESTS @@ -54,3 +61,7 @@ if(BEMAN_INPLACE_VECTOR_BUILD_TESTS) include(CTest) add_subdirectory(tests/beman/inplace_vector) endif() + +if(BEMAN_EXEMPLAR_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() diff --git a/README.md b/README.md index 1ea97a3..e10ce1f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,30 @@ which dynamic memory allocations are undesired. ### Code example ```cpp -// TODO: Add example +#include +#include + +#include + +using namespace beman::inplace_vector; + +/** + * Generates fibonacci sequence using inplace_vector. + * See: https://en.wikipedia.org/wiki/Fibonacci_sequence + */ +template inplace_vector fibonacci_to(int num) { + assert(num < Capacity); + + inplace_vector vec; + + constexpr static std::array first_two{0, 1}; + for (auto i = 0; i <= num; ++i) { + auto new_val = i < 2 ? first_two[i] : vec[i - 1] + vec[i - 2]; + vec.push_back(new_val); + } + + return vec; +} ``` ## How to Build diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..496bc08 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set(ALL_EXAMPLES fibonacci) + +message("Examples to be built: ${ALL_EXAMPLES}") + +foreach(example ${ALL_EXAMPLES}) + add_executable(beman.inplace_vector.examples.${example}) + target_sources( + beman.inplace_vector.examples.${example} + PRIVATE ${example}.cpp + ) + target_link_libraries( + beman.inplace_vector.examples.${example} + beman.inplace_vector + ) +endforeach() diff --git a/examples/fibonacci.cpp b/examples/fibonacci.cpp new file mode 100644 index 0000000..2757f11 --- /dev/null +++ b/examples/fibonacci.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +#include + +using namespace beman::inplace_vector; + +/** + * Generates fibonacci sequence using inplace_vector. + * See: https://en.wikipedia.org/wiki/Fibonacci_sequence + */ +template inplace_vector fibonacci_to(int num) { + assert(num < Capacity); + + inplace_vector vec; + + constexpr static std::array first_two{0, 1}; + for (auto i = 0; i <= num; ++i) { + auto new_val = i < 2 ? first_two[i] : vec[i - 1] + vec[i - 2]; + vec.push_back(new_val); + } + + return vec; +} + +/** + * Expected program output: + * + * 0: 0 + * 1: 1 + * 2: 1 + * 3: 2 + * 4: 3 + * 5: 5 + * 6: 8 + * 7: 13 + * 8: 21 + * 9: 34 + * 10: 55 + * + */ +int main() { + auto fib_seq = fibonacci_to<50>(10); + for (auto i = 0u; i < fib_seq.size(); ++i) + std::cout << i << ": " << fib_seq[i] << "\n"; + std::cout << std::endl; +} diff --git a/include/beman/inplace_vector/inplace_vector.hpp b/include/beman/inplace_vector/inplace_vector.hpp index 13c2ca2..70fbf8b 100644 --- a/include/beman/inplace_vector/inplace_vector.hpp +++ b/include/beman/inplace_vector/inplace_vector.hpp @@ -115,10 +115,10 @@ struct inplace_vector_base : public inplace_vector_destruct_base { } inplace_vector_base(inplace_vector_base &&other) noexcept( Capacity == 0 || std::is_nothrow_move_constructible_v) - : inplace_vector_destruct_base(other.size) { + : inplace_vector_destruct_base(other.size_) { std::copy(other.begin(), other.end(), begin()); std::destroy(other.begin(), other.end()); - other.size = 0; + other.size_ = 0; } inplace_vector_base &operator=(const inplace_vector_base &other) noexcept( std::is_nothrow_copy_constructible_v &&