Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed May 10, 2023
1 parent 29ca45c commit 26195b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
43 changes: 21 additions & 22 deletions src/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Base.empty!(c::AbstractComponent) = empty!(component(c))

Base.@propagate_inbounds @inline Base.getindex(c::AbstractComponent, i::Integer) = data(c)[data_index(c, i)]

@inline function Base.getindex(c::AbstractComponent, e::AbstractEntity)
Base.@propagate_inbounds @inline function Base.getindex(c::AbstractComponent, e::AbstractEntity)
@boundscheck if !in(e, c)
throw(BoundsError(c, Entity(e)))
end
Expand Down Expand Up @@ -139,7 +139,7 @@ Base.@propagate_inbounds @inline data_index(c::Component, e::AbstractEntity) = c

##### BASE Extensions ####

@inline function Base.setindex!(c::Component{T}, v::T, e::AbstractEntity) where {T}
Base.@propagate_inbounds @inline function Base.setindex!(c::Component{T}, v::T, e::AbstractEntity) where {T}
eid = Entity(e).id
@boundscheck if !in(e, c)
push!(c.indices, eid)
Expand All @@ -161,7 +161,7 @@ end
pops the data for `entity` out of `component`.
"""
function Base.pop!(c::Component, e::AbstractEntity)
Base.@propagate_inbounds @inline function Base.pop!(c::Component, e::AbstractEntity)
@boundscheck if !in(e, c)
throw(BoundsError(c, Entity(e)))
end
Expand All @@ -175,7 +175,7 @@ function Base.pop!(c::Component, e::AbstractEntity)
end
end

function Base.pop!(c::Component)
Base.@propagate_inbounds @inline function Base.pop!(c::Component)
@boundscheck if isempty(c)
throw(BoundsError(c))
end
Expand Down Expand Up @@ -305,29 +305,28 @@ Base.@propagate_inbounds @inline Base.parent(c::PooledComponent, e::Entity) = pa

# c[entity] = value
# set value of <only> this entity
@inline function Base.setindex!(c::PooledComponent{T}, v::T, e::AbstractEntity) where {T}
Base.@propagate_inbounds @inline function Base.setindex!(c::PooledComponent{T}, v::T, e::AbstractEntity) where {T}
eid = e.id
if in(e, c)
@inbounds begin
pid = c.indices[eid]
g = c.pool[pid]
if c.pool_size[g] == 1
# the entity already has its own (otherwise empty) pool - adjust value
c.data[g] = v
else
# the entity is part of a larger pool - create a new one
c.pool_size[g] -= 1
push!(c.data, v)
push!(c.pool_size, 1)
c.pool[pid] = length(c.data)
end
end
else
# the entity is not in the component - add it
@boundscheck if !in(e, c)
push!(c.indices, eid)
push!(c.pool, length(c.data) + 1)
push!(c.pool_size, 1)
push!(c.data, v)
return v
end
@inbounds begin
pid = c.indices[eid]
g = c.pool[pid]
if c.pool_size[g] == 1
# the entity already has its own (otherwise empty) pool - adjust value
c.data[g] = v
else
# the entity is part of a larger pool - create a new one
c.pool_size[g] -= 1
push!(c.data, v)
push!(c.pool_size, 1)
c.pool[pid] = length(c.data)
end
end
return v
end
Expand Down
6 changes: 3 additions & 3 deletions src/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ end
return false
else
page = @inbounds rev[pageid]
return page !== NULL_INT_PAGE && !isempty(page) && @inbounds page[offset] != 0
return page !== NULL_INT_PAGE && @inbounds page[offset] != 0
end
end

Expand Down Expand Up @@ -146,7 +146,7 @@ end
return s
end

@inline Base.@propagate_inbounds function Base.pop!(s::Indices)
Base.@propagate_inbounds @inline function Base.pop!(s::Indices)
if isempty(s)
throw(ArgumentError("Cannot pop an empty set."))
end
Expand All @@ -158,7 +158,7 @@ end
return id
end

@inline Base.@propagate_inbounds function Base.pop!(s::Indices, id::Integer)
Base.@propagate_inbounds @inline function Base.pop!(s::Indices, id::Integer)
id < 0 && throw(ArgumentError("Int to pop needs to be positive."))

@boundscheck if !in(id, s)
Expand Down
4 changes: 2 additions & 2 deletions src/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ Base.in(e::AbstractEntity, i::EntityIterator) = in(e.id, i.it)
@inline function Base.iterate(i::EntityIterator, state = i.it isa ReverseIndicesIterator ? length(i.it.shortest) : 1)
n = iterate(i.it, state)
n === nothing && return n
e = Entity(n[1])
return EntityState(e, i.components), n[2]
@inbounds e = Entity(n[1])
@inbounds return EntityState(e, i.components), n[2]
end

Base.last(i::EntityIterator) = EntityState(Entity(last(i.it)), i.components)
Expand Down
9 changes: 8 additions & 1 deletion src/ledger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ function Base.empty!(m::AbstractLedger)
end

function Base.getindex(m::AbstractLedger, ::Type{T})::component_type(T) where {T}
return get!(components(m), T, component_type(T)())
comps = components(m)
if haskey(comps, T)
return comps[T]::component_type(T)
else
c = component_type(T)()
comps[T] = c
return c
end
end

Base.copy(m::AbstractLedger) = Ledger(copy(entities(m)),
Expand Down

4 comments on commit 26195b5

@louisponet
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unexpected error occurred during registration.

@louisponet
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83321

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 26195b5429b47092b6c4db1f3a1b5bad1f484191
git push origin v0.3.1

Also, note the warning: Version 0.3.1 skips over 0.3.0
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.