MemoryMapper#

class numpy::MemoryMapper#

numpy C++ class.

Example#

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

// Use MemoryMapper
MemoryMapper obj;
// ... operations ...

Type Checking#

Signature

Return Type

Location

Example

bool is_open()

bool

NP_MEMMAP.H:270

View

Other Methods#

Signature

Return Type

Location

Example

void close()

void

NP_MEMMAP.H:235

View

void \* data()

void *

NP_MEMMAP.H:268

View

void flush()

void

NP_MEMMAP.H:219

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

View

Code Examples#

The following examples are extracted from the test suite.

is_open (np_test_3_all.cpp:11184)
11174      mmap1.setElementAt({ 1, 1 }, 42);
11175      mmap1.flush();
11176
11177      // Move constructor
11178      auto mmap2 = std::move(mmap1);
11179      if (!(mmap2.getElementAt({ 1, 1 }) == 42)) {
11180          std::string description = std::string("np_test_io_memmap_move_semantics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mmap2.getElementAt({ 1, 1 }) == 42)";
11181          std::cout << std::string("[FAIL] ") + description << std::endl;
11182          throw std::runtime_error(description);
11183      }
11184      if (!(mmap2.is_open())) {
11185          std::string description = std::string("np_test_io_memmap_move_semantics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(mmap2.is_open())";
11186          std::cout << std::string("[FAIL] ") + description << std::endl;
11187          throw std::runtime_error(description);
11188      }
11189      // std::cout << "[OK] Move constructor works" << std::endl;
11190
11191      // Move assignment
11192      auto mmap3 = memmap::memmap<int32_t>(filename, { 3, 3 }, "r+");
11193      mmap3 = std::move(mmap2);
11194      if (!(mmap3.getElementAt({ 1, 1 }) == 42)) {
close (np_test_1_all.cpp:26157)
26147    void np_test_recarray_fromfile() {
26148      std::cout << "========= fromfile: create RecordArray from file =======================";
26149
26150      // Create temporary test file
26151      std::string filename = "D:/Projects/Cpp2/temp/test_recarray.csv";
26152      std::ofstream outfile(filename);
26153      outfile << "1,10.5\n";
26154      outfile << "2,20.5\n";
26155      outfile << "3,30.5\n";
26156      outfile.close();
26157
26158      // Define dtype
26159      std::vector<std::pair<std::string, numpy::DType>> fields = {
26160          {"id", numpy::DType::INT32},
26161          {"value", numpy::DType::FLOAT64}
26162      };
26163      auto dtype = std::make_shared<numpy::StructuredDType>(fields);
26164
26165      // Load from file
26166      auto result = numpy::fromfile(filename, dtype, ",");
data (np_test_1_all.cpp:2084)
2074    }
2075    if (!(derived_ptr->extra == 2)) {
2076        std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(derived_ptr->extra == 2)";
2077        std::cout << std::string("[FAIL] ") + description << std::endl;
2078        throw std::runtime_error(description);
2079    }
2080
2081    // Test 6: Large object handling
2082    struct LargeObject {
2083        std::vector<double> data;
2084        LargeObject() : data(10000, 3.14159) {}  // Large object
2085    };
2086
2087    LargeObject large;
2088    object_ large_obj(large);  // Should handle large objects
2089    if (!(!large_obj.is_null())) {
2090        std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!large_obj.is_null())";
2091        std::cout << std::string("[FAIL] ") + description << std::endl;
2092        throw std::runtime_error(description);
2093    }
2094    if (!(large_obj.is_type<LargeObject>())) {
flush (np_test_2_all.cpp:10646)
10636      std::vector<size_t> shape = { 10, 5 };
10637      auto mmap_array = numpy::memmap::memmap<float>(TEMP_DIR + "test_memmap_new.npy", shape, "w+");
10638
10639      // Write some data
10640      for (size_t i = 0; i < 10; ++i) {
10641        for (size_t j = 0; j < 5; ++j) {
10642          mmap_array.setElementAt({ i, j }, static_cast<float>(i * 5 + j + 1));
10643        }
10644      }
10645
10646      mmap_array.flush();
10647      // std::cout << "[OK] Created and wrote to memory-mapped file\n";
10648
10649      // Test reading from existing memory-mapped file
10650      auto mmap_readonly = numpy::memmap::memmap<float>(TEMP_DIR + "test_memmap_new.npy", shape, "r");
10651
10652      for (size_t i = 0; i < 10; ++i) {
10653        for (size_t j = 0; j < 5; ++j) {
10654          float expected = static_cast<float>(i * 5 + j + 1);
10655          if (!(approx_equal(mmap_readonly.getElementAt({ i, j }), expected, 1e-6f))) {
10656              std::string description = std::string("testMemoryMapping():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mmap_readonly.getElementAt({ i, j }), expected, 1e-6f))";
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);