MCPcopy
hub / github.com/tinode/chat / stringSliceDelta

Function stringSliceDelta

server/utils.go:74–114  ·  view source on GitHub ↗

stringSliceDelta extracts the slices of added and removed strings from two slices: added := newSlice - (oldSlice & newSlice) -- present in new but missing in old removed := oldSlice - (oldSlice & newSlice) -- present in old but missing in new intersection := oldSlice & newSlice -- present in bo

(rold, rnew []string)

Source from the content-addressed store, hash-verified

72// removed := oldSlice - (oldSlice & newSlice) -- present in old but missing in new
73// intersection := oldSlice & newSlice -- present in both old and new
74func stringSliceDelta(rold, rnew []string) (added, removed, intersection []string) {
75 if len(rold) == 0 && len(rnew) == 0 {
76 return nil, nil, nil
77 }
78 if len(rold) == 0 {
79 return rnew, nil, nil
80 }
81 if len(rnew) == 0 {
82 return nil, rold, nil
83 }
84
85 sort.Strings(rold)
86 sort.Strings(rnew)
87
88 // Match old slice against the new slice and separate removed strings from added.
89 o, n := 0, 0
90 lold, lnew := len(rold), len(rnew)
91 for o < lold || n < lnew {
92 if o == lold || (n < lnew && rold[o] > rnew[n]) {
93 // Present in new, missing in old: added
94 added = append(added, rnew[n])
95 n++
96
97 } else if n == lnew || rold[o] < rnew[n] {
98 // Present in old, missing in new: removed
99 removed = append(removed, rold[o])
100 o++
101
102 } else {
103 // present in both
104 intersection = append(intersection, rold[o])
105 if o < lold {
106 o++
107 }
108 if n < lnew {
109 n++
110 }
111 }
112 }
113 return added, removed, intersection
114}
115
116// Process credentials for correctness: remove duplicate and unknown methods.
117// In case of duplicate methods only the first one satisfying valueRequired is kept.

Callers 8

TestStringSliceDeltaFunction · 0.85
loginMethod · 0.85
replyGetSubMethod · 0.85
replySetTagsMethod · 0.85
replyDelCredMethod · 0.85
replyCreateUserFunction · 0.85
replyUpdateUserFunction · 0.85
gcProxySessionsMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestStringSliceDeltaFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…