github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/structs/sortedmap/sortedmap.go (about)

     1  package sortedmap
     2  
     3  import (
     4  	"sort"
     5  )
     6  
     7  type SortedMap struct {
     8  	data map[string]interface{}
     9  	keys []string
    10  }
    11  
    12  func (s *SortedMap) Put(k string, v interface{}) {
    13  	s.data[k] = v
    14  	i := sort.Search(len(s.keys), func(i int) bool { return s.keys[i] >= k })
    15  	s.keys = append(s.keys, "")
    16  	copy(s.keys[i+1:], s.keys[i:])
    17  	s.keys[i] = k
    18  }
    19  
    20  func (s *SortedMap) Get(k string) (v interface{}) {
    21  	return s.data[k]
    22  }
    23  
    24  func (s *SortedMap) Keys() []string {
    25  	return s.keys
    26  }
    27  
    28  func New() *SortedMap {
    29  	return &SortedMap{
    30  		data: make(map[string]interface{}),
    31  		keys: make([]string, 0),
    32  	}
    33  }