branch.go raw

   1  package lattice
   2  
   3  // TritPath packs up to 16 trits, two bits each; trit 0 is the branch selector.
   4  // iskradb/lattice/coord.mx TritPath, which is the 16-wide form the storage
   5  // layer uses. iskra/branch.mx redeclares a second TritPath with the same name
   6  // but an 8-trit depth in the same package; the two cannot both be ported.
   7  type TritPath uint32
   8  
   9  // TritEnd is the terminator trit value.
  10  const TritEnd = 0
  11  
  12  // Trit returns the trit at position i.
  13  func (p TritPath) Trit(i int) uint8 { return uint8((p >> (uint(i) * 2)) & 0x3) }
  14  
  15  // Depth is the number of assigned trits before the first TritEnd.
  16  func (p TritPath) Depth() int {
  17  	for i := 0; i < 16; i++ {
  18  		if p.Trit(i) == TritEnd {
  19  			return i
  20  		}
  21  	}
  22  	return 16
  23  }
  24  
  25  // Branch returns the branch selector trit.
  26  func (p TritPath) Branch() uint8 { return p.Trit(0) }
  27  
  28  // MakeTritPath builds a TritPath from trit values 0-3.
  29  func MakeTritPath(trits ...uint8) TritPath {
  30  	var p TritPath
  31  	for i, t := range trits {
  32  		if i >= 16 {
  33  			break
  34  		}
  35  		p |= TritPath(t&0x3) << (uint(i) * 2)
  36  	}
  37  	return p
  38  }
  39  
  40  // BranchTritPath returns a path holding only the branch selector.
  41  func BranchTritPath(b Branch) TritPath { return TritPath(b) + 1 }
  42  
  43  // Parent drops the last trit of a path.
  44  func Parent(p TritPath) TritPath {
  45  	d := p.Depth()
  46  	if d <= 1 {
  47  		return 0
  48  	}
  49  	return p &^ (TritPath(0x3) << (uint(d-1) * 2))
  50  }
  51  
  52  // Child appends a trit to a path.
  53  func Child(p TritPath, dir uint8) TritPath {
  54  	d := p.Depth()
  55  	if d >= 16 {
  56  		return p
  57  	}
  58  	return p | TritPath(dir&0x3)<<(uint(d)*2)
  59  }
  60  
  61  // LCA returns the longest common prefix of two paths.
  62  func LCA(a, b TritPath) TritPath {
  63  	shared := sharedPrefix(a, b)
  64  	if shared == 0 {
  65  		return 0
  66  	}
  67  	return a & TritPath((1<<(uint(shared)*2))-1)
  68  }
  69  
  70  // Distance is the hop count between two paths through their common prefix.
  71  // Returns -1 when either path is unassigned (depth 0).
  72  func Distance(a, b TritPath) int {
  73  	da, db := a.Depth(), b.Depth()
  74  	if da == 0 || db == 0 {
  75  		return -1
  76  	}
  77  	shared := sharedPrefix(a, b)
  78  	return (da - shared) + (db - shared)
  79  }
  80  
  81  // Resolve replicates every trit after the branch selector mult times.
  82  func Resolve(p TritPath, mult uint8) TritPath {
  83  	if mult <= 1 {
  84  		return p
  85  	}
  86  	d := p.Depth()
  87  	if d <= 1 {
  88  		return p
  89  	}
  90  	out := TritPath(p.Trit(0))
  91  	pos := 1
  92  	for i := 1; i < d; i++ {
  93  		t := p.Trit(i)
  94  		for j := uint8(0); j < mult && pos < 16; j++ {
  95  			out |= TritPath(t&0x3) << (uint(pos) * 2)
  96  			pos++
  97  		}
  98  	}
  99  	return out
 100  }
 101  
 102  func sharedPrefix(a, b TritPath) int {
 103  	da, db := a.Depth(), b.Depth()
 104  	limit := da
 105  	if db < limit {
 106  		limit = db
 107  	}
 108  	shared := 0
 109  	for i := 0; i < limit; i++ {
 110  		if a.Trit(i) != b.Trit(i) {
 111  			break
 112  		}
 113  		shared++
 114  	}
 115  	return shared
 116  }
 117  
 118  // Code-lattice branch constants. iskra/branch.mx.
 119  const (
 120  	BranchType = 1
 121  	BranchFunc = 2
 122  	BranchData = 3
 123  
 124  	SubField  = 1
 125  	SubMethod = 2
 126  	SubEmbed  = 3
 127  
 128  	SubStatement  = 1
 129  	SubExpression = 2
 130  	SubControl    = 3
 131  
 132  	SubConst  = 1
 133  	SubVar    = 2
 134  	SubImport = 3
 135  )
 136