MergeUnique returns a deduplicated slice that starts with base and appends any items from extra that are not already present in base. Order is preserved.
(base []T, extra ...T)
| 92 | // MergeUnique returns a deduplicated slice that starts with base and appends any |
| 93 | // items from extra that are not already present in base. Order is preserved. |
| 94 | func MergeUnique[T comparable](base []T, extra ...T) []T { |
| 95 | capacity := len(base) |
| 96 | if len(extra) <= int(^uint(0)>>1)-capacity { |
| 97 | capacity += len(extra) |
| 98 | } |
| 99 | |
| 100 | seen := make(map[T]struct{}, capacity) |
| 101 | result := make([]T, 0, capacity) |
| 102 | for _, item := range base { |
| 103 | if _, exists := seen[item]; !exists { |
| 104 | seen[item] = struct{}{} |
| 105 | result = append(result, item) |
| 106 | } |
| 107 | } |
| 108 | for _, item := range extra { |
| 109 | if _, exists := seen[item]; !exists { |
| 110 | seen[item] = struct{}{} |
| 111 | result = append(result, item) |
| 112 | } |
| 113 | } |
| 114 | if sliceutilLog.Enabled() { |
| 115 | sliceutilLog.Printf("MergeUnique: base=%d extra=%d result=%d", len(base), len(extra), len(result)) |
| 116 | } |
| 117 | return result |
| 118 | } |
| 119 | |
| 120 | // Exclude returns a new slice containing the items from base that do not appear |
| 121 | // in the exclude set. Order of remaining items is preserved. |