ndtypes 0.2.0dev4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +50 -0
- data/Gemfile +2 -0
- data/History.md +0 -0
- data/README.md +19 -0
- data/Rakefile +125 -0
- data/ext/ruby_ndtypes/extconf.rb +55 -0
- data/ext/ruby_ndtypes/gc_guard.c +36 -0
- data/ext/ruby_ndtypes/gc_guard.h +12 -0
- data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
- data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
- data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
- data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
- data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
- data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
- data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
- data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
- data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
- data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
- data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
- data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
- data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
- data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
- data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
- data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
- data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
- data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
- data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
- data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
- data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
- data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
- data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
- data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
- data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
- data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
- data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
- data/lib/ndtypes.rb +45 -0
- data/lib/ndtypes/errors.rb +2 -0
- data/lib/ndtypes/version.rb +6 -0
- data/ndtypes.gemspec +47 -0
- data/spec/gc_table_spec.rb +10 -0
- data/spec/ndtypes_spec.rb +289 -0
- data/spec/spec_helper.rb +241 -0
- metadata +242 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* BSD 3-Clause License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2017-2018, plures
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
*
|
|
10
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
* this list of conditions and the following disclaimer.
|
|
12
|
+
*
|
|
13
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
14
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
15
|
+
* and/or other materials provided with the distribution.
|
|
16
|
+
*
|
|
17
|
+
* 3. Neither the name of the copyright holder nor the names of its
|
|
18
|
+
* contributors may be used to endorse or promote products derived from
|
|
19
|
+
* this software without specific prior written permission.
|
|
20
|
+
*
|
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
22
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
23
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
24
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
25
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
26
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
27
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
28
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
29
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
30
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
#include <stdlib.h>
|
|
35
|
+
#include <stdint.h>
|
|
36
|
+
#include <inttypes.h>
|
|
37
|
+
#include <string.h>
|
|
38
|
+
#include <assert.h>
|
|
39
|
+
#include "ndtypes.h"
|
|
40
|
+
#include "overflow.h"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
static int64_t write_type(char * const ptr, int64_t offset, const ndt_t * const t, bool *overflow);
|
|
44
|
+
|
|
45
|
+
typedef double float64_t;
|
|
46
|
+
typedef bool bool_t;
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/*****************************************************************************/
|
|
50
|
+
/* Write values to the bytes buffer */
|
|
51
|
+
/*****************************************************************************/
|
|
52
|
+
|
|
53
|
+
#define WRITE(type) \
|
|
54
|
+
static inline int64_t \
|
|
55
|
+
write_##type(char * const ptr, int64_t offset, const type##_t value, \
|
|
56
|
+
bool *overflow) \
|
|
57
|
+
{ \
|
|
58
|
+
const size_t size = sizeof(type##_t); \
|
|
59
|
+
\
|
|
60
|
+
if (ptr != NULL) { \
|
|
61
|
+
memcpy(ptr+offset, &value, size); \
|
|
62
|
+
} \
|
|
63
|
+
\
|
|
64
|
+
return ADDi64(offset, size, overflow); \
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#define WRITE_ARRAY(type) \
|
|
68
|
+
static inline int64_t \
|
|
69
|
+
write_##type##_array(char * const ptr, int64_t offset, const type##_t *v, \
|
|
70
|
+
const int64_t nmemb, bool *overflow) \
|
|
71
|
+
{ \
|
|
72
|
+
size_t size = MULi64_size(nmemb, sizeof(type##_t), overflow); \
|
|
73
|
+
\
|
|
74
|
+
if (ptr != NULL) { \
|
|
75
|
+
memcpy(ptr+offset, v, size); \
|
|
76
|
+
} \
|
|
77
|
+
\
|
|
78
|
+
return ADDi64(offset, size, overflow); \
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
WRITE(bool)
|
|
82
|
+
WRITE(uint8)
|
|
83
|
+
WRITE(uint16)
|
|
84
|
+
WRITE(uint32)
|
|
85
|
+
WRITE(int32)
|
|
86
|
+
WRITE(int64)
|
|
87
|
+
WRITE(float64)
|
|
88
|
+
WRITE_ARRAY(uint16)
|
|
89
|
+
WRITE_ARRAY(int32)
|
|
90
|
+
WRITE_ARRAY(int64)
|
|
91
|
+
WRITE_ARRAY(ndt_slice)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
static inline int64_t
|
|
95
|
+
alloc_int64_array(const int64_t offset, const int64_t shape, bool *overflow)
|
|
96
|
+
{
|
|
97
|
+
int64_t size = MULi64(shape, sizeof(int64_t), overflow);
|
|
98
|
+
return ADDi64(offset, size, overflow);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
static inline int64_t
|
|
102
|
+
write_string(char * const ptr, int64_t offset, char *src, bool *overflow)
|
|
103
|
+
{
|
|
104
|
+
size_t size;
|
|
105
|
+
|
|
106
|
+
if (ptr != NULL) {
|
|
107
|
+
strcpy(ptr+offset, src);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
size = strlen(src) + 1;
|
|
111
|
+
return ADDi64(offset, (int64_t)size, overflow);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
static inline int64_t
|
|
115
|
+
write_string_array(char * const ptr, int64_t offset, char *s[],
|
|
116
|
+
const int64_t shape, bool *overflow)
|
|
117
|
+
{
|
|
118
|
+
for (int64_t i = 0; i < shape; i++) {
|
|
119
|
+
offset = write_string(ptr, offset, s[i], overflow);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return offset;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static inline int64_t
|
|
126
|
+
write_ndt_value_array(char * const ptr, int64_t offset, const ndt_value_t *v,
|
|
127
|
+
const int64_t nmemb, bool *overflow)
|
|
128
|
+
{
|
|
129
|
+
for (int64_t i = 0; i < nmemb; i++) {
|
|
130
|
+
offset = write_uint8(ptr, offset, (uint8_t)v[i].tag, overflow);
|
|
131
|
+
|
|
132
|
+
switch (v[i].tag) {
|
|
133
|
+
case ValNA:
|
|
134
|
+
break;
|
|
135
|
+
case ValBool:
|
|
136
|
+
offset = write_bool(ptr, offset, v[i].ValBool, overflow);
|
|
137
|
+
break;
|
|
138
|
+
case ValInt64:
|
|
139
|
+
offset = write_int64(ptr, offset, v[i].ValInt64, overflow);
|
|
140
|
+
break;
|
|
141
|
+
case ValFloat64:
|
|
142
|
+
offset = write_float64(ptr, offset, v[i].ValFloat64, overflow);
|
|
143
|
+
break;
|
|
144
|
+
case ValString:
|
|
145
|
+
offset = write_string(ptr, offset, v[i].ValString, overflow);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return offset;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
/******************************************************************************/
|
|
155
|
+
/* Serialize types */
|
|
156
|
+
/******************************************************************************/
|
|
157
|
+
|
|
158
|
+
static int64_t
|
|
159
|
+
write_common_fields(char *ptr, int64_t offset, const ndt_t * const t,
|
|
160
|
+
bool *overflow)
|
|
161
|
+
{
|
|
162
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->tag, overflow);
|
|
163
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->access, overflow);
|
|
164
|
+
offset = write_uint32(ptr, offset, t->flags, overflow);
|
|
165
|
+
offset = write_int32(ptr, offset, t->ndim, overflow);
|
|
166
|
+
offset = write_int64(ptr, offset, t->datasize, overflow);
|
|
167
|
+
return write_uint16(ptr, offset, t->align, overflow);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static int64_t
|
|
171
|
+
write_module(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
172
|
+
bool *overflow)
|
|
173
|
+
{
|
|
174
|
+
offset = write_string(ptr, offset, t->Module.name, overflow);
|
|
175
|
+
return write_type(ptr, offset, t->Module.type, overflow);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static int64_t
|
|
179
|
+
write_function(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
180
|
+
bool *overflow)
|
|
181
|
+
{
|
|
182
|
+
const int64_t nargs = t->Function.nargs;
|
|
183
|
+
int64_t metaoffset;
|
|
184
|
+
|
|
185
|
+
offset = write_int64(ptr, offset, t->Function.nin, overflow);
|
|
186
|
+
offset = write_int64(ptr, offset, t->Function.nout, overflow);
|
|
187
|
+
offset = write_int64(ptr, offset, nargs, overflow);
|
|
188
|
+
|
|
189
|
+
metaoffset = offset;
|
|
190
|
+
offset = alloc_int64_array(offset, nargs, overflow);
|
|
191
|
+
|
|
192
|
+
for (int64_t i = 0; i < nargs; i++) {
|
|
193
|
+
metaoffset = write_int64(ptr, metaoffset, offset, overflow);
|
|
194
|
+
offset = write_type(ptr, offset, t->Function.types[i], overflow);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return offset;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static int64_t
|
|
201
|
+
write_fixed_dim(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
202
|
+
bool *overflow)
|
|
203
|
+
{
|
|
204
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->FixedDim.tag, overflow);
|
|
205
|
+
offset = write_int64(ptr, offset, t->FixedDim.shape, overflow);
|
|
206
|
+
offset = write_int64(ptr, offset, t->Concrete.FixedDim.step, overflow);
|
|
207
|
+
offset = write_int64(ptr, offset, t->Concrete.FixedDim.itemsize, overflow);
|
|
208
|
+
return write_type(ptr, offset, t->FixedDim.type, overflow);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static int64_t
|
|
212
|
+
write_symbolic_dim(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
213
|
+
bool *overflow)
|
|
214
|
+
{
|
|
215
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->SymbolicDim.tag, overflow);
|
|
216
|
+
offset = write_string(ptr, offset, t->SymbolicDim.name, overflow);
|
|
217
|
+
return write_type(ptr, offset, t->SymbolicDim.type, overflow);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
static int64_t
|
|
221
|
+
write_ellipsis_dim(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
222
|
+
bool *overflow)
|
|
223
|
+
{
|
|
224
|
+
char *cp = t->EllipsisDim.name ? t->EllipsisDim.name : "";
|
|
225
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->EllipsisDim.tag, overflow);
|
|
226
|
+
offset = write_string(ptr, offset, cp, overflow);
|
|
227
|
+
return write_type(ptr, offset, t->EllipsisDim.type, overflow);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static int64_t
|
|
231
|
+
write_var_dim(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
232
|
+
bool *overflow)
|
|
233
|
+
{
|
|
234
|
+
const int32_t noffsets = t->Concrete.VarDim.noffsets;
|
|
235
|
+
const int32_t nslices = t->Concrete.VarDim.nslices;
|
|
236
|
+
|
|
237
|
+
offset = write_int64(ptr, offset, t->Concrete.VarDim.itemsize, overflow);
|
|
238
|
+
offset = write_int32(ptr, offset, noffsets, overflow);
|
|
239
|
+
offset = write_int32(ptr, offset, t->Concrete.VarDim.nslices, overflow);
|
|
240
|
+
offset = write_int32_array(ptr, offset, t->Concrete.VarDim.offsets, noffsets, overflow);
|
|
241
|
+
offset = write_ndt_slice_array(ptr, offset, t->Concrete.VarDim.slices, nslices, overflow);
|
|
242
|
+
return write_type(ptr, offset, t->VarDim.type, overflow);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
static int64_t
|
|
246
|
+
write_tuple(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
247
|
+
bool *overflow)
|
|
248
|
+
{
|
|
249
|
+
const int64_t shape = t->Tuple.shape;
|
|
250
|
+
int64_t metaoffset;
|
|
251
|
+
|
|
252
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->Tuple.flag, overflow);
|
|
253
|
+
offset = write_int64(ptr, offset, shape, overflow);
|
|
254
|
+
offset = write_int64_array(ptr, offset, t->Concrete.Tuple.offset, shape, overflow);
|
|
255
|
+
offset = write_uint16_array(ptr, offset, t->Concrete.Tuple.align, shape, overflow);
|
|
256
|
+
offset = write_uint16_array(ptr, offset, t->Concrete.Tuple.pad, shape, overflow);
|
|
257
|
+
|
|
258
|
+
metaoffset = offset;
|
|
259
|
+
offset = alloc_int64_array(offset, shape, overflow);
|
|
260
|
+
|
|
261
|
+
for (int64_t i = 0; i < shape; i++) {
|
|
262
|
+
metaoffset = write_int64(ptr, metaoffset, offset, overflow);
|
|
263
|
+
offset = write_type(ptr, offset, t->Tuple.types[i], overflow);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return offset;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static int64_t
|
|
270
|
+
write_record(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
271
|
+
bool *overflow)
|
|
272
|
+
{
|
|
273
|
+
const int64_t shape = t->Record.shape;
|
|
274
|
+
int64_t metaoffset;
|
|
275
|
+
|
|
276
|
+
offset = write_uint8(ptr, offset, (uint8_t)t->Record.flag, overflow);
|
|
277
|
+
offset = write_int64(ptr, offset, shape, overflow);
|
|
278
|
+
offset = write_int64_array(ptr, offset, t->Concrete.Record.offset, shape, overflow);
|
|
279
|
+
offset = write_uint16_array(ptr, offset, t->Concrete.Record.align, shape, overflow);
|
|
280
|
+
offset = write_uint16_array(ptr, offset, t->Concrete.Tuple.pad, shape, overflow);
|
|
281
|
+
offset = write_string_array(ptr, offset, t->Record.names, shape, overflow);
|
|
282
|
+
|
|
283
|
+
metaoffset = offset;
|
|
284
|
+
offset = alloc_int64_array(offset, shape, overflow);
|
|
285
|
+
|
|
286
|
+
for (int64_t i = 0; i < shape; i++) {
|
|
287
|
+
metaoffset = write_int64(ptr, metaoffset, offset, overflow);
|
|
288
|
+
offset = write_type(ptr, offset, t->Record.types[i], overflow);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return offset;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
static int64_t
|
|
295
|
+
write_ref(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
296
|
+
bool *overflow)
|
|
297
|
+
{
|
|
298
|
+
return write_type(ptr, offset, t->Ref.type, overflow);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static int64_t
|
|
302
|
+
write_constr(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
303
|
+
bool *overflow)
|
|
304
|
+
{
|
|
305
|
+
offset = write_string(ptr, offset, t->Constr.name, overflow);
|
|
306
|
+
return write_type(ptr, offset, t->Constr.type, overflow);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
static int64_t
|
|
310
|
+
write_nominal(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
311
|
+
bool *overflow)
|
|
312
|
+
{
|
|
313
|
+
offset = write_string(ptr, offset, t->Nominal.name, overflow);
|
|
314
|
+
return write_type(ptr, offset, t->Nominal.type, overflow);
|
|
315
|
+
/* The constraint function pointer is deliberately omitted. It must be
|
|
316
|
+
* looked up and restored during deserialization. */
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static int64_t
|
|
320
|
+
write_categorical(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
321
|
+
bool *overflow)
|
|
322
|
+
{
|
|
323
|
+
const int64_t ntypes = t->Categorical.ntypes;
|
|
324
|
+
|
|
325
|
+
offset = write_int64(ptr, offset, ntypes, overflow);
|
|
326
|
+
return write_ndt_value_array(ptr, offset, t->Categorical.types, ntypes, overflow);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
static int64_t
|
|
330
|
+
write_fixed_string(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
331
|
+
bool *overflow)
|
|
332
|
+
{
|
|
333
|
+
offset = write_int64(ptr, offset, t->FixedString.size, overflow);
|
|
334
|
+
return write_uint8(ptr, offset, (uint8_t)t->FixedString.encoding, overflow);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
static int64_t
|
|
338
|
+
write_fixed_bytes(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
339
|
+
bool *overflow)
|
|
340
|
+
{
|
|
341
|
+
offset = write_int64(ptr, offset, t->FixedBytes.size, overflow);
|
|
342
|
+
return write_uint16(ptr, offset, t->FixedBytes.align, overflow);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
static int64_t
|
|
346
|
+
write_bytes(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
347
|
+
bool *overflow)
|
|
348
|
+
{
|
|
349
|
+
return write_uint16(ptr, offset, t->Bytes.target_align, overflow);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
static int64_t
|
|
353
|
+
write_char(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
354
|
+
bool *overflow)
|
|
355
|
+
{
|
|
356
|
+
return write_uint8(ptr, offset, (uint8_t)t->Char.encoding, overflow);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static int64_t
|
|
360
|
+
write_typevar(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
361
|
+
bool *overflow)
|
|
362
|
+
{
|
|
363
|
+
return write_string(ptr, offset, t->Typevar.name, overflow);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
static int64_t
|
|
367
|
+
write_type(char * const ptr, int64_t offset, const ndt_t * const t,
|
|
368
|
+
bool *overflow)
|
|
369
|
+
{
|
|
370
|
+
offset = write_common_fields(ptr, offset, t, overflow);
|
|
371
|
+
|
|
372
|
+
switch (t->tag) {
|
|
373
|
+
case Module: return write_module(ptr, offset, t, overflow);
|
|
374
|
+
case Function: return write_function(ptr, offset, t, overflow);
|
|
375
|
+
case FixedDim: return write_fixed_dim(ptr, offset, t, overflow);
|
|
376
|
+
case SymbolicDim: return write_symbolic_dim(ptr, offset, t, overflow);
|
|
377
|
+
case EllipsisDim: return write_ellipsis_dim(ptr, offset, t, overflow);
|
|
378
|
+
case VarDim: return write_var_dim(ptr, offset, t, overflow);
|
|
379
|
+
case Tuple: return write_tuple(ptr, offset, t, overflow);
|
|
380
|
+
case Record: return write_record(ptr, offset, t, overflow);
|
|
381
|
+
case Ref: return write_ref(ptr, offset, t, overflow);
|
|
382
|
+
case Constr: return write_constr(ptr, offset, t, overflow);
|
|
383
|
+
case Nominal: return write_nominal(ptr, offset, t, overflow);
|
|
384
|
+
case Categorical: return write_categorical(ptr, offset, t, overflow);
|
|
385
|
+
case FixedString: return write_fixed_string(ptr, offset, t, overflow);
|
|
386
|
+
case FixedBytes: return write_fixed_bytes(ptr, offset, t, overflow);
|
|
387
|
+
case Bytes: return write_bytes(ptr, offset, t, overflow);
|
|
388
|
+
case Char: return write_char(ptr, offset, t, overflow);
|
|
389
|
+
case Typevar: return write_typevar(ptr, offset, t, overflow);
|
|
390
|
+
|
|
391
|
+
case Bool:
|
|
392
|
+
case Int8: case Int16: case Int32: case Int64:
|
|
393
|
+
case Uint8: case Uint16: case Uint32: case Uint64:
|
|
394
|
+
case Float16: case Float32: case Float64:
|
|
395
|
+
case Complex32: case Complex64: case Complex128:
|
|
396
|
+
case String:
|
|
397
|
+
|
|
398
|
+
case AnyKind:
|
|
399
|
+
case ScalarKind: case SignedKind: case UnsignedKind: case FloatKind:
|
|
400
|
+
case ComplexKind: case FixedStringKind: case FixedBytesKind:
|
|
401
|
+
return offset;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/* NOT REACHED: tags should be exhaustive. */
|
|
405
|
+
ndt_internal_error("invalid tag");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
int64_t
|
|
409
|
+
ndt_serialize(char **dest, const ndt_t * const t, ndt_context_t *ctx)
|
|
410
|
+
{
|
|
411
|
+
bool overflow = 0;
|
|
412
|
+
int64_t len;
|
|
413
|
+
char *bytes;
|
|
414
|
+
|
|
415
|
+
*dest = NULL;
|
|
416
|
+
|
|
417
|
+
len = write_type(NULL, 0, t, &overflow);
|
|
418
|
+
if (overflow) {
|
|
419
|
+
ndt_err_format(ctx, NDT_ValueError,
|
|
420
|
+
"overflow during type serialization");
|
|
421
|
+
return -1;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
bytes = ndt_alloc(len, 1);
|
|
425
|
+
if (bytes == NULL) {
|
|
426
|
+
(void)ndt_memory_error(ctx);
|
|
427
|
+
return -1;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
overflow = 0;
|
|
431
|
+
int64_t n = write_type(bytes, 0, t, &overflow);
|
|
432
|
+
if (overflow || n != len) {
|
|
433
|
+
ndt_err_format(ctx, NDT_RuntimeError,
|
|
434
|
+
"unexpected overflow or different length in second pass "
|
|
435
|
+
"of serialization");
|
|
436
|
+
ndt_free(bytes);
|
|
437
|
+
return -1;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
*dest = bytes;
|
|
441
|
+
return len;
|
|
442
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* PSF copyright: original by Jim Hugunin and Chris Chase. */
|
|
2
|
+
|
|
3
|
+
int64_t
|
|
4
|
+
ndt_slice_adjust_indices(int64_t length, int64_t *start, int64_t *stop, int64_t step)
|
|
5
|
+
{
|
|
6
|
+
/* this is harder to get right than you might think */
|
|
7
|
+
|
|
8
|
+
assert(step != 0);
|
|
9
|
+
assert(step >= -INT64_MAX);
|
|
10
|
+
|
|
11
|
+
if (*start < 0) {
|
|
12
|
+
*start += length;
|
|
13
|
+
if (*start < 0) {
|
|
14
|
+
*start = (step < 0) ? -1 : 0;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else if (*start >= length) {
|
|
18
|
+
*start = (step < 0) ? length - 1 : length;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (*stop < 0) {
|
|
22
|
+
*stop += length;
|
|
23
|
+
if (*stop < 0) {
|
|
24
|
+
*stop = (step < 0) ? -1 : 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else if (*stop >= length) {
|
|
28
|
+
*stop = (step < 0) ? length - 1 : length;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (step < 0) {
|
|
32
|
+
if (*stop < *start) {
|
|
33
|
+
return (*start - *stop - 1) / (-step) + 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (*start < *stop) {
|
|
38
|
+
return (*stop - *start - 1) / step + 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return 0;
|
|
42
|
+
}
|