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

Record should hold a pointer to RecordArray, not an instance. #106

Merged
merged 2 commits into from Feb 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION_INFO
@@ -1 +1 @@
0.1.94
0.1.106
8 changes: 4 additions & 4 deletions include/awkward/array/Record.h
Expand Up @@ -8,8 +8,8 @@
namespace awkward {
class Record: public Content {
public:
Record(const RecordArray& array, int64_t at);
const std::shared_ptr<Content> array() const;
Record(const std::shared_ptr<RecordArray>& array, int64_t at);
const std::shared_ptr<RecordArray> array() const;
int64_t at() const;
const std::vector<std::shared_ptr<Content>> contents() const;
const std::shared_ptr<util::RecordLookup> recordlookup() const;
Expand Down Expand Up @@ -57,7 +57,7 @@ namespace awkward {
const std::shared_ptr<Content> field(const std::string& key) const;
const std::vector<std::shared_ptr<Content>> fields() const;
const std::vector<std::pair<std::string, std::shared_ptr<Content>>> fielditems() const;
const Record astuple() const;
const std::shared_ptr<Record> astuple() const;

protected:
const std::shared_ptr<Content> getitem_next(const SliceAt& at, const Slice& tail, const Index64& advanced) const override;
Expand All @@ -67,7 +67,7 @@ namespace awkward {
const std::shared_ptr<Content> getitem_next(const SliceFields& fields, const Slice& tail, const Index64& advanced) const override;

private:
RecordArray array_;
std::shared_ptr<RecordArray> array_;
int64_t at_;
};
}
Expand Down
2 changes: 1 addition & 1 deletion include/awkward/array/RecordArray.h
Expand Up @@ -63,7 +63,7 @@ namespace awkward {
const std::shared_ptr<Content> field(const std::string& key) const;
const std::vector<std::shared_ptr<Content>> fields() const;
const std::vector<std::pair<std::string, std::shared_ptr<Content>>> fielditems() const;
const RecordArray astuple() const;
const std::shared_ptr<RecordArray> astuple() const;

void append(const std::shared_ptr<Content>& content, const std::string& key);
void append(const std::shared_ptr<Content>& content);
Expand Down
85 changes: 37 additions & 48 deletions src/libawkward/array/Record.cpp
Expand Up @@ -10,13 +10,13 @@
#include "awkward/array/Record.h"

namespace awkward {
Record::Record(const RecordArray& array, int64_t at)
: Content(Identities::none(), array.parameters())
Record::Record(const std::shared_ptr<RecordArray>& array, int64_t at)
: Content(Identities::none(), array.get()->parameters())
, array_(array)
, at_(at) { }

const std::shared_ptr<Content> Record::array() const {
return array_.shallow_copy();
const std::shared_ptr<RecordArray> Record::array() const {
return array_;
}

int64_t Record::at() const {
Expand All @@ -25,18 +25,18 @@ namespace awkward {

const std::vector<std::shared_ptr<Content>> Record::contents() const {
std::vector<std::shared_ptr<Content>> out;
for (auto item : array_.contents()) {
for (auto item : array_.get()->contents()) {
out.push_back(item.get()->getitem_at_nowrap(at_));
}
return out;
}

const std::shared_ptr<util::RecordLookup> Record::recordlookup() const {
return array_.recordlookup();
return array_.get()->recordlookup();
}

bool Record::istuple() const {
return array_.istuple();
return array_.get()->istuple();
}

bool Record::isscalar() const {
Expand All @@ -48,7 +48,7 @@ namespace awkward {
}

const std::shared_ptr<Identities> Record::identities() const {
std::shared_ptr<Identities> recidentities = array_.identities();
std::shared_ptr<Identities> recidentities = array_.get()->identities();
if (recidentities.get() == nullptr) {
return recidentities;
}
Expand All @@ -66,20 +66,15 @@ namespace awkward {
}

const std::shared_ptr<Type> Record::type() const {
std::shared_ptr<Type> out = array_.type();
std::shared_ptr<Type> out = array_.get()->type();
out.get()->setparameters(parameters_);
return out;
}

const std::shared_ptr<Content> Record::astype(const std::shared_ptr<Type>& type) const {
std::shared_ptr<Content> record = array_.astype(type);
std::shared_ptr<Content> record = array_.get()->astype(type);
if (RecordArray* raw = dynamic_cast<RecordArray*>(record.get())) {
if (raw->numfields() == 0) {
return std::make_shared<Record>(RecordArray(raw->identities(), raw->parameters(), raw->length(), raw->istuple()), at_);
}
else {
return std::make_shared<Record>(RecordArray(raw->identities(), raw->parameters(), raw->contents(), raw->recordlookup()), at_);
}
return std::make_shared<Record>(array_, at_);
}
else {
throw std::invalid_argument(classname() + std::string(" cannot be converted to type ") + type.get()->tostring());
Expand All @@ -92,21 +87,21 @@ namespace awkward {
if (!parameters_.empty()) {
out << parameters_tostring(indent + std::string(" "), "", "\n");
}
out << array_.tostring_part(indent + std::string(" "), "", "\n");
out << array_.get()->tostring_part(indent + std::string(" "), "", "\n");
out << indent << "</" << classname() << ">" << post;
return out.str();
}

void Record::tojson_part(ToJson& builder) const {
size_t cols = (size_t)numfields();
std::shared_ptr<util::RecordLookup> keys = array_.recordlookup();
std::shared_ptr<util::RecordLookup> keys = array_.get()->recordlookup();
if (istuple()) {
keys = std::make_shared<util::RecordLookup>();
for (size_t j = 0; j < cols; j++) {
keys.get()->push_back(std::to_string(j));
}
}
std::vector<std::shared_ptr<Content>> contents = array_.contents();
std::vector<std::shared_ptr<Content>> contents = array_.get()->contents();
builder.beginrecord();
for (size_t j = 0; j < cols; j++) {
builder.field(keys.get()->at(j).c_str());
Expand All @@ -116,7 +111,7 @@ namespace awkward {
}

void Record::nbytes_part(std::map<size_t, int64_t>& largest) const {
return array_.nbytes_part(largest);
return array_.get()->nbytes_part(largest);
}

int64_t Record::length() const {
Expand All @@ -128,19 +123,13 @@ namespace awkward {
}

const std::shared_ptr<Content> Record::deep_copy(bool copyarrays, bool copyindexes, bool copyidentities) const {
std::shared_ptr<Content> out = array_.deep_copy(copyarrays, copyindexes, copyidentities);
RecordArray* raw = dynamic_cast<RecordArray*>(out.get());
if (raw->numfields() == 0) {
return std::make_shared<Record>(RecordArray(raw->identities(), raw->parameters(), raw->length(), raw->istuple()), at_);
}
else {
return std::make_shared<Record>(RecordArray(raw->identities(), raw->parameters(), raw->contents(), raw->recordlookup()), at_);
}
std::shared_ptr<Content> out = array_.get()->deep_copy(copyarrays, copyindexes, copyidentities);
return std::make_shared<Record>(std::dynamic_pointer_cast<RecordArray>(out), at_);
}

void Record::check_for_iteration() const {
if (array_.identities().get() != nullptr && array_.identities().get()->length() != 1) {
util::handle_error(failure("len(identities) != 1 for scalar Record", kSliceNone, kSliceNone), array_.identities().get()->classname(), nullptr);
if (array_.get()->identities().get() != nullptr && array_.get()->identities().get()->length() != 1) {
util::handle_error(failure("len(identities) != 1 for scalar Record", kSliceNone, kSliceNone), array_.get()->identities().get()->classname(), nullptr);
}
}

Expand All @@ -165,19 +154,19 @@ namespace awkward {
}

const std::shared_ptr<Content> Record::getitem_field(const std::string& key) const {
return array_.field(key).get()->getitem_at_nowrap(at_);
return array_.get()->field(key).get()->getitem_at_nowrap(at_);
}

const std::shared_ptr<Content> Record::getitem_fields(const std::vector<std::string>& keys) const {
RecordArray out(array_.identities(), parameters_, length(), istuple());
RecordArray out(array_.get()->identities(), parameters_, length(), istuple());
if (istuple()) {
for (auto key : keys) {
out.append(array_.field(key));
out.append(array_.get()->field(key));
}
}
else {
for (auto key : keys) {
out.append(array_.field(key), key);
out.append(array_.get()->field(key), key);
}
}
return out.getitem_at_nowrap(at_);
Expand All @@ -196,28 +185,28 @@ namespace awkward {
}

const std::pair<int64_t, int64_t> Record::minmax_depth() const {
std::pair<int64_t, int64_t> out = array_.minmax_depth();
std::pair<int64_t, int64_t> out = array_.get()->minmax_depth();
return std::pair<int64_t, int64_t>(out.first - 1, out.second - 1);
}

int64_t Record::numfields() const {
return array_.numfields();
return array_.get()->numfields();
}

int64_t Record::fieldindex(const std::string& key) const {
return array_.fieldindex(key);
return array_.get()->fieldindex(key);
}

const std::string Record::key(int64_t fieldindex) const {
return array_.key(fieldindex);
return array_.get()->key(fieldindex);
}

bool Record::haskey(const std::string& key) const {
return array_.haskey(key);
return array_.get()->haskey(key);
}

const std::vector<std::string> Record::keys() const {
return array_.keys();
return array_.get()->keys();
}

const Index64 Record::count64() const {
Expand All @@ -241,42 +230,42 @@ namespace awkward {
}

const std::shared_ptr<Content> Record::field(int64_t fieldindex) const {
return array_.field(fieldindex).get()->getitem_at_nowrap(at_);
return array_.get()->field(fieldindex).get()->getitem_at_nowrap(at_);
}

const std::shared_ptr<Content> Record::field(const std::string& key) const {
return array_.field(key).get()->getitem_at_nowrap(at_);
return array_.get()->field(key).get()->getitem_at_nowrap(at_);
}

const std::vector<std::shared_ptr<Content>> Record::fields() const {
std::vector<std::shared_ptr<Content>> out;
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(array_.field(j).get()->getitem_at_nowrap(at_));
out.push_back(array_.get()->field(j).get()->getitem_at_nowrap(at_));
}
return out;
}

const std::vector<std::pair<std::string, std::shared_ptr<Content>>> Record::fielditems() const {
std::vector<std::pair<std::string, std::shared_ptr<Content>>> out;
std::shared_ptr<util::RecordLookup> keys = array_.recordlookup();
std::shared_ptr<util::RecordLookup> keys = array_.get()->recordlookup();
if (istuple()) {
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(std::pair<std::string, std::shared_ptr<Content>>(std::to_string(j), array_.field(j).get()->getitem_at_nowrap(at_)));
out.push_back(std::pair<std::string, std::shared_ptr<Content>>(std::to_string(j), array_.get()->field(j).get()->getitem_at_nowrap(at_)));
}
}
else {
int64_t cols = numfields();
for (int64_t j = 0; j < cols; j++) {
out.push_back(std::pair<std::string, std::shared_ptr<Content>>(keys.get()->at((size_t)j), array_.field(j).get()->getitem_at_nowrap(at_)));
out.push_back(std::pair<std::string, std::shared_ptr<Content>>(keys.get()->at((size_t)j), array_.get()->field(j).get()->getitem_at_nowrap(at_)));
}
}
return out;
}

const Record Record::astuple() const {
return Record(array_.astuple(), at_);
const std::shared_ptr<Record> Record::astuple() const {
return std::make_shared<Record>(array_.get()->astuple(), at_);
}

const std::shared_ptr<Content> Record::getitem_next(const SliceAt& at, const Slice& tail, const Index64& advanced) const {
Expand Down
6 changes: 3 additions & 3 deletions src/libawkward/array/RecordArray.cpp
Expand Up @@ -272,7 +272,7 @@ namespace awkward {
}

const std::shared_ptr<Content> RecordArray::getitem_at_nowrap(int64_t at) const {
return std::make_shared<Record>(*this, at);
return std::make_shared<Record>(std::dynamic_pointer_cast<RecordArray>(shallow_copy()), at);
}

const std::shared_ptr<Content> RecordArray::getitem_range(int64_t start, int64_t stop) const {
Expand Down Expand Up @@ -587,8 +587,8 @@ namespace awkward {
return out;
}

const RecordArray RecordArray::astuple() const {
return RecordArray(identities_, parameters_, contents_);
const std::shared_ptr<RecordArray> RecordArray::astuple() const {
return std::make_shared<RecordArray>(identities_, parameters_, contents_);
}

void RecordArray::append(const std::shared_ptr<Content>& content, const std::string& key) {
Expand Down
8 changes: 5 additions & 3 deletions src/pyawkward.cpp
Expand Up @@ -1439,7 +1439,7 @@ py::class_<ak::RecordArray, std::shared_ptr<ak::RecordArray>, ak::Content> make_
return out;
})
.def_property_readonly("astuple", [](ak::RecordArray& self) -> py::object {
return box(self.astuple().shallow_copy());
return box(self.astuple());
})

.def("append", [](ak::RecordArray& self, py::object content, py::object key) -> void {
Expand All @@ -1456,7 +1456,9 @@ py::class_<ak::RecordArray, std::shared_ptr<ak::RecordArray>, ak::Content> make_

py::class_<ak::Record, std::shared_ptr<ak::Record>> make_Record(py::handle m, std::string name) {
return py::class_<ak::Record, std::shared_ptr<ak::Record>>(m, name.c_str())
.def(py::init<ak::RecordArray, int64_t>())
.def(py::init([](std::shared_ptr<ak::RecordArray> recordarray, int64_t at) -> ak::Record {
return ak::Record(recordarray, at);
}), py::arg("recordarray"), py::arg("at"))
.def("__repr__", &repr<ak::Record>)
.def_property_readonly("identities", [](ak::Record& self) -> py::object { return box(self.identities()); })
.def("__getitem__", &getitem<ak::Record>)
Expand Down Expand Up @@ -1508,7 +1510,7 @@ py::class_<ak::Record, std::shared_ptr<ak::Record>> make_Record(py::handle m, st
return out;
})
.def_property_readonly("astuple", [](ak::Record& self) -> py::object {
return box(self.astuple().shallow_copy());
return box(self.astuple());
})
.def_property_readonly("identity", &identity<ak::Record>)

Expand Down