github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/interfaces/sorting.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package interfaces
    21  
    22  import (
    23  	"sort"
    24  
    25  	"github.com/snapcore/snapd/snap"
    26  )
    27  
    28  type byConnRef []*ConnRef
    29  
    30  func (c byConnRef) Len() int      { return len(c) }
    31  func (c byConnRef) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    32  func (c byConnRef) Less(i, j int) bool {
    33  	return c[i].SortsBefore(c[j])
    34  }
    35  
    36  type byPlugSnapAndName []*snap.PlugInfo
    37  
    38  func (c byPlugSnapAndName) Len() int      { return len(c) }
    39  func (c byPlugSnapAndName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    40  func (c byPlugSnapAndName) Less(i, j int) bool {
    41  	if c[i].Snap.SnapName() != c[j].Snap.SnapName() {
    42  		return c[i].Snap.SnapName() < c[j].Snap.SnapName()
    43  	}
    44  	if c[i].Snap.InstanceKey != c[j].Snap.InstanceKey {
    45  		return c[i].Snap.InstanceKey < c[j].Snap.InstanceKey
    46  	}
    47  	return c[i].Name < c[j].Name
    48  }
    49  
    50  type bySlotSnapAndName []*snap.SlotInfo
    51  
    52  func (c bySlotSnapAndName) Len() int      { return len(c) }
    53  func (c bySlotSnapAndName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    54  func (c bySlotSnapAndName) Less(i, j int) bool {
    55  	if c[i].Snap.SnapName() != c[j].Snap.SnapName() {
    56  		return c[i].Snap.SnapName() < c[j].Snap.SnapName()
    57  	}
    58  	if c[i].Snap.InstanceKey != c[j].Snap.InstanceKey {
    59  		return c[i].Snap.InstanceKey < c[j].Snap.InstanceKey
    60  	}
    61  	return c[i].Name < c[j].Name
    62  }
    63  
    64  func sortedSnapNamesWithPlugs(m map[string]map[string]*snap.PlugInfo) []string {
    65  	keys := make([]string, 0, len(m))
    66  	for key := range m {
    67  		keys = append(keys, key)
    68  	}
    69  	sort.Strings(keys)
    70  	return keys
    71  }
    72  
    73  func sortedPlugNames(m map[string]*snap.PlugInfo) []string {
    74  	keys := make([]string, 0, len(m))
    75  	for key := range m {
    76  		keys = append(keys, key)
    77  	}
    78  	sort.Strings(keys)
    79  	return keys
    80  }
    81  
    82  func sortedSnapNamesWithSlots(m map[string]map[string]*snap.SlotInfo) []string {
    83  	keys := make([]string, 0, len(m))
    84  	for key := range m {
    85  		keys = append(keys, key)
    86  	}
    87  	sort.Strings(keys)
    88  	return keys
    89  }
    90  
    91  func sortedSlotNames(m map[string]*snap.SlotInfo) []string {
    92  	keys := make([]string, 0, len(m))
    93  	for key := range m {
    94  		keys = append(keys, key)
    95  	}
    96  	sort.Strings(keys)
    97  	return keys
    98  }
    99  
   100  type byInterfaceName []Interface
   101  
   102  func (c byInterfaceName) Len() int      { return len(c) }
   103  func (c byInterfaceName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
   104  func (c byInterfaceName) Less(i, j int) bool {
   105  	return c[i].Name() < c[j].Name()
   106  }