NDIter ====== .. cpp:class:: numpy::NDIter numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use NDIter NDIter obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``NDIter(const NDArray&array, boolc_order = true)`` - NP_NDITER.H:40 - * - ``NDIter(const NDArray&array, const std::vector&order)`` - NP_NDITER.H:62 - Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``value_type operator\*()`` - value_type - NP_NDITER.H:86 - * - ``NDIter & operator++()`` - NDIter & - NP_NDITER.H:97 - * - ``NDIter operator++(int)`` - NDIter - NP_NDITER.H:121 - * - ``bool operator==(const NDIter &other)`` - bool - NP_NDITER.H:128 - * - ``bool operator!=(const NDIter &other)`` - bool - NP_NDITER.H:139 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_finished()`` - bool - NP_NDITER.H:144 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``NDIter end(const NDArray&array)`` - NDIter - NP_NDITER.H:177 - :ref:`View ` * - ``size_t flat_index()`` - size_t - NP_NDITER.H:154 - :ref:`View ` * - ``const std::vector& indices()`` - const std::vector& - NP_NDITER.H:149 - :ref:`View ` * - ``void reset()`` - void - NP_NDITER.H:171 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-nditer-is_finished-0: .. dropdown:: is_finished (np_test_2_all.cpp:3824) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3814 :emphasize-lines: 11 for (size_t i = 0; i < 2; ++i) { for (size_t j = 0; j < 3; ++j) { arr.setElementAt({ i, j }, static_cast(i * 3 + j)); } } auto iter = nditer(arr); size_t count = 0; double expected_values[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; for (; !iter.is_finished(); ++iter, ++count) { if (!(isApproxEqualMCO(*iter, expected_values[count]))) { std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(*iter, expected_values[count]))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } if (!(count == 6)) { std::string description = std::string("testIterators():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(count == 6)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-nditer-end-1: .. dropdown:: end (np_test_1_all.cpp:6171) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6161 :emphasize-lines: 11 // Test sorted data generation std::vector data = gen.generate(50, DataPattern::SORTED); if (!(data.size() == 50)) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 50)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Verify data is sorted if (!(std::is_sorted(data.begin(), data.end()))) { std::string description = std::string("test_data_generator_sortedBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(std::is_sorted(data.begin(), data.end()))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Sorted data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-nditer-flat_index-2: .. dropdown:: flat_index (np_test_5_all.cpp:11181) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11171 :emphasize-lines: 11 for (size_t i = 0; i < 12; ++i) { arr.flat(i) = static_cast(i); } // Test C-order iteration auto it = numpy::nditer(arr, true); int count = 0; while (!it.is_finished()) { int value = *it; auto indices = it.indices(); size_t flat_idx = it.flat_index(); // Verify correctness if (value != count || flat_idx != count) { std::cout << " [FAIL] : in np_test_nditer_basic() : incorrect iteration order"; throw std::runtime_error("np_test_nditer_basic failed: incorrect iteration order"); } ++it; count++; } .. _example-nditer-indices-3: .. 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-nditer-reset-4: .. 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; }