SimpleCache =========== .. cpp:class:: numpy::SimpleCache numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use SimpleCache SimpleCache obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``SimpleCache(size_tmax_size = 1000)`` - NP_VECTORIZE_UTILS.H:202 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``const Value & get(const Key &key)`` - const Value & - NP_VECTORIZE_UTILS.H:208 - :ref:`View ` * - ``void put(const Key &key, const Value &value)`` - void - NP_VECTORIZE_UTILS.H:216 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void clear()`` - void - NP_VECTORIZE_UTILS.H:224 - :ref:`View ` * - ``bool contains(const Key &key)`` - bool - NP_VECTORIZE_UTILS.H:204 - :ref:`View ` * - ``size_t size()`` - size_t - NP_VECTORIZE_UTILS.H:228 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-simplecache-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-simplecache-put-1: .. dropdown:: put (np_test_5_all.cpp:4389) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4379 :emphasize-lines: 11 numpy::NDArray indices({ 3 }); indices.setElementAt({ 0 }, 5); // Last position indices.setElementAt({ 1 }, 2); // Middle position indices.setElementAt({ 2 }, 0); // First position numpy::NDArray values({ 3 }); values.setElementAt({ 0 }, 100); values.setElementAt({ 1 }, 200); values.setElementAt({ 2 }, 300); numpy::put(arr, indices, values); // Check modified values if (arr.getElementAt({ 0 }) != 300 || arr.getElementAt({ 2 }) != 200 || arr.getElementAt({ 5 }) != 100) { std::cout << " [FAIL] : in np_test_put_basic() : Values not correctly placed"; throw std::runtime_error("Test failed"); } // Check unmodified values if (arr.getElementAt({ 1 }) != 10 || arr.getElementAt({ 3 }) != 30 || arr.getElementAt({ 4 }) != 40) { std::cout << " [FAIL] : in np_test_put_basic() : Unmodified values changed"; .. _example-simplecache-clear-2: .. dropdown:: clear (np_test_2_all.cpp:4161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4151 :emphasize-lines: 11 auto array = createFloat32Array({ 10, 10 }, static_cast(i)); temp_arrays.push_back(array); auto view = array.view(); temp_views.push_back(view); } // std::cout << "Created 100 arrays and 100 views" << std::endl; // Clear all arrays (views will keep memory alive) temp_arrays.clear(); // std::cout << "Cleared original arrays" << std::endl; // Verify views still work if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) { std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Views still valid after originals cleared"; .. _example-simplecache-contains-3: .. dropdown:: contains (np_test_2_all.cpp:10521) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10511 :emphasize-lines: 11 auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz"); // Verify keys auto keys = npz_file.keys(); if (!(keys.size() == 3)) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("integers"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("floats"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("complex_nums"))) { .. _example-simplecache-size-4: .. 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);