-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
141 lines (124 loc) · 2.86 KB
/
data.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
137
138
139
140
141
package main
import (
"log"
"math/rand"
"time"
)
var (
sync <-chan time.Time
numOfComparison int
numOfSwaps int
dataProcessed bool
)
// Data is the interface to the sorting algorithms
type Data struct {
array []int
min, max int // For split and merge operations
}
var globalData Data
func (data *Data) Split(i int) (left, right *Data) {
left = &Data{
array: data.array[:i],
min: data.min,
max: i,
}
right = &Data{
array: data.array[i:],
min: i,
max: data.max,
}
log.Println("Split> i:", i, "data.min:", data.min, "data.max:", data.max)
return left, right
}
func (data *Data) Merge(l, r *Data) *Data {
left := make([]int, l.Len())
right := make([]int, r.Len())
results := make([]int, 0)
copy(left, l.array)
copy(right, r.array)
for len(left) > 0 || len(right) > 0 {
if len(left) > 0 && len(right) > 0 {
if left[0] <= right[0] {
numOfComparison++
results = append(results, left[0])
left = left[1:len(left)]
} else {
results = append(results, right[0])
right = right[1:len(right)]
}
} else if len(left) > 0 {
results = append(results, left[0])
left = left[1:len(left)]
} else if len(right) > 0 {
results = append(results, right[0])
right = right[1:len(right)]
}
}
log.Println("Merge> l.min:", l.min, "r.max:", r.max)
d := &Data{
array: globalData.array[l.min:r.max],
min: l.min,
max: r.max,
}
copy(d.array, results)
return d
}
func (data *Data) Swap(i, j int) {
<-sync
setTestedElements(i, j)
numOfSwaps++
data.array[i], data.array[j] = data.array[j], data.array[i]
}
func (data *Data) Less(i, j int) bool {
<-sync
setTestedElements(i, j)
if debug {
log.Printf("(i, j) = (%v, %v)\n", i, j)
}
numOfComparison++
return data.array[i] < data.array[j]
}
func (data *Data) Len() int {
return len(data.array)
}
var prevI, prevJ int
var firstComparison = true
func setTestedElements(i, j int) {
if firstComparison {
firstComparison = false
prevI, prevJ = i, j
}
histogram[globalData.array[prevI]].isComparing = false
histogram[globalData.array[prevJ]].isComparing = false
histogram[globalData.array[i]].isComparing = true
histogram[globalData.array[j]].isComparing = true
prevI, prevJ = i, j
}
func (data *Data) endOfWork() {
histogram[data.array[prevI]].isComparing = false
histogram[data.array[prevJ]].isComparing = false
dataProcessed = true
elapsedTime = time.Since(runningTime)
}
func newData(s <-chan time.Time) {
if dataAsc {
globalData.array = make([]int, size)
for i := 0; i < size; i++ {
globalData.array[i] = i
}
} else if dataDesc {
globalData.array = make([]int, size)
for i := 0; i < size; i++ {
globalData.array[i] = size - i - 1
}
} else {
if dataRndSeed > 0 {
rand.Seed(dataRndSeed)
} else {
rand.Seed(time.Now().UnixNano())
}
globalData.array = rand.Perm(size)
}
globalData.min, globalData.max = 0, size
sync = s
}