Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added implementation of buffer #1

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ OPTION(BUILD_TESTS "xparrow test suite" OFF)
# =====

set(XPARROW_HEADERS
JohanMabille marked this conversation as resolved.
Show resolved Hide resolved
${XPARROW_INCLUDE_DIR}/xparrow/xparrow_version.pp
${XPARROW_INCLUDE_DIR}/xparrow/xbuffer.hpp
${XPARROW_INCLUDE_DIR}/xparrow/xparrow_version.hpp
)

add_library(xparrow INTERFACE)
Expand Down Expand Up @@ -65,7 +66,7 @@ install(TARGETS xparrow
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")

install(FILES ${Xparrow_HEADERS}
install(FILES ${XPARROW_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xparrow)

set(XPARROW_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}" CACHE
Expand Down
230 changes: 230 additions & 0 deletions include/xparrow/xbuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/***************************************************************************
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
***************************************************************************/

#pragma once

#include <algorithm>
#include <concepts>
#include <cstdint>

namespace xparrow
{
namespace impl
{
template <class T>
struct xbuffer_data
{
using value_type = T;
using pointer = T*;
using size_type = std::size_t;

bool empty() const noexcept;
size_type size() const noexcept;

template <class U = T>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to allow conversion of the stored element type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we may need to have some buffer of "raw" memory (i.e. uint8_t) that we need to reinterpret as another type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se my comment in the implementation.

U* data() noexcept;

template <class U = T>
const U* data() const noexcept;

void swap(xbuffer_data& rhs) noexcept;
bool equal(const xbuffer_data& rhs) const;

pointer p_data = nullptr;
size_type m_size = 0;
};
}

/**
* @class xbuffer
* @brief Object that owns a piece of contiguous memory
*/
template <class T>
class xbuffer : private impl::xbuffer_data<T>
{
public:

using base_type = impl::xbuffer_data<T>;
using value_type = typename base_type::value_type;
using pointer = typename base_type::pointer;
using size_type = typename base_type::size_type;

xbuffer() = default;
explicit xbuffer(size_type size);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the non-defaulted constructors, consider checking if noexcept is usable.

BTW I suspect this type could be constexpr (and could be tested at compile-time too) but maybe lets consider that in another pass.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructors allocates memory, so I'm not sure we can use noexcept here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on what's used for allocation exactly, for example std::vector can be instantiated in constexpr context in c++20. But otherwise o lets noexcept where it makes sense. My main worry is copy and move operations as they have an impact on the performance when stored in a vector.

xbuffer(pointer data, size_type size);

~xbuffer();

xbuffer(const xbuffer&);
xbuffer& operator=(const xbuffer&);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same points as previously with assignations


xbuffer(xbuffer&&);
xbuffer& operator=(xbuffer&&);

using base_type::empty;
using base_type::size;
using base_type::data;

void resize(size_type new_size);

void swap(xbuffer&) noexcept;
bool equal(const xbuffer& rhs) const;

private:

pointer allocate(size_type size) const;
void deallocate(pointer mem) const;
};

template <class T>
bool operator==(const xbuffer<T>& lhs, const xbuffer<T>& rhs);

/******************************
* xbuffer_data implementation *
******************************/

namespace impl
{
template <class T>
bool xbuffer_data<T>::empty() const noexcept
{
return size() == size_type(0);
}

template <class T>
auto xbuffer_data<T>::size() const noexcept -> size_type
Klaim marked this conversation as resolved.
Show resolved Hide resolved
{
return m_size;
}

template <class T>
template <class U>
U* xbuffer_data<T>::data() noexcept
{
return reinterpret_cast<U*>(p_data);
Klaim marked this conversation as resolved.
Show resolved Hide resolved
}

template <class T>
template <class U>
const U* xbuffer_data<T>::data() const noexcept
{
return reinterpret_cast<const U*>(p_data);
}

template <class T>
void xbuffer_data<T>::swap(xbuffer_data<T>& rhs) noexcept
{
std::swap(p_data, rhs.p_data);
std::swap(m_size, rhs.m_size);
}

template <class T>
bool xbuffer_data<T>::equal(const xbuffer_data<T>& rhs) const
{
return m_size == rhs.m_size && std::equal(p_data, p_data + m_size, rhs.p_data);
}
}

/*************************
* xbuffer implementation *
*************************/

template <class T>
xbuffer<T>::xbuffer(size_type size)
: base_type{allocate(size), size}
{
}

template <class T>
xbuffer<T>::xbuffer(pointer data, size_type size)
: base_type{data, size}
{
}

template <class T>
xbuffer<T>::~xbuffer()
{
deallocate(this->p_data);
}

template <class T>
xbuffer<T>::xbuffer(const xbuffer<T>& rhs)
: base_type{allocate(rhs.m_size), rhs.size()}
{
std::copy(rhs.data(), rhs.data() + rhs.size(), data());
}

template <class T>
xbuffer<T>& xbuffer<T>::operator=(const xbuffer<T>& rhs)
{
if (this != &rhs)
{
xbuffer<T> tmp(rhs);
swap(tmp);
}
return *this;
}

template <class T>
xbuffer<T>::xbuffer(xbuffer&& rhs)
: base_type{rhs.data(), rhs.size()}
{
rhs.p_data = nullptr;
rhs.m_size = 0u;
}

template <class T>
xbuffer<T>& xbuffer<T>::operator=(xbuffer<T>&& rhs)
{
swap(rhs);
return *this;
}

template <class T>
void xbuffer<T>::resize(size_type n)
{
// TODO: add capacity, resize if growing only and define a shrink_to_fit method
if (n != size())
{
xbuffer<T> tmp(n);
std::copy(data(), data() + size(), tmp.data());
swap(tmp);
}
}

template <class T>
void xbuffer<T>::swap(xbuffer<T>& rhs) noexcept
{
base_type::swap(rhs);
}

template <class T>
bool xbuffer<T>::equal(const xbuffer<T>& rhs) const
{
return base_type::equal(rhs);
}

template <class T>
auto xbuffer<T>::allocate(size_type size) const -> pointer
{
return new T[size];
}

template <class T>
void xbuffer<T>::deallocate(pointer mem) const
{
delete[] mem;
}

template <class T>
bool operator==(const xbuffer<T>& lhs, const xbuffer<T>& rhs)
{
return lhs.equal(rhs);
}
jjerphan marked this conversation as resolved.
Show resolved Hide resolved
}

5 changes: 2 additions & 3 deletions include/xparrow/xparrow_version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* *
* The full license is in the file LICENSE, distributed with this software. *
***************************************************************************/
#ifndef XPARROW_VERSION_HPP
#define XPARROW_VERSION_HPP

#pragma once

#define XPARROW_VERSION_MAJOR 0
#define XPARROW_VERSION_MINOR 0
#define XPARROW_VERSION_PATCH 1

#endif
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ endif()

set(XPARROW_TESTS
main.cpp
test_xbuffer.cpp
)
set(test_target "test_xparrow_lib")
add_executable(${test_target} ${XPARROW_TESTS})
target_link_libraries(${test_target} PRIVATE doctest::doctest)
target_link_libraries(${test_target} PRIVATE xparrow doctest::doctest)
Loading
Loading