MCPcopy Create free account
hub / github.com/brimdata/super / NormalizeMap

Function NormalizeMap

complex.go:127–151  ·  view source on GitHub ↗

NormalizeMap interprets zv as a map body and returns an equivalent map body that is normalized according to the BSUP specification (i.e., the tag-counted value of each entry's key is lexicographically greater than that of the preceding entry).

(zv scode.Bytes)

Source from the content-addressed store, hash-verified

125// value of each entry's key is lexicographically greater than that of the
126// preceding entry).
127func NormalizeMap(zv scode.Bytes) scode.Bytes {
128 elements := make([]keyval, 0, 8)
129 for it := zv.Iter(); !it.Done(); {
130 key := it.NextTagAndBody()
131 val := it.NextTagAndBody()
132 elements = append(elements, keyval{key, val})
133 }
134 if len(elements) < 2 {
135 return zv
136 }
137 sort.Slice(elements, func(i, j int) bool {
138 return bytes.Compare(elements[i].key, elements[j].key) == -1
139 })
140 norm := make(scode.Bytes, 0, len(zv))
141 norm = append(norm, elements[0].key...)
142 norm = append(norm, elements[0].val...)
143 for i := 1; i < len(elements); i++ {
144 // Skip duplicates.
145 if !bytes.Equal(elements[i].key, elements[i-1].key) {
146 norm = append(norm, elements[i].key...)
147 norm = append(norm, elements[i].val...)
148 }
149 }
150 return norm
151}
152
153type TypeNamed struct {
154 id int

Callers 3

EvalMethod · 0.92
toMapMethod · 0.92
ResultMethod · 0.92

Calls 5

IterMethod · 0.80
NextTagAndBodyMethod · 0.80
DoneMethod · 0.45
CompareMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected