github.com/cloudcentricdev/golang-tutorials/03@v0.0.0-20231018054503-b24024842337/skiplist/visualizer.go (about) 1 package skiplist 2 3 import "fmt" 4 5 const ( 6 ArrowShaft = "-" 7 ArrowHead = ">" 8 Arrow = ArrowShaft + ArrowHead 9 LowestLevel = 0 // index corresponding to the lowest level in SkipList 10 Padding = 2 // number of whitespaces surrounding displayed key 11 ) 12 13 type visualizer struct { 14 sl *SkipList 15 } 16 17 func (v *visualizer) visualize() string { 18 var output string 19 20 lowestLevel := v.extractLowestLevel() 21 22 for level := v.sl.height - 1; level >= 0; level-- { 23 output += fmt.Sprintf("L%02d ", level) 24 for i, next := 0, v.sl.head.tower[level]; next != nil; i, next = i+1, next.tower[level] { 25 var key string 26 for key = string(next.key); lowestLevel[i] != key; i++ { 27 output += v.paddedArrowShaft(len(lowestLevel[i])) 28 } 29 output += fmt.Sprintf("%v %v ", Arrow, key) 30 } 31 output += "\n" 32 } 33 34 return output 35 } 36 37 func (v *visualizer) extractLowestLevel() []string { 38 var lowestLevel []string 39 for next := v.sl.head.tower[LowestLevel]; next != nil; next = next.tower[LowestLevel] { 40 lowestLevel = append(lowestLevel, string(next.key)) 41 } 42 return lowestLevel 43 } 44 45 func (v *visualizer) paddedArrowShaft(keyLen int) string { 46 var str string 47 for i := 0; i < keyLen+len(Arrow)+Padding; i++ { 48 str += ArrowShaft 49 } 50 return str 51 }