package lattice // AdjLink is a translation or semantic edge between entries. // // This type is carried over from go-ref. iskra/meta.mx removed // AdjCount/AdjOffset from its MetaEntry and says cross-stage adjacency is // key-implicit while cross-branch adjacency lives in Record.Link. Nothing in // an .iskr or .meta file written by the shipped moxie code stores AdjLink // entries, so the only tree that can hold them is one this package built in // memory. type AdjLink struct { TargetMeta uint32 LinkType uint8 _pad [3]byte } // 8 bytes // AddAdjDirect appends an edge to Tree.Adj, skipping a duplicate. func (t *Tree) AddAdjDirect(src *MetaEntry, tgt uint32) { if src.AdjCount > 0 { end := int(src.AdjOffset) + int(src.AdjCount) if end <= len(t.Adj) { for _, l := range t.Adj[src.AdjOffset:end] { if l.TargetMeta == tgt { return } } } } newIdx := len(t.Adj) t.Adj = append(t.Adj, AdjLink{TargetMeta: tgt}) if src.AdjCount == 0 { src.AdjOffset = uint32(newIdx) } src.AdjCount++ } // RemoveAdj drops the edge from src to tgt by swapping in the last edge of the // block. func (t *Tree) RemoveAdj(src *MetaEntry, tgt uint32) bool { if src.AdjCount == 0 { return false } end := int(src.AdjOffset) + int(src.AdjCount) if end > len(t.Adj) { end = len(t.Adj) } if int(src.AdjOffset) > end { return false } links := t.Adj[src.AdjOffset:end] for i := range links { if links[i].TargetMeta == tgt { last := len(links) - 1 if i != last { links[i] = links[last] } src.AdjCount-- return true } } return false } // AdjLinks returns the edge block for a MetaEntry. func (t *Tree) AdjLinks(src *MetaEntry) []AdjLink { if src.AdjCount == 0 { return nil } end := int(src.AdjOffset) + int(src.AdjCount) if end > len(t.Adj) { end = len(t.Adj) } if int(src.AdjOffset) > end { return nil } return t.Adj[src.AdjOffset:end] }