IndexBase ========= .. cpp:class:: numpy::IndexBase numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use IndexBase IndexBase obj; // ... operations ... Array Creation -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual bool empty() const = 0`` - virtual bool - df_index_base.h:44 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual int64_t get_loc_str(const std::string& key_str) const = 0`` - virtual int64_t - df_index_base.h:88 - * - ``virtual std::string get_value_str(size_t index) const = 0`` - virtual std::string - df_index_base.h:105 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual std::string to_string() const = 0`` - virtual std::string - df_index_base.h:120 - :ref:`View ` * - ``virtual std::vector to_string_vector() const = 0`` - virtual std::vector - df_index_base.h:98 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``virtual bool is_unique() const = 0`` - virtual bool - df_index_base.h:134 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void clear_cache() const override = 0`` - void - df_index_base.h:151 - * - ``virtual std::unique_ptr clone() const = 0`` - virtual std::unique_ptr - df_index_base.h:111 - :ref:`View ` * - ``virtual bool contains_str(const std::string& key_str) const = 0`` - virtual bool - df_index_base.h:81 - * - ``virtual std::string dtype_name() const = 0`` - virtual std::string - df_index_base.h:55 - :ref:`View ` * - ``bool has_cached_values() const override = 0`` - bool - df_index_base.h:156 - * - ``virtual bool has_duplicates() const = 0`` - virtual bool - df_index_base.h:139 - * - ``virtual std::string inferred_type() const = 0`` - virtual std::string - df_index_base.h:70 - * - ``virtual std::optional name() const = 0`` - virtual std::optional - df_index_base.h:60 - :ref:`View ` * - ``virtual size_t nbytes() const = 0`` - virtual size_t - df_index_base.h:50 - :ref:`View ` * - ``virtual std::string repr() const = 0`` - virtual std::string - df_index_base.h:125 - * - ``virtual void set_name(const std::optional& name) = 0`` - virtual void - df_index_base.h:65 - * - ``virtual size_t size() const = 0`` - virtual size_t - df_index_base.h:39 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-indexbase-empty-0: .. dropdown:: empty (np_test_1_all.cpp:6316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6306 :emphasize-lines: 11 } void test_data_generator_emptyBenchmarkSorting() { std::cout << "========= test_data_generator_empty ======================="; DataGenerator gen(42); // Test empty data generation std::vector data = gen.generate(0, DataPattern::RANDOM); if (!(data.empty())) { std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Empty data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-indexbase-to_string-1: .. dropdown:: to_string (np_test_1_all.cpp:454) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 444 :emphasize-lines: 11 // Modify through different views view1.setElementAt({0, 0}, 100); view2.setElementAt({2, 1}, 200); // This is (1, 2) in original view3.setElementAt({0, 0}, 300); // This is (1, 1) in original // std::cout << "After modifications through multiple views:" << std::endl; //original.printArray(); // Verify all changes are reflected if (!(original.getElementAt({0, 0}) == 100)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 2}) == 200)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(original.getElementAt({1, 1}) == 300)) { std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)"; .. _example-indexbase-is_unique-2: .. dropdown:: is_unique (np_test_1_all.cpp:19676) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 19666 :emphasize-lines: 11 unique_array.setElementAt({ 1 }, 2); unique_array.setElementAt({ 2 }, 3); unique_array.setElementAt({ 3 }, 4); auto duplicate_array = createInt32Array({ 4 }, 0); duplicate_array.setElementAt({ 0 }, 1); duplicate_array.setElementAt({ 1 }, 2); duplicate_array.setElementAt({ 2 }, 2); // Duplicate duplicate_array.setElementAt({ 3 }, 4); if (!(set_ops::is_unique(unique_array) == true)) { std::string description = std::string("testUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(set_ops::is_unique(unique_array) == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(set_ops::is_unique(duplicate_array) == false)) { std::string description = std::string("testUtilityFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(set_ops::is_unique(duplicate_array) == false)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] is_unique function works correctly\n"; .. _example-indexbase-clone-3: .. dropdown:: clone (np_test_1_all.cpp:24942) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24932 :emphasize-lines: 11 } // Test reproducibility numpy::random::PCG64DXSM rng2(11111); if (rng2.next_uint64() != val) { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : not reproducible"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } // Test clone auto cloned = rng.clone(); if (cloned->name() != "PCG64DXSM") { std::cout << " [FAIL] : in np_test_bitgen_pcg64dxsm() : clone name incorrect"; throw std::runtime_error("np_test_bitgen_pcg64dxsm failed"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PHILOX TESTS .. _example-indexbase-dtype_name-4: .. dropdown:: dtype_name (np_test_2_all.cpp:3567) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3557 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(flags_info.writeable == true)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.writeable == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test dtype functions if (!(dtype_name(arr) == "float64")) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_name(arr) == \"float64\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(dtype_itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(dtype_itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } .. _example-indexbase-name-5: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-indexbase-nbytes-6: .. dropdown:: nbytes (np_test_2_all.cpp:3547) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3537 :emphasize-lines: 11 } // Test itemsize if (!(itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test nbytes if (!(nbytes(arr) == 24 * sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test flags auto flags_info = flags(arr); if (!(flags_info.owndata == true)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.owndata == true)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-indexbase-size-7: .. dropdown:: size (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);