SimpleCache#

class numpy::SimpleCache#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use SimpleCache
SimpleCache obj;
// ... operations ...

Constructors#

Signature

Location

Example

SimpleCache(size_tmax_size = 1000)

NP_VECTORIZE_UTILS.H:202

Indexing / Selection#

Signature

Return Type

Location

Example

const Value & get(const Key &key)

const Value &

NP_VECTORIZE_UTILS.H:208

View

void put(const Key &key, const Value &value)

void

NP_VECTORIZE_UTILS.H:216

View

Other Methods#

Signature

Return Type

Location

Example

void clear()

void

NP_VECTORIZE_UTILS.H:224

View

bool contains(const Key &key)

bool

NP_VECTORIZE_UTILS.H:204

View

size_t size()

size_t

NP_VECTORIZE_UTILS.H:228

View

Code Examples#

The following examples are extracted from the test suite.

get (np_test_1_all.cpp:28526)
28516      std::cout << " -> tests passed" << std::endl;
28517    }
28518
28519    void np_test_indexing_mask_indices() {
28520      std::cout << "========= mask_indices: triangular mask indices =======================";
28521
28522      // Get upper triangular indices for 3x3 matrix
28523      auto triu_idx = numpy::mask_indices(3, "triu", 0);
28524
28525      // Should return tuple of 2 arrays
28526      bool passed = (std::get<0>(triu_idx).getSize() > 0);
28527      passed = passed && (std::get<1>(triu_idx).getSize() > 0);
28528      passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize());
28529
28530      // Get lower triangular indices
28531      auto tril_idx = numpy::mask_indices(3, "tril", 0);
28532      passed = passed && (std::get<0>(tril_idx).getSize() > 0);
28533      passed = passed && (std::get<1>(tril_idx).getSize() > 0);
28534
28535      if (!passed) {
28536        std::cout << "  [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect";
put (np_test_5_all.cpp:4389)
4379      numpy::NDArray<size_t> indices({ 3 });
4380      indices.setElementAt({ 0 }, 5);  // Last position
4381      indices.setElementAt({ 1 }, 2);  // Middle position
4382      indices.setElementAt({ 2 }, 0);  // First position
4383
4384      numpy::NDArray<int32_t> values({ 3 });
4385      values.setElementAt({ 0 }, 100);
4386      values.setElementAt({ 1 }, 200);
4387      values.setElementAt({ 2 }, 300);
4388
4389      numpy::put(arr, indices, values);
4390
4391      // Check modified values
4392      if (arr.getElementAt({ 0 }) != 300 || arr.getElementAt({ 2 }) != 200 || arr.getElementAt({ 5 }) != 100) {
4393        std::cout << "  [FAIL] : in np_test_put_basic() : Values not correctly placed";
4394        throw std::runtime_error("Test failed");
4395      }
4396
4397      // Check unmodified values
4398      if (arr.getElementAt({ 1 }) != 10 || arr.getElementAt({ 3 }) != 30 || arr.getElementAt({ 4 }) != 40) {
4399        std::cout << "  [FAIL] : in np_test_put_basic() : Unmodified values changed";
clear (np_test_2_all.cpp:4161)
4151        auto array = createFloat32Array({ 10, 10 }, static_cast<float>(i));
4152        temp_arrays.push_back(array);
4153
4154        auto view = array.view();
4155        temp_views.push_back(view);
4156      }
4157
4158      // std::cout << "Created 100 arrays and 100 views" << std::endl;
4159
4160      // Clear all arrays (views will keep memory alive)
4161      temp_arrays.clear();
4162      // std::cout << "Cleared original arrays" << std::endl;
4163
4164      // Verify views still work
4165      if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) {
4166          std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)";
4167          std::cout << std::string("[FAIL] ") + description << std::endl;
4168          throw std::runtime_error(description);
4169      }
4170      // std::cout << "[OK] Views still valid after originals cleared";
contains (np_test_2_all.cpp:10521)
10511      auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz");
10512
10513      // Verify keys
10514      auto keys = npz_file.keys();
10515      if (!(keys.size() == 3)) {
10516          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)";
10517          std::cout << std::string("[FAIL] ") + description << std::endl;
10518          throw std::runtime_error(description);
10519      }
10520      if (!(npz_file.contains("integers"))) {
10521          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))";
10522          std::cout << std::string("[FAIL] ") + description << std::endl;
10523          throw std::runtime_error(description);
10524      }
10525      if (!(npz_file.contains("floats"))) {
10526          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))";
10527          std::cout << std::string("[FAIL] ") + description << std::endl;
10528          throw std::runtime_error(description);
10529      }
10530      if (!(npz_file.contains("complex_nums"))) {
size (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);