package index import "sync" type InvertedIndex struct { Mu sync.RWMutex Data map[string][]int DocNames map[int]string } func New() *InvertedIndex { return &InvertedIndex{ Data: make(map[string][]int), DocNames: make(map[int]string), } } func (idx *InvertedIndex) Add(docID int, title string, tokens []string) { idx.Mu.Lock() defer idx.Mu.Unlock() idx.DocNames[docID] = title seen := make(map[string]struct{}) for _, token := range tokens { if _, exists := seen[token]; !exists { idx.Data[token] = append(idx.Data[token], docID) seen[token] = struct{}{} } } }