MCPcopy
hub / github.com/PuerkitoBio/goquery / appendWithoutDuplicates

Function appendWithoutDuplicates

utilities.go:131–159  ·  view source on GitHub ↗

Appends the new nodes to the target slice, making sure no duplicate is added. There is no check to the original state of the target slice, so it may still contain duplicates. The target slice is returned because append() may create a new underlying array. If targetSet is nil, a local set is created

(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool)

Source from the content-addressed store, hash-verified

129// a new underlying array. If targetSet is nil, a local set is created with the
130// target if len(target) + len(nodes) is greater than minNodesForSet.
131func appendWithoutDuplicates(target []*html.Node, nodes []*html.Node, targetSet map[*html.Node]bool) []*html.Node {
132 // if there are not that many nodes, don't use the map, faster to just use nested loops
133 // (unless a non-nil targetSet is passed, in which case the caller knows better).
134 if targetSet == nil && len(target)+len(nodes) < minNodesForSet {
135 for _, n := range nodes {
136 if !isInSlice(target, n) {
137 target = append(target, n)
138 }
139 }
140 return target
141 }
142
143 // if a targetSet is passed, then assume it is reliable, otherwise create one
144 // and initialize it with the current target contents.
145 if targetSet == nil {
146 targetSet = make(map[*html.Node]bool, len(target))
147 for _, n := range target {
148 targetSet[n] = true
149 }
150 }
151 for _, n := range nodes {
152 if !targetSet[n] {
153 target = append(target, n)
154 targetSet[n] = true
155 }
156 }
157
158 return target
159}
160
161// Loop through a selection, returning only those nodes that pass the predicate
162// function.

Callers 2

mapNodesFunction · 0.85
AddNodesMethod · 0.85

Calls 1

isInSliceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…