ObjectArrayIterator =================== .. cpp:class:: numpy::ObjectArrayIterator numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use ObjectArrayIterator NDArray arr = np::arange(12).reshape({3, 4}); for (auto it = arr.begin(); it != arr.end(); ++it) { // ... process elements ... } Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``ObjectArrayIterator(NDArray\*arr, boolat_end = false)`` - NP_NESTED_ARRAY.H:27 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T operator\*()`` - T - NP_NESTED_ARRAY.H:35 - * - ``T \* operator->()`` - T \* - NP_NESTED_ARRAY.H:43 - * - ``ObjectArrayIterator & operator++()`` - ObjectArrayIterator & - NP_NESTED_ARRAY.H:54 - * - ``ObjectArrayIterator operator++(int)`` - ObjectArrayIterator - NP_NESTED_ARRAY.H:61 - * - ``bool operator==(const ObjectArrayIterator &other)`` - bool - NP_NESTED_ARRAY.H:68 - * - ``bool operator!=(const ObjectArrayIterator &other)`` - bool - NP_NESTED_ARRAY.H:74 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t flatIndex()`` - size_t - NP_NESTED_ARRAY.H:82 - :ref:`View ` * - ``bool incrementIndices()`` - bool - NP_NESTED_ARRAY.H:108 - :ref:`View ` * - ``const std::vector& indices()`` - const std::vector& - NP_NESTED_ARRAY.H:79 - :ref:`View ` * - ``void reset()`` - void - NP_NESTED_ARRAY.H:97 - :ref:`View ` * - ``bool valid()`` - bool - NP_NESTED_ARRAY.H:94 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-objectarrayiterator-flatindex-0: .. dropdown:: flatIndex (np_test_3_all.cpp:20906) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20896 :emphasize-lines: 11 // After increment, should be at [0, 1] ++it2; const auto& indices2 = it2.indices(); if (indices2.size() != 2 || indices2[0] != 0 || indices2[1] != 1) { std::cout << " [FAIL] : iterator incorrect indices after increment" << std::endl; throw std::runtime_error("np_test_extract_iterator failed: incorrect indices after increment"); } // Test flat index if (it2.flatIndex() != 1) { std::cout << " [FAIL] : iterator incorrect flat index" << std::endl; throw std::runtime_error("np_test_extract_iterator failed: incorrect flat index"); } // Test range-based for loop iteration_count = 0; for (auto obj : numpy::iterate(arr)) { int* val = obj.get_as(); if (val && *val == iteration_count) { ++iteration_count; .. _example-objectarrayiterator-incrementindices-1: .. dropdown:: incrementIndices (np_test_1_all.cpp:52) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 42 :emphasize-lines: 11 // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); .. _example-objectarrayiterator-indices-2: .. dropdown:: indices (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); .. _example-objectarrayiterator-reset-3: .. dropdown:: reset (np_test_2_all.cpp:22610) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22600 :emphasize-lines: 11 // Count iterations size_t outer_count = 0; size_t total_inner_count = 0; for (; !outer.is_finished(); ++outer) { size_t inner_count = 0; for (; !inner.is_finished(); ++inner) { inner_count++; total_inner_count++; } outer_count++; inner.reset(); } if (outer_count != 3 || total_inner_count != 12) { std::cout << " [FAIL] : Iteration counts incorrect"; throw std::runtime_error("np_test_nested_iters_basic failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-objectarrayiterator-valid-4: .. dropdown:: valid (np_test_2_all.cpp:6221) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6211 :emphasize-lines: 11 data.setElementAt({ 1 }, 20.0); data.setElementAt({ 2 }, 30.0); auto mask = createBoolArray({ 3 }, false); mask.setElementAt({ 1 }, true); // mask middle element MaskedArray ma(data, mask, -999.0); // Test equality comparison - skip for now as it needs scalar comparison support // auto eq_result = ma == 20.0; // assert(eq_result.is_masked_at({0})); // 10 != 20, but result should be valid (False) // assert(eq_result.is_masked_at({1})); // masked element propagates mask // assert(eq_result.is_masked_at({2})); // 30 != 20, but result should be valid (False) // std::cout << "[OK] Equality comparison with mask propagation\n"; // Test greater than - skip for now as it needs scalar comparison support // auto gt_result = ma > 15.0; // assert(gt_result.is_masked_at({1})); // masked element propagates // Note: comparison results for valid elements would depend on implementation // std::cout << "[OK] Greater than comparison with mask propagation\n";