This small package contains:
-
cavity!
andcavity
: Functions to compute theN
all-but-one operations betweenN
elements in timeO(N)
. The operation is arbitrary and needs only to be associative. This is equivalent to computing[reduce(op, (src[j] for j in eachindex(src) if i != j); init) for i in eachindex(src)]
which however would needN*(N-1)
evaluations ofop
. Ifop
is commutative with exact inverseinvop
, you could obtain the same result ofcavity(src, op, init)
, also in timeO(N)
, withinvop.(reduce(op, src; init), src)
. -
Accumulator
: Ana = Accumulator(v::AbstractVector)
works as a replacement forv
with extra tracking computations.- Construction of
a
requires timeO(N)
whereN == length(v)
. sum(a)
requires timeO(1)
.cumsum(a)
,cavity(a)
require timeO(1)
and return respectively aCumSum
andCavity
objects that are linked toa
.
- Construction of
-
c::CumSum(a::Accumulator)
: keeps a live-updatedcumsum
ofa
.- Create it with
c = cumsum(a::Accumulator)
- Retrieval
c[i]
takes timeO(log N)
. collect(c)
takes timeO(N)
searchsortedfirst(r, c)
takes timeO(log N)
- Create it with
-
c::Cavity(a::Accumulator)
: keeps a live-updatedcavity
ofa
.- Create it with
c = cavity(a::Accumulator)
. - Retrieval
c[i]
takes timeO(log N)
. collect(c)
takes timeO(N)
(but is slower thancavity(v::Vector)
).
- Create it with