github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/val/triple.go (about) 1 // Copyright 2022 Dolthub, Inc. 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 val 16 17 import "github.com/dolthub/dolt/go/store/pool" 18 19 const ( 20 tripleOffSz = int(uint16Size + uint16Size) 21 ) 22 23 func NewTriple[V ~[]byte](pool pool.BuffPool, one, two, three V) (tri Triple[V]) { 24 o1 := len(one) 25 o2 := len(two) + o1 26 end := len(three) + o2 27 tri = pool.Get(uint64(end + tripleOffSz)) 28 29 // populate fields 30 copy(tri, one) 31 copy(tri[o1:], two) 32 copy(tri[o2:], three) 33 34 // populate offsets 35 WriteUint16(tri[end:end+2], uint16(o1)) 36 WriteUint16(tri[end+2:], uint16(o2)) 37 return 38 } 39 40 type Triple[V ~[]byte] []byte 41 42 func (t Triple[V]) First() V { 43 l := len(t) 44 o1 := ReadUint16(t[l-4 : l-2]) 45 return V(t[:o1]) 46 } 47 48 func (t Triple[V]) Second() V { 49 l := len(t) 50 o1 := ReadUint16(t[l-4 : l-2]) 51 o2 := ReadUint16(t[l-2 : l]) 52 return V(t[o1:o2]) 53 } 54 55 func (t Triple[V]) Third() V { 56 l := len(t) 57 o2 := ReadUint16(t[l-2 : l]) 58 return V(t[o2 : l-4]) 59 }