-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.odin
46 lines (38 loc) · 950 Bytes
/
convert.odin
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
package mdspan
scalar :: proc(value: $P/^$E) -> Span(E,0) {
return transmute(Span(E,0)) value
}
array :: proc(values: $P/[]$E) -> Span(E,1) {
return transmute(Span(E,1)) values
}
from_slice :: proc(data: $P/[]$E, shape: [$R]int) -> (result: Span(E,R), ok: bool) where R > 0 #optional_ok {
shape := shape
found_fill := false
fill_position := 0
for i in 0 ..< R {
if shape[i] < 0 {
if found_fill { return }
found_fill = true
fill_position = i
}
}
if found_fill {
shape[fill_position] = len(data)
for i in 0 ..< R {
if i != fill_position {
if shape[fill_position] % shape[i] != 0 { return }
shape[fill_position] /= shape[i]
}
}
} else {
size := 1
for i in 0 ..<R { size *= shape[i] }
if size != len(data) { return }
}
result = { raw_data(data), shape }
return
}
to_slice :: proc (span: $S/Span($E,$R)) -> []E {
size := 1; for i in 0 ..< R { size *= span.shape[i] }
return span.ravel[:size]
}