github.com/cilium/cilium@v1.16.2/pkg/labels/arraylist.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package labels
     5  
     6  import "sort"
     7  
     8  // LabelArrayList is an array of LabelArrays. It is primarily intended as a
     9  // simple collection
    10  type LabelArrayList []LabelArray
    11  
    12  // DeepCopy returns a deep copy of the LabelArray, with each element also copied.
    13  func (ls LabelArrayList) DeepCopy() LabelArrayList {
    14  	if ls == nil {
    15  		return nil
    16  	}
    17  
    18  	o := make(LabelArrayList, 0, len(ls))
    19  	for _, v := range ls {
    20  		o = append(o, v.DeepCopy())
    21  	}
    22  	return o
    23  }
    24  
    25  // GetModel returns the LabelArrayList as a [][]string. Each member LabelArray
    26  // becomes a []string.
    27  func (ls LabelArrayList) GetModel() [][]string {
    28  	res := make([][]string, 0, len(ls))
    29  	for _, v := range ls {
    30  		res = append(res, v.GetModel())
    31  	}
    32  	return res
    33  }
    34  
    35  // Equals returns true if the label arrays lists have the same label arrays in the same order.
    36  func (ls LabelArrayList) Equals(b LabelArrayList) bool {
    37  	if len(ls) != len(b) {
    38  		return false
    39  	}
    40  	for l := range ls {
    41  		if !ls[l].Equals(b[l]) {
    42  			return false
    43  		}
    44  	}
    45  	return true
    46  }
    47  
    48  // Sort sorts the LabelArrayList in-place, but also returns the sorted list
    49  // for convenience. The LabelArrays themselves must already be sorted. This is
    50  // true for all constructors of LabelArray.
    51  func (ls LabelArrayList) Sort() LabelArrayList {
    52  	sort.Slice(ls, func(i, j int) bool {
    53  		return ls[i].Less(ls[j])
    54  	})
    55  
    56  	return ls
    57  }
    58  
    59  // Merge incorporates new LabelArrays into an existing LabelArrayList, without
    60  // introducing duplicates, returning the result for convenience. Existing
    61  // duplication in either list is not removed.
    62  func (lsp *LabelArrayList) Merge(include ...LabelArray) LabelArrayList {
    63  	lsp.Sort()
    64  	incl := LabelArrayList(include).Sort()
    65  	return lsp.MergeSorted(incl)
    66  }
    67  
    68  // MergeSorted incorporates new labels from 'include' to the receiver,
    69  // both of which must be already sorted.
    70  // LabelArrays are inserted from 'include' to the receiver as needed.
    71  func (lsp *LabelArrayList) MergeSorted(include LabelArrayList) LabelArrayList {
    72  	merged := *lsp
    73  	i := 0
    74  	for j := 0; i < len(include) && j < len(merged); j++ {
    75  		if include[i].Less(merged[j]) {
    76  			merged = append(merged[:j+1], merged[j:]...) // make space at merged[j]
    77  			merged[j] = include[i]
    78  			i++
    79  		} else if include[i].Equals(merged[j]) {
    80  			i++
    81  		}
    82  	}
    83  
    84  	// 'include' may have more entries after original labels have been exhausted
    85  	if i < len(include) {
    86  		merged = append(merged, include[i:]...)
    87  	}
    88  
    89  	*lsp = merged
    90  	return *lsp
    91  }