-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgenerators.go
174 lines (141 loc) · 3.26 KB
/
generators.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2013 Zhen, LLC. http://zhen.io. All rights reserved.
* Use of this source code is governed by the Apache 2.0 license.
*
*/
package generators
import (
"bytes"
"encoding/binary"
"errors"
"github.com/willf/bitset"
"math/rand"
"sort"
)
const (
c1 int64 = 0xcc9e2d51
c2 int64 = 0x1b873593
)
func GenerateUniformInBytes(N, max int) *bytes.Buffer {
data := GenerateUniform(N, max)
b := make([]byte, N*4)
for i := 0; i < N; i++ {
binary.LittleEndian.PutUint32(b[i*4:], uint32(data[i]))
}
return bytes.NewBuffer(b)
}
func GenerateClusteredInBytes(N, max int) *bytes.Buffer {
data := GenerateClustered(N, max)
b := make([]byte, N*4)
for i := 0; i < N; i++ {
binary.LittleEndian.PutUint32(b[i*4:], uint32(data[i]))
}
return bytes.NewBuffer(b)
}
func GenerateUniform(N, max int) []int32 {
if N*2 > max {
return negate(GenerateUniform(max-N, max), max)
}
if 2048*N > max {
r, _ := generateUniformBitmap(N, max)
return r
}
r, _ := generateUniformHash(N, max)
return r
}
func GenerateClustered(N, max int) []int32 {
ans := make([]int32, N)
fillClustered(ans, 0, N, 0, max)
return ans
}
func fillUniform(ans []int32, offset, length, min, max int) {
v := GenerateUniform(length, max-min)
for k := 0; k < len(v); k++ {
ans[k+offset] = int32(min) + v[k]
}
}
func fillClustered(ans []int32, offset, length, min, max int) {
btwn := max - min
if btwn == length || length <= 10 {
fillUniform(ans, offset, length, min, max)
return
}
r := rand.New(rand.NewSource(c1))
cut := length / 2
if btwn-length-1 > 0 {
cut += int(r.Int31n(int32(btwn - length - 1)))
}
p := r.Float64()
if p < 0.25 {
fillUniform(ans, offset, length/2, min, min+cut)
fillClustered(ans, offset+length/2, length-length/2, min+cut, max)
} else if p < 0.5 {
fillClustered(ans, offset, length/2, min, min+cut)
fillUniform(ans, offset+length/2, length-length/2, min+cut, max)
} else {
fillClustered(ans, offset, length/2, min, min+cut)
fillClustered(ans, offset+length/2, length-length/2, min+cut, max)
}
}
func negate(x []int32, max int) []int32 {
ans := make([]int32, max-len(x))
var i, c int32
for j := 0; j < len(x); j++ {
v := x[j]
for ; i < v; i++ {
ans[c] = i
c += 1
}
i += 1
}
for int(c) < len(ans) {
ans[c] = i
c += 1
i += 1
}
return ans
}
func generateUniformBitmap(N, max int) ([]int32, error) {
if N > max {
return nil, errors.New("encoding/generateUniformBitmap: N > max, not possible")
}
r := rand.New(rand.NewSource(c1))
ans := make([]int32, N)
bs := bitset.New(uint(max))
cardinality := uint(0)
for int(cardinality) < N {
v := r.Int31n(int32(max))
if !bs.Test(uint(v)) {
bs.Set(uint(v))
cardinality += 1
}
}
for i, c := int32(0), 0; c < N; i++ {
if bs.Test(uint(i)) {
ans[c] = i
c += 1
}
}
return ans, nil
}
func generateUniformHash(N, max int) ([]int32, error) {
if N > max {
return nil, errors.New("encoding/generateUniformBitmap: N > max, not possible")
}
r := rand.New(rand.NewSource(c2))
ans := make([]int32, N)
s := make(map[int]bool)
for len(s) < N {
s[int(r.Int31n(int32(max)))] = true
}
c := 0
tmpans := make([]int, N)
for k, _ := range s {
tmpans[c] = k
}
sort.Ints(tmpans)
for i := 0; i < len(tmpans); i++ {
ans[i] = int32(tmpans[i])
}
return ans, nil
}