-
Notifications
You must be signed in to change notification settings - Fork 12
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 null_type and implemented null_layout #120
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
// Copyright 2024 Man Group Operations Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <functional> | ||
#include <optional> | ||
#include <ranges> | ||
|
||
#include "sparrow/data_type.hpp" | ||
#include "sparrow/iterator.hpp" | ||
|
||
namespace sparrow | ||
{ | ||
|
||
/* | ||
* @class empty_iterator | ||
* | ||
* @brief Iterator used by the null_layout class. | ||
* | ||
* @tparam T the value_type of the iterator | ||
*/ | ||
template <class T> | ||
class empty_iterator : public iterator_base< | ||
empty_iterator<T>, | ||
T, | ||
std::contiguous_iterator_tag, | ||
T> | ||
{ | ||
public: | ||
|
||
using self_type = empty_iterator<T>; | ||
using base_type = iterator_base< | ||
self_type, | ||
T, | ||
std::contiguous_iterator_tag, | ||
T>; | ||
using reference = typename base_type::reference; | ||
using difference_type = typename base_type::difference_type; | ||
|
||
explicit empty_iterator(difference_type index = difference_type()) noexcept; | ||
|
||
private: | ||
|
||
reference dereference() const; | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void increment(); | ||
void decrement(); | ||
void advance(difference_type n); | ||
difference_type distance_to(const self_type& rhs) const; | ||
bool equal(const self_type& rhs) const; | ||
bool less_than(const self_type& rhs) const; | ||
|
||
difference_type m_index; | ||
|
||
friend class iterator_access; | ||
}; | ||
|
||
/* | ||
* @class null_layout | ||
* | ||
* @brief Memory-efficient layout for the Null data type. | ||
* | ||
* This layout is a memory-efficient layout for the Null data type where | ||
* all values are null. In this case, no memory buffers are allocated. | ||
*/ | ||
class null_layout | ||
{ | ||
public: | ||
|
||
using inner_value_type = null_type; | ||
using value_type = std::optional<inner_value_type>; | ||
using iterator = empty_iterator<value_type>; | ||
using const_iterator = empty_iterator<value_type>; | ||
using reference = iterator::reference; | ||
using const_reference = const_iterator::reference; | ||
using size_type = std::size_t; | ||
using difference_type = iterator::difference_type; | ||
using iterator_tag = std::contiguous_iterator_tag; | ||
|
||
|
||
using const_value_iterator = empty_iterator<int>; | ||
using const_bitmap_iterator = empty_iterator<bool>; | ||
|
||
using const_value_range = std::ranges::subrange<const_value_iterator>; | ||
using const_bitmap_range = std::ranges::subrange<const_bitmap_iterator>; | ||
|
||
explicit null_layout(array_data& data); | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void rebind_data(array_data& data); | ||
|
||
size_type size() const; | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
reference operator[](size_type i); | ||
const_reference operator[](size_type i) const; | ||
|
||
iterator begin(); | ||
iterator end(); | ||
|
||
const_iterator cbegin() const; | ||
const_iterator cend() const; | ||
|
||
const_value_range values() const; | ||
const_bitmap_range bitmap() const; | ||
|
||
private: | ||
|
||
difference_type ssize() const; | ||
|
||
array_data& data_ref(); | ||
const array_data& data_ref() const; | ||
|
||
std::reference_wrapper<array_data> m_data; | ||
}; | ||
|
||
/********************************* | ||
* empty_iterator implementation * | ||
*********************************/ | ||
|
||
template <class T> | ||
empty_iterator<T>::empty_iterator(difference_type index) noexcept | ||
: m_index(index) | ||
{ | ||
} | ||
|
||
template <class T> | ||
auto empty_iterator<T>::dereference() const -> reference | ||
{ | ||
return T(); | ||
} | ||
|
||
template <class T> | ||
void empty_iterator<T>::increment() | ||
{ | ||
++m_index; | ||
} | ||
|
||
template <class T> | ||
void empty_iterator<T>::decrement() | ||
{ | ||
--m_index; | ||
} | ||
|
||
template <class T> | ||
void empty_iterator<T>::advance(difference_type n) | ||
{ | ||
m_index += n; | ||
} | ||
|
||
template <class T> | ||
auto empty_iterator<T>::distance_to(const self_type& rhs) const -> difference_type | ||
{ | ||
return rhs.m_index - m_index; | ||
} | ||
|
||
template <class T> | ||
bool empty_iterator<T>::equal(const self_type& rhs) const | ||
{ | ||
return m_index == rhs.m_index; | ||
} | ||
|
||
template <class T> | ||
bool empty_iterator<T>::less_than(const self_type& rhs) const | ||
{ | ||
return m_index < rhs.m_index; | ||
} | ||
|
||
/****************************** | ||
* null_layout implementation * | ||
******************************/ | ||
|
||
inline null_layout::null_layout(array_data& data) | ||
: m_data(data) | ||
{ | ||
SPARROW_ASSERT_TRUE(data_ref().buffers.size() == 0u); | ||
Alex-PLACET marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
inline void null_layout::rebind_data(array_data& data) | ||
{ | ||
SPARROW_ASSERT_TRUE(data_ref().buffers.size() == 0u); | ||
m_data = data; | ||
} | ||
|
||
inline auto null_layout::size() const -> size_type | ||
{ | ||
return static_cast<size_type>(data_ref().length); | ||
} | ||
|
||
inline auto null_layout::operator[](size_type i) -> reference | ||
{ | ||
SPARROW_ASSERT_TRUE(i < size()); | ||
return *(begin()); | ||
} | ||
|
||
inline auto null_layout::operator[](size_type i) const -> const_reference | ||
{ | ||
SPARROW_ASSERT_TRUE(i < size()); | ||
return *(cbegin()); | ||
} | ||
|
||
inline auto null_layout::begin() -> iterator | ||
{ | ||
return iterator(0); | ||
} | ||
|
||
inline auto null_layout::end() -> iterator | ||
{ | ||
return iterator(ssize()); | ||
} | ||
|
||
inline auto null_layout::cbegin() const -> const_iterator | ||
{ | ||
return const_iterator(0); | ||
} | ||
|
||
inline auto null_layout::cend() const -> const_iterator | ||
{ | ||
return const_iterator(ssize()); | ||
} | ||
|
||
inline auto null_layout::values() const -> const_value_range | ||
{ | ||
return std::ranges::subrange(const_value_iterator(0), const_value_iterator(ssize())); | ||
} | ||
|
||
inline auto null_layout::bitmap() const -> const_bitmap_range | ||
{ | ||
return std::ranges::subrange(const_bitmap_iterator(0), const_bitmap_iterator(ssize())); | ||
} | ||
|
||
inline auto null_layout::ssize() const -> difference_type | ||
{ | ||
return static_cast<difference_type>(size()); | ||
} | ||
|
||
inline array_data& null_layout::data_ref() | ||
{ | ||
return m_data.get(); | ||
} | ||
|
||
inline const array_data& null_layout::data_ref() const | ||
{ | ||
return m_data.get(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang format