| 1 |
package tensor |
| 2 |
|
| 3 |
import ( |
| 4 |
"unsafe" |
| 5 |
) |
| 6 |
|
| 7 |
// FillValue returns the value used to fill the invalid entries of a masked array |
| 8 |
func (t *Dense) FillValue() interface{} { |
| 9 |
switch t.Dtype() { |
| 10 |
case Bool: |
| 11 |
return true |
| 12 |
case Int: |
| 13 |
return int(999999) |
| 14 |
case Int8: |
| 15 |
return int8(99) |
| 16 |
case Int16: |
| 17 |
return int16(9999) |
| 18 |
case Int32: |
| 19 |
return int32(999999) |
| 20 |
case Int64: |
| 21 |
return int64(999999) |
| 22 |
case Uint: |
| 23 |
return uint(999999) |
| 24 |
case Byte: |
| 25 |
return byte(99) |
| 26 |
case Uint8: |
| 27 |
return uint8(99) |
| 28 |
case Uint16: |
| 29 |
return uint16(9999) |
| 30 |
case Uint32: |
| 31 |
return uint32(999999) |
| 32 |
case Uint64: |
| 33 |
return uint64(999999) |
| 34 |
case Float32: |
| 35 |
return float32(1.0e20) |
| 36 |
case Float64: |
| 37 |
return float64(1.0e20) |
| 38 |
case Complex64: |
| 39 |
return complex64(1.0e20 + 0i) |
| 40 |
case Complex128: |
| 41 |
return complex128(1.0e20 + 0i) |
| 42 |
case String: |
| 43 |
return `N/A` |
| 44 |
case Uintptr: |
| 45 |
return uintptr(0x999999) |
| 46 |
case UnsafePointer: |
| 47 |
return unsafe.Pointer(nil) |
| 48 |
default: |
| 49 |
return nil |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Filled returns a tensor with masked data replaced by default fill value, |
| 54 |
// or by optional passed value |
| 55 |
func (t *Dense) Filled(val ...interface{}) (interface{}, error) { |
| 56 |
tc := t.Clone().(*Dense) |
| 57 |
if !t.IsMasked() { |
| 58 |
return tc, nil |
| 59 |
} |
| 60 |
fillval := t.FillValue() |
| 61 |
if len(val) > 0 { |
| 62 |
fillval = val[0] |
| 63 |
} |
| 64 |
switch { |
| 65 |
case tc.IsScalar(): |
| 66 |
if tc.mask[0] { |
| 67 |
tc.Set(0, fillval) |
| 68 |
} |
| 69 |
case tc.IsRowVec() || tc.IsColVec(): |
| 70 |
sliceList := t.FlatMaskedContiguous() |
| 71 |
|
| 72 |
for i := range sliceList { |
| 73 |
tt, err := tc.Slice(nil, sliceList[i]) |
| 74 |
if err != nil { |
| 75 |
ts := tt.(*Dense) |
| 76 |
ts.Memset(fillval) |
| 77 |
} |
| 78 |
} |
| 79 |
default: |
| 80 |
it := IteratorFromDense(tc) |
| 81 |
for i, _, err := it.NextInvalid(); err == nil; i, _, err = it.NextInvalid() { |
| 82 |
tc.Set(i, fillval) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return tc, nil |
| 87 |
} |
| 88 |
|
| 89 |
// FilledInplace replaces masked data with default fill value, |
| 90 |
// or by optional passed value |
| 91 |
func (t *Dense) FilledInplace(val ...interface{}) (interface{}, error) { |
| 92 |
if !t.IsMasked() { |
| 93 |
return t, nil |
| 94 |
} |
| 95 |
fillval := t.FillValue() |
| 96 |
if len(val) > 0 { |
| 97 |
fillval = val[0] |
| 98 |
} |
| 99 |
switch { |
| 100 |
case t.IsScalar(): |
| 101 |
if t.mask[0] { |
| 102 |
t.Set(0, fillval) |
| 103 |
} |
| 104 |
case t.IsRowVec() || t.IsColVec(): |
| 105 |
sliceList := t.FlatMaskedContiguous() |
| 106 |
|
| 107 |
for i := range sliceList { |
| 108 |
tt, err := t.Slice(nil, sliceList[i]) |
| 109 |
if err != nil { |
| 110 |
ts := tt.(*Dense) |
| 111 |
ts.Memset(fillval) |
| 112 |
} |
| 113 |
} |
| 114 |
default: |
| 115 |
it := IteratorFromDense(t) |
| 116 |
for i, _, err := it.NextInvalid(); err == nil; i, _, err = it.NextInvalid() { |
| 117 |
t.Set(i, fillval) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return t, nil |
| 122 |
} |