AddNode upserts a node by ID. Re-adding an existing ID updates its display fields and keeps the larger Weight, never dropping edges. Empty IDs and titles are ignored. Returns the stored node.
(n Node)
| 70 | // fields and keeps the larger Weight, never dropping edges. Empty IDs and |
| 71 | // titles are ignored. Returns the stored node. |
| 72 | func (g *Graph) AddNode(n Node) *Node { |
| 73 | if strings.TrimSpace(n.ID) == "" { |
| 74 | return nil |
| 75 | } |
| 76 | if existing, ok := g.nodes[n.ID]; ok { |
| 77 | if n.Title != "" { |
| 78 | existing.Title = n.Title |
| 79 | } |
| 80 | if n.Summary != "" { |
| 81 | existing.Summary = n.Summary |
| 82 | } |
| 83 | if n.Weight > existing.Weight { |
| 84 | existing.Weight = n.Weight |
| 85 | } |
| 86 | return existing |
| 87 | } |
| 88 | cp := n |
| 89 | g.nodes[n.ID] = &cp |
| 90 | return &cp |
| 91 | } |
| 92 | |
| 93 | // AddEdge adds (or reinforces) an undirected edge. It is a no-op when either |
| 94 | // endpoint is missing or when a == b, so callers can wire edges optimistically |
no outgoing calls