Repository navigation
Expand file tree
/
Copy pathselection.v
More file actions
180 lines (172 loc) · 4.99 KB
/
Copy pathselection.v
File metadata and controls
180 lines (172 loc) · 4.99 KB
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
175
176
177
178
179
180
module vtl
// partition returns a copy with the value at kth in its sorted position and
// every preceding value less than or equal to it. Remaining values are
// unspecified, matching NumPy's default last-axis behavior.
pub fn partition[T](t &Tensor[T], kth int) !&Tensor[T] {
return partition_axis[T](t, [kth], -1)
}
// partition_axis partially sorts each one-dimensional slice along axis. kth
// positions are local to each slice; multiple positions can be requested in
// one pass without fully sorting the data.
pub fn partition_axis[T](t &Tensor[T], kths []int, axis int) !&Tensor[T] {
axis_index := normalize_sort_axis[T](t, axis)!
if kths.len == 0 {
return error('partition_axis: at least one kth position is required')
}
axis_size := t.shape[axis_index]
for kth in kths {
if kth < 0 || kth >= axis_size {
return error('partition_axis: kth ${kth} out of bounds for axis of size ${axis_size}')
}
}
mut result := empty[T](t.shape, memory: .row_major)
if t.size == 0 {
return result
}
line_count := t.size / axis_size
mut index := []int{len: t.rank()}
mut values := []T{len: axis_size}
for line in 0 .. line_count {
decode_sort_line(line, t.shape, axis_index, mut index)
for position in 0 .. axis_size {
index[axis_index] = position
values[position] = t.get[T](index)
}
for kth in kths {
select_value[T](mut values, kth)
}
for position, value in values {
index[axis_index] = position
result.set(index, value)
}
}
return result
}
// argpartition returns local indices that place kth at its sorted position
// along the last axis. The order of the other indices is unspecified.
pub fn argpartition[T](t &Tensor[T], kth int) !&Tensor[int] {
return argpartition_axis[T](t, [kth], -1)
}
// argpartition_axis partially orders local indices along axis. The returned
// tensor has the same shape as t and supports multiple kth positions.
pub fn argpartition_axis[T](t &Tensor[T], kths []int, axis int) !&Tensor[int] {
axis_index := normalize_sort_axis[T](t, axis)!
if kths.len == 0 {
return error('argpartition_axis: at least one kth position is required')
}
axis_size := t.shape[axis_index]
for kth in kths {
if kth < 0 || kth >= axis_size {
return error('argpartition_axis: kth ${kth} out of bounds for axis of size ${axis_size}')
}
}
mut result := empty[int](t.shape, memory: .row_major)
if t.size == 0 {
return result
}
line_count := t.size / axis_size
mut index := []int{len: t.rank()}
mut values := []T{len: axis_size}
mut positions := []int{len: axis_size}
for line in 0 .. line_count {
decode_sort_line(line, t.shape, axis_index, mut index)
for position in 0 .. axis_size {
index[axis_index] = position
values[position] = t.get[T](index)
positions[position] = position
}
for kth in kths {
select_positions[T](values, mut positions, kth)
}
for position, source_index in positions {
index[axis_index] = position
result.set(index, source_index)
}
}
return result
}
fn select_value[T](mut values []T, kth int) {
mut low := 0
mut high := values.len
for high - low > 1 {
middle := low + (high - low) / 2
pivot := selection_median[T](values[low], values[middle], values[high - 1])
mut less := low
mut current := low
mut greater := high
for current < greater {
comparison := compare_selection_values[T](values[current], pivot)
if comparison < 0 {
values[less], values[current] = values[current], values[less]
less++
current++
} else if comparison > 0 {
greater--
values[current], values[greater] = values[greater], values[current]
} else {
current++
}
}
if kth < less {
high = less
} else if kth >= greater {
low = greater
} else {
return
}
}
}
fn select_positions[T](values []T, mut positions []int, kth int) {
mut low := 0
mut high := positions.len
for high - low > 1 {
middle := low + (high - low) / 2
pivot := selection_median[T](values[positions[low]], values[positions[middle]],
values[positions[high - 1]])
mut less := low
mut current := low
mut greater := high
for current < greater {
comparison := compare_selection_values[T](values[positions[current]], pivot)
if comparison < 0 {
positions[less], positions[current] = positions[current], positions[less]
less++
current++
} else if comparison > 0 {
greater--
positions[current], positions[greater] = positions[greater], positions[current]
} else {
current++
}
}
if kth < less {
high = less
} else if kth >= greater {
low = greater
} else {
return
}
}
}
fn selection_median[T](a T, b T, c T) T {
if compare_selection_values[T](a, b) > 0 {
if compare_selection_values[T](b, c) > 0 {
return b
}
return if compare_selection_values[T](a, c) > 0 { c } else { a }
}
if compare_selection_values[T](a, c) > 0 {
return a
}
return if compare_selection_values[T](b, c) > 0 { c } else { b }
}
fn compare_selection_values[T](a T, b T) int {
$if T is bool {
if a == b {
return 0
}
return if a { 1 } else { -1 }
} $else {
return compare_sort_values[T](a, b)
}
}