github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/cosmos/libs/common/kvpair.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"sort"
     6  )
     7  
     8  //----------------------------------------
     9  // KVPair
    10  
    11  /*
    12  Defined in types.proto
    13  
    14  type KVPair struct {
    15  	Key   []byte
    16  	Value []byte
    17  }
    18  */
    19  
    20  type KVPairs []KVPair
    21  
    22  // Sorting
    23  func (kvs KVPairs) Len() int { return len(kvs) }
    24  func (kvs KVPairs) Less(i, j int) bool {
    25  	switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
    26  	case -1:
    27  		return true
    28  	case 0:
    29  		return bytes.Compare(kvs[i].Value, kvs[j].Value) < 0
    30  	case 1:
    31  		return false
    32  	default:
    33  		panic("invalid comparison result")
    34  	}
    35  }
    36  func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
    37  func (kvs KVPairs) Sort()         { sort.Sort(kvs) }
    38  
    39  //----------------------------------------
    40  // KI64Pair
    41  
    42  /*
    43  Defined in types.proto
    44  type KI64Pair struct {
    45  	Key   []byte
    46  	Value int64
    47  }
    48  */
    49  
    50  type KI64Pairs []KI64Pair
    51  
    52  // Sorting
    53  func (kvs KI64Pairs) Len() int { return len(kvs) }
    54  func (kvs KI64Pairs) Less(i, j int) bool {
    55  	switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
    56  	case -1:
    57  		return true
    58  	case 0:
    59  		return kvs[i].Value < kvs[j].Value
    60  	case 1:
    61  		return false
    62  	default:
    63  		panic("invalid comparison result")
    64  	}
    65  }
    66  func (kvs KI64Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
    67  func (kvs KI64Pairs) Sort()         { sort.Sort(kvs) }