Pick takes any vector vec and an index and returns a new vector consisting of the elements in the index.
(val Any, index []uint32)
| 7 | // Pick takes any vector vec and an index and returns a new vector consisting of the |
| 8 | // elements in the index. |
| 9 | func Pick(val Any, index []uint32) Any { |
| 10 | switch val := val.(type) { |
| 11 | case *Bool: |
| 12 | return NewBool(val.Bits.Pick(index)) |
| 13 | case *Const: |
| 14 | return NewConst(val.Any, uint32(len(index))) |
| 15 | case *Dict: |
| 16 | index2 := make([]byte, len(index)) |
| 17 | counts := make([]uint32, val.Any.Len()) |
| 18 | for k, idx := range index { |
| 19 | v := val.Index[idx] |
| 20 | index2[k] = v |
| 21 | counts[v]++ |
| 22 | } |
| 23 | return NewDict(val.Any, index2, counts) |
| 24 | case *Error: |
| 25 | return NewError(val.Typ, Pick(val.Vals, index)) |
| 26 | case *None: |
| 27 | return NewNoneWithError(OptType(val), NewError(val.Typ, Pick(val.Vals, index))) |
| 28 | case *Union: |
| 29 | tags, values := viewForUnionOrDynamic(index, val.Tags, val.ForwardTagMap(), val.Values) |
| 30 | return NewUnion(val.Typ, tags, values) |
| 31 | case *Dynamic: |
| 32 | return NewDynamic(viewForUnionOrDynamic(index, val.Tags, val.ForwardTagMap(), val.Values)) |
| 33 | case *Optional: |
| 34 | return NewDynamic(viewForUnionOrDynamic(index, val.Tags, val.ForwardTagMap(), val.Values)) |
| 35 | case *View: |
| 36 | index2 := make([]uint32, len(index)) |
| 37 | for k, idx := range index { |
| 38 | index2[k] = uint32(val.Index[idx]) |
| 39 | } |
| 40 | return NewView(val.Any, index2) |
| 41 | case *Named: |
| 42 | // Wrapped View under Named so vector.Under still works. |
| 43 | return &Named{val.Typ, Pick(val.Any, index)} |
| 44 | } |
| 45 | return &View{val, index} |
| 46 | } |
| 47 | |
| 48 | func viewForUnionOrDynamic(index, tags, forward []uint32, values []Any) ([]uint32, []Any) { |
| 49 | indexes := make([][]uint32, len(values)) |