MCPcopy Create free account
hub / github.com/cortexproject/cortex / MergeSortedSlices

Function MergeSortedSlices

pkg/util/strings.go:111–141  ·  view source on GitHub ↗

MergeSortedSlices merges a set of sorted string slices into a single ones while removing all duplicates.

(ctx context.Context, a ...[]string)

Source from the content-addressed store, hash-verified

109// MergeSortedSlices merges a set of sorted string slices into a single ones
110// while removing all duplicates.
111func MergeSortedSlices(ctx context.Context, a ...[]string) ([]string, error) {
112 if len(a) == 1 {
113 return a[0], nil
114 }
115 its := make([]*StringListIter, 0, len(a))
116 sumLengh := 0
117 for _, s := range a {
118 sumLengh += len(s)
119 its = append(its, NewStringListIter(s))
120 }
121 lt := loser.New(its, MAX_STRING)
122
123 if sumLengh == 0 {
124 return []string{}, nil
125 }
126
127 r := make([]string, 0, sumLengh*2/10)
128 var current string
129 cnt := 0
130 for lt.Next() {
131 cnt++
132 if cnt%CheckContextEveryNIterations == 0 && ctx.Err() != nil {
133 return nil, ctx.Err()
134 }
135 if lt.At() != current {
136 current = lt.At()
137 r = append(r, current)
138 }
139 }
140 return r, nil
141}
142
143type Interner interface {
144 Intern(s string) string

Callers 1

MergeSlicesParallelFunction · 0.85

Calls 4

NewStringListIterFunction · 0.85
NextMethod · 0.65
ErrMethod · 0.65
AtMethod · 0.65

Tested by

no test coverage detected