1 package lattice
2 3 // AdjLink is a translation or semantic edge between entries.
4 //
5 // This type is carried over from go-ref. iskra/meta.mx removed
6 // AdjCount/AdjOffset from its MetaEntry and says cross-stage adjacency is
7 // key-implicit while cross-branch adjacency lives in Record.Link. Nothing in
8 // an .iskr or .meta file written by the shipped moxie code stores AdjLink
9 // entries, so the only tree that can hold them is one this package built in
10 // memory.
11 type AdjLink struct {
12 TargetMeta uint32
13 LinkType uint8
14 _pad [3]byte
15 } // 8 bytes
16 17 // AddAdjDirect appends an edge to Tree.Adj, skipping a duplicate.
18 func (t *Tree) AddAdjDirect(src *MetaEntry, tgt uint32) {
19 if src.AdjCount > 0 {
20 end := int(src.AdjOffset) + int(src.AdjCount)
21 if end <= len(t.Adj) {
22 for _, l := range t.Adj[src.AdjOffset:end] {
23 if l.TargetMeta == tgt {
24 return
25 }
26 }
27 }
28 }
29 newIdx := len(t.Adj)
30 t.Adj = append(t.Adj, AdjLink{TargetMeta: tgt})
31 if src.AdjCount == 0 {
32 src.AdjOffset = uint32(newIdx)
33 }
34 src.AdjCount++
35 }
36 37 // RemoveAdj drops the edge from src to tgt by swapping in the last edge of the
38 // block.
39 func (t *Tree) RemoveAdj(src *MetaEntry, tgt uint32) bool {
40 if src.AdjCount == 0 {
41 return false
42 }
43 end := int(src.AdjOffset) + int(src.AdjCount)
44 if end > len(t.Adj) {
45 end = len(t.Adj)
46 }
47 if int(src.AdjOffset) > end {
48 return false
49 }
50 links := t.Adj[src.AdjOffset:end]
51 for i := range links {
52 if links[i].TargetMeta == tgt {
53 last := len(links) - 1
54 if i != last {
55 links[i] = links[last]
56 }
57 src.AdjCount--
58 return true
59 }
60 }
61 return false
62 }
63 64 // AdjLinks returns the edge block for a MetaEntry.
65 func (t *Tree) AdjLinks(src *MetaEntry) []AdjLink {
66 if src.AdjCount == 0 {
67 return nil
68 }
69 end := int(src.AdjOffset) + int(src.AdjCount)
70 if end > len(t.Adj) {
71 end = len(t.Adj)
72 }
73 if int(src.AdjOffset) > end {
74 return nil
75 }
76 return t.Adj[src.AdjOffset:end]
77 }
78