Skip to content

Commit 5fdbf2d

Browse files
committed
Refactored handling of encodings and symbols.
All symbols are now represented by a SymbolIndex. This includes the tags on the pairlist, the names of the environment variables, the function name and the argument names in a language object, etc. Basically, anything that was a symbol in the RDS file is now represented by a SymbolIndex that points to the entry of the 'symbols' vector to get the actual symbol name (and its encoding). This change saves memory by avoiding duplication of strings for common symbols. Previously, we'd copy the string (and encoding) to each location where it was used; now we just hold an index to a unique set of symbols. It also simplifies the representation of many other things as we don't have to store names or encodings. That said, it is a little bit less convenient as it adds a layer of indirection, but honestly, most users won't be messing around with symbols. We also add dedicated classes for individual strings, attributes, pairlist elements, environment variables, etc. so that we don't have to check for consistency across multiple vectors. We also make more use of <optional> to represent, e.g., missing strings or untagged pairlist elements.
1 parent 6ef6e14 commit 5fdbf2d

34 files changed

Lines changed: 606 additions & 777 deletions

examples/src/write_dgCMatrix.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,35 @@ int main () {
1111
obj.package_name = "Matrix";
1212
obj.package_encoding = rds2cpp::StringEncoding::UTF8;
1313

14-
obj.attributes.names.push_back("i");
15-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
16-
auto ivec = new rds2cpp::IntegerVector;
17-
obj.attributes.values.emplace_back(ivec);
14+
auto ivec = std::make_unique<rds2cpp::IntegerVector>();
1815
ivec->data = std::vector<int32_t>{ 6, 8, 0, 3, 5, 6, 0, 1, 3, 7 };
16+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(ivec));
17+
file_info.symbols.emplace_back("i", rds2cpp::StringEncoding::UTF8);
1918

20-
obj.attributes.names.push_back("p");
21-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
22-
auto pvec = new rds2cpp::IntegerVector;
23-
obj.attributes.values.emplace_back(pvec);
19+
auto pvec = std::make_unique<rds2cpp::IntegerVector>();
2420
pvec->data = std::vector<int32_t>{ 0, 0, 2, 3, 4, 5, 6, 8, 8, 8, 10 };
21+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(pvec));
22+
file_info.symbols.emplace_back("p", rds2cpp::StringEncoding::UTF8);
2523

26-
obj.attributes.names.push_back("x");
27-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
28-
auto xvec = new rds2cpp::DoubleVector;
29-
obj.attributes.values.emplace_back(xvec);
24+
auto xvec = std::make_unique<rds2cpp::DoubleVector>();
3025
xvec->data = std::vector<double>{ 0.96, -0.34, 0.82, -2, -0.072, 0.39, 0.16, 0.36, -1.5, -0.047 };
26+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(xvec));
27+
file_info.symbols.emplace_back("x", rds2cpp::StringEncoding::UTF8);
3128

32-
obj.attributes.names.push_back("Dim");
33-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
34-
auto dims = new rds2cpp::IntegerVector;
35-
obj.attributes.values.emplace_back(dims);
29+
auto dims = std::make_unique<rds2cpp::IntegerVector>();
3630
dims->data = std::vector<int32_t>{ 10, 10 };
31+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(dims));
32+
file_info.symbols.emplace_back("Dim", rds2cpp::StringEncoding::UTF8);
3733

38-
obj.attributes.names.push_back("Dimnames");
39-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
40-
auto dimnames = new rds2cpp::GenericVector;
41-
obj.attributes.values.emplace_back(dimnames);
34+
auto dimnames = std::make_unique<rds2cpp::GenericVector>();
4235
dimnames->data.emplace_back(new rds2cpp::Null);
4336
dimnames->data.emplace_back(new rds2cpp::Null);
37+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(dimnames));
38+
file_info.symbols.emplace_back("Dimnames", rds2cpp::StringEncoding::UTF8);
4439

45-
obj.attributes.names.push_back("factors");
46-
obj.attributes.encodings.push_back(rds2cpp::StringEncoding::UTF8);
47-
auto factors = new rds2cpp::GenericVector;
48-
obj.attributes.values.emplace_back(factors);
40+
auto factors = std::make_unique<rds2cpp::GenericVector>();
41+
obj.attributes.emplace_back(file_info.symbols.size(), std::move(factors));
42+
file_info.symbols.emplace_back("factors", rds2cpp::StringEncoding::UTF8);
4943

5044
rds2cpp::write_rds(file_info, "my_matrix.rds", {});
5145
return 0;

include/rds2cpp/Environment.hpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

include/rds2cpp/ExternalPointer.hpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)