Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,31 @@ which dynamic memory allocations are undesired.
### Code example

```cpp
// TODO: Add example
#include <array>
#include <cassert>

#include <beman/inplace_vector/inplace_vector.hpp>

using namespace beman::inplace_vector;

/**
* Generates fibonacci sequence using inplace_vector.
* See: https://en.wikipedia.org/wiki/Fibonacci_sequence
*/
template <int Capacity> inplace_vector<int, Capacity> fibonacci_to(int num) {
assert(num < Capacity);

inplace_vector<int, Capacity> vec;

for (auto i = 0; i <= num; ++i) {
static std::array<int, 2> first_two = {0, 1};

auto new_val = i < 2 ? first_two[i] : vec[i - 1] + vec[i - 2];
vec.push_back(new_val);
}

return vec;
}
```

## How to Build
Expand Down
17 changes: 17 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 49 additions & 0 deletions examples/fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <array>
#include <cassert>
#include <iostream>

#include <beman/inplace_vector/inplace_vector.hpp>

using namespace beman::inplace_vector;

/**
* Generates fibonacci sequence using inplace_vector.
* See: https://en.wikipedia.org/wiki/Fibonacci_sequence
*/
template <int Capacity> inplace_vector<int, Capacity> fibonacci_to(int num) {
assert(num < Capacity);

inplace_vector<int, Capacity> vec;

for (auto i = 0; i <= num; ++i) {
static std::array<int, 2> first_two = {0, 1};

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;
}
4 changes: 2 additions & 2 deletions include/beman/inplace_vector/inplace_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ struct inplace_vector_base : public inplace_vector_destruct_base<T, Capacity> {
}
inplace_vector_base(inplace_vector_base &&other) noexcept(
Capacity == 0 || std::is_nothrow_move_constructible_v<T>)
: inplace_vector_destruct_base<T, Capacity>(other.size) {
: inplace_vector_destruct_base<T, Capacity>(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<T> &&
Expand Down