forked from arafatkatze/DataViz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_playground_code_go
136 lines (114 loc) · 3.78 KB
/
test_playground_code_go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Package binaryheap implements a binary heap backed by array list.
//
// Comparator defines this heap as either min or max heap.
//
// Structure is not thread safe.
//
// References: http://en.wikipedia.org/wiki/Binary_heap
package main
import (
"fmt"
"strconv"
utilsRaw "github.com/emirpasic/gods/utils"
"github.com/pennz/DataViz/trees"
"github.com/pennz/DataViz/trees/binaryheap"
"github.com/pennz/DataViz/viz"
"gonum.org/v1/gonum/graph/formats/dot"
)
func assertTreeImplementationV() {
var _ trees.Tree = (*HeapV)(nil)
}
// HeapV holds elements in an array-list, and for visualizer
type HeapV struct {
*binaryheap.Heap
stepper viz.Stepper
enabledV bool
}
// EnableV enable visualization related data
func (heap *HeapV) EnableV() {
heap.enabledV = true
}
// SSteps why not direct use not possible?
func (heap *HeapV) SSteps() (gs []string, err error) {
gs, err = heap.stepper.Steps()
return
}
// NewWithV instantiates a new empty heap tree with the custom comparator.
func NewWithV(comparator utilsRaw.Comparator) *HeapV {
return &HeapV{binaryheap.NewWith(comparator), viz.NewVisualizerStepper(), false}
}
// NewWithIntComparatorV instantiates a new empty heap with the IntComparator, i.e. elements are of type int.
func NewWithIntComparatorV() *HeapV {
return &HeapV{binaryheap.NewWithIntComparator(), viz.NewVisualizerStepper(), false}
}
// NewWithStringComparatorV instantiates a new empty heap with the StringComparator, i.e. elements are of type string.
func NewWithStringComparatorV() *HeapV {
return &HeapV{binaryheap.NewWithStringComparator(), viz.NewVisualizerStepper(), false}
}
// Push adds a value onto the heap and bubbles it up accordingly.
func (heap *HeapV) Push(values ...interface{}) {
heap.Heap.Push(values...)
if heap.enabledV {
heap.stepper.Record(heap.visualize())
}
}
// Pop removes top element on heap and returns it, or nil if heap is empty.
// Second return parameter is true, unless the heap was empty and there was nothing to pop.
func (heap *HeapV) Pop() (value interface{}, ok bool) {
value, ok = heap.Heap.Pop()
if heap.enabledV {
heap.stepper.Record(heap.visualize())
}
return
}
// Check that the index is within bounds of the list
func (heap *HeapV) withinRange(index int) bool {
return index >= 0 && index < heap.Heap.Size()
}
func listGet(list []interface{}, index int, size int) (interface{}, bool) {
if !(index >= 0 && index < size) {
return nil, false
}
return list[index], true
}
func (heap *HeapV) visualize() string {
size := heap.Heap.Size()
indexValueMap := make(map[int]interface{})
dotString := "digraph graphname{bgcolor=white;"
stringValues := []string{}
list := heap.Heap.Values()
for i := 0; i < (2 * size); i++ {
value, exists := listGet(list, i, size)
if exists {
indexValueMap[i] = value // Anybody who exists is connected to parent
if i != 0 {
dotString += (strconv.Itoa((i-1)/2) + " -> " + strconv.Itoa((i)) + ";")
stringValues = append(stringValues, fmt.Sprintf("%v", value))
dotString += (strconv.Itoa(i) + "[color=steelblue1, style=filled, fillcolor = steelblue1, fontcolor=white,label=" + stringValues[len(stringValues)-1] + "];")
} else {
stringValues = append(stringValues, fmt.Sprintf("%v", value))
dotString += (strconv.Itoa(i) + "[color=steelblue1, style=filled, fillcolor = steelblue1, fontcolor=white,label=" + stringValues[len(stringValues)-1] + "];")
}
}
}
dotString += "}"
return dotString
}
func astFormatOut(s string) {
ast, err := dot.ParseString(s)
if err == nil {
fmt.Println(ast)
}
fmt.Print("")
}
func main() {
l := arraylist.New(),
avw := NewAlgVisualWrapper()
got := avw.Wrap(l) // already a pointer now...
fmt.Println(avw.Visualize())
avw.Call("Add", 3)
avw.Call("Add", 4)
avw.Call("Add", 5)
avw.Call("Swap", 0, 1)
log.Println(avw.Visualize())
}