istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/ledger/util.go (about)

     1  // Copyright 2019 Istio Authors
     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 ledger
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/spaolacci/murmur3"
    21  )
    22  
    23  // defaultLeaf is the Trie default value : hash of 0x0
    24  var defaultLeaf = hasher([]byte{0x0})
    25  
    26  const (
    27  	hashLength = 8
    28  )
    29  
    30  type hash [hashLength]byte
    31  
    32  func bitIsSet(bits []byte, i int) bool {
    33  	return bits[i/8]&(1<<uint(7-i%8)) != 0
    34  }
    35  
    36  func hasher(data ...[]byte) []byte {
    37  	hasher := murmur3.New64()
    38  	for i := 0; i < len(data); i++ {
    39  		_, _ = hasher.Write(data[i])
    40  	}
    41  	result := hasher.Sum(nil)
    42  	return result
    43  }
    44  
    45  // for sorting
    46  type dataArray [][]byte
    47  
    48  func (d dataArray) Len() int {
    49  	return len(d)
    50  }
    51  
    52  func (d dataArray) Swap(i, j int) {
    53  	d[i], d[j] = d[j], d[i]
    54  }
    55  
    56  func (d dataArray) Less(i, j int) bool {
    57  	return bytes.Compare(d[i], d[j]) == -1
    58  }