-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsynumpy.cpp
More file actions
781 lines (664 loc) · 26.7 KB
/
synumpy.cpp
File metadata and controls
781 lines (664 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/* Symisc syNumpy: A C++17 NumPy .npy Reader and Writer Standalone Library.
* Copyright (C) 2026, Symisc Systems https://pixlab.io/numpy-cpp-library
* Version 1.9.7
* For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* licensing@symisc.net
* contact@symisc.net
* or visit:
* https://pixlab.io/numpy-cpp-library
*/
/*
* Copyright (C) 2026 Symisc Systems, S.U.A.R.L [M.I.A.G Mrad Chams Eddine <chm@symisc.net>].
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY SYMISC SYSTEMS ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL SYMISC SYSTEMS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* $SymiscID: synumpy.cpp v1.9.7 Win11 2026-04-01 stable <contact@symisc.net> $ */
#include "synumpy.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <cctype>
#include <fstream>
#include <limits>
#include <sstream>
#include <system_error>
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#endif
namespace syNumpy {
namespace {
constexpr int kImplementationVersionMajor = 1;
constexpr int kImplementationVersionMinor = 9;
constexpr int kImplementationVersionPatch = 7;
static_assert(
syNumpy::kVersionMajor == kImplementationVersionMajor &&
syNumpy::kVersionMinor == kImplementationVersionMinor &&
syNumpy::kVersionPatch == kImplementationVersionPatch,
"synumpy version mismatch between header and source");
constexpr std::array<unsigned char, 6> kMagic{{0x93, 'N', 'U', 'M', 'P', 'Y'}};
constexpr std::size_t kAlignment = 64;
struct ParsedHeader {
std::string dtype_descr;
bool fortran_order = false;
std::vector<std::size_t> shape;
std::size_t word_size = 0;
std::size_t num_values = 0;
std::size_t num_bytes = 0;
std::size_t data_offset = 0;
std::uint8_t major_version = 0;
std::uint8_t minor_version = 0;
};
[[noreturn]] void fail(const std::string& message) {
throw Error(message);
}
std::string trim(std::string_view value) {
std::size_t start = 0;
while (start < value.size() && std::isspace(static_cast<unsigned char>(value[start])) != 0) {
++start;
}
std::size_t end = value.size();
while (end > start && std::isspace(static_cast<unsigned char>(value[end - 1])) != 0) {
--end;
}
return std::string(value.substr(start, end - start));
}
std::size_t checkedMultiply(std::size_t lhs, std::size_t rhs, const char* what) {
if (lhs == 0 || rhs == 0) {
return 0;
}
if (lhs > (std::numeric_limits<std::size_t>::max() / rhs)) {
fail(std::string("overflow while computing ") + what);
}
return lhs * rhs;
}
std::size_t checkedAdd(std::size_t lhs, std::size_t rhs, const char* what) {
if (lhs > (std::numeric_limits<std::size_t>::max() - rhs)) {
fail(std::string("overflow while computing ") + what);
}
return lhs + rhs;
}
std::size_t elementCount(const std::vector<std::size_t>& shape) {
std::size_t count = 1;
for (std::size_t dim : shape) {
count = checkedMultiply(count, dim, "element count");
}
return count;
}
std::size_t parseUnsigned(std::string_view text, const char* field_name) {
const std::string token = trim(text);
if (token.empty()) {
fail(std::string("missing unsigned integer for ") + field_name);
}
std::size_t value = 0;
for (char ch : token) {
if (ch < '0' || ch > '9') {
fail(std::string("invalid unsigned integer for ") + field_name + ": '" + token + "'");
}
const std::size_t digit = static_cast<std::size_t>(ch - '0');
if (value > ((std::numeric_limits<std::size_t>::max() - digit) / 10u)) {
fail(std::string("overflow while parsing ") + field_name);
}
value = (value * 10u) + digit;
}
return value;
}
std::size_t parseItemSize(std::string_view dtype_descr) {
if (dtype_descr.size() < 3) {
fail("dtype descriptor is too short: '" + std::string(dtype_descr) + "'");
}
return parseUnsigned(dtype_descr.substr(2), "dtype item size");
}
std::string normalizeDescr(std::string_view dtype_descr) {
if (dtype_descr.size() < 3) {
fail("unsupported dtype descriptor: '" + std::string(dtype_descr) + "'");
}
char endian = dtype_descr[0];
const char kind = dtype_descr[1];
const std::size_t item_size = parseItemSize(dtype_descr);
switch (kind) {
case 'b':
case 'i':
case 'u':
case 'f':
case 'c':
break;
default:
fail("unsupported dtype kind: '" + std::string(1, kind) + "'");
}
if (kind == 'b') {
if (item_size != 1) {
fail("unsupported bool dtype size: '" + std::string(dtype_descr) + "'");
}
return "|b1";
}
if (item_size == 1) {
if (kind == 'f' || kind == 'c') {
fail("unsupported single-byte floating or complex dtype: '" + std::string(dtype_descr) + "'");
}
return std::string("|") + kind + "1";
}
if (endian == '=') {
// NumPy uses '=' for native-endian payloads. Normalize it to an
// explicit host-endian descriptor so comparisons stay deterministic.
endian = detail::hostEndianChar();
}
if (endian != '<' && endian != '>') {
fail("unsupported dtype endianness: '" + std::string(1, endian) + "'");
}
if (endian != detail::hostEndianChar()) {
fail("dtype endianness does not match host endianness: '" + std::string(dtype_descr) + "'");
}
return std::string(1, endian) + kind + std::to_string(item_size);
}
std::size_t headerLengthFieldSize(std::uint8_t major_version) {
switch (major_version) {
case 1:
return 2;
case 2:
case 3:
return 4;
default:
fail("unsupported .npy format version: " + std::to_string(major_version));
}
}
std::size_t readLittleEndianUnsigned(const std::uint8_t* bytes, std::size_t width) {
std::size_t value = 0;
for (std::size_t i = 0; i < width; ++i) {
value |= (static_cast<std::size_t>(bytes[i]) << (8u * i));
}
return value;
}
std::string parseQuotedField(std::string_view header, std::string_view key) {
const std::string quoted_key = "'" + std::string(key) + "'";
const std::size_t key_pos = header.find(quoted_key);
if (key_pos == std::string_view::npos) {
fail("missing header field '" + std::string(key) + "'");
}
const std::size_t colon_pos = header.find(':', key_pos + quoted_key.size());
if (colon_pos == std::string_view::npos) {
fail("invalid header field '" + std::string(key) + "'");
}
std::size_t value_pos = colon_pos + 1;
while (value_pos < header.size() &&
std::isspace(static_cast<unsigned char>(header[value_pos])) != 0) {
++value_pos;
}
if (value_pos >= header.size()) {
fail("missing value for header field '" + std::string(key) + "'");
}
const char quote = header[value_pos];
if (quote != '\'' && quote != '"') {
fail("header field '" + std::string(key) + "' is not a plain string");
}
const std::size_t value_end = header.find(quote, value_pos + 1);
if (value_end == std::string_view::npos) {
fail("unterminated string value for header field '" + std::string(key) + "'");
}
return std::string(header.substr(value_pos + 1, value_end - value_pos - 1));
}
bool parseBoolField(std::string_view header, std::string_view key) {
const std::string quoted_key = "'" + std::string(key) + "'";
const std::size_t key_pos = header.find(quoted_key);
if (key_pos == std::string_view::npos) {
fail("missing header field '" + std::string(key) + "'");
}
const std::size_t colon_pos = header.find(':', key_pos + quoted_key.size());
if (colon_pos == std::string_view::npos) {
fail("invalid header field '" + std::string(key) + "'");
}
std::size_t value_pos = colon_pos + 1;
while (value_pos < header.size() &&
std::isspace(static_cast<unsigned char>(header[value_pos])) != 0) {
++value_pos;
}
if (header.compare(value_pos, 4, "True") == 0) {
return true;
}
if (header.compare(value_pos, 5, "False") == 0) {
return false;
}
fail("invalid boolean value for header field '" + std::string(key) + "'");
}
std::vector<std::size_t> parseShapeField(std::string_view header) {
const std::string quoted_key = "'shape'";
const std::size_t key_pos = header.find(quoted_key);
if (key_pos == std::string_view::npos) {
fail("missing header field 'shape'");
}
const std::size_t colon_pos = header.find(':', key_pos + quoted_key.size());
if (colon_pos == std::string_view::npos) {
fail("invalid header field 'shape'");
}
std::size_t tuple_start = colon_pos + 1;
while (tuple_start < header.size() &&
std::isspace(static_cast<unsigned char>(header[tuple_start])) != 0) {
++tuple_start;
}
if (tuple_start >= header.size() || header[tuple_start] != '(') {
fail("shape field is not a tuple");
}
const std::size_t tuple_end = header.find(')', tuple_start + 1);
if (tuple_end == std::string_view::npos) {
fail("unterminated shape tuple");
}
const std::string inner = trim(header.substr(tuple_start + 1, tuple_end - tuple_start - 1));
if (inner.empty()) {
return {};
}
std::vector<std::size_t> shape;
std::size_t token_start = 0;
while (token_start < inner.size()) {
const std::size_t comma = inner.find(',', token_start);
const std::size_t token_end = (comma == std::string::npos) ? inner.size() : comma;
const std::string token = trim(std::string_view(inner).substr(token_start, token_end - token_start));
if (!token.empty()) {
shape.push_back(parseUnsigned(token, "shape"));
}
if (comma == std::string::npos) {
break;
}
token_start = comma + 1;
}
return shape;
}
ParsedHeader parseHeaderPrefix(const std::uint8_t* bytes, std::size_t size) {
if (size < 10) {
fail("buffer is too small to contain a valid .npy header");
}
if (!std::equal(kMagic.begin(), kMagic.end(), bytes)) {
fail("buffer does not start with a valid .npy magic header");
}
ParsedHeader result;
result.major_version = bytes[6];
result.minor_version = bytes[7];
const std::size_t length_field_size = headerLengthFieldSize(result.major_version);
const std::size_t prefix_size = 8 + length_field_size;
if (size < prefix_size) {
fail("buffer ended before the full .npy prefix could be read");
}
const std::size_t header_length = readLittleEndianUnsigned(bytes + 8, length_field_size);
result.data_offset = prefix_size + header_length;
if (size < result.data_offset) {
fail("buffer ended before the declared .npy header finished");
}
const std::string_view header(
reinterpret_cast<const char*>(bytes + prefix_size),
header_length);
// Only plain scalar descriptors are accepted here. Structured, object,
// and unicode dtypes intentionally stay out of scope for this library.
result.dtype_descr = normalizeDescr(parseQuotedField(header, "descr"));
result.fortran_order = parseBoolField(header, "fortran_order");
result.shape = parseShapeField(header);
result.word_size = parseItemSize(result.dtype_descr);
result.num_values = elementCount(result.shape);
result.num_bytes = checkedMultiply(result.num_values, result.word_size, "array byte size");
return result;
}
std::string shapeToString(const std::vector<std::size_t>& shape) {
std::ostringstream stream;
stream << "(";
for (std::size_t i = 0; i < shape.size(); ++i) {
if (i != 0) {
stream << ", ";
}
stream << shape[i];
}
if (shape.size() == 1) {
stream << ",";
}
stream << ")";
return stream.str();
}
std::vector<std::uint8_t> buildHeader(std::string_view dtype_descr, const std::vector<std::size_t>& shape) {
const std::string dict =
"{'descr': '" + std::string(dtype_descr) + "', 'fortran_order': False, 'shape': " +
shapeToString(shape) + ", }";
std::uint8_t major_version = 1;
std::uint8_t minor_version = 0;
std::size_t length_field_size = 2;
std::string header_text;
for (;;) {
const std::size_t prefix_size = 8 + length_field_size;
// NumPy expects the full preamble+header block to align cleanly.
// Pad with spaces and end with '\n' to produce a valid header record.
const std::size_t total_without_padding = prefix_size + dict.size() + 1;
const std::size_t padding = (kAlignment - (total_without_padding % kAlignment)) % kAlignment;
header_text = dict;
header_text.append(padding, ' ');
header_text.push_back('\n');
if (major_version == 1 && header_text.size() > std::numeric_limits<std::uint16_t>::max()) {
// Version 1 stores the header length in 16 bits. Promote to
// version 2 when the serialized dictionary no longer fits.
major_version = 2;
length_field_size = 4;
continue;
}
break;
}
std::vector<std::uint8_t> out;
out.reserve(8 + length_field_size + header_text.size());
out.insert(out.end(), kMagic.begin(), kMagic.end());
out.push_back(major_version);
out.push_back(minor_version);
if (length_field_size == 2) {
const std::uint16_t header_len = static_cast<std::uint16_t>(header_text.size());
out.push_back(static_cast<std::uint8_t>(header_len & 0xffu));
out.push_back(static_cast<std::uint8_t>((header_len >> 8u) & 0xffu));
} else {
const std::uint32_t header_len = static_cast<std::uint32_t>(header_text.size());
out.push_back(static_cast<std::uint8_t>(header_len & 0xffu));
out.push_back(static_cast<std::uint8_t>((header_len >> 8u) & 0xffu));
out.push_back(static_cast<std::uint8_t>((header_len >> 16u) & 0xffu));
out.push_back(static_cast<std::uint8_t>((header_len >> 24u) & 0xffu));
}
out.insert(out.end(), header_text.begin(), header_text.end());
return out;
}
std::string normalizeMode(std::string_view mode) {
if (mode.empty()) {
return "w";
}
if (mode == "w" || mode == "wb") {
return "w";
}
if (mode == "a" || mode == "ab") {
return "a";
}
fail("unsupported save mode: '" + std::string(mode) + "'");
}
bool fileExists(const std::filesystem::path& path) {
std::error_code ec;
return std::filesystem::exists(path, ec);
}
std::vector<std::uint8_t> readExact(std::istream& stream, std::size_t count, const char* context) {
std::vector<std::uint8_t> buffer(count);
if (count == 0) {
return buffer;
}
stream.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(count));
if (stream.gcount() != static_cast<std::streamsize>(count)) {
fail(std::string("failed to read ") + context);
}
return buffer;
}
ParsedHeader readHeaderFromFile(std::ifstream& stream) {
// Read the fixed 8-byte prologue first, then read the version-dependent
// header-length field and payload header bytes.
const std::vector<std::uint8_t> prologue = readExact(stream, 8, "file header prologue");
if (!std::equal(kMagic.begin(), kMagic.end(), prologue.begin())) {
fail("file does not start with a valid .npy magic header");
}
const std::uint8_t major_version = prologue[6];
const std::size_t length_field_size = headerLengthFieldSize(major_version);
const std::vector<std::uint8_t> length_bytes = readExact(stream, length_field_size, "header length");
const std::size_t prefix_size = 8 + length_field_size;
const std::size_t header_length = readLittleEndianUnsigned(length_bytes.data(), length_field_size);
std::vector<std::uint8_t> header_bytes(prefix_size + header_length);
std::copy(prologue.begin(), prologue.end(), header_bytes.begin());
std::copy(length_bytes.begin(), length_bytes.end(), header_bytes.begin() + 8);
if (header_length != 0) {
stream.read(
reinterpret_cast<char*>(header_bytes.data() + prefix_size),
static_cast<std::streamsize>(header_length));
if (stream.gcount() != static_cast<std::streamsize>(header_length)) {
fail("failed to read full .npy header");
}
}
return parseHeaderPrefix(header_bytes.data(), header_bytes.size());
}
void writeBytes(std::ostream& stream, const void* data, std::size_t size, const char* context) {
if (size == 0) {
return;
}
stream.write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size));
if (!stream) {
fail(std::string("failed to write ") + context);
}
}
void copyBytes(std::istream& input, std::ostream& output, std::size_t count) {
std::array<char, 1 << 20> buffer{};
std::size_t remaining = count;
while (remaining != 0) {
const std::size_t chunk = std::min<std::size_t>(remaining, buffer.size());
input.read(buffer.data(), static_cast<std::streamsize>(chunk));
if (input.gcount() != static_cast<std::streamsize>(chunk)) {
fail("failed while copying existing array data during append");
}
output.write(buffer.data(), static_cast<std::streamsize>(chunk));
if (!output) {
fail("failed while writing appended array data");
}
remaining -= chunk;
}
}
std::filesystem::path makeTempPath(const std::filesystem::path& path) {
const auto ticks = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::filesystem::path temp = path;
temp += ".";
temp += std::to_string(ticks);
temp += ".tmp";
return temp;
}
void replaceFile(const std::filesystem::path& source, const std::filesystem::path& target) {
#ifdef _WIN32
const std::wstring from = source.wstring();
const std::wstring to = target.wstring();
if (::MoveFileExW(
from.c_str(),
to.c_str(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) == 0) {
const DWORD code = ::GetLastError();
fail("failed to replace file '" + target.string() + "' (Win32 error " +
std::to_string(static_cast<unsigned long>(code)) + ")");
}
#else
std::error_code ec;
std::filesystem::rename(source, target, ec);
if (ec) {
fail("failed to replace file '" + target.string() + "': " + ec.message());
}
#endif
}
void writeFreshFile(
const std::filesystem::path& path,
const std::vector<std::uint8_t>& header,
const void* data,
std::size_t byte_count) {
std::ofstream output(path, std::ios::binary | std::ios::trunc);
if (!output) {
fail("unable to open '" + path.string() + "' for writing");
}
writeBytes(output, header.data(), header.size(), "NPY header");
writeBytes(output, data, byte_count, "NPY payload");
}
void rewriteForAppend(
const std::filesystem::path& path,
const ParsedHeader& existing,
const std::vector<std::uint8_t>& new_header,
const void* append_data,
std::size_t append_bytes) {
// If the updated shape makes the serialized header longer, the payload
// offset changes and the file must be rewritten instead of patched in place.
const std::filesystem::path temp_path = makeTempPath(path);
std::ifstream input(path, std::ios::binary);
if (!input) {
fail("unable to reopen '" + path.string() + "' for append rewrite");
}
std::ofstream output(temp_path, std::ios::binary | std::ios::trunc);
if (!output) {
fail("unable to create temporary file '" + temp_path.string() + "'");
}
writeBytes(output, new_header.data(), new_header.size(), "replacement NPY header");
input.seekg(static_cast<std::streamoff>(existing.data_offset), std::ios::beg);
if (!input) {
fail("failed to seek existing payload in '" + path.string() + "'");
}
copyBytes(input, output, existing.num_bytes);
writeBytes(output, append_data, append_bytes, "appended NPY payload");
output.close();
input.close();
replaceFile(temp_path, path);
}
void validateSaveArguments(
const void* data,
std::size_t byte_count,
const std::vector<std::size_t>& shape,
std::string_view dtype_descr) {
const std::string normalized_descr = normalizeDescr(dtype_descr);
const std::size_t item_size = parseItemSize(normalized_descr);
const std::size_t values = elementCount(shape);
const std::size_t expected_bytes = checkedMultiply(values, item_size, "save byte count");
if (expected_bytes != byte_count) {
fail("saveNpyRaw byte count does not match shape and dtype");
}
if (byte_count != 0 && data == nullptr) {
fail("saveNpyRaw received a null data pointer for non-empty input");
}
}
} // namespace
Error::Error(const std::string& message)
: std::runtime_error(message) {}
NpyArray::NpyArray(std::vector<std::size_t> shape, std::string dtype_descr, bool fortran_order)
: shape_(std::move(shape)),
dtype_descr_(normalizeDescr(dtype_descr)),
word_size_(parseItemSize(dtype_descr_)),
fortran_order_(fortran_order),
num_values_(elementCount(shape_)),
num_bytes_(checkedMultiply(num_values_, word_size_, "array byte size")) {
if (num_bytes_ != 0) {
data_ = std::shared_ptr<std::uint8_t>(new std::uint8_t[num_bytes_], std::default_delete<std::uint8_t[]>());
}
}
const std::vector<std::size_t>& NpyArray::shape() const noexcept {
return shape_;
}
std::string_view NpyArray::dtypeDescr() const noexcept {
return dtype_descr_;
}
std::size_t NpyArray::wordSize() const noexcept {
return word_size_;
}
bool NpyArray::fortranOrder() const noexcept {
return fortran_order_;
}
std::size_t NpyArray::numValues() const noexcept {
return num_values_;
}
std::size_t NpyArray::numBytes() const noexcept {
return num_bytes_;
}
const std::uint8_t* NpyArray::bytes() const noexcept {
return data_.get();
}
std::uint8_t* NpyArray::bytes() noexcept {
return data_.get();
}
NpyArray loadNpyBuffer(const void* buffer, std::size_t size) {
if (buffer == nullptr) {
fail("loadNpyBuffer received a null buffer");
}
const auto* bytes = static_cast<const std::uint8_t*>(buffer);
const ParsedHeader parsed = parseHeaderPrefix(bytes, size);
if ((size - parsed.data_offset) < parsed.num_bytes) {
fail("buffer ended before the full array payload was available");
}
NpyArray array(parsed.shape, parsed.dtype_descr, parsed.fortran_order);
if (parsed.num_bytes != 0) {
std::memcpy(array.bytes(), bytes + parsed.data_offset, parsed.num_bytes);
}
return array;
}
NpyArray loadNpy(const std::filesystem::path& path) {
std::ifstream input(path, std::ios::binary);
if (!input) {
fail("unable to open '" + path.string() + "' for reading");
}
const ParsedHeader parsed = readHeaderFromFile(input);
NpyArray array(parsed.shape, parsed.dtype_descr, parsed.fortran_order);
if (parsed.num_bytes != 0) {
input.read(reinterpret_cast<char*>(array.bytes()), static_cast<std::streamsize>(parsed.num_bytes));
if (input.gcount() != static_cast<std::streamsize>(parsed.num_bytes)) {
fail("failed to read full payload from '" + path.string() + "'");
}
}
return array;
}
void saveNpyRaw(
const std::filesystem::path& path,
const void* data,
std::size_t byte_count,
const std::vector<std::size_t>& shape,
std::string_view dtype_descr,
std::string_view mode) {
validateSaveArguments(data, byte_count, shape, dtype_descr);
const std::string normalized_mode = normalizeMode(mode);
const std::string normalized_descr = normalizeDescr(dtype_descr);
if (normalized_mode == "w" || !fileExists(path)) {
const std::vector<std::uint8_t> header = buildHeader(normalized_descr, shape);
writeFreshFile(path, header, data, byte_count);
return;
}
std::ifstream input(path, std::ios::binary);
if (!input) {
fail("unable to open '" + path.string() + "' for append");
}
const ParsedHeader existing = readHeaderFromFile(input);
if (existing.fortran_order) {
fail("append mode does not support Fortran-order arrays");
}
if (shape.empty() || existing.shape.empty()) {
fail("append mode requires arrays with at least one dimension");
}
if (existing.dtype_descr != normalized_descr) {
fail("append mode requires the same dtype as the existing file");
}
if (existing.shape.size() != shape.size()) {
fail("append mode requires the same rank as the existing file");
}
for (std::size_t i = 1; i < shape.size(); ++i) {
if (existing.shape[i] != shape[i]) {
fail("append mode requires all dimensions except axis 0 to match");
}
}
std::vector<std::size_t> merged_shape = existing.shape;
merged_shape[0] = checkedAdd(merged_shape[0], shape[0], "append axis size");
const std::vector<std::uint8_t> new_header = buildHeader(normalized_descr, merged_shape);
if (new_header.size() == existing.data_offset) {
std::fstream io(path, std::ios::binary | std::ios::in | std::ios::out);
if (!io) {
fail("unable to reopen '" + path.string() + "' for in-place append");
}
io.seekp(0, std::ios::beg);
writeBytes(io, new_header.data(), new_header.size(), "updated NPY header");
io.seekp(0, std::ios::end);
writeBytes(io, data, byte_count, "appended NPY payload");
return;
}
rewriteForAppend(path, existing, new_header, data, byte_count);
}
} // namespace syNumpy