Use Kwker from C and C++
The C package holds kwker.h (C11, also valid C++), the header-only C++17 API kwker.hpp, static and shared
libraries, a pkg-config file and a CMake package. Unpack the release archive for your platform; <prefix> below is
that directory (Installation).
The project file
cmake_minimum_required(VERSION 3.16)
project(latency C CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(Kwker 0.1 REQUIRED)
add_executable(latency main.cpp)
target_link_libraries(latency PRIVATE Kwker::kwker) # or Kwker::kwker_static
The program
The three slowest requests, without sorting the whole array:
#include <inttypes.h>
#include <stdio.h>
#include <kwker.h>
int main(void) {
const double latency_ms[] = {12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0};
double values[3];
uint64_t positions[3];
kwker_f64_top_k(latency_ms, 7, 3, KWKER_DESCENDING, 1, values, positions);
for (int i = 0; i < 3; i++) printf("%g at %" PRIu64 "\n", values[i], positions[i]);
return 0;
}
Output
310.2 at 4 260 at 6 250.4 at 1
#include <iostream>
#include <vector>
#include <kwker.hpp>
int main() {
std::vector<double> latency_ms{12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0};
auto [values, positions] = kwker::top_k(latency_ms, 3, kwker::Order::descending);
for (size_t i = 0; i < values.size(); i++) std::cout << values[i] << " at " << positions[i] << '\n';
}
Output
310.2 at 4 260 at 6 250.4 at 1
Build and run
cmake -B build -DCMAKE_PREFIX_PATH=<prefix>
cmake --build build
./build/latency
Without CMake, pkg-config gives the flags: c++ -std=c++17 main.cpp $(pkg-config --cflags --libs kwker) -o latency,
with PKG_CONFIG_PATH=<prefix>/lib/pkgconfig when the archive is outside the standard places.
Errors
- C functions return
0on success and-1for invalid arguments, and change nothing then. - The C++ functions throw
std::invalid_argumentfor invalid arguments andstd::runtime_errorfor I/O errors.
Next steps
- C API reference and C++ API reference: every function, its arguments and errors.
- Quickstart: Kwker Core: sorting, order and medians in every language.