package iskra import "git.smesh.lol/iskradb/lattice" // SubLattice is a domain-scoped view of an iskradb tree. // All keys in a sub-lattice share the same domain byte, ensuring // non-collision with other domains in the same physical tree. type SubLattice struct { Tree *lattice.Tree Pool []byte Domain uint8 } // NewSubLattice creates a SubLattice view over an existing tree. func NewSubLattice(tree *lattice.Tree, pool []byte, domain uint8) SubLattice { return SubLattice{Tree: tree, Pool: pool, Domain: domain} } // MakeKey returns the key for (word, coord) in this sub-lattice's domain. // The word is normalized via the registered KeyNormalizer for the domain. func (sl SubLattice) MakeKey(coord uint64, word string) lattice.Key { return MakeKey(sl.Domain, coord, NormalizeKey(sl.Domain, word)) } // Translate finds the best match for word in dstDomain by: // 1. Looking up word in src sub-lattice at coord // 2. Following Record.Link[0] to the primary cross-domain translation // 3. Reading the target form from the dst sub-lattice record // // coord should encode the semantic/morph context of word in the source domain. // RelaxCoord is applied if the exact coord is not found. func Translate(src, dst SubLattice, word string, coord uint64) string { norm := NormalizeKey(src.Domain, word) branches := [3]lattice.Branch{lattice.Bnoun, lattice.Bverb, lattice.Bmodifier} for _, c := range RelaxCoord(coord) { key := MakeKey(src.Domain, c, norm) for _, b := range branches { ri := src.Tree.LookupRecIdx(b, key) if ri == lattice.NullRec { continue } rec := src.Tree.GetRecord(ri) if rec == nil || rec.Link[0] == lattice.NullRec { continue } dst_rec := dst.Tree.GetRecord(rec.Link[0]) if dst_rec == nil { continue } n := int(dst_rec.Inline[23]) if n > 0 && n <= 23 { return string(dst_rec.Inline[:n]) } if dst_rec.DataFile == 1 && dst_rec.DataLen > 0 { end := dst_rec.DataOff + dst_rec.DataLen if int(end) <= len(dst.Pool) { return string(dst.Pool[dst_rec.DataOff:end]) } } } } return "" } // Compose registers src as a sub-lattice within dst's tree by sharing the // physical tree. Since domain bytes differ, keys cannot collide. // Both sub-lattices must use the same physical *lattice.Tree. // This is a conceptual operation - the tree is already shared if both // SubLattices were created from the same tree pointer. func Compose(a, b SubLattice) bool { return a.Tree == b.Tree }