RecFunctions#

class numpy::RecFunctions#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use RecFunctions
RecFunctions obj;
// ... operations ...

Construction#

Signature

Return Type

Location

Example

StructuredValue createComparisonValue(const StructuredRecord &record)

StructuredValue

NP_RECFUNCTIONS.H:535

Indexing / Selection#

Signature

Return Type

Location

Example

std::string get_fieldstructure(const RecordArray &array)

std::string

NP_RECFUNCTIONS.H:412

View

Joining / Splitting#

Signature

Return Type

Location

Example

RecordArray append_fields(const RecordArray &base, const std::vector<std::string>&names, const std::vector<std::pair<DType,std::shared_ptr<void>>>&data, const std::vector<StructuredValue>&fill_values = {}, boolusemask = false, boolasrecarray = true)

RecordArray

NP_RECFUNCTIONS.H:13

View

RecordArray stack_arrays(const std::vector<RecordArray>&arrays, booldefaults = false, boolusemask = false, boolasrecarray = true, boolautoconvert = false)

RecordArray

NP_RECFUNCTIONS.H:217

View

Other Methods#

Signature

Return Type

Location

Example

size_t calculateLinearIndex(const std::vector<size_t>&indices, const std::vector<size_t>&shape)

size_t

NP_RECFUNCTIONS.H:498

RecordArray drop_fields(const RecordArray &base, const std::vector<std::string>&drop_names, boolusemask = false, boolasrecarray = true)

RecordArray

NP_RECFUNCTIONS.H:62

View

RecordArray join_by(const std::string &key, const RecordArray &r1, const RecordArray &r2, const std::string &jointype = "inner", const std::string &r1postfix = "1", const std::string &r2postfix = "2", booldefaults = false, boolusemask = false, boolasrecarray = true)

RecordArray

NP_RECFUNCTIONS.H:270

View

RecordArray merge_arrays(const std::vector<RecordArray>&arrays, boolflatten = false, boolusemask = false, boolasrecarray = true)

RecordArray

NP_RECFUNCTIONS.H:147

View

RecordArray rename_fields(const RecordArray &base, const std::unordered_map<std::string,std::string>&namemap)

RecordArray

NP_RECFUNCTIONS.H:105

View

RecordArray repack_fields(const RecordArray &array, boolalign = false)

RecordArray

NP_RECFUNCTIONS.H:416

View

void setFieldFromData(RecordArray &array, const std::vector<size_t>&indices, const std::string &field_name, const std::pair<DType,std::shared_ptr<void>>&data, size_toffset)

void

NP_RECFUNCTIONS.H:508

Code Examples#

The following examples are extracted from the test suite.

get_fieldstructure (np_test_1_all.cpp:21735)
21725    // std::cout << "Base array:" << std::endl;
21726    //base.printArray();
21727
21728    std::vector<std::string> names = get_names(base);
21729    // std::cout << "Field names: ";
21730    for (const auto& name : names) {
21731      // std::cout << name << " ";
21732    }
21733    // std::cout << std::endl;
21734
21735    // std::cout << "Field structure: " << get_fieldstructure(base) << std::endl;
21736
21737    auto dropped = drop_fields(base, { "score" });
21738    // std::cout << "After dropping 'score' field:";
21739    //dropped.printArray();
21740
21741    std::unordered_map<std::string, std::string> rename_map = { {"name", "student_name"} };
21742    auto renamed = rename_fields(base, rename_map);
21743    // std::cout << "After renaming 'name' to 'student_name':" << std::endl;
21744    //renamed.printArray();
append_fields (np_test_1_all.cpp:25911)
25901      auto score_ptr = make_shared_data(score_data);
25902      auto rank_ptr = make_shared_data(rank_data);
25903
25904      // Append fields
25905      std::vector<std::string> new_field_names = { "score", "rank" };
25906      std::vector<std::pair<numpy::DType, std::shared_ptr<void>>> new_field_data = {
25907          {numpy::DType::FLOAT64, score_ptr},
25908          {numpy::DType::INT32, rank_ptr}
25909      };
25910
25911      auto result = numpy::RecFunctions::append_fields(base_arr, new_field_names, new_field_data);
25912
25913      // Verify result has 4 fields
25914      auto field_names = result.getFieldNames();
25915      bool passed = (field_names.size() == 4);
25916
25917      if (passed) {
25918        // Verify original fields preserved
25919        passed = (result.getFieldValue<numpy::int32>({ 0 }, "id") == 1);
25920        passed = passed && (result.getFieldValue<numpy::int32>({ 1 }, "id") == 2);
25921        passed = passed && (result.getFieldValue<numpy::int32>({ 2 }, "name_length") == 6);
stack_arrays (np_test_1_all.cpp:21784)
21774    // std::cout << "Array 1:" << std::endl;
21775    //arr1.printArray();
21776
21777    // std::cout << "Array 2:" << std::endl;
21778    //arr2.printArray();
21779
21780    auto merged = merge_arrays({ arr1, arr2 });
21781    // std::cout << "Merged arrays:";
21782    //merged.printArray();
21783
21784    auto stacked = stack_arrays({ arr1 });
21785    // std::cout << "Stacked arrays (single array):";
21786    //stacked.printArray();
21787
21788    std::cout << " -> tests passed" << std::endl;
21789  }
21790
21791  void testRecFunctionsJoin() {
21792    std::cout << "========= testRecFunctionsJoin =======================";
21793
21794    auto dtype = std::make_shared<StructuredDType>(std::vector<std::pair<std::string, DType>>{
drop_fields (np_test_1_all.cpp:21737)
21727    std::vector<std::string> names = get_names(base);
21728    // std::cout << "Field names: ";
21729    for (const auto& name : names) {
21730      // std::cout << name << " ";
21731    }
21732    // std::cout << std::endl;
21733
21734    // std::cout << "Field structure: " << get_fieldstructure(base) << std::endl;
21735
21736    auto dropped = drop_fields(base, { "score" });
21737    // std::cout << "After dropping 'score' field:";
21738    //dropped.printArray();
21739
21740    std::unordered_map<std::string, std::string> rename_map = { {"name", "student_name"} };
21741    auto renamed = rename_fields(base, rename_map);
21742    // std::cout << "After renaming 'name' to 'student_name':" << std::endl;
21743    //renamed.printArray();
21744
21745    std::cout << " -> tests passed" << std::endl;
21746  }
join_by (np_test_1_all.cpp:21826)
21816    right.setFieldValue({ 1 }, "info", std::string("B"));
21817    right.setFieldValue({ 2 }, "key", static_cast<int32>(4));
21818    right.setFieldValue({ 2 }, "info", std::string("D"));
21819
21820    // std::cout << "Left array:" << std::endl;
21821    //left.printArray();
21822
21823    // std::cout << "Right array:" << std::endl;
21824    //right.printArray();
21825
21826    auto inner_join = join_by("key", left, right, "inner");
21827    // std::cout << "Inner join on 'key':" << std::endl;
21828    //inner_join.printArray();
21829
21830    auto outer_join = join_by("key", left, right, "outer");
21831    // std::cout << "Outer join on 'key':" << std::endl;
21832    //outer_join.printArray();
21833
21834    std::cout << " -> tests passed" << std::endl;
21835  }
merge_arrays (np_test_1_all.cpp:21780)
21770    arr2.setFieldValue({ 0 }, "name", std::string("Alice"));
21771    arr2.setFieldValue({ 1 }, "id", static_cast<int32>(4));
21772    arr2.setFieldValue({ 1 }, "name", std::string("Bob"));
21773
21774    // std::cout << "Array 1:" << std::endl;
21775    //arr1.printArray();
21776
21777    // std::cout << "Array 2:" << std::endl;
21778    //arr2.printArray();
21779
21780    auto merged = merge_arrays({ arr1, arr2 });
21781    // std::cout << "Merged arrays:";
21782    //merged.printArray();
21783
21784    auto stacked = stack_arrays({ arr1 });
21785    // std::cout << "Stacked arrays (single array):";
21786    //stacked.printArray();
21787
21788    std::cout << " -> tests passed" << std::endl;
21789  }
rename_fields (np_test_1_all.cpp:21742)
21732    }
21733    // std::cout << std::endl;
21734
21735    // std::cout << "Field structure: " << get_fieldstructure(base) << std::endl;
21736
21737    auto dropped = drop_fields(base, { "score" });
21738    // std::cout << "After dropping 'score' field:";
21739    //dropped.printArray();
21740
21741    std::unordered_map<std::string, std::string> rename_map = { {"name", "student_name"} };
21742    auto renamed = rename_fields(base, rename_map);
21743    // std::cout << "After renaming 'name' to 'student_name':" << std::endl;
21744    //renamed.printArray();
21745
21746    std::cout << " -> tests passed" << std::endl;
21747  }
21748
21749  void testRecFunctionsMergeStack() {
21750    std::cout << "========= testRecFunctionsMergeStack =======================";
21751
21752    auto dtype1 = std::make_shared<StructuredDType>(std::vector<std::pair<std::string, DType>>{
repack_fields (np_test_1_all.cpp:21867)
21857    // std::cout << "Array with duplicates:" << std::endl;
21858    //arr.printArray();
21859
21860    auto duplicates = find_duplicates(arr, "id");
21861    // std::cout << "Duplicate indices based on 'id' field: ";
21862    for (size_t idx : duplicates) {
21863      // std::cout << idx << " ";
21864    }
21865    // std::cout << std::endl;
21866
21867    auto repacked = repack_fields(arr, false);
21868    // std::cout << "Repacked array:" << std::endl;
21869    //repacked.printArray();
21870
21871    std::cout << " -> tests passed" << std::endl;
21872  }
21873
21874  void testStringParsing() {
21875    std::cout << "========= testStringParsing =======================";
21876
21877    auto dtype = std::make_shared<StructuredDType>(std::vector<std::pair<std::string, DType>>{