table_view.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
19 #include <cudf/types.hpp>
20 
21 #include <algorithm>
22 #include <vector>
23 
35 namespace cudf {
36 namespace detail {
49 template <typename ColumnView>
51  static_assert(std::is_same<ColumnView, column_view>::value or
52  std::is_same<ColumnView, mutable_column_view>::value,
53  "table_view_base can only be instantiated with column_view or "
54  "column_view_base.");
55 
56  private:
57  std::vector<ColumnView> _columns{};
58  size_type _num_rows{};
59 
60  public:
61  using iterator = decltype(std::begin(_columns));
62  using const_iterator = decltype(std::cbegin(_columns));
63 
80  explicit table_view_base(std::vector<ColumnView> const& cols);
81 
85  iterator begin() noexcept { return std::begin(_columns); }
86 
90  const_iterator begin() const noexcept { return std::begin(_columns); }
91 
98  iterator end() noexcept { return std::end(_columns); }
99 
106  const_iterator end() const noexcept { return std::end(_columns); }
107 
117  ColumnView const& column(size_type column_index) const;
118 
122  size_type num_columns() const noexcept { return _columns.size(); }
123 
127  size_type num_rows() const noexcept { return _num_rows; }
128 
132  size_type is_empty() const noexcept { return num_columns() == 0; }
133 
134  table_view_base() = default;
135 
136  ~table_view_base() = default;
137 
138  table_view_base(table_view_base const&) = default;
139 
140  table_view_base(table_view_base&&) = default;
141  table_view_base& operator=(table_view_base const&) = default;
142  table_view_base& operator=(table_view_base&&) = default;
143 };
144 } // namespace detail
145 
154 class table_view : public detail::table_view_base<column_view> {
156 
157  public:
158  using ColumnView = column_view;
159 
160  table_view() = default;
161 
180  table_view(std::vector<table_view> const& views);
181 
193  template <typename InputIterator>
194  table_view select(InputIterator begin, InputIterator end) const
195  {
196  std::vector<column_view> columns(std::distance(begin, end));
197  std::transform(begin, end, columns.begin(), [this](auto index) { return this->column(index); });
198  return table_view(columns);
199  }
200 
211  table_view select(std::vector<size_type> const& column_indices) const;
212 };
213 
222 class mutable_table_view : public detail::table_view_base<mutable_column_view> {
224 
225  public:
227 
228  mutable_table_view() = default;
229 
230  mutable_column_view& column(size_type column_index) const
231  {
232  return const_cast<mutable_column_view&>(table_view_base::column(column_index));
233  }
237  operator table_view();
238 
257  mutable_table_view(std::vector<mutable_table_view> const& views);
258 };
259 
260 inline bool has_nulls(table_view view)
261 {
262  return std::any_of(view.begin(), view.end(), [](column_view col) { return col.has_nulls(); });
263 }
264 
272 inline bool have_same_types(table_view const& lhs, table_view const& rhs)
273 {
274  return std::equal(
275  lhs.begin(),
276  lhs.end(),
277  rhs.begin(),
278  rhs.end(),
279  [](column_view const& lcol, column_view const& rcol) { return (lcol.type() == rcol.type()); });
280 }
281 
296  std::vector<size_type> const& map,
297  table_view const& target);
298 
299 namespace detail {
308 template <typename TableView>
309 bool is_relationally_comparable(TableView const& lhs, TableView const& rhs);
310 
311 extern template bool is_relationally_comparable<table_view>(table_view const& lhs,
312  table_view const& rhs);
313 extern template bool is_relationally_comparable<mutable_table_view>(mutable_table_view const& lhs,
314  mutable_table_view const& rhs);
315 } // namespace detail
316 } // namespace cudf
cudf::mutable_table_view
A set of mutable_column_views of the same size.
Definition: table_view.hpp:222
cudf::column
A container of nullable device data as a column of elements.
Definition: column.hpp:45
cudf::detail::table_view_base::begin
const_iterator begin() const noexcept
Returns an iterator to the first view in the table.
Definition: table_view.hpp:90
cudf::mutable_table_view::mutable_table_view
mutable_table_view(std::vector< mutable_table_view > const &views)
Construct a table from a vector of table views.
cudf::table_view::select
table_view select(std::vector< size_type > const &column_indices) const
Returns a table_view with set of specified columns.
cudf::table_view::select
table_view select(InputIterator begin, InputIterator end) const
Returns a table_view built from a range of column indices.
Definition: table_view.hpp:194
cudf::column_view
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Definition: column_view.hpp:294
types.hpp
Type declarations for libcudf.
cudf::detail::table_view_base::num_rows
size_type num_rows() const noexcept
Returns the number of rows.
Definition: table_view.hpp:127
cudf::mutable_column_view
A non-owning, mutable view of device data as a column of elements, some of which may be null as indic...
Definition: column_view.hpp:401
cudf::table_view::table_view
table_view(std::vector< table_view > const &views)
Construct a table from a vector of table views.
cudf::detail::table_view_base::begin
iterator begin() noexcept
Returns an iterator to the first view in the table.
Definition: table_view.hpp:85
cudf::detail::table_view_base::end
iterator end() noexcept
Returns an iterator one past the last column view in the table.
Definition: table_view.hpp:98
cudf::detail::table_view_base::num_columns
size_type num_columns() const noexcept
Returns the number of columns.
Definition: table_view.hpp:122
cudf::detail::table_view_base::table_view_base
table_view_base(std::vector< ColumnView > const &cols)
Construct a table from a vector of column views.
cudf::detail::table_view_base::end
const_iterator end() const noexcept
Returns an iterator one past the last column view in the table.
Definition: table_view.hpp:106
cudf::scatter_columns
table_view scatter_columns(table_view const &source, std::vector< size_type > const &map, table_view const &target)
Copy column_views from a table_view into another table_view according to a column indices map.
cudf::table_view
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:154
cudf::transform
std::unique_ptr< column > transform(column_view const &input, std::string const &unary_udf, data_type output_type, bool is_ptx, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Creates a new column by applying a unary function against every element of an input column.
cudf::have_same_types
bool have_same_types(table_view const &lhs, table_view const &rhs)
Checks if two table_views have columns of same types.
Definition: table_view.hpp:272
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::detail::table_view_base
Base class for a table of ColumnViews.
Definition: table_view.hpp:50
cudf::distance
size_type distance(T f, T l)
Similar to std::distance but returns cudf::size_type and performs static_cast
Definition: types.hpp:103
cudf::detail::table_view_base::column
ColumnView const & column(size_type column_index) const
Returns a reference to the view of the specified column.
cudf::is_relationally_comparable
constexpr bool is_relationally_comparable()
Indicates whether objects of types L and R can be relationally compared.
Definition: traits.hpp:105
column_view.hpp
column view class definitons
cudf::detail::table_view_base::is_empty
size_type is_empty() const noexcept
Returns true if num_columns() returns zero, or false otherwise.
Definition: table_view.hpp:132