|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package common |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/binary" |
| 22 | + "math" |
| 23 | +) |
| 24 | + |
| 25 | +var ItemSketchFloatComparator = func(reverseOrder bool) CompareFn[float32] { |
| 26 | + return func(a float32, b float32) bool { |
| 27 | + if reverseOrder { |
| 28 | + return a > b |
| 29 | + } |
| 30 | + return a < b |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// ItemSketchFloatSerDe handles serialization and deserialization of floating-point sketch items. |
| 35 | +type ItemSketchFloatSerDe struct{} |
| 36 | + |
| 37 | +func (s ItemSketchFloatSerDe) SizeOf(item float32) int { |
| 38 | + return 4 |
| 39 | +} |
| 40 | + |
| 41 | +func (s ItemSketchFloatSerDe) SizeOfMany(mem []byte, offsetBytes int, numItems int) (int, error) { |
| 42 | + return numItems * 4, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (s ItemSketchFloatSerDe) SerializeOneToSlice(item float32) []byte { |
| 46 | + bytes := make([]byte, 4) |
| 47 | + binary.LittleEndian.PutUint32(bytes, math.Float32bits(item)) |
| 48 | + return bytes |
| 49 | +} |
| 50 | + |
| 51 | +func (s ItemSketchFloatSerDe) SerializeManyToSlice(items []float32) []byte { |
| 52 | + if len(items) == 0 { |
| 53 | + return []byte{} |
| 54 | + } |
| 55 | + |
| 56 | + bytes := make([]byte, 4*len(items)) |
| 57 | + offset := 0 |
| 58 | + for _, item := range items { |
| 59 | + binary.LittleEndian.PutUint32(bytes[offset:], math.Float32bits(item)) |
| 60 | + offset += 4 |
| 61 | + } |
| 62 | + return bytes |
| 63 | +} |
| 64 | + |
| 65 | +func (s ItemSketchFloatSerDe) DeserializeManyFromSlice(mem []byte, offsetBytes int, numItems int) ([]float32, error) { |
| 66 | + if numItems == 0 { |
| 67 | + return []float32{}, nil |
| 68 | + } |
| 69 | + |
| 70 | + array := make([]float32, 0, numItems) |
| 71 | + for i := 0; i < numItems; i++ { |
| 72 | + array = append(array, math.Float32frombits(binary.LittleEndian.Uint32(mem[offsetBytes:]))) |
| 73 | + offsetBytes += 4 |
| 74 | + } |
| 75 | + return array, nil |
| 76 | +} |
0 commit comments