MemoryMapper ============ .. cpp:class:: numpy::MemoryMapper numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use MemoryMapper MemoryMapper obj; // ... operations ... Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_open()`` - bool - NP_MEMMAP.H:270 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void close()`` - void - NP_MEMMAP.H:235 - :ref:`View ` * - ``void \* data()`` - void \* - NP_MEMMAP.H:268 - :ref:`View ` * - ``void flush()`` - void - NP_MEMMAP.H:219 - :ref:`View ` * - ``void \* map(const std::string &filename, size_tsize, size_toffset, MMapModemode)`` - void \* - NP_MEMMAP.H:203 - * - ``void \* map_posix(const std::string &filename, size_tsize, size_toffset, MMapModemode)`` - void \* - NP_MEMMAP.H:420 - * - ``void \* map_windows(const std::string &filename, size_tsize, size_toffset, MMapModemode)`` - void \* - NP_MEMMAP.H:274 - * - ``size_t size()`` - size_t - NP_MEMMAP.H:269 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-memorymapper-is_open-0: .. dropdown:: is_open (np_test_3_all.cpp:11184) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 11174 :emphasize-lines: 11 mmap1.setElementAt({ 1, 1 }, 42); mmap1.flush(); // Move constructor auto mmap2 = std::move(mmap1); if (!(mmap2.getElementAt({ 1, 1 }) == 42)) { std::string description = std::string("np_test_io_memmap_move_semantics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mmap2.getElementAt({ 1, 1 }) == 42)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(mmap2.is_open())) { std::string description = std::string("np_test_io_memmap_move_semantics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mmap2.is_open())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Move constructor works" << std::endl; // Move assignment auto mmap3 = memmap::memmap(filename, { 3, 3 }, "r+"); mmap3 = std::move(mmap2); if (!(mmap3.getElementAt({ 1, 1 }) == 42)) { .. _example-memorymapper-close-1: .. dropdown:: close (np_test_1_all.cpp:26157) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26147 :emphasize-lines: 11 void np_test_recarray_fromfile() { std::cout << "========= fromfile: create RecordArray from file ======================="; // Create temporary test file std::string filename = "D:/Projects/Cpp2/temp/test_recarray.csv"; std::ofstream outfile(filename); outfile << "1,10.5\n"; outfile << "2,20.5\n"; outfile << "3,30.5\n"; outfile.close(); // Define dtype std::vector> fields = { {"id", numpy::DType::INT32}, {"value", numpy::DType::FLOAT64} }; auto dtype = std::make_shared(fields); // Load from file auto result = numpy::fromfile(filename, dtype, ","); .. _example-memorymapper-data-2: .. dropdown:: data (np_test_1_all.cpp:2084) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2074 :emphasize-lines: 11 } if (!(derived_ptr->extra == 2)) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(derived_ptr->extra == 2)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 6: Large object handling struct LargeObject { std::vector data; LargeObject() : data(10000, 3.14159) {} // Large object }; LargeObject large; object_ large_obj(large); // Should handle large objects if (!(!large_obj.is_null())) { std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!large_obj.is_null())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(large_obj.is_type())) { .. _example-memorymapper-flush-3: .. dropdown:: flush (np_test_2_all.cpp:10646) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10636 :emphasize-lines: 11 std::vector shape = { 10, 5 }; auto mmap_array = numpy::memmap::memmap(TEMP_DIR + "test_memmap_new.npy", shape, "w+"); // Write some data for (size_t i = 0; i < 10; ++i) { for (size_t j = 0; j < 5; ++j) { mmap_array.setElementAt({ i, j }, static_cast(i * 5 + j + 1)); } } mmap_array.flush(); // std::cout << "[OK] Created and wrote to memory-mapped file\n"; // Test reading from existing memory-mapped file auto mmap_readonly = numpy::memmap::memmap(TEMP_DIR + "test_memmap_new.npy", shape, "r"); for (size_t i = 0; i < 10; ++i) { for (size_t j = 0; j < 5; ++j) { float expected = static_cast(i * 5 + j + 1); if (!(approx_equal(mmap_readonly.getElementAt({ i, j }), expected, 1e-6f))) { std::string description = std::string("testMemoryMapping():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mmap_readonly.getElementAt({ i, j }), expected, 1e-6f))"; .. _example-memorymapper-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);