Skip to content

Commit 4813392

Browse files
authored
Merge pull request #459 from control-toolbox/move/ctflows-to-ctbase-phase-a
Add AbstractCache and make_coerce to CTBase.Core
2 parents f8d0eee + 0d536e9 commit 4813392

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/Core/Core.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DocStringExtensions: TYPEDEF, TYPEDSIGNATURES
1212

1313
include("types.jl")
1414
include("tags.jl")
15+
include("caches.jl")
1516
include("default.jl")
1617

1718
# Private utilities
@@ -24,7 +25,8 @@ include("palette.jl")
2425
include("display.jl")
2526

2627
# Export public API
27-
export ctNumber, matrix2vec, to_out_of_place, @ensure
28+
export ctNumber, matrix2vec, to_out_of_place, make_coerce, @ensure
29+
export AbstractTag, AbstractCache
2830
export Style, Palette
2931
export DEFAULT_PALETTE, MONOCHROME_PALETTE, HIGH_CONTRAST_PALETTE
3032
export current_palette, set_palette!, set_color!, reset_palette!, show_palette

src/Core/caches.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
$(TYPEDEF)
3+
4+
Abstract base type for computation caches.
5+
6+
Caches store pre-allocated buffers and prepared plans (for example automatic
7+
differentiation plans) so that repeated computations avoid reallocating on every
8+
call. Concrete cache types are defined by the packages or extensions that provide
9+
a specific backend.
10+
11+
# Interface Requirements
12+
13+
Concrete cache subtypes typically:
14+
- Hold pre-allocated buffers and/or a prepared plan
15+
- Are constructed once and reused across many calls
16+
- Are backend- or extension-specific
17+
18+
# Example
19+
```julia
20+
struct MyCache <: AbstractCache
21+
buffer::Vector{Float64}
22+
end
23+
```
24+
25+
See also: [`AbstractTag`](@ref).
26+
"""
27+
abstract type AbstractCache end

src/Core/function_utils.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,38 @@ function to_out_of_place(f!, n; T=Float64)
3030
end
3131
return isnothing(f!) ? nothing : f
3232
end
33+
34+
"""
35+
make_coerce(x) -> coerce_fn
36+
37+
Return a coercion function matching the shape of `x`.
38+
39+
For scalars (`Number`), returns `only`, which extracts the single element from a
40+
1-element vector. For arrays (`AbstractVector`, `AbstractMatrix`), returns
41+
`identity` (a no-op). This is used to map a uniform vector-valued result back to
42+
the natural shape of the original input.
43+
44+
# Arguments
45+
- `x`: A value whose type determines the coercion strategy.
46+
47+
# Returns
48+
- A coercion function with signature `(y) -> coerced_y`.
49+
50+
# Example
51+
```julia-repl
52+
julia> coerce_scalar = make_coerce(1.0);
53+
54+
julia> coerce_scalar([5.0])
55+
5.0
56+
57+
julia> coerce_vector = make_coerce([1.0, 2.0]);
58+
59+
julia> coerce_vector([3.0, 4.0])
60+
2-element Vector{Float64}:
61+
3.0
62+
4.0
63+
```
64+
"""
65+
make_coerce(::Number) = only
66+
make_coerce(::AbstractVector) = identity
67+
make_coerce(::AbstractMatrix) = identity

0 commit comments

Comments
 (0)