github.com/cilium/cilium@v1.16.2/pkg/counter/integer.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package counter
     5  
     6  import (
     7  	"sort"
     8  )
     9  
    10  // IntCounter tracks references for integers with an optional limiter.
    11  //
    12  // No threadsafety is provided within this structure, the user is expected to
    13  // handle concurrent access to this structure if it is used from multiple
    14  // threads.
    15  type IntCounter Counter[int]
    16  
    17  // Add increments the reference count for the specified integer key.
    18  func (i IntCounter) Add(key int) (changed bool) {
    19  	return Counter[int](i).Add(key)
    20  }
    21  
    22  // Delete decrements the reference count for the specified integer key.
    23  func (i IntCounter) Delete(key int) bool {
    24  	return Counter[int](i).Delete(key)
    25  }
    26  
    27  // DeepCopy makes a new copy of the received IntCounter.
    28  func (i IntCounter) DeepCopy() IntCounter {
    29  	return IntCounter(Counter[int](i).DeepCopy())
    30  }
    31  
    32  // ToBPFData returns the keys as a slice, sorted from high to low.
    33  func (i IntCounter) ToBPFData() []int {
    34  	result := make([]int, 0, len(i))
    35  	for key := range i {
    36  		result = append(result, key)
    37  	}
    38  	sort.Sort(sort.Reverse(sort.IntSlice(result)))
    39  	return result
    40  }