Skip to content

Commit 3ccfc14

Browse files
refactor: use OrderedDict/OrderedSet over Dict/Set
1 parent 963596d commit 3ccfc14

4 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/irstructure.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ Return a new [`SymbolicUtils.IRStructure`](@ref) containing only the expressions
436436
along with their dependencies.
437437
"""
438438
function subset_ir(ir::IRStructure{T}, expr) where {T}
439-
exprs = Set{BasicSymbolic{T}}()
439+
exprs = OrderedSet{BasicSymbolic{T}}()
440440
buffer = IRStructureSearchBuffer(ir, exprs)
441441
# `Returns(true)` gets all top-level expressions
442442
search_variables!(buffer, expr; is_atomic = Returns(true))

src/safe_ctors.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Helper struct for tracking index variable usage in array operations.
291291
292292
# Fields
293293
- `idx_to_axes::IdxToAxesT{T}`: Maps index variables to the axes they index
294-
- `search_buffer::Set{BasicSymbolic{T}}`: Reusable buffer for variable searches
294+
- `search_buffer::OrderedSet{BasicSymbolic{T}}`: Reusable buffer for variable searches
295295
- `buffers::Vector{Vector{IndexedAxis{T}}}`: Pool of reusable buffers
296296
297297
# Details
@@ -300,12 +300,12 @@ are indexed by which index variables and validates consistency.
300300
"""
301301
struct IndexedAxes{T}
302302
idx_to_axes::IdxToAxesT{T}
303-
search_buffer::Set{BasicSymbolic{T}}
303+
search_buffer::OrderedSet{BasicSymbolic{T}}
304304
buffers::Vector{Vector{IndexedAxis{T}}}
305305
end
306306

307307
function IndexedAxes{T}() where {T}
308-
IndexedAxes{T}(IdxToAxesT{T}(), Set{BasicSymbolic{T}}(), Vector{IndexedAxis{T}}[])
308+
IndexedAxes{T}(IdxToAxesT{T}(), OrderedSet{BasicSymbolic{T}}(), Vector{IndexedAxis{T}}[])
309309
end
310310

311311
function Base.empty!(ix::IndexedAxes)

src/substitute.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ end
133133
return DefaultSubstituter{Fold}(d, filter, infer_vartype(d))
134134
end
135135
@inline function DefaultSubstituter{Fold}(d::Pair, filter::F) where {Fold, F}
136-
DefaultSubstituter{Fold}(Dict(d), filter)
136+
DefaultSubstituter{Fold}(OrderedDict(d), filter)
137137
end
138138
@inline function DefaultSubstituter{Fold}(d::AbstractArray{<:Pair}, filter::F) where {Fold, F}
139-
DefaultSubstituter{Fold}(Dict(d), filter)
139+
DefaultSubstituter{Fold}(OrderedDict(d), filter)
140140
end
141141

142142
function (s::Substituter)(ex)
@@ -261,7 +261,7 @@ julia> substitute(1+sqrt(y), Dict(y => 2), fold=Val(false))
261261
function substitute(expr, dict; fold::Val{Fold}=Val{false}(), filterer=default_substitute_filter) where {Fold}
262262
# This is kind of ugly (inlines some of the constructor logic of `DefaultSubstituter` but is needed to avoid runtime subtyping in
263263
# when calling this function. It makes a very big difference in runtime.
264-
d = dict isa AbstractDict ? dict : Dict(dict)
264+
d = dict isa AbstractDict ? dict : OrderedDict(dict)
265265
isempty(d) && !Fold && return expr
266266
VT = infer_vartype(d)
267267
if VT === Nothing
@@ -412,7 +412,7 @@ function search_variables!(buffer, expr::SparseMatrixCSC; kw...)
412412
search_variables!(buffer, V; kw...)
413413
end
414414

415-
_default_buffer(::BasicSymbolic{T}) where {T} = Set{BasicSymbolic{T}}()
415+
_default_buffer(::BasicSymbolic{T}) where {T} = OrderedSet{BasicSymbolic{T}}()
416416
_default_buffer(x::Any) = unwrap(x) === x ? Set() : _default_buffer(unwrap(x))
417417

418418
function search_variables(expr; kw...)
@@ -423,13 +423,13 @@ end
423423

424424
struct ArrayOpReduceCache{T}
425425
new_ranges::RangesT{T}
426-
subrules::Dict{BasicSymbolic{T}, Int}
427-
collapsed_idxs::Set{BasicSymbolic{T}}
426+
subrules::OrderedDict{BasicSymbolic{T}, Int}
427+
collapsed_idxs::OrderedSet{BasicSymbolic{T}}
428428
collapsed_ranges::Vector{StepRange{Int, Int}}
429429
end
430430

431431
function ArrayOpReduceCache{T}() where {T}
432-
ArrayOpReduceCache{T}(RangesT{T}(), Dict{BasicSymbolic{T}, Int}(), Set{BasicSymbolic{T}}(), StepRange{Int, Int}[])
432+
ArrayOpReduceCache{T}(RangesT{T}(), OrderedDict{BasicSymbolic{T}, Int}(), OrderedSet{BasicSymbolic{T}}(), StepRange{Int, Int}[])
433433
end
434434

435435
function Base.empty!(x::ArrayOpReduceCache)
@@ -651,7 +651,7 @@ scalarization_function(::Type{ArrayOp{T}}) where {T} = _scalarize_arrayop
651651
function _scalarize_arrayop(_, x::BasicSymbolic{T}, ::Val{toplevel}) where {T, toplevel}
652652
@match x begin
653653
BSImpl.ArrayOp(; output_idx, expr, term, ranges, reduce, shape = sh) => begin
654-
subrules = Dict()
654+
subrules = OrderedDict()
655655
new_expr = reduce_eliminated_idxs(expr, output_idx, ranges, reduce)
656656
empty!(subrules)
657657

src/types.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Core ADT for symbolic expressions.
169169
end
170170
struct AddMul
171171
const coeff::Any
172-
const dict::Dict{BasicSymbolicImpl.Type{T}, Number}
172+
const dict::OrderedDict{BasicSymbolicImpl.Type{T}, Number}
173173
const variant::AddMulVariant.T
174174
const metadata::MetadataT
175175
const shape::ShapeT
@@ -211,7 +211,7 @@ Core ADT for symbolic expressions.
211211
const term::Union{BasicSymbolicImpl.Type{T}, Nothing}
212212
# Optional map from symbolic indices in `output_idx` to the range they can
213213
# take. Any index not present in this takes its full range of values.
214-
const ranges::Dict{BasicSymbolicImpl.Type{T}, StepRange{Int, Int}}
214+
const ranges::OrderedDict{BasicSymbolicImpl.Type{T}, StepRange{Int, Int}}
215215
const metadata::MetadataT
216216
const shape::ShapeT
217217
const type::TypeT
@@ -262,15 +262,15 @@ The type of the dictionary stored in [`BSImpl.AddMul`](@ref). Passing this to th
262262
[`SymbolicUtils.Add`](@ref) or [`SymbolicUtils.Mul`](@ref) constructors will avoid
263263
allocating a new dictionary.
264264
"""
265-
const ACDict{T} = Dict{BasicSymbolic{T}, Number}
265+
const ACDict{T} = OrderedDict{BasicSymbolic{T}, Number}
266266
"""
267267
The type of the `output_idxs` field in [`BSImpl.ArrayOp`](@ref).
268268
"""
269269
const OutIdxT{T} = SmallV{Union{Int, BasicSymbolic{T}}}
270270
"""
271271
The type of the `ranges` field in [`BSImpl.ArrayOp`](@ref).
272272
"""
273-
const RangesT{T} = Dict{BasicSymbolic{T}, StepRange{Int, Int}}
273+
const RangesT{T} = OrderedDict{BasicSymbolic{T}, StepRange{Int, Int}}
274274
"""
275275
The type of the `sequence` field in [`BSImpl.ArrayMaker`](@ref).
276276
"""

0 commit comments

Comments
 (0)