lvc is a set of C++ containers extracted form LLVM for an easier integration in external projects.
To avoid any potential conflit, the llvm
namespace has been replaced by lvc
.
lvc::SmallVector
: astd::vector
like container with an inline small buffer to avoid dynamic allocations when a small number of elements is used.
More containers will be added over time or on request. Feel free to open a PR to add a new one.
#include <lvc/SmallVector.h>
// Take any SmallVector of ints no matter its inline buffer size
void addInt(lvc::SmallVectorImpl<int>& vec) {
vec.push_back(42);
}
int main() {
// reserve storage for two elements on the stack
lvc::SmallVector<int, 2> vec;
auto is_inline_buffer_used = [&v] {
const void* vec_start = &v;
constexpr auto vec_size = sizeof(v);
const void* buffer_start = v.data();
return (buffer_start > vec_start) && (buffer_start < vec_start + size);
};
addInt(vec);
addInt(vec);
// elements fit the inline buffer
assert(is_inline_buffer_used());
addInt(vec);
// too many elements, buffer has to be heap allocated
assert(!is_inline_buffer_used());
}
Unless tests are built, no external dependencies are required. The only tools you need to build lvc are:
- CMake >= 3.14
- A C++14 compatible compiler
Download/clone this repo, then build and install it as usual with CMake, e.g:
git clone https://github.com/BenjaminNavarro/lvc.git
cd lvc
mkdir build
cd build
cmake ..
cmake --build . --target install --parallel
Then in your project's CMakeLists.txt simply do:
find_package(lvc)
target_link_libraries(my-app PRIVATE lvc::lvc)
Add https://bnavarro.jfrog.io/artifactory/api/conan/bnavarro
to your remotes and declare a dependency to lvc (e.g lvc/11.1.0@bnavarro/stable
).
You can of course download/clone the repo then run the usual conan install
and conan create
commands.
The project is FetchContent friendly so CPM.cmake can be used.
For instance in your CMakeLists.txt you could do:
include(cmake/CPM.cmake)
CPMAddPackage("gh:BenjaminNavarro/lvc@11.1.0")
The source code comes from the LLVM project and receives only minimal modifications: namespace changed to lvc
and support files trimmed down to keep the number of included files low and the project small.
The initial project structure was generated by cmake-init.
The project's is the LLVM's version where the containers were extracted from.
The patch version may differ if the project requires fixes or if new containers are added.