@@ -20,13 +20,14 @@ nextnext3d(c) = (c + 1) % 3 + 1
20
20
const forward = true
21
21
const backward = false
22
22
23
- function select! (direction, coord, v:: Array{T,1} , k:: Integer , lo:: Integer , hi:: Integer ) where T<: AbstractVector
23
+ function select! (direction, coord, v:: Array{T,1} , k:: Integer , lo:: Integer , hi:: Integer , carry :: CT ) where { T<: AbstractVector , CT}
24
24
# lo <= k <= hi || error("select index $k is out of range $lo:$hi")
25
25
if direction == forward
26
26
@inbounds while lo < hi
27
27
if isone (hi- lo)
28
28
if v[hi][coord] < v[lo][coord]
29
29
v[lo], v[hi] = v[hi], v[lo]
30
+ if CT != = Nothing; carry[lo], carry[hi] = carry[hi], carry[lo]; end
30
31
end
31
32
return # v[k]
32
33
end
@@ -38,6 +39,7 @@ function select!(direction, coord, v::Array{T,1}, k::Integer, lo::Integer, hi::I
38
39
while pivot_elt < v[j][coord]; j -= 1 ; end
39
40
i <= j || break
40
41
v[i], v[j] = v[j], v[i]
42
+ if CT != = Nothing; carry[i], carry[j] = carry[j], carry[i]; end
41
43
i += 1 ; j -= 1
42
44
end
43
45
if k <= j
@@ -53,6 +55,7 @@ function select!(direction, coord, v::Array{T,1}, k::Integer, lo::Integer, hi::I
53
55
if isone (hi- lo)
54
56
if v[hi][coord] > v[lo][coord]
55
57
v[lo], v[hi] = v[hi], v[lo]
58
+ if CT != = Nothing; carry[lo], carry[hi] = carry[hi], carry[lo]; end
56
59
end
57
60
return # v[k]
58
61
end
@@ -64,6 +67,7 @@ function select!(direction, coord, v::Array{T,1}, k::Integer, lo::Integer, hi::I
64
67
while pivot_elt > v[j][coord]; j -= 1 ; end
65
68
i <= j || break
66
69
v[i], v[j] = v[j], v[i]
70
+ if CT != = Nothing; carry[i], carry[j] = carry[j], carry[i]; end
67
71
i += 1 ; j -= 1
68
72
end
69
73
if k <= j
100
104
# return a
101
105
# end
102
106
103
- function hilbertsort! (directionx, directiony, directionz, coordinate, a:: Vector , lo:: Integer , hi:: Integer , lim:: Integer = 8 )
107
+ function hilbertsort! (directionx, directiony, directionz, coordinate, a:: Vector , lo:: Integer , hi:: Integer , lim:: Integer , carry )
104
108
hi- lo <= lim && return a
105
109
106
110
i4 = (lo+ hi)>>> 1
@@ -111,27 +115,31 @@ function hilbertsort!(directionx, directiony, directionz, coordinate, a::Vector,
111
115
i5 = (i4+ i6)>>> 1
112
116
i7 = (i6+ hi)>>> 1
113
117
114
- select! (directionx, coordinate, a, i4, lo, hi)
115
- select! (directiony, next3d (coordinate), a, i2, lo, i4)
116
- select! (directionz, nextnext3d (coordinate), a, i1, lo, i2)
117
- select! (! directionz, nextnext3d (coordinate), a, i3, i2, i4)
118
- select! (! directiony, next3d (coordinate), a, i6, i4, hi)
119
- select! (directionz, nextnext3d (coordinate), a, i5, i4, i6)
120
- select! (! directionz, nextnext3d (coordinate), a, i7, i6, hi)
121
-
122
- hilbertsort! ( directionz, directionx, directiony, nextnext3d (coordinate), a, lo, i1, lim)
123
- hilbertsort! ( directiony, directionz, directionx, next3d (coordinate), a, i1, i2, lim)
124
- hilbertsort! ( directiony, directionz, directionx, next3d (coordinate), a, i2, i3, lim)
125
- hilbertsort! ( directionx, ! directiony, ! directionz, coordinate, a, i3, i4, lim)
126
- hilbertsort! ( directionx, ! directiony, ! directionz, coordinate, a, i4, i5, lim)
127
- hilbertsort! (! directiony, directionz, ! directionx, next3d (coordinate), a, i5, i6, lim)
128
- hilbertsort! (! directiony, directionz, ! directionx, next3d (coordinate), a, i6, i7, lim)
129
- hilbertsort! (! directionz, ! directionx, directiony, nextnext3d (coordinate), a, i7, hi, lim)
118
+ select! (directionx, coordinate, a, i4, lo, hi, carry )
119
+ select! (directiony, next3d (coordinate), a, i2, lo, i4, carry )
120
+ select! (directionz, nextnext3d (coordinate), a, i1, lo, i2, carry )
121
+ select! (! directionz, nextnext3d (coordinate), a, i3, i2, i4, carry )
122
+ select! (! directiony, next3d (coordinate), a, i6, i4, hi, carry )
123
+ select! (directionz, nextnext3d (coordinate), a, i5, i4, i6, carry )
124
+ select! (! directionz, nextnext3d (coordinate), a, i7, i6, hi, carry )
125
+
126
+ hilbertsort! ( directionz, directionx, directiony, nextnext3d (coordinate), a, lo, i1, lim, carry )
127
+ hilbertsort! ( directiony, directionz, directionx, next3d (coordinate), a, i1, i2, lim, carry )
128
+ hilbertsort! ( directiony, directionz, directionx, next3d (coordinate), a, i2, i3, lim, carry )
129
+ hilbertsort! ( directionx, ! directiony, ! directionz, coordinate, a, i3, i4, lim, carry )
130
+ hilbertsort! ( directionx, ! directiony, ! directionz, coordinate, a, i4, i5, lim, carry )
131
+ hilbertsort! (! directiony, directionz, ! directionx, next3d (coordinate), a, i5, i6, lim, carry )
132
+ hilbertsort! (! directiony, directionz, ! directionx, next3d (coordinate), a, i6, i7, lim, carry )
133
+ hilbertsort! (! directionz, ! directionx, directiony, nextnext3d (coordinate), a, i7, hi, lim, carry )
130
134
131
135
return a
132
136
end
133
137
134
138
# hilbertsort!(a::Array{T,1}) where {T<:AbstractPoint2D} = hilbertsort!(backward, backward, coordinatey, a, 1, length(a))
135
139
# hilbertsort!(a::Array{T,1}, lo::Int64, hi::Int64, lim::Int64) where {T<:AbstractPoint2D} = hilbertsort!(backward, backward, coordinatey, a, lo, hi, lim)
136
- hilbertsort! (a:: Vector ) = hilbertsort! (backward, backward, backward, coordinatez, a, 1 , length (a))
140
+ """
141
+ Hilbert Sorting. If `carry` is specified, this array will be permuted in line with the
142
+ specified array.
143
+ """
144
+ hilbertsort! (a:: Vector , carry= nothing ) = hilbertsort! (backward, backward, backward, coordinatez, a, 1 , length (a), 8 , carry)
137
145
hilbertsort! (a:: Vector , lo:: Int64 , hi:: Int64 , lim:: Int64 ) = hilbertsort! (backward, backward, backward, coordinatey, a, lo, hi, lim)
0 commit comments