Source file src/simd/archsimd/_gen/specgen/index.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package specgen
     6  
     7  // indexKey identifies an API function or method by receiver type name and
     8  // method name. For package-level functions, recv is empty.
     9  type indexKey struct {
    10  	recv string
    11  	name string
    12  }
    13  
    14  // Index provides lookup of concrete spec functions by (receiver, name).
    15  type Index struct {
    16  	m     map[indexKey]*Func
    17  	funcs []*Func
    18  }
    19  
    20  // NewIndex builds an Index over the given slice of spec functions.
    21  func NewIndex(funcs []*Func) *Index {
    22  	m := make(map[indexKey]*Func, len(funcs))
    23  	for _, fn := range funcs {
    24  		var recv string
    25  		if fn.Recv.Type != nil {
    26  			recv = fn.Recv.Type.String()
    27  		}
    28  		m[indexKey{recv: recv, name: fn.Name}] = fn
    29  	}
    30  	return &Index{
    31  		m:     m,
    32  		funcs: funcs,
    33  	}
    34  }
    35  
    36  // Lookup finds the spec function for the given receiver and function/method name.
    37  // For package-level functions, recv should be "".
    38  func (idx *Index) Lookup(recv, name string) *Func {
    39  	if idx == nil {
    40  		return nil
    41  	}
    42  	return idx.m[indexKey{recv: recv, name: name}]
    43  }
    44  
    45  // Funcs returns all functions in the index.
    46  func (idx *Index) Funcs() []*Func {
    47  	if idx == nil {
    48  		return nil
    49  	}
    50  	return idx.funcs
    51  }
    52  
    53  // Len returns the number of functions in the index.
    54  func (idx *Index) Len() int {
    55  	if idx == nil {
    56  		return 0
    57  	}
    58  	return len(idx.m)
    59  }
    60  

View as plain text