ObjectArrayIterator#
-
class numpy::ObjectArrayIterator#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use ObjectArrayIterator
NDArray<double> arr = np::arange<double>(12).reshape({3, 4});
for (auto it = arr.begin(); it != arr.end(); ++it) {
// ... process elements ...
}
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
NP_NESTED_ARRAY.H:27 |
Operators#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
T |
NP_NESTED_ARRAY.H:35 |
|
|
T * |
NP_NESTED_ARRAY.H:43 |
|
|
ObjectArrayIterator & |
NP_NESTED_ARRAY.H:54 |
|
|
ObjectArrayIterator |
NP_NESTED_ARRAY.H:61 |
|
|
bool |
NP_NESTED_ARRAY.H:68 |
|
|
bool |
NP_NESTED_ARRAY.H:74 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
NP_NESTED_ARRAY.H:82 |
|
|
bool |
NP_NESTED_ARRAY.H:108 |
|
|
const std::vector<size_t>& |
NP_NESTED_ARRAY.H:79 |
|
|
void |
NP_NESTED_ARRAY.H:97 |
|
|
bool |
NP_NESTED_ARRAY.H:94 |
Code Examples#
The following examples are extracted from the test suite.
flatIndex (np_test_3_all.cpp:20906)
20896 // After increment, should be at [0, 1]
20897 ++it2;
20898 const auto& indices2 = it2.indices();
20899 if (indices2.size() != 2 || indices2[0] != 0 || indices2[1] != 1) {
20900 std::cout << " [FAIL] : iterator incorrect indices after increment" << std::endl;
20901 throw std::runtime_error("np_test_extract_iterator failed: incorrect indices after increment");
20902 }
20903
20904 // Test flat index
20905 if (it2.flatIndex() != 1) {
20906 std::cout << " [FAIL] : iterator incorrect flat index" << std::endl;
20907 throw std::runtime_error("np_test_extract_iterator failed: incorrect flat index");
20908 }
20909
20910 // Test range-based for loop
20911 iteration_count = 0;
20912 for (auto obj : numpy::iterate(arr)) {
20913 int* val = obj.get_as<int>();
20914 if (val && *val == iteration_count) {
20915 ++iteration_count;
incrementIndices (np_test_1_all.cpp:52)
42// Helper functions for array comparison tests
43namespace {
44 const double TOLERANCE = 1e-10;
45
46 bool allTrue(const NDArray<bool>& arr) {
47 std::vector<size_t> indices(arr.getShape().size(), 0);
48 do {
49 if (!arr.getElementAt(indices)) {
50 return false;
51 }
52 } while (incrementIndices(indices, arr.getShape()));
53 return true;
54 }
55
56 bool allFalse(const NDArray<bool>& arr) {
57 std::vector<size_t> indices(arr.getShape().size(), 0);
58 do {
59 if (arr.getElementAt(indices)) {
60 return false;
61 }
62 } while (incrementIndices(indices, arr.getShape()));
indices (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43 const double TOLERANCE = 1e-10;
44
45 bool allTrue(const NDArray<bool>& arr) {
46 std::vector<size_t> indices(arr.getShape().size(), 0);
47 do {
48 if (!arr.getElementAt(indices)) {
49 return false;
50 }
51 } while (incrementIndices(indices, arr.getShape()));
52 return true;
53 }
54
55 bool allFalse(const NDArray<bool>& arr) {
56 std::vector<size_t> indices(arr.getShape().size(), 0);
reset (np_test_2_all.cpp:22610)
22600 // Count iterations
22601 size_t outer_count = 0;
22602 size_t total_inner_count = 0;
22603 for (; !outer.is_finished(); ++outer) {
22604 size_t inner_count = 0;
22605 for (; !inner.is_finished(); ++inner) {
22606 inner_count++;
22607 total_inner_count++;
22608 }
22609 outer_count++;
22610 inner.reset();
22611 }
22612
22613 if (outer_count != 3 || total_inner_count != 12) {
22614 std::cout << " [FAIL] : Iteration counts incorrect";
22615 throw std::runtime_error("np_test_nested_iters_basic failed");
22616 }
22617
22618 std::cout << " -> tests passed" << std::endl;
22619 }
valid (np_test_2_all.cpp:6221)
6211 data.setElementAt({ 1 }, 20.0);
6212 data.setElementAt({ 2 }, 30.0);
6213
6214 auto mask = createBoolArray({ 3 }, false);
6215 mask.setElementAt({ 1 }, true); // mask middle element
6216
6217 MaskedArray<double> ma(data, mask, -999.0);
6218
6219 // Test equality comparison - skip for now as it needs scalar comparison support
6220 // auto eq_result = ma == 20.0;
6221 // assert(eq_result.is_masked_at({0})); // 10 != 20, but result should be valid (False)
6222 // assert(eq_result.is_masked_at({1})); // masked element propagates mask
6223 // assert(eq_result.is_masked_at({2})); // 30 != 20, but result should be valid (False)
6224 // std::cout << "[OK] Equality comparison with mask propagation\n";
6225
6226 // Test greater than - skip for now as it needs scalar comparison support
6227 // auto gt_result = ma > 15.0;
6228 // assert(gt_result.is_masked_at({1})); // masked element propagates
6229 // Note: comparison results for valid elements would depend on implementation
6230 // std::cout << "[OK] Greater than comparison with mask propagation\n";