Avoid repeated glossary selection and membership work
This commit is contained in:
@@ -18,9 +18,14 @@ type identity struct{ collection, id string }
|
||||
|
||||
// Select retains the first occurrence of each selected collection entry.
|
||||
func Select(collections map[string]config.GlossaryCollection, names []string) []Entry {
|
||||
visited := make(map[string]struct{})
|
||||
seen := make(map[identity]struct{})
|
||||
var selected []Entry
|
||||
for _, name := range names {
|
||||
if _, exists := visited[name]; exists {
|
||||
continue
|
||||
}
|
||||
visited[name] = struct{}{}
|
||||
for _, item := range collections[name].Entries {
|
||||
key := identity{collection: name, id: item.ID}
|
||||
if _, exists := seen[key]; exists {
|
||||
|
||||
@@ -29,6 +29,11 @@ func Vocabulary(entries, excluded []Entry) []Term {
|
||||
}
|
||||
}
|
||||
}
|
||||
type contribution struct {
|
||||
spelling string
|
||||
identity Identity
|
||||
}
|
||||
seen := make(map[contribution]struct{})
|
||||
indices := make(map[string]int)
|
||||
var terms []Term
|
||||
for _, entry := range entries {
|
||||
@@ -38,23 +43,18 @@ func Vocabulary(entries, excluded []Entry) []Term {
|
||||
if _, excluded := exclusions[key]; excluded {
|
||||
continue
|
||||
}
|
||||
member := contribution{spelling: key, identity: id}
|
||||
if _, exists := seen[member]; exists {
|
||||
continue
|
||||
}
|
||||
seen[member] = struct{}{}
|
||||
index, exists := indices[key]
|
||||
if !exists {
|
||||
index = len(terms)
|
||||
indices[key] = index
|
||||
terms = append(terms, Term{Spelling: spelling})
|
||||
}
|
||||
identities := terms[index].Identities
|
||||
found := false
|
||||
for _, existing := range identities {
|
||||
if existing == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
terms[index].Identities = append(identities, id)
|
||||
}
|
||||
terms[index].Identities = append(terms[index].Identities, id)
|
||||
}
|
||||
}
|
||||
return append(terms, exempt...)
|
||||
|
||||
@@ -10,7 +10,8 @@ func TestVocabularyKeepsContributorsAndExemptsOnlyActiveSpellings(t *testing.T)
|
||||
active := []Entry{{Collection: "spells", ID: "shield", Name: "Shield", Aliases: []string{"Ward"}}}
|
||||
protected := []Entry{
|
||||
active[0],
|
||||
{Collection: "items", ID: "shield", Name: "shield", Aliases: []string{"Aegis"}, Plural: &plural},
|
||||
{Collection: "items", ID: "shield", Name: "shield", Aliases: []string{"Aegis", "SHIELD"}, Plural: &plural},
|
||||
active[0],
|
||||
}
|
||||
full := Vocabulary(protected, nil)
|
||||
if !reflect.DeepEqual(full[0].Identities, []Identity{{Collection: "spells", ID: "shield"}, {Collection: "items", ID: "shield"}}) {
|
||||
|
||||
Reference in New Issue
Block a user