github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/mergesort/uuids/func.go (about) 1 // Copyright 2021 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package uuids 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/types" 19 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers" 20 ) 21 22 func Sort(col containers.Vector, idx []uint32) (ret containers.Vector) { 23 n := len(idx) 24 dataWithIdx := make(sortSlice, n) 25 26 for i := 0; i < n; i++ { 27 dataWithIdx[i] = sortElem{data: col.Get(i).(types.Uuid), idx: uint32(i)} 28 } 29 30 sortUnstable(dataWithIdx) 31 32 for i, v := range dataWithIdx { 33 idx[i] = v.idx 34 col.Update(i, v.data) 35 } 36 ret = col 37 return 38 } 39 40 func Merge(col []containers.Vector, src *[]uint32, fromLayout, toLayout []uint32) (ret []containers.Vector, mapping []uint32) { 41 ret = make([]containers.Vector, len(toLayout)) 42 mapping = make([]uint32, len(*src)) 43 44 offset := make([]uint32, len(fromLayout)) 45 offset[0] = 0 46 for i := 1; i < len(fromLayout); i++ { 47 offset[i] = offset[i-1] + fromLayout[i-1] 48 } 49 50 for i := range toLayout { 51 ret[i] = containers.MakeVector(col[0].GetType(), col[0].Nullable()) 52 } 53 54 nBlk := len(col) 55 heap := make(heapSlice, nBlk) 56 57 for i := 0; i < nBlk; i++ { 58 heap[i] = heapElem{data: col[i].Get(0).(types.Uuid), src: uint32(i), next: 1} 59 } 60 heapInit(heap) 61 62 k := 0 63 for i := 0; i < len(toLayout); i++ { 64 for j := 0; j < int(toLayout[i]); j++ { 65 top := heapPop(&heap) 66 (*src)[k] = top.src 67 ret[i].Append(top.data) 68 mapping[offset[top.src]+top.next-1] = uint32(k) 69 k++ 70 if int(top.next) < int(fromLayout[top.src]) { 71 heapPush(&heap, heapElem{data: col[top.src].Get(int(top.next)).(types.Uuid), src: top.src, next: top.next + 1}) 72 } 73 } 74 } 75 return 76 }