fd82cee452
- Fix CryptoRandomString/CryptoRandomBytes callers (now return error) - Add missing DiffSlice[T] generic implementation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
41 lines
990 B
Go
41 lines
990 B
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package util
|
|
|
|
// DiffSlice compares two slices and returns elements added (in new but not old)
|
|
// and removed (in old but not new). Output is deduplicated.
|
|
func DiffSlice[T comparable](oldSlice, newSlice []T) (added, removed []T) {
|
|
oldSet := make(map[T]struct{}, len(oldSlice))
|
|
for _, v := range oldSlice {
|
|
oldSet[v] = struct{}{}
|
|
}
|
|
|
|
newSet := make(map[T]struct{}, len(newSlice))
|
|
for _, v := range newSlice {
|
|
newSet[v] = struct{}{}
|
|
}
|
|
|
|
addedSet := make(map[T]struct{})
|
|
for _, v := range newSlice {
|
|
if _, exists := oldSet[v]; !exists {
|
|
if _, dup := addedSet[v]; !dup {
|
|
added = append(added, v)
|
|
addedSet[v] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
removedSet := make(map[T]struct{})
|
|
for _, v := range oldSlice {
|
|
if _, exists := newSet[v]; !exists {
|
|
if _, dup := removedSet[v]; !dup {
|
|
removed = append(removed, v)
|
|
removedSet[v] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
return added, removed
|
|
}
|