StructuredRecord ================ .. cpp:class:: numpy::StructuredRecord numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use StructuredRecord StructuredRecord obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T get(const std::string &field_name)`` - T - NP_STRUCTURED_DTYPE.H:225 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool hasField(const std::string &field_name)`` - bool - NP_STRUCTURED_DTYPE.H:239 - :ref:`View ` * - ``void set(const std::string &field_name, const T &value)`` - void - NP_STRUCTURED_DTYPE.H:234 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-structuredrecord-get-0: .. dropdown:: get (np_test_1_all.cpp:28526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28516 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_indexing_mask_indices() { std::cout << "========= mask_indices: triangular mask indices ======================="; // Get upper triangular indices for 3x3 matrix auto triu_idx = numpy::mask_indices(3, "triu", 0); // Should return tuple of 2 arrays bool passed = (std::get<0>(triu_idx).getSize() > 0); passed = passed && (std::get<1>(triu_idx).getSize() > 0); passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize()); // Get lower triangular indices auto tril_idx = numpy::mask_indices(3, "tril", 0); passed = passed && (std::get<0>(tril_idx).getSize() > 0); passed = passed && (std::get<1>(tril_idx).getSize() > 0); if (!passed) { std::cout << " [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect"; .. _example-structuredrecord-hasfield-1: .. dropdown:: hasField (np_test_2_all.cpp:18634) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 18624 :emphasize-lines: 11 // Verify field count auto dtype = data.getDType(); if (dtype.getFieldCount() != 5) { std::cout << " [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Expected 5 fields, got " << dtype.getFieldCount() << std::endl; throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong field count"); } // Verify field names if (!dtype.hasField("name") || !dtype.hasField("age") || !dtype.hasField("salary")) { std::cout << " [FAIL] : in np_test_io_extensions_recfromcsv_basic() : Missing expected fields"; throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: missing fields"); } // Verify data values auto name = data.getFieldValue({ 0 }, "name"); if (std::string(name) != "Alice") { std::cout << " [FAIL] : Expected 'Alice', got '" << std::string(name) << "'"; throw std::runtime_error("np_test_io_extensions_recfromcsv_basic failed: wrong name"); } .. _example-structuredrecord-set-2: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; }