2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://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 implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <array/ResultSet.h>
|
|
|
|
#include <graph/FlatUtils.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
|
|
|
ResultSet::ResultSet() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultSet::ResultSet(const sd::graph::FlatResult* result) {
|
|
|
|
for (int e = 0; e < result->variables()->size(); e++) {
|
|
|
|
auto var = result->variables()->Get(e);
|
|
|
|
|
|
|
|
NDArray* array;
|
|
|
|
|
|
|
|
if (var->ndarray() != nullptr) {
|
|
|
|
array = sd::graph::FlatUtils::fromFlatArray(var->ndarray());
|
|
|
|
} else if (var->shape() != nullptr) {
|
|
|
|
std::vector<Nd4jLong> shapeInfo;
|
|
|
|
for (int i = 0; i < var->shape()->size(); i++) {
|
|
|
|
shapeInfo.emplace_back(var->shape()->Get(i));
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
// we just create empty array here
|
|
|
|
int s0 = shapeInfo.at(0);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
std::vector<Nd4jLong> shape;
|
|
|
|
for (int i = 0; i < s0; i++) {
|
|
|
|
shape.emplace_back(shapeInfo.at(i + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
array = new NDArray((char) shapeInfo.at(shapeInfo.size() - 1), shape, DataTypeUtils::fromFlatDataType(var->dtype()));
|
|
|
|
} else {
|
|
|
|
nd4j_printf("Either shape or NDArray should be defined in FlatResult variable\n","");
|
|
|
|
throw std::runtime_error("Empty variable");
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
2020-03-02 10:49:41 +01:00
|
|
|
|
|
|
|
_content.push_back(array);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultSet::ResultSet(const ResultSet& other) noexcept{
|
|
|
|
for (const auto v:other._content)
|
|
|
|
_content.emplace_back(v);
|
|
|
|
|
|
|
|
_status = other._status;
|
|
|
|
_removable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// move constructor
|
|
|
|
ResultSet::ResultSet(ResultSet&& other) noexcept {
|
|
|
|
|
|
|
|
_content = std::move(other._content);
|
|
|
|
_status = other._status;
|
|
|
|
_removable = other._removable;
|
|
|
|
other._removable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2020-03-10 05:42:50 +01:00
|
|
|
// move assignment operator
|
2019-06-06 14:21:15 +02:00
|
|
|
ResultSet& ResultSet::operator=(ResultSet&& other) noexcept {
|
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
if (this == &other)
|
2019-06-06 14:21:15 +02:00
|
|
|
return *this;
|
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
delContent();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
_content = std::move(other._content);
|
2020-03-10 05:42:50 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
_status = other._status;
|
|
|
|
_removable = other._removable;
|
|
|
|
other._removable = false;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultSet& ResultSet::operator=(const ResultSet& other) noexcept {
|
|
|
|
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
delContent();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
for (const auto v : other._content)
|
|
|
|
_content.push_back(v);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
_status = other._status;
|
|
|
|
_removable = false;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
void ResultSet::delContent() {
|
2019-06-06 14:21:15 +02:00
|
|
|
if (_removable)
|
2020-03-10 05:42:50 +01:00
|
|
|
for (auto v : _content)
|
2019-06-06 14:21:15 +02:00
|
|
|
delete v;
|
|
|
|
}
|
|
|
|
|
2020-03-10 05:42:50 +01:00
|
|
|
ResultSet::~ResultSet() {
|
|
|
|
|
|
|
|
delContent();
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
void ResultSet::setNonRemovable() {
|
|
|
|
_removable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResultSet::size() {
|
|
|
|
return (int) _content.size();
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::NDArray* ResultSet::at(const unsigned long idx) const {
|
2019-06-06 14:21:15 +02:00
|
|
|
return _content.at(idx);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::NDArray* ResultSet::operator[](const unsigned long idx) const {
|
2019-06-06 14:21:15 +02:00
|
|
|
return _content[idx];
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void ResultSet::push_back(sd::NDArray *array) {
|
2019-06-06 14:21:15 +02:00
|
|
|
_content.emplace_back(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jStatus ResultSet::status() {
|
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResultSet::setStatus(Nd4jStatus status) {
|
|
|
|
_status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResultSet::purge() {
|
|
|
|
_content.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|