Skip to content

Vector

lockfree_ring_buffer

lockfree_ring_buffer class

template <typename T> lockfree_ring_buffer

Single producer single consumer lock-free ring buffer

Source code
template <typename T>
struct lockfree_ring_buffer
{
    lockfree_ring_buffer() : front(0), tail(0) {}

    size_t size() const
    {
        return tail.load(std::memory_order_relaxed) - front.load(std::memory_order_relaxed);
    }

    template <univector_tag Tag>
    size_t try_enqueue(const T* source, size_t size, univector<T, Tag>& buffer, bool partial = false)
    {
        const size_t cur_tail   = tail.load(std::memory_order_relaxed);
        const size_t avail_size = buffer.size() - (cur_tail - front.load(std::memory_order_relaxed));
        if (size > avail_size)
        {
            if (!partial)
                return 0;
            size = std::min(size, avail_size);
        }
        std::atomic_thread_fence(std::memory_order_acquire);

        const size_t real_tail  = cur_tail % buffer.size();
        const size_t first_size = std::min(buffer.size() - real_tail, size);
        builtin_memcpy(buffer.data() + real_tail, source, first_size * sizeof(T));
        builtin_memcpy(buffer.data(), source + first_size, (size - first_size) * sizeof(T));

        std::atomic_thread_fence(std::memory_order_release);

        tail.store(cur_tail + size, std::memory_order_relaxed);
        return size;
    }

    template <univector_tag Tag>
    size_t try_dequeue(T* dest, size_t size, const univector<T, Tag>& buffer, bool partial = false)
    {
        const size_t cur_front  = front.load(std::memory_order_relaxed);
        const size_t avail_size = tail.load(std::memory_order_relaxed) - cur_front;
        if (size > avail_size)
        {
            if (!partial)
                return 0;
            size = std::min(size, avail_size);
        }
        std::atomic_thread_fence(std::memory_order_acquire);

        const size_t real_front = cur_front % buffer.size();
        const size_t first_size = std::min(buffer.size() - real_front, size);
        builtin_memcpy(dest, buffer.data() + real_front, first_size * sizeof(T));
        builtin_memcpy(dest + first_size, buffer.data(), (size - first_size) * sizeof(T));

        std::atomic_thread_fence(std::memory_order_release);

        front.store(cur_front + size, std::memory_order_relaxed);
        return size;
    }

private:
    std::atomic<size_t> front;
    char cacheline_filler[64 - sizeof(std::atomic<size_t>)];
    std::atomic<size_t> tail;
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L575

make_univector function

template <typename T>
univector_ref<T> make_univector(T *data, size_t size)

Creates univector from data and size

Source code
template <typename T>
KFR_INTRINSIC univector_ref<T> make_univector(T* data, size_t size)
{
    return univector_ref<T>(data, size);
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L531

template <typename T>
univector_ref<const T> make_univector(const T *data,
                                      size_t size)

Creates univector from data and size

Source code
template <typename T>
KFR_INTRINSIC univector_ref<const T> make_univector(const T* data, size_t size)
{
    return univector_ref<const T>(data, size);
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L538

template <typename Container,
          KFR_ENABLE_IF(kfr::has_data_size<Container>),
          typename T = value_type_of<Container>>
univector_ref<const T>
make_univector(const Container &container)

Creates univector from a container (must have data() and size() methods)

Source code
template <typename Container, KFR_ENABLE_IF(kfr::has_data_size<Container>),
          typename T = value_type_of<Container>>
KFR_INTRINSIC univector_ref<const T> make_univector(const Container& container)
{
    return univector_ref<const T>(container.data(), container.size());
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L546

template <typename Container,
          KFR_ENABLE_IF(kfr::has_data_size<Container>),
          typename T = value_type_of<Container>>
univector_ref<T> make_univector(Container &container)

Creates univector from a container (must have data() and size() methods)

Source code
template <typename Container, KFR_ENABLE_IF(kfr::has_data_size<Container>),
          typename T = value_type_of<Container>>
KFR_INTRINSIC univector_ref<T> make_univector(Container& container)
{
    return univector_ref<T>(container.data(), container.size());
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L554

template <typename T, size_t N>
univector_ref<T> make_univector(T (&arr)[N])

Creates univector from a sized array

Source code
template <typename T, size_t N>
KFR_INTRINSIC univector_ref<T> make_univector(T (&arr)[N])
{
    return univector_ref<T>(arr, N);
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L561

template <typename T, size_t N>
univector_ref<const T> make_univector(const T (&arr)[N])

Creates univector from a sized array

Source code
template <typename T, size_t N>
KFR_INTRINSIC univector_ref<const T> make_univector(const T (&arr)[N])
{
    return univector_ref<const T>(arr, N);
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L568

render function

template <typename Expr,
          typename T = expression_value_type<Expr>>
univector<T> render(Expr &&expr)

Converts an expression to univector

Source code
template <typename Expr, typename T = expression_value_type<Expr>>
KFR_INTRINSIC univector<T> render(Expr&& expr)
{
    static_assert(expression_dims<Expr> == 1);
    static_assert(!is_infinite<Expr>,
                  "render: Can't process infinite expressions. Pass size as a second argument to render.");
    univector<T> result;
    result.resize(get_shape(expr).front());
    result = expr;
    return result;
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L658

template <typename Expr,
          typename T = expression_value_type<Expr>>
univector<T> render(Expr &&expr, size_t size,
                    size_t offset = 0)

Converts an expression to univector

Source code
template <typename Expr, typename T = expression_value_type<Expr>>
KFR_INTRINSIC univector<T> render(Expr&& expr, size_t size, size_t offset = 0)
{
    univector<T> result;
    result.resize(size);
    result = slice(expr, offset, size);
    return result;
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L671

template <typename Expr, size_t Size,
          typename T = expression_value_type<Expr>>
univector<T, Size> render(Expr &&expr, csize_t<Size>)

Converts an expression to univector

Source code
template <typename Expr, size_t Size, typename T = expression_value_type<Expr>>
KFR_INTRINSIC univector<T, Size> render(Expr&& expr, csize_t<Size>)
{
    univector<T, Size> result;
    result = expr;
    return result;
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L681

slice function

univector<T, 0> slice(size_t start = 0,
                      size_t size = max_size_t)

Returns subrange of the vector. If start is greater or equal to this->size, returns empty univector If requested size is greater than this->size, returns only available elements

Source code
univector<T, 0> slice(size_t start = 0, size_t size = max_size_t)
{
    T* data                = derived_cast<Class>(this)->data();
    const size_t this_size = derived_cast<Class>(this)->size();
    return array_ref<T>(data + start, std::min(size, start < this_size ? this_size - start : 0));
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L109

univector<const T, 0> slice(size_t start = 0,
                            size_t size = max_size_t) const

Returns subrange of the vector. If start is greater or equal to this->size, returns empty univector If requested size is greater than this->size, returns only available elements

Source code
univector<const T, 0> slice(size_t start = 0, size_t size = max_size_t) const
{
    const T* data          = derived_cast<Class>(this)->data();
    const size_t this_size = derived_cast<Class>(this)->size();
    return array_ref<const T>(data + start, std::min(size, start < this_size ? this_size - start : 0));
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L119

truncate function

univector<T, 0> truncate(size_t size = max_size_t)

Returns subrange of the vector starting from 0. If requested size is greater than this->size, returns only available elements

Source code
univector<T, 0> truncate(size_t size = max_size_t)
{
    T* data                = derived_cast<Class>(this)->data();
    const size_t this_size = derived_cast<Class>(this)->size();
    return array_ref<T>(data, std::min(size, this_size));
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L128

univector<const T, 0>
truncate(size_t size = max_size_t) const

Returns subrange of the vector starting from 0. If requested size is greater than this->size, returns only available elements

Source code
univector<const T, 0> truncate(size_t size = max_size_t) const
{
    const T* data          = derived_cast<Class>(this)->data();
    const size_t this_size = derived_cast<Class>(this)->size();
    return array_ref<const T>(data, std::min(size, this_size));
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L137

univector

univector class

template <typename T,
          univector_tag Tag = tag_dynamic_vector>
univector

Class that represent data in KFR. Many KFR functions can take this class as an argument. Can inherit from std::vector, std::array or keep only reference to data and its size.

univector is inherited from std::vector univector is inherited from std::array univector contains only reference to data

To convert a plain pointer to univector, call make_univector:

double* buffer;
size_t size;
univector<double, 0> v = make_univector(buffer, size);
// or pass result vector directly to a function:
some_function(make_univector(buffer, size));

Source code
template <typename T, univector_tag Tag = tag_dynamic_vector>
struct univector

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L88

univector

template <typename T, size_t Size> univector

Class that represent data in KFR. Many KFR functions can take this class as an argument. Can inherit from std::vector, std::array or keep only reference to data and its size.

univector is inherited from std::vector univector is inherited from std::array univector contains only reference to data

To convert a plain pointer to univector, call make_univector:

double* buffer;
size_t size;
univector<double, 0> v = make_univector(buffer, size);
// or pass result vector directly to a function:
some_function(make_univector(buffer, size));

Source code
template <typename T, size_t Size>
struct alignas(platform<>::maximum_vector_alignment) univector
    : std::array<T, Size>,
      univector_base<T, univector<T, Size>, is_vec_element<T>>
{
    static_assert(!std::is_const_v<T>, "Static vector doesn't allow T to be const");

    using std::array<T, Size>::size;
    using size_type = size_t;
#if !defined CMT_COMPILER_MSVC || defined CMT_COMPILER_CLANG
    univector(univector& v) : univector(const_cast<const univector&>(v)) {}
#endif
    univector(const univector& v)   = default;
    univector(univector&&) noexcept = default;
    template <typename Input, KFR_ACCEPT_EXPRESSIONS(Input)>
    univector(Input&& input)
    {
        this->assign_expr(std::forward<Input>(input));
    }
    template <typename... Args>
    constexpr univector(const T& x, const Args&... args) CMT_NOEXCEPT
        : std::array<T, Size>{ { x, static_cast<T>(args)... } }
    {
    }

    constexpr univector() CMT_NOEXCEPT_SPEC(noexcept(std::array<T, Size>())) = default;
    constexpr univector(size_t, const T& value) { std::fill(this->begin(), this->end(), value); }
    constexpr static bool size_known    = true;
    constexpr static size_t static_size = Size;
    constexpr static bool is_array      = true;
    constexpr static bool is_array_ref  = false;
    constexpr static bool is_vector     = false;
    constexpr static bool is_aligned    = true;
    using value_type                    = T;

    value_type get(size_t index, value_type fallback_value) const CMT_NOEXCEPT
    {
        return index < this->size() ? this->operator[](index) : fallback_value;
    }
    using univector_base<T, univector, is_vec_element<T>>::operator=;

    void resize(size_t) CMT_NOEXCEPT {}
}

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L297

univector_base

univector_base class

template <typename T, typename Class, bool is_expression>
univector_base

Base class for all univector specializations.

Source code
template <typename T, typename Class, bool is_expression>
struct univector_base

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L92

univector_dyn typedef

template <typename T>
univector_dyn = univector<T, tag_dynamic_vector>

Alias for univector<T, tag_dynamic_vector>;

Source code
template <typename T>
using univector_dyn = univector<T, tag_dynamic_vector>

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L520

univector_dyn = univector<T, tag_dynamic_vector>

Alias for univector<T, tag_dynamic_vector>;

Source code
using univector_dyn = univector<T, tag_dynamic_vector>

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L520

univector_ref typedef

template <typename T>
univector_ref = univector<T, tag_array_ref>

Alias for univector<T, tag_array_ref>;

Source code
template <typename T>
using univector_ref = univector<T, tag_array_ref>

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L516

univector_ref = univector<T, tag_array_ref>

Alias for univector<T, tag_array_ref>;

Source code
using univector_ref = univector<T, tag_array_ref>

https://github.com/kfrlib/kfr/blob//include/kfr/base/univector.hpp#L516


Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/