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

Fully implement data slice API #526

Merged
merged 7 commits into from
Apr 7, 2019
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
45 changes: 12 additions & 33 deletions cpp/perspective/src/cpp/data_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ t_data_slice<CTX_T>::t_data_slice(std::shared_ptr<CTX_T> ctx, t_uindex start_row
template <typename CTX_T>
t_data_slice<CTX_T>::~t_data_slice() {}

/**
* @brief Returns the t_tscalar at the declared indices in the data slice,
* or an invalid t_tscalar if the indices should be skipped.
*
* @param ridx row index into the slice
* @param cidx column index into the slice
*
* @return t_tscalar a valid scalar containing the underlying data, or a new
* t_tscalar initialized with an invalid flag.
*/
// Public API
template <typename CTX_T>
t_tscalar
t_data_slice<CTX_T>::get(t_uindex ridx, t_uindex cidx) const {
Expand All @@ -63,35 +54,15 @@ t_data_slice<CTX_T>::get(t_uindex ridx, t_uindex cidx) const {
if (idx >= m_slice->size()) {
rv.clear();
} else {
rv = m_slice->at(idx);
rv = m_slice->operator[](idx);
}
return rv;
}

template <>
t_tscalar
t_data_slice<t_ctx2>::get(t_uindex ridx, t_uindex cidx) const {
t_uindex idx = get_slice_idx(ridx, cidx);
t_tscalar rv = m_slice->operator[](idx);
/* if (m_column_indices.size() > 0) {
t_uindex idx_skip_headers;
} else {

} */
return rv;
}

template <typename CTX_T>
std::vector<t_tscalar>
t_data_slice<CTX_T>::get_row_path(t_uindex idx) const {
return m_ctx->unity_get_row_path(idx);
}

template <typename CTX_T>
t_uindex
t_data_slice<CTX_T>::get_slice_idx(t_uindex ridx, t_uindex cidx) const {
t_uindex idx = (ridx - m_start_row) * m_stride + (cidx - m_start_col);
return idx;
t_data_slice<CTX_T>::get_row_path(t_uindex ridx) const {
return m_ctx->unity_get_row_path(ridx);
}

// Getters
Expand Down Expand Up @@ -148,6 +119,14 @@ t_data_slice<CTX_T>::get_data_extents() const {
return ext;
}

// Private
template <typename CTX_T>
t_uindex
t_data_slice<CTX_T>::get_slice_idx(t_uindex ridx, t_uindex cidx) const {
t_uindex idx = (ridx - m_start_row) * m_stride + (cidx - m_start_col);
return idx;
}

// Explicitly instantiate data slice for each context
template class t_data_slice<t_ctx0>;
template class t_data_slice<t_ctx1>;
Expand Down
76 changes: 2 additions & 74 deletions cpp/perspective/src/cpp/emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,76 +1881,6 @@ namespace binding {
return arr;
}

/**
* @brief Get a slice of data foe each column from the underlying view,
* serialized to val.
*
* @tparam CTX_T
* @param view
* @param start_row
* @param end_row
* @param start_col
* @param end_col
* @return val
*/
template <typename CTX_T>
val
get_data(std::shared_ptr<View<CTX_T>> view, std::uint32_t start_row, std::uint32_t end_row,
std::uint32_t start_col, std::uint32_t end_col) {
val arr = val::array();
auto data_slice = view->get_data(start_row, end_row, start_col, end_col);
auto slice = data_slice->get_slice();
for (auto idx = 0; idx < slice->size(); ++idx) {
arr.set(idx, scalar_to_val(slice->at(idx)));
}
return arr;
}

/**
* @brief Get a slice of data from the underlying view, serlializing to val
* and, for sorted views, ignoring the sort headers which aren't part of the
* underlying data.
*
* @param view
* @param start_row
* @param end_row
* @param start_col
* @param end_col
* @return val
*/
template <>
val
get_data(std::shared_ptr<View<t_ctx2>> view, std::uint32_t start_row, std::uint32_t end_row,
std::uint32_t start_col, std::uint32_t end_col) {
val arr = val::array();
auto data_slice = view->get_data(start_row, end_row, start_col, end_col);
auto slice = data_slice->get_slice();
auto column_indices = data_slice->get_column_indices();

if (column_indices.size() > 0) {
t_uindex i = 0;
auto iter = slice->begin();
while (iter != slice->end()) {
t_uindex prev = column_indices.front();
for (auto idx = column_indices.begin(); idx != column_indices.end();
idx++, i++) {
t_uindex col_num = *idx;
iter += col_num - prev;
prev = col_num;
arr.set(i, scalar_to_val(*iter));
}
if (iter != slice->end())
iter++;
}
} else {
for (auto idx = 0; idx < slice->size(); ++idx) {
arr.set(idx, scalar_to_val(slice->at(idx)));
}
}

return arr;
}

/**
* @brief Get the t_data_slice object, which contains an underlying slice of data and
* metadata required to interact with it.
Expand Down Expand Up @@ -2150,7 +2080,8 @@ EMSCRIPTEN_BINDINGS(perspective) {
class_<t_data_slice<t_ctx2>>("t_data_slice_ctx2")
.smart_ptr<std::shared_ptr<t_data_slice<t_ctx2>>>("shared_ptr<t_data_slice<t_ctx2>>>")
.function<const std::vector<std::string>&>(
"get_column_names", &t_data_slice<t_ctx2>::get_column_names);
"get_column_names", &t_data_slice<t_ctx2>::get_column_names)
.function<std::vector<t_tscalar>>("get_row_path", &t_data_slice<t_ctx2>::get_row_path);

/******************************************************************************
*
Expand Down Expand Up @@ -2276,9 +2207,6 @@ EMSCRIPTEN_BINDINGS(perspective) {
function("clone_gnode_table", &clone_gnode_table<val>, allow_raw_pointers());
function("scalar_vec_to_val", &scalar_vec_to_val);
function("table_add_computed_column", &table_add_computed_column<val>);
function("get_data_zero", &get_data<t_ctx0>);
function("get_data_one", &get_data<t_ctx1>);
function("get_data_two", &get_data<t_ctx2>);
function("col_to_js_typed_array_zero", &col_to_js_typed_array<t_ctx0>);
function("col_to_js_typed_array_one", &col_to_js_typed_array<t_ctx1>);
function("col_to_js_typed_array_two", &col_to_js_typed_array<t_ctx2>);
Expand Down
Loading