Skip to content

Commit

Permalink
fixed some things, got more segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
olynch committed Nov 21, 2022
1 parent 902efbf commit 287cd55
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/acsets/ACSetInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ function view_or_slice end
@inline view_or_slice(x::AbstractVector, ::Colon) = x
@inline Base.@propagate_inbounds view_or_slice(x::AbstractVector, i) = @view x[i]

collect_or_id(i) = i
collect_or_id(xs::AbstractVector{T}) where {T} = T[xs...]

function subpart(acs, part, names::AbstractVector{Symbol})
foldl(names, init=part) do part, name
subpart(acs, part, name)
collect_or_id(subpart(acs, part, name))
end
end

subpart(acs, names::AbstractVector{Symbol}) =
subpart(acs, subpart(acs, names[1]), names[2:end])
subpart(acs, Int[subpart(acs, names[1])...], names[2:end])

Base.getindex(acs::ACSet, part, name) = subpart(acs, part, name)
Base.getindex(acs::ACSet, name) = subpart(acs, name)
Expand Down
24 changes: 18 additions & 6 deletions src/acsets/Mappings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const VecMapStorage{T} = Union{AbstractPartialVector{T}, AbstractVector{T}}

struct VecMap{T, V} <: Mapping{Int, T}
v::V
function VecMap{T,V}(v::V) where {T,V}
new{T,V}(v)
end
end

VecMap{T,V}() where {T,V} = VecMap{T,V}(V())
Expand Down Expand Up @@ -65,7 +68,7 @@ function extend!(v::AbstractVector, n::Int)
append!(v, Iterators.map(_ -> nothing, Base.OneTo(k)))
end

@propagate_inbounds function Base.getindex(m::VecMap, i::Int)
@propagate_inbounds function _get_or_nothing(m::VecMap, i::Int)
@boundscheck begin
if i > length(m.v)
return nothing
Expand All @@ -74,8 +77,17 @@ end
@inbounds m.v[i]
end

@propagate_inbounds function Base.getindex(m::VecMap{T}, i::Int) where {T}
x = _get_or_nothing(m, i)
if isnothing(x)
throw(BoundsError(m, i))
else
x::T
end
end

function Base.get(m::VecMap, i::Int, def)
v = m[i]
v = _get_or_nothing(m, i)
if isnothing(v)
def
else
Expand All @@ -84,7 +96,7 @@ function Base.get(m::VecMap, i::Int, def)
end

function Base.get(f::Any, m::VecMap, i::Int)
v = m[i]
v = _get_or_nothing(m, i)
if isnothing(v)
f()
else
Expand All @@ -111,7 +123,7 @@ end
end

function Base.get!(m::VecMap{T}, i::Int, def::T) where {T}
v = m[i]
v = _get_or_nothing(m, i)
if isnothing(v)
m[i] = def
else
Expand All @@ -120,7 +132,7 @@ function Base.get!(m::VecMap{T}, i::Int, def::T) where {T}
end

function Base.get!(f::Union{Function, Type}, m::VecMap{T}, i::Int) where {T}
v = m[i]
v = _get_or_nothing(m, i)
if isnothing(v)
m[i] = f()
else
Expand All @@ -129,7 +141,7 @@ function Base.get!(f::Union{Function, Type}, m::VecMap{T}, i::Int) where {T}
end

Base.haskey(m::VecMap, i::Int) =
i 1:length(m.v) && !isnothing(@inbounds m[i])
!isnothing(_get_or_nothing(m, i))

Base.keys(m::VecMap) = Iterators.filter(i -> haskey(m, i), 1:length(m.v))

Expand Down
6 changes: 3 additions & 3 deletions src/acsets/PreimageCaches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ end
Base.copy(pc::TrivialCache) = pc

function preimage(dom, m::Mapping{S,T}, ::TrivialCache{S,T}, y) where {S,T}
[x for x in dom if m[x] == y]
[x for x in dom if get(m, x, nothing) == y]
end

function preimage_multi(dom, m::Mapping{S,T}, pc::TrivialCache{S,T}, ys) where {S,T}
Expand Down Expand Up @@ -144,8 +144,8 @@ function preimage(
values(get(() -> Preimage(), pc.preimages, y))
end

function preimage_multi(dom, m::Mapping, pc::StoredPreimageCache, ys)
Vector{Vector{Any}}(collect(Iterators.map(values, view_with_default(pc.preimages, ys, DefaultEmpty))))
function preimage_multi(dom, m::Mapping, pc::StoredPreimageCache{S,T,Preimage}, ys) where {S,T,Preimage}
Vector{Vector{S}}(collect(Iterators.map(values, view_with_default(pc.preimages, ys, DefaultEmpty{Preimage}))))
end

# Assumes that i.preimages[y] is already stored
Expand Down
2 changes: 1 addition & 1 deletion src/categorical_algebra/FinSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ function colimit(F::Functor{<:FinCat{Int},<:TypeCat{<:FinSet{Int}}})
# Uses the general formula for colimits in Set (Leinster, 2014, Basic Category
# Theory, Example 5.2.16).
J = dom(F)
coprod = coproduct(map(x -> ob_map(F, x), ob_generators(J)))
coprod = coproduct(map(x -> ob_map(F, x)::FinSet{Int,Int}, ob_generators(J)))
n, ιs = length(ob(coprod)), legs(coprod)
sets = IntDisjointSets(n)
for f in hom_generators(J)
Expand Down

0 comments on commit 287cd55

Please sign in to comment.