github.com/cloudwego/frugal@v0.1.15/internal/binary/encoder/unique.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package encoder
    18  
    19  import (
    20      `unsafe`
    21  
    22      `github.com/cloudwego/frugal/internal/atm/hir`
    23      `github.com/cloudwego/frugal/internal/rt`
    24  )
    25  
    26  const (
    27      _N_u32 = unsafe.Sizeof(uint32(0))
    28      _N_u64 = unsafe.Sizeof(uint64(0))
    29      _N_str = unsafe.Sizeof(rt.GoString{})
    30  )
    31  
    32  func u32at(p unsafe.Pointer, i int) uint32 {
    33      return *(*uint32)(unsafe.Pointer(uintptr(p) + uintptr(i) * _N_u32))
    34  }
    35  
    36  func u64at(p unsafe.Pointer, i int) uint64 {
    37      return *(*uint64)(unsafe.Pointer(uintptr(p) + uintptr(i) * _N_u64))
    38  }
    39  
    40  func straddr(p unsafe.Pointer, i int) unsafe.Pointer {
    41      return unsafe.Pointer(uintptr(p) + uintptr(i) * _N_str)
    42  }
    43  
    44  func unique32(p unsafe.Pointer, nb int) bool {
    45      dup := false
    46      bmp := newBucket(nb * 2)
    47  
    48      /* put all the items */
    49      for i := 0; !dup && i < nb; i++ {
    50          dup = bucketAppend64(bmp, uint64(u32at(p, i)))
    51      }
    52  
    53      /* free the bucket */
    54      freeBucket(bmp)
    55      return dup
    56  }
    57  
    58  func unique64(p unsafe.Pointer, nb int) bool {
    59      dup := false
    60      bmp := newBucket(nb * 2)
    61  
    62      /* put all the items */
    63      for i := 0; !dup && i < nb; i++ {
    64          dup = bucketAppend64(bmp, u64at(p, i))
    65      }
    66  
    67      /* free the bucket */
    68      freeBucket(bmp)
    69      return dup
    70  }
    71  
    72  func uniquestr(p unsafe.Pointer, nb int) bool {
    73      dup := false
    74      bmp := newBucket(nb * 2)
    75  
    76      /* put all the items */
    77      for i := 0; !dup && i < nb; i++ {
    78          dup = bucketAppendStr(bmp, straddr(p, i))
    79      }
    80  
    81      /* free the bucket */
    82      freeBucket(bmp)
    83      return dup
    84  }
    85  
    86  var (
    87      F_unique32  = hir.RegisterGCall(unique32, emu_gcall_unique32)
    88      F_unique64  = hir.RegisterGCall(unique64, emu_gcall_unique64)
    89      F_uniquestr = hir.RegisterGCall(uniquestr, emu_gcall_uniquestr)
    90  )