Skip to content

Commit

Permalink
Merge pull request #24 from abraunst/sort
Browse files Browse the repository at this point in the history
fix for #23
  • Loading branch information
AnderGray authored Feb 28, 2022
2 parents 8bd6eb6 + 4c75308 commit 11b2b57
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/interval_unions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
###
function intervalUnion(v)
x = IntervalUnion(v)
sort!(x.v)
sort!(x.v; by = left)
remove_empties!(x)
condense!(x)
closeGaps!(x)
Expand Down Expand Up @@ -75,7 +75,7 @@ function closeGaps!(x :: IntervalUnion, maxInts = MAXINTS[1])
while length(x.v) > maxInts # Global

# Complement code
v = sort(x.v)
v = sort(x.v; by = left)
vLo = left.(v)
vHi = right.(v)
vLo[1] == -? popfirst!(vLo) : pushfirst!(vHi, -∞)
Expand All @@ -89,14 +89,14 @@ function closeGaps!(x :: IntervalUnion, maxInts = MAXINTS[1])
popat!(x.v, i)
popat!(x.v, i-1)
push!(x.v, merge)
sort!(x.v)
sort!(x.v; by = left)
end
return x
end

function condense!(f::Function, x :: IntervalUnion)
v = x.v
sort!(v)
sort!(v; by = left)
i = 1
while i < length(v)
these = (i - 1) .+ findall(collect(!f(v[i] v[j]) for j in i:length(v)))
Expand Down
25 changes: 9 additions & 16 deletions src/set_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ hull(x :: IntervalUnion) = IntervalUnion([hull(x.v)])
# Computes the complement of a IntervalUnion
function complement(x :: IntervalUnion)

v = sort(x.v)
v = sort(x.v, by = left)
vLo = left.(v)
vHi = right.(v)

Expand Down Expand Up @@ -44,30 +44,23 @@ function bisect( x :: IntervalUnion, α = 0)

new = IntervalUnion(v bs[1] bs[2])
remove_empties!(new)
sort!(new.v)
sort!(new.v, by = left)
return new

end


function intersect(x :: IntervalUnion, y :: IntervalUnion)
intersects = [intersect(xv, yv) for xv in x.v, yv in y.v]
if all(intersects .== ∅); return ∅; end
return intervalUnion(intersects[:])
intersects = (intersect(xv, yv) for xv in x.v, yv in y.v)
return intervalUnion([x for x in intersects if !isempty(x)])
end

function intersect(x :: IntervalUnion, y :: Interval)
intersects = [intersect(xv, y) for xv in x.v]
if all(intersects .== ∅); return ∅; end
return intervalUnion(intersects[:])
end

function intersect(x :: Interval, y :: IntervalUnion)
intersects = [intersect(x, yv) for yv in y.v]
if all(intersects .== ∅); return ∅; end
return intervalUnion(intersects[:])
intersects = (intersect(xv, y) for xv in x.v)
return intervalUnion([x for x in intersects if !isempty(x)])
end

intersect(x :: Interval, y :: IntervalUnion) = intersect(y, x)

function setdiff(x :: IntervalUnion, y :: IntervalUnion)
yc = complement(y)
Expand Down Expand Up @@ -118,8 +111,8 @@ end

function ==(x :: IntervalUnion, y ::IntervalUnion)

xv = sort(x.v);
yv = sort(y.v);
xv = sort(x.v; by = left);
yv = sort(y.v; by = left);

return all(xv .== yv)

Expand Down
4 changes: 2 additions & 2 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ right(x :: Interval) = x.hi
left(x :: IntervalUnion) = left.(x.v)
right(x :: IntervalUnion) = right.(x.v)

isequal(x :: IntervalUnion, y ::IntervalUnion) = all(isequal.(sort(x.v),sort(y.v)))
isequal(x :: IntervalUnion, y ::IntervalUnion) = all(isequal.(sort(x.v; by = left),sort(y.v; by = left)))

function in( x:: IntervalUnion, y :: Vector{IntervalUnion})
for i =1:length(y)
Expand All @@ -26,7 +26,7 @@ function Base.show(io::IO, this::IntervalUnion)
elseif length(this.v) == 1;
print(io, "$(this.v[1])");
else
v = sort(this.v)
v = sort(this.v; by = left)
print(io, join(v, ""));
end
end
5 changes: 5 additions & 0 deletions test/set_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
@test !(b interval(0, 3))


a = interval(0,360)
b = intervalUnion([interval(x...) for x in [[2.79673, 8.91372], [39.2939, 56.4471], [67.1466, 70.1468], [80.2861, 93.2176], [110.055, 130.796], [139.832, 142.835], [151.597, 154.8], [175.313, 178.315], [188.508, 200.028], [234.445, 246.861], [272.586, 275.589], [286.656, 298.892], [315.723, 318.724], [333.795, 343.149], [362.617, 372.152], [388.38, 398.013], [420.947, 438.171], [448.538, 451.541], [470.079, 478.298], [494.766, 505.138], [515.836, 521.239], [539.73, 564.545], [582.778, 595.462], [617.058, 621.725], [631.357, 634.36], [644.533, 647.535], [666.469, 670.569], [688.826, 704.525], [713.249, 716.25], [740.816, 745.865], [764.322, 785.23], [794.686, 797.688], [809.239, 812.241], [821.892, 829.197], [845.522, 848.524], [860.125, 881.507], [896.821, 902.851], [913.554, 916.557], [925.626, 928.629] , [937.907, 940.909] , [953.773, 966.779] , [979.707, 982.709] , [992.479, 995.481] , [1004.19, 1007.21] , [1018.72, 1042.07] , [1055.94, 1071.58] , [1091.95, 1105.8] , [1125.04, 1129.45] , [1140.79, 1151.73] , [1160.92, 1172.92] , [1183.24, 1207.91] , [1224.19, 1233.81] , [1252.02, 1255.77] , [1273.52, 1276.54] , [1291.92, 1294.94] , [1307.19, 1319.72] , [1331.31, 1342.02] , [1354.02, 1361.17] , [1371.06, 1379.12] , [1395.09, 1398.11] , [1408.4, 1411.42] , [1421.66, 1430.58] , [1447.54, 1450.56] , [1462.94, 1480.01] , [1507.29, 1515.55] , [1526.6, 1529.62] , [1542.84, 1545.86] , [1559.41, 1562.43] , [1573.36, 1577.9] , [1597.89, 1603.73] , [1623.08, 1646.25] , [1669.9, 1685.05] , [1697.35, 1700.37] , [1722.14, 1748.25] , [1764.96, 1791.44] , [1804.68, 1808.32] , [1840.8, 1853.31] , [1865.21, 1868.23] , [1878.51, 1884.73] , [1901.24, 1904.97] , [1927.78, 1932.05] , [1949.6, 1954.92] , [1972, 1984.02] , [1997.24, 2000.25] , [2013.48, 2043.86] , [2056.4, 2059.43] , [2074.69, 2077.72] , [2086.82, 2089.85] , [2101.82, 2106.68] , [2116.44, 2119.47] , [2135.3, 2148.33] , [2162.94, 2173.99] , [2193.26, 2196.29] , [2206.51, 2225.08] , [2242.53, 2245.54] , [2254.69, 2257.7] , [2274.09, 2289.49] , [2312.13, 2336.16] , [2356.9, 2367.83] , [2376.17, 2383.41]]
])
@test ab a # fix to "Error in intersection" issue #23

end

0 comments on commit 11b2b57

Please sign in to comment.